1 /* nat/Runtime.c - java/lang/Runtime
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
31 $Id: VMRuntime.c 930 2004-03-02 21:18:23Z jowenn $
39 #include <sys/utsname.h>
47 #include "toolbox/loging.h"
48 #include "toolbox/memory.h"
49 #include "java_io_File.h"
50 #include "java_lang_String.h"
51 #include "java_lang_Process.h"
52 #include "java_util_Properties.h" /* needed for java_lang_Runtime.h */
53 #include "java_lang_Runtime.h"
60 static int activeprops = 19;
62 static char *proplist[MAXPROPS][2] = {
63 { "java.class.path", NULL },
64 { "java.home", NULL },
65 { "user.home", NULL },
66 { "user.name", NULL },
71 { "os.version", NULL },
73 { "java.class.version", "45.3" },
74 { "java.version", PACKAGE":"VERSION },
75 { "java.vendor", "CACAO Team" },
76 { "java.vendor.url", "http://www.complang.tuwien.ac.at/java/cacao/" },
77 { "java.vm.name", "CACAO"},
78 { "java.tmpdir", "/tmp/"},
79 { "java.io.tmpdir", "/tmp/"},
81 { "path.separator", ":" },
82 { "file.separator", "/" },
83 { "line.separator", "\n" },
84 { "java.protocol.handler.pkgs", "gnu.java.net.protocol"}
87 void attach_property(char *name, char *value)
89 if (activeprops >= MAXPROPS) panic("Too many properties defined");
90 proplist[activeprops][0] = name;
91 proplist[activeprops][1] = value;
95 * Class: java_lang_Runtime
96 * Method: execInternal
97 * Signature: ([Ljava/lang/String;[Ljava/lang/String;Ljava/io/File;)Ljava/lang/Process;
99 JNIEXPORT java_lang_Process* JNICALL Java_java_lang_Runtime_execInternal(JNIEnv *env, java_lang_Runtime *this, java_objectarray *cmd, java_objectarray *shellenv, java_io_File *workingdir)
101 log_text("Java_java_lang_Runtime_execInternal called");
108 * Class: java/lang/Runtime
109 * Method: exitInternal
112 JNIEXPORT void JNICALL Java_java_lang_Runtime_exitInternal(JNIEnv *env, java_lang_Runtime *this, s4 par1)
114 cacao_shutdown(par1);
119 * Class: java/lang/Runtime
123 JNIEXPORT s8 JNICALL Java_java_lang_Runtime_freeMemory(JNIEnv *env, java_lang_Runtime *this)
125 log_text ("java_lang_Runtime_freeMemory called");
127 return builtin_i2l(0);
132 * Class: java/lang/Runtime
136 JNIEXPORT void JNICALL Java_java_lang_Runtime_gc(JNIEnv *env, java_lang_Runtime *this)
143 * Class: java/lang/Runtime
144 * Method: runFinalization
147 JNIEXPORT void JNICALL Java_java_lang_Runtime_runFinalization(JNIEnv *env, java_lang_Runtime *this)
154 * Class: java/lang/Runtime
155 * Method: runFinalizersOnExit
158 JNIEXPORT void JNICALL Java_java_lang_Runtime_runFinalizersOnExitInternal(JNIEnv *env, jclass clazz, s4 par1)
160 log_text("Java_java_lang_Runtime_runFinalizersOnExit0 called");
165 * Class: java/lang/Runtime
166 * Method: totalMemory
169 JNIEXPORT s8 JNICALL Java_java_lang_Runtime_totalMemory(JNIEnv *env, java_lang_Runtime *this)
171 log_text ("java_lang_Runtime_totalMemory called");
173 return builtin_i2l(0);
178 * Class: java/lang/Runtime
179 * Method: traceInstructions
182 JNIEXPORT void JNICALL Java_java_lang_Runtime_traceInstructions(JNIEnv *env, java_lang_Runtime *this, s4 par1)
184 log_text("Java_java_lang_Runtime_traceInstructions called");
189 * Class: java/lang/Runtime
190 * Method: traceMethodCalls
193 JNIEXPORT void JNICALL Java_java_lang_Runtime_traceMethodCalls(JNIEnv *env, java_lang_Runtime *this, s4 par1)
195 log_text("Java_java_lang_Runtime_traceMethodCalls called");
198 #define CPULINELEN 100
200 #if defined(__ALPHA__)
203 char line[CPULINELEN];
204 cpuinfo=fopen("/proc/cpuinfo","r");
206 if (verbose) log_text("/proc/cpuinfo not accessible, assuming 1 available processor");
207 return 1; /* we have at least one cpu ;) */
209 while (fgets(line,CPULINELEN,cpuinfo)!=EOF) {
210 if (strncmp("cpus detected",line,13)==0) {
215 for (p=&line[strlen(line)-2];(*p>='0') && (*p<='9');p--);
216 cpucnt=strtol(p,&ep,10);
219 if (verbose) log_text("returning value retrieved by parsing cpus detected line");
224 if (verbose) log_text("/proc/cpuinfo did not contain a cpus detected line, assuming 1 available processor");
225 return 1; /* we have at least one cpu ;) */
229 #if (defined(__I386__) || defined(__X86_64__))
232 char line[CPULINELEN];
234 cpuinfo=fopen("/proc/cpuinfo","r");
236 if (verbose) log_text("/proc/cpuinfo not accessible, assuming 1 available processor");
237 return 1; /* we have at least one cpu ;) */
240 while (fgets(line,CPULINELEN,cpuinfo)!=EOF) {
241 if (strncmp("processor",line,9)==0) {
249 if (verbose) log_text("/proc/cpuinfo did not contain processor, assuming 1 available processor");
250 return 1; /* we have at least one cpu ;) */
258 * Class: java_lang_Runtime
259 * Method: availableProcessors
262 JNIEXPORT s4 JNICALL Java_java_lang_Runtime_availableProcessors(JNIEnv *env, java_lang_Runtime *this)
264 /* log_text("Java_java_lang_Runtime_availableProcessors called, returning hardcoded 1"); */
265 /* the classpath documentations says, that the number of available cpu's for the vm could vary. No idea how to really determine
266 that. Let's try to find out the total number of cpus in the system for now (jowenn) */
268 /* If this gets more complex, it should be considered moving this into the jit/ subdirectory (jowenn)*/
269 #if (!defined(__LINUX__))
272 #if defined(__ALPHA__)
273 return getCPUCount();
275 #if (defined(__I386__) || defined(__X86_64__))
276 return getCPUCount();
286 * Class: java_lang_Runtime
288 * Signature: (Ljava/lang/String;)I
290 JNIEXPORT s4 JNICALL Java_java_lang_Runtime_nativeLoad(JNIEnv *env, java_lang_Runtime *this, java_lang_String *par1)
297 data = javastring_toutf(par1, 0);
300 log_text("nativeLoad: Error: empty string");
304 buffer_len = utf_strlen(data) + 40;
307 buffer = MNEW(char, buffer_len);
309 strcpy(buffer, "Java_java_lang_Runtime_nativeLoad:");
310 utf_sprint(buffer + strlen((char *) data), data);
313 MFREE(buffer, char, buffer_len);
315 log_text("Java_java_lang_Runtime_nativeLoad");
322 * Class: java_lang_Runtime
323 * Method: nativeGetLibname
324 * Signature: (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
326 JNIEXPORT java_lang_String* JNICALL Java_java_lang_Runtime_nativeGetLibname(JNIEnv *env, jclass clazz, java_lang_String *par1, java_lang_String *par2)
333 data = javastring_toutf(par2, 0);
336 log_text("nativeGetLibName: Error: empty string");
340 buffer_len = utf_strlen(data) + 40;
342 buffer = MNEW(char, buffer_len);
344 strcpy(buffer, "Java_java_lang_Runtime_nativeGetLibname:");
345 utf_sprint(buffer + strlen((char *) data), data);
348 MFREE(buffer, char, buffer_len);
350 log_text("Java_java_lang_Runtime_nativeGetLibname");
357 * Class: java_lang_Runtime
358 * Method: insertSystemProperties
359 * Signature: (Ljava/util/Properties;)V
361 JNIEXPORT void JNICALL Java_java_lang_Runtime_insertSystemProperties(JNIEnv *env, jclass clazz, java_util_Properties *p)
364 #define BUFFERSIZE 200
367 char buffer[BUFFERSIZE];
368 struct utsname utsnamebuf;
370 proplist[0][1] = classpath;
371 proplist[1][1] = getenv("JAVA_HOME");
372 proplist[2][1] = getenv("HOME");
373 proplist[3][1] = getenv("USER");
374 proplist[4][1] = getcwd(buffer, BUFFERSIZE);
376 /* get properties from system */
378 proplist[5][1] = utsnamebuf.machine;
379 proplist[6][1] = utsnamebuf.sysname;
380 proplist[7][1] = utsnamebuf.release;
383 panic("Java_java_lang_Runtime_insertSystemProperties called with NULL-Argument");
385 /* search for method to add properties */
386 m = class_resolvemethod(p->header.vftbl->class,
388 utf_new_char("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;")
392 panic("Can not find method 'put' for class Properties");
394 /* add the properties */
395 for (i = 0; i < activeprops; i++) {
397 if (proplist[i][1] == NULL) proplist[i][1] = "";
399 asm_calljavafunction(m,
401 javastring_new_char(proplist[i][0]),
402 javastring_new_char(proplist[i][1]),
412 * Class: java_lang_Runtime
416 JNIEXPORT s8 JNICALL Java_java_lang_Runtime_maxMemory(JNIEnv *env, java_lang_Runtime *this)
418 log_text("Java_java_lang_Runtime_maxMemory");
425 * These are local overrides for various environment variables in Emacs.
426 * Please do not remove this and leave it at the end of the file, where
427 * Emacs will automagically detect them.
428 * ---------------------------------------------------------------------
431 * indent-tabs-mode: t