1 /* src/native/vm/VMRuntime.c - java/lang/VMRuntime
3 Copyright (C) 1996-2005, 2006 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 Contact: cacao@cacaojvm.org
27 Authors: Roman Obermaiser
29 Changes: Joseph Wenninger
32 $Id: VMRuntime.c 4874 2006-05-05 14:36:18Z edwin $
42 #include <sys/utsname.h>
44 #if !defined(WITH_STATIC_CLASSPATH)
48 #if defined(__DARWIN__)
49 # define OS_INLINE /* required for <libkern/ppc/OSByteOrder.h> */
50 # include <mach/mach.h>
56 #include "mm/memory.h"
57 #include "native/jni.h"
58 #include "native/native.h"
59 #include "native/include/java_io_File.h"
60 #include "native/include/java_lang_String.h"
61 #include "native/include/java_lang_Process.h"
62 #include "toolbox/logging.h"
63 #include "vm/builtin.h"
64 #include "vm/exceptions.h"
65 #include "vm/loader.h"
66 #include "vm/options.h"
67 #include "vm/stringlocal.h"
71 /* this should work on BSD */
73 #if defined(__DARWIN__)
74 #include <sys/sysctl.h>
79 /* should we run all finalizers on exit? */
80 static bool finalizeOnExit = false;
84 * Class: java/lang/VMRuntime
85 * Method: exitInternal
88 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_exit(JNIEnv *env, jclass clazz, s4 par1)
98 * Class: java/lang/VMRuntime
102 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_freeMemory(JNIEnv *env, jclass clazz)
104 return gc_get_free_bytes();
109 * Class: java/lang/VMRuntime
110 * Method: totalMemory
113 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_totalMemory(JNIEnv *env, jclass clazz)
115 return gc_get_heap_size();
120 * Class: java_lang_VMRuntime
124 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_maxMemory(JNIEnv *env, jclass clazz)
126 return gc_get_max_heap_size();
131 * Class: java/lang/VMRuntime
135 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_gc(JNIEnv *env, jclass clazz)
142 * Class: java/lang/VMRuntime
143 * Method: runFinalization
146 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalization(JNIEnv *env, jclass clazz)
148 gc_invoke_finalizers();
153 * Class: java/lang/VMRuntime
154 * Method: runFinalizersOnExit
157 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizersOnExit(JNIEnv *env, jclass clazz, s4 value)
161 finalizeOnExit = value;
166 * Class: java/lang/VMRuntime
167 * Method: runFinalizationsForExit
170 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizationForExit(JNIEnv *env, jclass clazz)
172 /* if (finalizeOnExit) { */
174 /* gc_finalize_all(); */
176 /* log_text("Java_java_lang_VMRuntime_runFinalizationForExit called"); */
177 /*gc_finalize_all();*/
178 /*gc_invoke_finalizers();*/
184 * Class: java/lang/VMRuntime
185 * Method: traceInstructions
188 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceInstructions(JNIEnv *env, jclass clazz, s4 par1)
195 * Class: java/lang/VMRuntime
196 * Method: traceMethodCalls
199 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceMethodCalls(JNIEnv *env, jclass clazz, s4 par1)
206 * Class: java_lang_Runtime
207 * Method: availableProcessors
210 JNIEXPORT s4 JNICALL Java_java_lang_VMRuntime_availableProcessors(JNIEnv *env, jclass clazz)
212 #if defined(_SC_NPROC_ONLN)
213 return (s4) sysconf(_SC_NPROC_ONLN);
215 #elif defined(_SC_NPROCESSORS_ONLN)
216 return (s4) sysconf(_SC_NPROCESSORS_ONLN);
218 #elif defined(__DARWIN__)
219 /* this should work in BSD */
221 int ncpu, mib[2], rc;
227 rc = sysctl(mib, 2, &ncpu, &len, NULL, 0);
232 host_basic_info_data_t hinfo;
233 mach_msg_type_number_t hinfo_count = HOST_BASIC_INFO_COUNT;
236 rc = host_info(mach_host_self(), HOST_BASIC_INFO,
237 (host_info_t) &hinfo, &hinfo_count);
239 if (rc != KERN_SUCCESS) {
243 return (s4) hinfo.avail_cpus;
252 * Class: java/lang/VMRuntime
254 * Signature: (Ljava/lang/String;Ljava/lang/ClassLoader;)I
256 JNIEXPORT s4 JNICALL Java_java_lang_VMRuntime_nativeLoad(JNIEnv *env, jclass clazz, java_lang_String *filename, java_lang_ClassLoader *loader)
258 #if !defined(WITH_STATIC_CLASSPATH)
265 if (filename == NULL) {
266 exceptions_throw_nullpointerexception();
270 #if defined(WITH_STATIC_CLASSPATH)
272 #else /* defined(WITH_STATIC_CLASSPATH) */
273 name = javastring_toutf(filename, 0);
275 /* is the library already loaded? */
277 if (native_hashtable_library_find(name, (java_objectheader *) loader))
280 /* try to open the library */
282 if (!(handle = lt_dlopen(name->text))) {
285 log_print("Java_java_lang_VMRuntime_nativeLoad: ");
286 log_print(lt_dlerror());
293 /* resolve JNI_OnLoad function */
295 if ((onload = lt_dlsym(handle, "JNI_OnLoad"))) {
296 JNIEXPORT s4 (JNICALL *JNI_OnLoad) (JavaVM *, void *);
299 JNI_OnLoad = (JNIEXPORT s4 (JNICALL *)(JavaVM *, void *)) (ptrint) onload;
301 (*env)->GetJavaVM(env, &vm);
303 version = JNI_OnLoad(vm, NULL);
305 /* if the version is not 1.2 and not 1.4 the library cannot be loaded */
307 if ((version != JNI_VERSION_1_2) && (version != JNI_VERSION_1_4)) {
314 /* insert the library name into the library hash */
316 native_hashtable_library_add(name, (java_objectheader *) loader, handle);
319 #endif /* defined(WITH_STATIC_CLASSPATH) */
324 * Class: java/lang/VMRuntime
325 * Method: mapLibraryName
326 * Signature: (Ljava/lang/String;)Ljava/lang/String;
328 JNIEXPORT java_lang_String* JNICALL Java_java_lang_VMRuntime_mapLibraryName(JNIEnv *env, jclass clazz, java_lang_String *libname)
337 exceptions_throw_nullpointerexception();
341 u = javastring_toutf(libname, 0);
343 /* calculate length of library name */
345 buffer_len = strlen("lib");
347 buffer_len += utf_get_number_of_u2s(u);
349 #if defined(__DARWIN__)
350 buffer_len += strlen(".dylib");
352 buffer_len += strlen(".so");
355 buffer_len += strlen("0");
357 dumpsize = dump_size();
358 buffer = DMNEW(char, buffer_len);
360 /* generate library name */
362 strcpy(buffer, "lib");
363 utf_strcat(buffer, u);
365 #if defined(__DARWIN__)
366 strcat(buffer, ".dylib");
368 strcat(buffer, ".so");
371 s = javastring_new_from_ascii(buffer);
375 dump_release(dumpsize);
382 * These are local overrides for various environment variables in Emacs.
383 * Please do not remove this and leave it at the end of the file, where
384 * Emacs will automagically detect them.
385 * ---------------------------------------------------------------------
388 * indent-tabs-mode: t