1 /* src/native/vm/gnu/java_lang_VMRuntime.c
3 Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 $Id: java_lang_VMRuntime.c 8295 2007-08-11 17:57:24Z michi $
37 #include <sys/utsname.h>
39 #if defined(__DARWIN__)
40 # define OS_INLINE /* required for <libkern/ppc/OSByteOrder.h> */
41 # include <mach/mach.h>
44 #include "mm/gc-common.h"
45 #include "mm/memory.h"
47 #include "native/jni.h"
48 #include "native/native.h"
50 #include "native/include/java_io_File.h"
51 #include "native/include/java_lang_ClassLoader.h"
52 #include "native/include/java_lang_String.h"
53 #include "native/include/java_lang_Process.h"
55 #include "native/include/java_lang_VMRuntime.h"
57 #include "native/vm/java_lang_Runtime.h"
59 #include "vm/builtin.h"
60 #include "vm/exceptions.h"
61 #include "vm/stringlocal.h"
64 #include "vmcore/utf8.h"
67 /* this should work on BSD */
69 #if defined(__DARWIN__)
70 #include <sys/sysctl.h>
75 /* native methods implemented by this file ************************************/
77 static JNINativeMethod methods[] = {
78 { "exit", "(I)V", (void *) (intptr_t) &Java_java_lang_VMRuntime_exit },
79 { "freeMemory", "()J", (void *) (intptr_t) &Java_java_lang_VMRuntime_freeMemory },
80 { "totalMemory", "()J", (void *) (intptr_t) &Java_java_lang_VMRuntime_totalMemory },
81 { "maxMemory", "()J", (void *) (intptr_t) &Java_java_lang_VMRuntime_maxMemory },
82 { "gc", "()V", (void *) (intptr_t) &Java_java_lang_VMRuntime_gc },
83 { "runFinalization", "()V", (void *) (intptr_t) &Java_java_lang_VMRuntime_runFinalization },
84 { "runFinalizersOnExit", "(Z)V", (void *) (intptr_t) &Java_java_lang_VMRuntime_runFinalizersOnExit },
85 { "runFinalizationForExit", "()V", (void *) (intptr_t) &Java_java_lang_VMRuntime_runFinalizationForExit },
86 { "traceInstructions", "(Z)V", (void *) (intptr_t) &Java_java_lang_VMRuntime_traceInstructions },
87 { "traceMethodCalls", "(Z)V", (void *) (intptr_t) &Java_java_lang_VMRuntime_traceMethodCalls },
88 { "availableProcessors", "()I", (void *) (intptr_t) &Java_java_lang_VMRuntime_availableProcessors },
89 { "nativeLoad", "(Ljava/lang/String;Ljava/lang/ClassLoader;)I", (void *) (intptr_t) &Java_java_lang_VMRuntime_nativeLoad },
90 { "mapLibraryName", "(Ljava/lang/String;)Ljava/lang/String;", (void *) (intptr_t) &Java_java_lang_VMRuntime_mapLibraryName },
94 /* _Jv_java_lang_VMRuntime_init ************************************************
96 Register native functions.
98 *******************************************************************************/
100 void _Jv_java_lang_VMRuntime_init(void)
104 u = utf_new_char("java/lang/VMRuntime");
106 native_method_register(u, methods, NATIVE_METHODS_COUNT);
111 * Class: java/lang/VMRuntime
115 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_exit(JNIEnv *env, jclass clazz, int32_t status)
117 _Jv_java_lang_Runtime_exit(status);
122 * Class: java/lang/VMRuntime
126 JNIEXPORT int64_t JNICALL Java_java_lang_VMRuntime_freeMemory(JNIEnv *env, jclass clazz)
128 return _Jv_java_lang_Runtime_freeMemory();
133 * Class: java/lang/VMRuntime
134 * Method: totalMemory
137 JNIEXPORT int64_t JNICALL Java_java_lang_VMRuntime_totalMemory(JNIEnv *env, jclass clazz)
139 return _Jv_java_lang_Runtime_totalMemory();
144 * Class: java/lang/VMRuntime
148 JNIEXPORT int64_t JNICALL Java_java_lang_VMRuntime_maxMemory(JNIEnv *env, jclass clazz)
150 return gc_get_max_heap_size();
155 * Class: java/lang/VMRuntime
159 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_gc(JNIEnv *env, jclass clazz)
161 _Jv_java_lang_Runtime_gc();
166 * Class: java/lang/VMRuntime
167 * Method: runFinalization
170 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalization(JNIEnv *env, jclass clazz)
172 gc_invoke_finalizers();
177 * Class: java/lang/VMRuntime
178 * Method: runFinalizersOnExit
181 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizersOnExit(JNIEnv *env, jclass clazz, int32_t value)
183 _Jv_java_lang_Runtime_runFinalizersOnExit(value);
188 * Class: java/lang/VMRuntime
189 * Method: runFinalizationsForExit
192 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizationForExit(JNIEnv *env, jclass clazz)
194 /* if (finalizeOnExit) { */
196 /* gc_finalize_all(); */
198 /* log_text("Java_java_lang_VMRuntime_runFinalizationForExit called"); */
199 /*gc_finalize_all();*/
200 /*gc_invoke_finalizers();*/
206 * Class: java/lang/VMRuntime
207 * Method: traceInstructions
210 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceInstructions(JNIEnv *env, jclass clazz, int32_t par1)
217 * Class: java/lang/VMRuntime
218 * Method: traceMethodCalls
221 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceMethodCalls(JNIEnv *env, jclass clazz, int32_t par1)
228 * Class: java/lang/VMRuntime
229 * Method: availableProcessors
232 JNIEXPORT int32_t JNICALL Java_java_lang_VMRuntime_availableProcessors(JNIEnv *env, jclass clazz)
234 #if defined(_SC_NPROC_ONLN)
235 return (int32_t) sysconf(_SC_NPROC_ONLN);
237 #elif defined(_SC_NPROCESSORS_ONLN)
238 return (int32_t) sysconf(_SC_NPROCESSORS_ONLN);
240 #elif defined(__DARWIN__)
241 /* this should work in BSD */
243 int ncpu, mib[2], rc;
249 rc = sysctl(mib, 2, &ncpu, &len, NULL, 0);
251 return (int32_t) ncpu;
254 host_basic_info_data_t hinfo;
255 mach_msg_type_number_t hinfo_count = HOST_BASIC_INFO_COUNT;
258 rc = host_info(mach_host_self(), HOST_BASIC_INFO,
259 (host_info_t) &hinfo, &hinfo_count);
261 if (rc != KERN_SUCCESS) {
265 return (int32_t) hinfo.avail_cpus;
274 * Class: java/lang/VMRuntime
276 * Signature: (Ljava/lang/String;Ljava/lang/ClassLoader;)I
278 JNIEXPORT int32_t JNICALL Java_java_lang_VMRuntime_nativeLoad(JNIEnv *env, jclass clazz, java_lang_String *libname, java_lang_ClassLoader *loader)
282 cl = (classloader *) loader;
284 #if defined(ENABLE_JNI)
285 return _Jv_java_lang_Runtime_loadLibrary(env, libname, cl);
287 return _Jv_java_lang_Runtime_loadLibrary(libname, cl);
293 * Class: java/lang/VMRuntime
294 * Method: mapLibraryName
295 * Signature: (Ljava/lang/String;)Ljava/lang/String;
297 JNIEXPORT java_lang_String* JNICALL Java_java_lang_VMRuntime_mapLibraryName(JNIEnv *env, jclass clazz, java_lang_String *libname)
305 if (libname == NULL) {
306 exceptions_throw_nullpointerexception();
310 u = javastring_toutf((java_handle_t *) libname, false);
312 /* calculate length of library name */
314 buffer_len = strlen("lib");
316 buffer_len += utf_bytes(u);
318 #if defined(__DARWIN__)
319 buffer_len += strlen(".dylib");
321 buffer_len += strlen(".so");
324 buffer_len += strlen("0");
326 dumpsize = dump_size();
327 buffer = DMNEW(char, buffer_len);
329 /* generate library name */
331 strcpy(buffer, "lib");
334 #if defined(__DARWIN__)
335 strcat(buffer, ".dylib");
337 strcat(buffer, ".so");
340 o = javastring_new_from_utf_string(buffer);
344 dump_release(dumpsize);
346 return (java_lang_String *) o;
351 * These are local overrides for various environment variables in Emacs.
352 * Please do not remove this and leave it at the end of the file, where
353 * Emacs will automagically detect them.
354 * ---------------------------------------------------------------------
357 * indent-tabs-mode: t
361 * vim:noexpandtab:sw=4:ts=4: