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 4357 2006-01-22 23:33:38Z twisti $
42 #include <sys/utsname.h>
44 #if !defined(ENABLE_STATICVM)
48 #if defined(__DARWIN__)
49 # define OS_INLINE /* required for <libkern/ppc/OSByteOrder.h> */
50 # include <mach/mach.h>
55 #include "cacao/cacao.h"
57 #include "mm/memory.h"
58 #include "native/jni.h"
59 #include "native/native.h"
60 #include "native/include/java_io_File.h"
61 #include "native/include/java_lang_String.h"
62 #include "native/include/java_lang_Process.h"
63 #include "toolbox/logging.h"
64 #include "vm/builtin.h"
65 #include "vm/exceptions.h"
66 #include "vm/loader.h"
67 #include "vm/stringlocal.h"
70 /* this should work on BSD */
72 #if defined(__DARWIN__)
73 #include <sys/sysctl.h>
78 /* should we run all finalizers on exit? */
79 static bool finalizeOnExit = false;
83 * Class: java/lang/VMRuntime
84 * Method: execInternal
85 * Signature: ([Ljava/lang/String;[Ljava/lang/String;Ljava/io/File;)Ljava/lang/Process;
87 JNIEXPORT java_lang_Process* JNICALL Java_java_lang_VMRuntime_execInternal(JNIEnv *env, jclass clazz, java_objectarray *cmd, java_objectarray *shellenv, java_io_File *workingdir)
89 log_text("Java_java_lang_Runtime_execInternal called");
96 * Class: java/lang/VMRuntime
97 * Method: exitInternal
100 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_exit(JNIEnv *env, jclass clazz, s4 par1)
105 cacao_shutdown(par1);
110 * Class: java/lang/VMRuntime
114 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_freeMemory(JNIEnv *env, jclass clazz)
116 return gc_get_free_bytes();
121 * Class: java/lang/VMRuntime
125 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_gc(JNIEnv *env, jclass clazz)
132 * Class: java/lang/VMRuntime
133 * Method: runFinalization
136 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalization(JNIEnv *env, jclass clazz)
138 gc_invoke_finalizers();
143 * Class: java/lang/VMRuntime
144 * Method: runFinalizersOnExit
147 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizersOnExit(JNIEnv *env, jclass clazz, s4 value)
151 finalizeOnExit = value;
156 * Class: java/lang/VMRuntime
157 * Method: runFinalizationsForExit
160 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizationForExit(JNIEnv *env, jclass clazz)
162 /* if (finalizeOnExit) { */
164 /* gc_finalize_all(); */
166 /* log_text("Java_java_lang_VMRuntime_runFinalizationForExit called"); */
167 /*gc_finalize_all();*/
168 /*gc_invoke_finalizers();*/
174 * Class: java/lang/VMRuntime
175 * Method: totalMemory
178 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_totalMemory(JNIEnv *env, jclass clazz)
180 return gc_get_heap_size();
185 * Class: java/lang/VMRuntime
186 * Method: traceInstructions
189 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceInstructions(JNIEnv *env, jclass clazz, s4 par1)
196 * Class: java/lang/VMRuntime
197 * Method: traceMethodCalls
200 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceMethodCalls(JNIEnv *env, jclass clazz, s4 par1)
207 * Class: java_lang_Runtime
208 * Method: availableProcessors
211 JNIEXPORT s4 JNICALL Java_java_lang_VMRuntime_availableProcessors(JNIEnv *env, jclass clazz)
213 #if defined(_SC_NPROC_ONLN)
214 return (s4) sysconf(_SC_NPROC_ONLN);
216 #elif defined(_SC_NPROCESSORS_ONLN)
217 return (s4) sysconf(_SC_NPROCESSORS_ONLN);
219 #elif defined(__DARWIN__)
220 /* this should work in BSD */
222 int ncpu, mib[2], rc;
228 rc = sysctl(mib, 2, &ncpu, &len, NULL, 0);
233 host_basic_info_data_t hinfo;
234 mach_msg_type_number_t hinfo_count = HOST_BASIC_INFO_COUNT;
237 rc = host_info(mach_host_self(), HOST_BASIC_INFO,
238 (host_info_t) &hinfo, &hinfo_count);
240 if (rc != KERN_SUCCESS) {
244 return (s4) hinfo.avail_cpus;
253 * Class: java/lang/VMRuntime
255 * Signature: (Ljava/lang/String;Ljava/lang/ClassLoader;)I
257 JNIEXPORT s4 JNICALL Java_java_lang_VMRuntime_nativeLoad(JNIEnv *env, jclass clazz, java_lang_String *filename, java_lang_ClassLoader *loader)
260 #if !defined(ENABLE_STATICVM)
267 exceptions_throw_nullpointerexception();
271 #if defined(ENABLE_STATICVM)
274 name = javastring_toutf(filename, 0);
276 /* is the library already loaded? */
278 if (native_hashtable_library_find(name, (java_objectheader *) loader))
281 /* try to open the library */
283 if (!(handle = lt_dlopen(name->text)))
286 /* resolve JNI_OnLoad function */
288 if ((onload = lt_dlsym(handle, "JNI_OnLoad"))) {
289 JNIEXPORT s4 (JNICALL *JNI_OnLoad) (JavaVM *, void *);
292 JNI_OnLoad = (JNIEXPORT s4 (JNICALL *)(JavaVM *, void *)) (ptrint) onload;
294 (*env)->GetJavaVM(env, &vm);
296 version = JNI_OnLoad(vm, NULL);
298 /* if the version is not 1.2 and not 1.4 the library cannot be loaded */
300 if (version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4) {
307 /* insert the library name into the library hash */
309 native_hashtable_library_add(name, (java_objectheader *) loader, handle);
317 * Class: java/lang/VMRuntime
318 * Method: mapLibraryName
319 * Signature: (Ljava/lang/String;)Ljava/lang/String;
321 JNIEXPORT java_lang_String* JNICALL Java_java_lang_VMRuntime_mapLibraryName(JNIEnv *env, jclass clazz, java_lang_String *libname)
330 exceptions_throw_nullpointerexception();
334 u = javastring_toutf(libname, 0);
336 /* calculate length of library name */
338 buffer_len = strlen("lib");
340 buffer_len += utf_strlen(u);
342 #if defined(__DARWIN__)
343 buffer_len += strlen(".dylib");
345 buffer_len += strlen(".so");
348 buffer_len += strlen("0");
350 dumpsize = dump_size();
351 buffer = DMNEW(char, buffer_len);
353 /* generate library name, we use lt_dlopenext so we don't need an */
356 strcpy(buffer, "lib");
357 utf_strcat(buffer, u);
359 #if defined(__DARWIN__)
360 strcat(buffer, ".dylib");
362 strcat(buffer, ".so");
365 s = javastring_new_char(buffer);
369 dump_release(dumpsize);
376 * Class: java_lang_VMRuntime
380 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_maxMemory(JNIEnv *env, jclass clazz)
382 return gc_get_max_heap_size();
387 * These are local overrides for various environment variables in Emacs.
388 * Please do not remove this and leave it at the end of the file, where
389 * Emacs will automagically detect them.
390 * ---------------------------------------------------------------------
393 * indent-tabs-mode: t