* src/native/vm/openjdk/sun_misc_Perf.cpp: Implement just the bare minimum
[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         case JMM_VERBOSE_GC:
217                 result = opt_verbosegc;
218                 break;
219         case JMM_VERBOSE_CLASS:
220                 result = opt_verboseclass;
221                 break;
222         default:
223                 log_println("jmm_GetBoolAttribute: Unknown attribute %d", att);
224                 return false;
225         }
226
227         return result;
228 }
229
230
231 jboolean jmm_SetBoolAttribute(JNIEnv* env, jmmBoolAttribute att, jboolean flag)
232 {
233         TRACEJMMCALLS(("jmm_SetBoolAttribute(env=%p, att=%d, flag=%d)", env, att, flag));
234
235         switch (att) {
236         case JMM_VERBOSE_GC:
237                 opt_verbosegc = flag;
238                 break;
239         case JMM_VERBOSE_CLASS:
240                 opt_verboseclass = flag;
241                 break;
242         default:
243                 log_println("jmm_SetBoolAttribute: Unknown attribute %d", att);
244                 return false;
245         }
246
247         return true;
248 }
249
250
251 jlong jmm_GetLongAttribute(JNIEnv* env, jobject obj, jmmLongAttribute att)
252 {
253         TRACEJMMCALLS(("jmm_GetLongAttribute(env=%p, obj=%p, att=%d)", env, obj, att));
254
255         jlong result;
256
257         switch (att) {
258         case JMM_CLASS_LOADED_COUNT:
259                 result = classcache_get_loaded_class_count();
260                 break;
261         case JMM_CLASS_UNLOADED_COUNT:
262                 // XXX Fix this once we support class unloading!
263                 result = 0;
264                 break;
265         case JMM_THREAD_TOTAL_COUNT:
266                 result = ThreadList::get_number_of_started_java_threads();
267                 break;
268         case JMM_THREAD_LIVE_COUNT:
269                 result = ThreadList::get_number_of_active_java_threads();
270                 break;
271         case JMM_THREAD_PEAK_COUNT:
272                 result = ThreadList::get_peak_of_active_java_threads();
273                 break;
274         case JMM_THREAD_DAEMON_COUNT:
275                 result = ThreadList::get_number_of_daemon_java_threads();
276                 break;
277         case JMM_JVM_INIT_DONE_TIME_MS:
278                 result = VM::get_current()->get_inittime();
279                 break;
280         case JMM_OS_PROCESS_ID:
281                 result = os::getpid();
282                 break;
283         default:
284                 log_println("jmm_GetLongAttribute: Unknown attribute %d", att);
285                 return -1;
286         }
287
288         return result;
289 }
290
291
292 jint jmm_GetLongAttributes(JNIEnv* env, jobject obj, jmmLongAttribute* atts, jint count, jlong* result)
293 {
294         log_println("jmm_GetLongAttributes: IMPLEMENT ME!");
295         return 0;
296 }
297
298
299 jint jmm_GetThreadInfo(JNIEnv* env, jlongArray ids, jint maxDepth, jobjectArray infoArray)
300 {
301         log_println("jmm_GetThreadInfo: IMPLEMENT ME!");
302         return 0;
303 }
304
305
306 jobjectArray jmm_DumpThreads(JNIEnv* env, jlongArray thread_ids, jboolean locked_monitors, jboolean locked_synchronizers)
307 {
308         log_println("jmm_DumpThreads: IMPLEMENT ME!");
309         return NULL;
310 }
311
312
313 jobjectArray jmm_GetLoadedClasses(JNIEnv* env)
314 {
315         log_println("jmm_GetLoadedClasses: IMPLEMENT ME!");
316         return NULL;
317 }
318
319
320 jboolean jmm_ResetStatistic(JNIEnv* env, jvalue obj, jmmStatisticType type)
321 {
322         TRACEJMMCALLS(("jmm_ResetStatistic(env=%p, obj=%p, type=%d)", env, obj, type));
323
324         switch (type) {
325         case JMM_STAT_PEAK_THREAD_COUNT:
326                 ThreadList::reset_peak_of_active_java_threads();
327                 break;
328         default:
329                 log_println("jmm_ResetStatistic: Unknown statistic type %d", type);
330                 return false;
331         }
332
333         return true;
334 }
335
336
337 jlong jmm_GetThreadCpuTime(JNIEnv* env, jlong thread_id)
338 {
339         log_println("jmm_GetThreadCpuTime: IMPLEMENT ME!");
340         return 0;
341 }
342
343
344 jlong jmm_GetThreadCpuTimeWithKind(JNIEnv* env, jlong thread_id, jboolean user_sys_cpu_time)
345 {
346         log_println("jmm_GetThreadCpuTimeWithKind: IMPLEMENT ME!");
347         return 0;
348 }
349
350
351 jobjectArray jmm_GetVMGlobalNames(JNIEnv* env)
352 {
353         log_println("jmm_GetVMGlobalNames: IMPLEMENT ME!");
354         return NULL;
355 }
356
357
358 jint jmm_GetVMGlobals(JNIEnv* env, jobjectArray names, jmmVMGlobal* globals, jint count)
359 {
360         log_println("jmm_GetVMGlobals: IMPLEMENT ME!");
361         return 0;
362 }
363
364
365 void jmm_SetVMGlobal(JNIEnv* env, jstring flag_name, jvalue new_value)
366 {
367         log_println("jmm_SetVMGlobal: IMPLEMENT ME!");
368 }
369
370
371 jint jmm_GetInternalThreadTimes(JNIEnv* env, jobjectArray names, jlongArray times)
372 {
373         log_println("jmm_GetInternalThreadTimes: IMPLEMENT ME!");
374         return 0;
375 }
376
377
378 jobjectArray jmm_FindDeadlockedThreads(JNIEnv* env, jboolean object_monitors_only)
379 {
380         log_println("jmm_FindDeadlockedThreads: IMPLEMENT ME!");
381         return NULL;
382 }
383
384
385 jobjectArray jmm_FindMonitorDeadlockedThreads(JNIEnv* env)
386 {
387         log_println("jmm_FindMonitorDeadlockedThreads: IMPLEMENT ME!");
388         return NULL;
389 }
390
391
392 jint jmm_GetGCExtAttributeInfo(JNIEnv* env, jobject mgr, jmmExtAttributeInfo* info, jint count)
393 {
394         log_println("jmm_GetGCExtAttributeInfo: IMPLEMENT ME!");
395         return 0;
396 }
397
398
399 void jmm_GetLastGCStat(JNIEnv* env, jobject obj, jmmGCStat* gc_stat)
400 {
401         log_println("jmm_GetLastGCStat: IMPLEMENT ME!");
402 }
403
404
405 jint jmm_DumpHeap0(JNIEnv* env, jstring outputfile, jboolean live)
406 {
407         log_println("jmm_DumpHeap0: IMPLEMENT ME!");
408         return 0;
409 }
410
411 } // extern "C"
412
413
414 const struct jmmInterface_1_ jmm_interface = {
415         NULL,
416         NULL,
417         jmm_GetVersion,
418         jmm_GetOptionalSupport,
419         jmm_GetInputArguments,
420         jmm_GetThreadInfo,
421         jmm_GetInputArgumentArray,
422         jmm_GetMemoryPools,
423         jmm_GetMemoryManagers,
424         jmm_GetMemoryPoolUsage,
425         jmm_GetPeakMemoryPoolUsage,
426         NULL,
427         jmm_GetMemoryUsage,
428         jmm_GetLongAttribute,
429         jmm_GetBoolAttribute,
430         jmm_SetBoolAttribute,
431         jmm_GetLongAttributes,
432         jmm_FindMonitorDeadlockedThreads,
433         jmm_GetThreadCpuTime,
434         jmm_GetVMGlobalNames,
435         jmm_GetVMGlobals,
436         jmm_GetInternalThreadTimes,
437         jmm_ResetStatistic,
438         jmm_SetPoolSensor,
439         jmm_SetPoolThreshold,
440         jmm_GetPoolCollectionUsage,
441         jmm_GetGCExtAttributeInfo,
442         jmm_GetLastGCStat,
443         jmm_GetThreadCpuTimeWithKind,
444         NULL,
445         jmm_DumpHeap0,
446         jmm_FindDeadlockedThreads,
447         jmm_SetVMGlobal,
448         NULL,
449         jmm_DumpThreads
450 };
451
452
453 /**
454  * Return the requested management interface.
455  *
456  * @param version Requested management interface version.
457  *
458  * @return Pointer to management interface structure.
459  */
460 void* Management::get_jmm_interface(int version)
461 {
462         if (version == JMM_VERSION_1_0) {
463                 return (void*) &jmm_interface;
464         }
465
466         return NULL;
467 }
468
469 /*
470  * These are local overrides for various environment variables in Emacs.
471  * Please do not remove this and leave it at the end of the file, where
472  * Emacs will automagically detect them.
473  * ---------------------------------------------------------------------
474  * Local variables:
475  * mode: c++
476  * indent-tabs-mode: t
477  * c-basic-offset: 4
478  * tab-width: 4
479  * End:
480  * vim:noexpandtab:sw=4:ts=4:
481  */