1 /* src/native/vm/VMRuntime.c - java/lang/VMRuntime
3 Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 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., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Roman Obermaiser
29 Changes: Joseph Wenninger
32 $Id: VMRuntime.c 2647 2005-06-13 13:56:42Z twisti $
42 #include <sys/utsname.h>
44 #if defined(__DARWIN__)
45 # include <mach/mach.h>
48 #if !defined(STATIC_CLASSPATH)
49 # include "src/libltdl/ltdl.h"
52 #include "cacao/cacao.h"
54 #include "mm/memory.h"
55 #include "native/jni.h"
56 #include "native/native.h"
57 #include "native/include/java_io_File.h"
58 #include "native/include/java_lang_String.h"
59 #include "native/include/java_lang_Process.h"
60 #include "toolbox/logging.h"
61 #include "vm/builtin.h"
62 #include "vm/exceptions.h"
63 #include "vm/loader.h"
64 #include "vm/stringlocal.h"
65 #include "vm/tables.h"
68 /* this should work on BSD */
70 #if defined(__DARWIN__)
71 #include <sys/sysctl.h>
77 /* should we run all finalizers on exit? */
78 static bool finalizeOnExit = false;
82 * Class: java/lang/VMRuntime
83 * Method: execInternal
84 * Signature: ([Ljava/lang/String;[Ljava/lang/String;Ljava/io/File;)Ljava/lang/Process;
86 JNIEXPORT java_lang_Process* JNICALL Java_java_lang_VMRuntime_execInternal(JNIEnv *env, jclass clazz, java_objectarray *cmd, java_objectarray *shellenv, java_io_File *workingdir)
88 log_text("Java_java_lang_Runtime_execInternal called");
95 * Class: java/lang/VMRuntime
96 * Method: exitInternal
99 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_exit(JNIEnv *env, jclass clazz, s4 par1)
104 cacao_shutdown(par1);
109 * Class: java/lang/VMRuntime
113 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_freeMemory(JNIEnv *env, jclass clazz)
115 return gc_get_free_bytes();
120 * Class: java/lang/VMRuntime
124 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_gc(JNIEnv *env, jclass clazz)
131 * Class: java/lang/VMRuntime
132 * Method: runFinalization
135 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalization(JNIEnv *env, jclass clazz)
137 gc_invoke_finalizers();
142 * Class: java/lang/VMRuntime
143 * Method: runFinalizersOnExit
146 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizersOnExit(JNIEnv *env, jclass clazz, s4 value)
150 finalizeOnExit = value;
155 * Class: java/lang/VMRuntime
156 * Method: runFinalizationsForExit
159 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizationForExit(JNIEnv *env, jclass clazz)
161 /* if (finalizeOnExit) { */
163 /* gc_finalize_all(); */
165 /* log_text("Java_java_lang_VMRuntime_runFinalizationForExit called"); */
166 /*gc_finalize_all();*/
167 /*gc_invoke_finalizers();*/
173 * Class: java/lang/VMRuntime
174 * Method: totalMemory
177 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_totalMemory(JNIEnv *env, jclass clazz)
179 return gc_get_heap_size();
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)
261 *exceptionptr = new_nullpointerexception();
265 name = javastring_toutf(filename, 0);
267 #if defined(STATIC_CLASSPATH)
270 /* here it could be interesting to store the references in a list eg for */
271 /* nicely cleaning up or for certain platforms */
273 if (lt_dlopenext(name->text))
282 * Class: java/lang/VMRuntime
283 * Method: mapLibraryName
284 * Signature: (Ljava/lang/String;)Ljava/lang/String;
286 JNIEXPORT java_lang_String* JNICALL Java_java_lang_VMRuntime_mapLibraryName(JNIEnv *env, jclass clazz, java_lang_String *libname)
295 *exceptionptr = new_nullpointerexception();
299 u = javastring_toutf(libname, 0);
301 /* calculate length of library name */
303 buffer_len = strlen("lib") + utf_strlen(u) + strlen(".so") + strlen("0");
305 dumpsize = dump_size();
306 buffer = DMNEW(char, buffer_len);
308 /* generate library name, we use lt_dlopenext so we don't need an */
311 strcpy(buffer, "lib");
312 utf_strcat(buffer, u);
313 strcat(buffer, ".so");
315 s = javastring_new_char(buffer);
319 dump_release(dumpsize);
326 * Class: java_lang_VMRuntime
330 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_maxMemory(JNIEnv *env, jclass clazz)
332 return gc_get_max_heap_size();
337 * These are local overrides for various environment variables in Emacs.
338 * Please do not remove this and leave it at the end of the file, where
339 * Emacs will automagically detect them.
340 * ---------------------------------------------------------------------
343 * indent-tabs-mode: t