1 /* native/vm/VMRuntime.c - java/lang/VMRuntime
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5 M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6 P. Tomsich, J. Wenninger
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 1621 2004-11-30 13:06:55Z twisti $
40 #include <sys/utsname.h>
42 #if !defined(STATIC_CLASSPATH)
47 #include "cacao/cacao.h"
49 #include "mm/memory.h"
50 #include "native/jni.h"
51 #include "native/native.h"
52 #include "native/include/java_io_File.h"
53 #include "native/include/java_lang_String.h"
54 #include "native/include/java_lang_Process.h"
55 #include "native/include/java_util_Properties.h" /* java_lang_VMRuntime.h */
56 #include "native/include/java_lang_VMRuntime.h"
57 #include "toolbox/logging.h"
58 #include "vm/builtin.h"
59 #include "vm/exceptions.h"
60 #include "vm/loader.h"
61 #include "vm/tables.h"
62 #include "vm/jit/asmpart.h"
65 /* this should work on BSD */
67 #if defined(__DARWIN__)
68 #include <sys/sysctl.h>
74 /* should we run all finalizers on exit? */
75 static bool finalizeOnExit = false;
77 /* temporary property structure */
79 typedef struct property property;
87 static property *properties = NULL;
90 /* create_property *************************************************************
92 Create a property entry for a command line property definition.
94 *******************************************************************************/
96 void create_property(char *key, char *value)
103 p->next = properties;
108 /* insert_property *************************************************************
110 Used for inserting a property into the system's properties table. Method m
111 (usually put) and the properties table must be given.
113 *******************************************************************************/
115 static void insert_property(methodinfo *m, java_util_Properties *p, char *key,
118 asm_calljavafunction(m,
120 javastring_new_char(key),
121 javastring_new_char(value),
127 * Class: java_lang_VMRuntime
128 * Method: execInternal
129 * Signature: ([Ljava/lang/String;[Ljava/lang/String;Ljava/io/File;)Ljava/lang/Process;
131 JNIEXPORT java_lang_Process* JNICALL Java_java_lang_VMRuntime_execInternal(JNIEnv *env, jclass clazz, java_objectarray *cmd, java_objectarray *shellenv, java_io_File *workingdir)
133 log_text("Java_java_lang_Runtime_execInternal called");
140 * Class: java/lang/VMRuntime
141 * Method: exitInternal
144 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_exit(JNIEnv *env, jclass clazz, s4 par1)
149 cacao_shutdown(par1);
154 * Class: java/lang/Runtime
158 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_freeMemory(JNIEnv *env, jclass clazz)
160 return gc_get_free_bytes();
165 * Class: java/lang/Runtime
169 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_gc(JNIEnv *env, jclass clazz)
176 * Class: java/lang/Runtime
177 * Method: runFinalization
180 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalization(JNIEnv *env, jclass clazz)
182 gc_invoke_finalizers();
187 * Class: java/lang/Runtime
188 * Method: runFinalizersOnExit
191 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizersOnExit(JNIEnv *env, jclass clazz, s4 value)
196 finalizeOnExit = value;
201 * Class: java/lang/Runtime
202 * Method: runFinalizationsForExit
205 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizationForExit(JNIEnv *env, jclass clazz)
207 /* if (finalizeOnExit) { */
209 /* gc_finalize_all(); */
211 /* log_text("Java_java_lang_VMRuntime_runFinalizationForExit called"); */
212 /*gc_finalize_all();*/
213 /*gc_invoke_finalizers();*/
219 * Class: java/lang/Runtime
220 * Method: totalMemory
223 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_totalMemory(JNIEnv *env, jclass clazz)
225 return gc_get_heap_size();
230 * Class: java/lang/Runtime
231 * Method: traceInstructions
234 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceInstructions(JNIEnv *env, jclass clazz, s4 par1)
241 * Class: java/lang/Runtime
242 * Method: traceMethodCalls
245 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceMethodCalls(JNIEnv *env, jclass clazz, s4 par1)
252 * Class: java_lang_Runtime
253 * Method: availableProcessors
256 JNIEXPORT s4 JNICALL Java_java_lang_VMRuntime_availableProcessors(JNIEnv *env, jclass clazz)
258 #if defined(_SC_NPROC_ONLN)
259 return (s4) sysconf(_SC_NPROC_ONLN);
261 #elif defined(_SC_NPROCESSORS_ONLN)
262 return (s4) sysconf(_SC_NPROCESSORS_ONLN);
264 #elif defined(__DARWIN__)
265 /* this should work in BSD */
267 int ncpu, mib[2], rc;
273 rc = sysctl(mib, 2, &ncpu, &len, NULL, 0);
278 host_basic_info_data_t hinfo;
279 mach_msg_type_number_t hinfo_count = HOST_BASIC_INFO_COUNT;
282 rc = host_info(mach_host_self(), HOST_BASIC_INFO,
283 (host_info_t) &hinfo, &hinfo_count);
285 if (rc != KERN_SUCCESS) {
289 return (s4) hinfo.avail_cpus;
298 * Class: java_lang_Runtime
300 * Signature: (Ljava/lang/String;)I
302 JNIEXPORT s4 JNICALL Java_java_lang_VMRuntime_nativeLoad(JNIEnv *env, jclass clazz, java_lang_String *par1)
311 log_text("Java_java_lang_VMRuntime_nativeLoad");
314 data = javastring_toutf(par1, 0);
317 log_text("nativeLoad: Error: empty string");
322 buffer_len = utf_strlen(data) + 40;
324 buffer = MNEW(char, buffer_len);
325 strcpy(buffer, "Java_java_lang_VMRuntime_nativeLoad:");
326 utf_sprint(buffer + strlen((char *) data), data);
330 MFREE(buffer, char, buffer_len);
333 #ifndef STATIC_CLASSPATH
334 /*here it could be interesting to store the references in a list eg for nicely cleaning up or for certain platforms*/
335 if (dlopen(data->text,RTLD_NOW | RTLD_GLOBAL)) {
336 log_text("LIBLOADED");
348 * Class: java/lang/VMRuntime
349 * Method: nativeGetLibname
350 * Signature: (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
352 JNIEXPORT java_lang_String* JNICALL Java_java_lang_VMRuntime_nativeGetLibname(JNIEnv *env, jclass clazz, java_lang_String *pathname, java_lang_String *libname)
360 *exceptionptr = new_nullpointerexception();
364 u = javastring_toutf(libname, 0);
367 log_text("nativeGetLibName: Error: empty string");
371 buffer_len = utf_strlen(u) + 6 /*lib .so */ +1 /*0*/;
372 buffer = MNEW(char, buffer_len);
374 sprintf(buffer, "lib");
375 utf_sprint(buffer + 3, u);
376 strcat(buffer, ".so");
379 log_text("nativeGetLibName:");
383 s = javastring_new_char(buffer);
385 MFREE(buffer, char, buffer_len);
392 * Class: java_lang_VMRuntime
393 * Method: insertSystemProperties
394 * Signature: (Ljava/util/Properties;)V
396 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_insertSystemProperties(JNIEnv *env, jclass clazz, java_util_Properties *p)
399 #define BUFFERSIZE 200
401 char cwd[BUFFERSIZE];
405 struct utsname utsnamebuf;
408 *exceptionptr = new_exception(string_java_lang_NullPointerException);
412 /* get properties from system */
414 (void) getcwd(cwd, BUFFERSIZE);
415 java_home = getenv("JAVA_HOME");
416 user = getenv("USER");
417 home = getenv("HOME");
420 /* search for method to add properties */
422 m = class_resolveclassmethod(p->header.vftbl->class,
424 utf_new_char("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"),
431 insert_property(m, p, "java.version", VERSION);
432 insert_property(m, p, "java.vendor", "CACAO Team");
433 insert_property(m, p, "java.vendor.url", "http://www.complang.tuwien.ac.at/java/cacao/");
434 insert_property(m, p, "java.home", java_home ? java_home : "null");
435 insert_property(m, p, "java.vm.specification.version", "1.0");
436 insert_property(m, p, "java.vm.specification.vendor", "Sun Microsystems Inc.");
437 insert_property(m, p, "java.vm.specification.name", "Java Virtual Machine Specification");
438 insert_property(m, p, "java.vm.version", VERSION);
439 insert_property(m, p, "java.vm.vendor", "CACAO Team");
440 insert_property(m, p, "java.vm.name", "CACAO");
441 insert_property(m, p, "java.specification.version", "1.4");
442 insert_property(m, p, "java.specification.vendor", "Sun Microsystems Inc.");
443 insert_property(m, p, "java.specification.name", "Java Platform API Specification");
444 insert_property(m, p, "java.class.version", "48.0");
445 insert_property(m, p, "java.class.path", classpath);
446 #if defined(STATIC_CLASSPATH)
447 insert_property(m, p, "java.library.path" , ".");
449 insert_property(m, p, "java.library.path" , getenv("LD_LIBRARY_PATH"));
451 insert_property(m, p, "java.io.tmpdir", "/tmp");
452 insert_property(m, p, "java.compiler", "cacao.jit");
453 insert_property(m, p, "java.ext.dirs", "");
454 insert_property(m, p, "os.name", utsnamebuf.sysname);
455 insert_property(m, p, "os.arch", utsnamebuf.machine);
456 insert_property(m, p, "os.version", utsnamebuf.release);
457 insert_property(m, p, "file.separator", "/");
458 /* insert_property(m, p, "file.encoding", "null"); -- this must be set properly */
459 insert_property(m, p, "path.separator", ":");
460 insert_property(m, p, "line.separator", "\n");
461 insert_property(m, p, "user.name", user ? user : "null");
462 insert_property(m, p, "user.home", home ? home : "null");
463 insert_property(m, p, "user.dir", cwd ? cwd : "null");
466 /* how do we get them? */
467 { "user.language", "en" },
468 { "user.region", "US" },
469 { "user.country", "US" },
470 { "user.timezone", "Europe/Vienna" },
472 /* XXX do we need this one? */
473 { "java.protocol.handler.pkgs", "gnu.java.net.protocol"}
475 insert_property(m,p,"java.protocol.handler.pkgs","gnu.java.net.protocol");
477 /* insert properties defined on commandline */
482 insert_property(m, p, properties->key, properties->value);
485 properties = properties->next;
494 * Class: java_lang_VMRuntime
498 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_maxMemory(JNIEnv *env, jclass clazz)
500 return gc_get_max_heap_size();
505 * These are local overrides for various environment variables in Emacs.
506 * Please do not remove this and leave it at the end of the file, where
507 * Emacs will automagically detect them.
508 * ---------------------------------------------------------------------
511 * indent-tabs-mode: t