da9c3d9dace0c64d5a729b695b59a27428abffea
[cacao.git] / src / native / vm / openjdk / management.cpp
1  /* src/native/vm/openjdk/management.cpp - HotSpot management interface functions
2
3    Copyright (C) 2009
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5    Copyright (C) 2008, 2009 Theobroma Systems Ltd.
6
7    This file is part of CACAO.
8
9    This program is free software; you can redistribute it and/or
10    modify it under the terms of the GNU General Public License as
11    published by the Free Software Foundation; either version 2, or (at
12    your option) any later version.
13
14    This program is distributed in the hope that it will be useful, but
15    WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17    General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22    02110-1301, USA.
23
24 */
25
26
27 #include "config.h"
28
29 #include <stdint.h>
30
31 // Include our JNI header before the JMM header, because the JMM
32 // header include jni.h and we want to override the typedefs in jni.h.
33 #include "native/jni.hpp"
34
35 #include INCLUDE_JMM_H
36
37 #include "native/vm/openjdk/management.hpp"
38
39 #include "threads/threadlist.hpp"
40
41 #include "toolbox/logging.hpp"
42
43 #include "vm/array.hpp"
44 #include "vm/classcache.hpp"
45 #include "vm/globals.hpp" // XXX Remove!!!
46 #include "vm/options.h"
47 #include "vm/os.hpp"
48 #include "vm/vm.hpp"
49
50
51 /* debugging macros ***********************************************************/
52
53 #if !defined(NDEBUG)
54
55 # define TRACEJMMCALLS(x)               \
56         do {                                \
57                 if (opt_TraceJMMCalls) {        \
58                         log_println x;              \
59                 }                               \
60         } while (0)
61
62 #else
63
64 # define TRACEJMMCALLS(x)
65
66 #endif
67
68
69 /**
70  * Initialize the Management subsystem.
71  */
72 Management::Management()
73 {
74         // Initialize optional support
75         _optional_support.isLowMemoryDetectionSupported = 1;
76         _optional_support.isCompilationTimeMonitoringSupported = 1;
77         _optional_support.isThreadContentionMonitoringSupported = 1;
78
79 //      if (os::is_thread_cpu_time_supported()) {
80         if (false) {
81                 _optional_support.isCurrentThreadCpuTimeSupported = 1;
82                 _optional_support.isOtherThreadCpuTimeSupported = 1;
83         }
84         else {
85                 _optional_support.isCurrentThreadCpuTimeSupported = 0;
86                 _optional_support.isOtherThreadCpuTimeSupported = 0;
87         }
88
89         _optional_support.isBootClassPathSupported = 1;
90         _optional_support.isObjectMonitorUsageSupported = 1;
91         _optional_support.isSynchronizerUsageSupported = 1;
92 }
93
94
95 /**
96  * Return a pointer to the optional support structure.
97  *
98  * @param Pointer to optional support structure.
99  */
100 const jmmOptionalSupport& Management::get_optional_support() const
101 {
102         return _optional_support;
103 }
104
105
106 // Interface functions are exported as C functions.
107 extern "C" {
108
109 jint jmm_GetVersion(JNIEnv* env)
110 {
111         return JMM_VERSION;
112 }
113
114
115 jint jmm_GetOptionalSupport(JNIEnv* env, jmmOptionalSupport* support)
116 {
117         if (support == NULL) {
118                 return -1;
119         }
120
121         Management& mm = VM::get_current()->get_management();
122
123         // memcpy the structure.
124         os::memcpy(support, &mm.get_optional_support(), sizeof(jmmOptionalSupport));
125
126         return 0;
127 }
128
129
130 jobject jmm_GetInputArguments(JNIEnv* env)
131 {
132         log_println("jmm_GetInputArguments: IMPLEMENT ME!");
133         return NULL;
134 }
135
136
137 jobjectArray jmm_GetInputArgumentArray(JNIEnv* env)
138 {
139         log_println("jmm_GetInputArgumentArray: IMPLEMENT ME!");
140         return NULL;
141 }
142
143
144 jobjectArray jmm_GetMemoryPools(JNIEnv* env, jobject obj)
145 {
146         TRACEJMMCALLS(("jmm_GetMemoryPools(env=%p, obj=%p)", env, obj));
147
148         // XXX This should be an array of java/lang/management/MemoryPoolMXBean.
149         log_println("jmm_GetMemoryPools: FIX ME!");
150         ObjectArray oa(0, class_java_lang_String);
151
152         return oa.get_handle();
153 }
154
155
156 jobjectArray jmm_GetMemoryManagers(JNIEnv* env, jobject obj)
157 {
158         TRACEJMMCALLS(("jmm_GetMemoryManagers(env=%p, obj=%p)", env, obj));
159
160         // XXX This should be an array of java/lang/management/MemoryManagerMXBean.
161         log_println("jmm_GetMemoryManagers: FIX ME!");
162         ObjectArray oa(0, class_java_lang_String);
163
164         return oa.get_handle();
165 }
166
167
168 jobject jmm_GetMemoryPoolUsage(JNIEnv* env, jobject obj)
169 {
170         log_println("jmm_GetMemoryPoolUsage: IMPLEMENT ME!");
171         return NULL;
172 }
173
174
175 jobject jmm_GetPeakMemoryPoolUsage(JNIEnv* env, jobject obj)
176 {
177         log_println("jmm_GetPeakMemoryPoolUsage: IMPLEMENT ME!");
178         return NULL;
179 }
180
181
182 jobject jmm_GetPoolCollectionUsage(JNIEnv* env, jobject obj)
183 {
184         log_println("jmm_GetPoolCollectionUsage: IMPLEMENT ME!");
185         return NULL;
186 }
187
188
189 void jmm_SetPoolSensor(JNIEnv* env, jobject obj, jmmThresholdType type, jobject sensorObj)
190 {
191         log_println("jmm_SetPoolSensor: IMPLEMENT ME!");
192 }
193
194
195 jlong jmm_SetPoolThreshold(JNIEnv* env, jobject obj, jmmThresholdType type, jlong threshold)
196 {
197         log_println("jmm_SetPoolThreshold: IMPLEMENT ME!");
198         return 0;
199 }
200
201
202 jobject jmm_GetMemoryUsage(JNIEnv* env, jboolean heap)
203 {
204         log_println("jmm_GetMemoryUsage: IMPLEMENT ME!");
205         return NULL;
206 }
207
208
209 jboolean jmm_GetBoolAttribute(JNIEnv* env, jmmBoolAttribute att)
210 {
211         TRACEJMMCALLS(("jmm_GetBoolAttribute(env=%p, att=%d)", env, att));
212
213         jboolean result;
214
215         switch (att) {
216         default:
217                 log_println("jmm_GetBoolAttribute: Unknown attribute %d", att);
218                 return false;
219         }
220
221         return result;
222 }
223
224
225 jboolean jmm_SetBoolAttribute(JNIEnv* env, jmmBoolAttribute att, jboolean flag)
226 {
227         TRACEJMMCALLS(("jmm_SetBoolAttribute(env=%p, att=%d, flag=%d)", env, att, flag));
228
229         switch (att) {
230         default:
231                 log_println("jmm_SetBoolAttribute: Unknown attribute %d", att);
232                 return false;
233         }
234
235         return true;
236 }
237
238
239 jlong jmm_GetLongAttribute(JNIEnv* env, jobject obj, jmmLongAttribute att)
240 {
241         TRACEJMMCALLS(("jmm_GetLongAttribute(env=%p, obj=%p, att=%d)", env, obj, att));
242
243         jlong result;
244
245         switch (att) {
246         case JMM_CLASS_LOADED_COUNT:
247                 result = classcache_get_loaded_class_count();
248                 break;
249         case JMM_CLASS_UNLOADED_COUNT:
250                 // XXX Fix this once we support class unloading!
251                 result = 0;
252                 break;
253         case JMM_THREAD_TOTAL_COUNT:
254                 result = ThreadList::get_number_of_started_java_threads();
255                 break;
256         case JMM_THREAD_LIVE_COUNT:
257                 result = ThreadList::get_number_of_active_java_threads();
258                 break;
259         case JMM_THREAD_PEAK_COUNT:
260                 result = ThreadList::get_peak_of_active_java_threads();
261                 break;
262         case JMM_THREAD_DAEMON_COUNT:
263                 result = ThreadList::get_number_of_daemon_java_threads();
264                 break;
265         case JMM_JVM_INIT_DONE_TIME_MS:
266                 result = VM::get_current()->get_inittime();
267                 break;
268         default:
269                 log_println("jmm_GetLongAttribute: Unknown attribute %d", att);
270                 return -1;
271         }
272
273         return result;
274 }
275
276
277 jint jmm_GetLongAttributes(JNIEnv* env, jobject obj, jmmLongAttribute* atts, jint count, jlong* result)
278 {
279         log_println("jmm_GetLongAttributes: IMPLEMENT ME!");
280         return 0;
281 }
282
283
284 jint jmm_GetThreadInfo(JNIEnv* env, jlongArray ids, jint maxDepth, jobjectArray infoArray)
285 {
286         log_println("jmm_GetThreadInfo: IMPLEMENT ME!");
287         return 0;
288 }
289
290
291 jobjectArray jmm_DumpThreads(JNIEnv* env, jlongArray thread_ids, jboolean locked_monitors, jboolean locked_synchronizers)
292 {
293         log_println("jmm_DumpThreads: IMPLEMENT ME!");
294         return NULL;
295 }
296
297
298 jobjectArray jmm_GetLoadedClasses(JNIEnv* env)
299 {
300         log_println("jmm_GetLoadedClasses: IMPLEMENT ME!");
301         return NULL;
302 }
303
304
305 jboolean jmm_ResetStatistic(JNIEnv* env, jvalue obj, jmmStatisticType type)
306 {
307         TRACEJMMCALLS(("jmm_ResetStatistic(env=%p, obj=%p, type=%d)", env, obj, type));
308
309         switch (type) {
310         case JMM_STAT_PEAK_THREAD_COUNT:
311                 ThreadList::reset_peak_of_active_java_threads();
312                 break;
313         default:
314                 log_println("jmm_ResetStatistic: Unknown statistic type %d", type);
315                 return false;
316         }
317
318         return true;
319 }
320
321
322 jlong jmm_GetThreadCpuTime(JNIEnv* env, jlong thread_id)
323 {
324         log_println("jmm_GetThreadCpuTime: IMPLEMENT ME!");
325         return 0;
326 }
327
328
329 jlong jmm_GetThreadCpuTimeWithKind(JNIEnv* env, jlong thread_id, jboolean user_sys_cpu_time)
330 {
331         log_println("jmm_GetThreadCpuTimeWithKind: IMPLEMENT ME!");
332         return 0;
333 }
334
335
336 jobjectArray jmm_GetVMGlobalNames(JNIEnv* env)
337 {
338         log_println("jmm_GetVMGlobalNames: IMPLEMENT ME!");
339         return NULL;
340 }
341
342
343 jint jmm_GetVMGlobals(JNIEnv* env, jobjectArray names, jmmVMGlobal* globals, jint count)
344 {
345         log_println("jmm_GetVMGlobals: IMPLEMENT ME!");
346         return 0;
347 }
348
349
350 void jmm_SetVMGlobal(JNIEnv* env, jstring flag_name, jvalue new_value)
351 {
352         log_println("jmm_SetVMGlobal: IMPLEMENT ME!");
353 }
354
355
356 jint jmm_GetInternalThreadTimes(JNIEnv* env, jobjectArray names, jlongArray times)
357 {
358         log_println("jmm_GetInternalThreadTimes: IMPLEMENT ME!");
359         return 0;
360 }
361
362
363 jobjectArray jmm_FindDeadlockedThreads(JNIEnv* env, jboolean object_monitors_only)
364 {
365         log_println("jmm_FindDeadlockedThreads: IMPLEMENT ME!");
366         return NULL;
367 }
368
369
370 jobjectArray jmm_FindMonitorDeadlockedThreads(JNIEnv* env)
371 {
372         log_println("jmm_FindMonitorDeadlockedThreads: IMPLEMENT ME!");
373         return NULL;
374 }
375
376
377 jint jmm_GetGCExtAttributeInfo(JNIEnv* env, jobject mgr, jmmExtAttributeInfo* info, jint count)
378 {
379         log_println("jmm_GetGCExtAttributeInfo: IMPLEMENT ME!");
380         return 0;
381 }
382
383
384 void jmm_GetLastGCStat(JNIEnv* env, jobject obj, jmmGCStat* gc_stat)
385 {
386         log_println("jmm_GetLastGCStat: IMPLEMENT ME!");
387 }
388
389
390 jint jmm_DumpHeap0(JNIEnv* env, jstring outputfile, jboolean live)
391 {
392         log_println("jmm_DumpHeap0: IMPLEMENT ME!");
393         return 0;
394 }
395
396 } // extern "C"
397
398
399 const struct jmmInterface_1_ jmm_interface = {
400         NULL,
401         NULL,
402         jmm_GetVersion,
403         jmm_GetOptionalSupport,
404         jmm_GetInputArguments,
405         jmm_GetThreadInfo,
406         jmm_GetInputArgumentArray,
407         jmm_GetMemoryPools,
408         jmm_GetMemoryManagers,
409         jmm_GetMemoryPoolUsage,
410         jmm_GetPeakMemoryPoolUsage,
411         NULL,
412         jmm_GetMemoryUsage,
413         jmm_GetLongAttribute,
414         jmm_GetBoolAttribute,
415         jmm_SetBoolAttribute,
416         jmm_GetLongAttributes,
417         jmm_FindMonitorDeadlockedThreads,
418         jmm_GetThreadCpuTime,
419         jmm_GetVMGlobalNames,
420         jmm_GetVMGlobals,
421         jmm_GetInternalThreadTimes,
422         jmm_ResetStatistic,
423         jmm_SetPoolSensor,
424         jmm_SetPoolThreshold,
425         jmm_GetPoolCollectionUsage,
426         jmm_GetGCExtAttributeInfo,
427         jmm_GetLastGCStat,
428         jmm_GetThreadCpuTimeWithKind,
429         NULL,
430         jmm_DumpHeap0,
431         jmm_FindDeadlockedThreads,
432         jmm_SetVMGlobal,
433         NULL,
434         jmm_DumpThreads
435 };
436
437
438 /**
439  * Return the requested management interface.
440  *
441  * @param version Requested management interface version.
442  *
443  * @return Pointer to management interface structure.
444  */
445 void* Management::get_jmm_interface(int version)
446 {
447         if (version == JMM_VERSION_1_0) {
448                 return (void*) &jmm_interface;
449         }
450
451         return NULL;
452 }
453
454 /*
455  * These are local overrides for various environment variables in Emacs.
456  * Please do not remove this and leave it at the end of the file, where
457  * Emacs will automagically detect them.
458  * ---------------------------------------------------------------------
459  * Local variables:
460  * mode: c++
461  * indent-tabs-mode: t
462  * c-basic-offset: 4
463  * tab-width: 4
464  * End:
465  * vim:noexpandtab:sw=4:ts=4:
466  */