Merged revisions 7797-7917 via svnmerge from
[cacao.git] / src / native / vm / gnu / java_lang_VMRuntime.c
1 /* src/native/vm/gnu/java_lang_VMRuntime.c
2
3    Copyright (C) 1996-2005, 2006, 2007 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
7
8    This file is part of CACAO.
9
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.
14
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.
19
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
23    02110-1301, USA.
24
25    $Id: java_lang_VMRuntime.c 7910 2007-05-16 08:02:52Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <sys/utsname.h>
36
37 #if defined(__DARWIN__)
38 # define OS_INLINE    /* required for <libkern/ppc/OSByteOrder.h> */
39 # include <mach/mach.h>
40 #endif
41
42 #include "vm/types.h"
43
44 #include "mm/gc-common.h"
45 #include "mm/memory.h"
46
47 #include "native/jni.h"
48 #include "native/native.h"
49
50 #include "native/include/java_io_File.h"
51 #include "native/include/java_lang_ClassLoader.h"
52 #include "native/include/java_lang_String.h"
53 #include "native/include/java_lang_Process.h"
54
55 #include "native/include/java_lang_VMRuntime.h"
56
57 #include "native/vm/java_lang_Runtime.h"
58
59 #include "vm/builtin.h"
60 #include "vm/exceptions.h"
61 #include "vm/stringlocal.h"
62 #include "vm/vm.h"
63
64
65 /* this should work on BSD */
66 /*
67 #if defined(__DARWIN__)
68 #include <sys/sysctl.h>
69 #endif
70 */
71
72
73 /* native methods implemented by this file ************************************/
74
75 static JNINativeMethod methods[] = {
76         { "exit",                   "(I)V",                                         (void *) (ptrint) &Java_java_lang_VMRuntime_exit                   },
77         { "freeMemory",             "()J",                                          (void *) (ptrint) &Java_java_lang_VMRuntime_freeMemory             },
78         { "totalMemory",            "()J",                                          (void *) (ptrint) &Java_java_lang_VMRuntime_totalMemory            },
79         { "maxMemory",              "()J",                                          (void *) (ptrint) &Java_java_lang_VMRuntime_maxMemory              },
80         { "gc",                     "()V",                                          (void *) (ptrint) &Java_java_lang_VMRuntime_gc                     },
81         { "runFinalization",        "()V",                                          (void *) (ptrint) &Java_java_lang_VMRuntime_runFinalization        },
82         { "runFinalizersOnExit",    "(Z)V",                                         (void *) (ptrint) &Java_java_lang_VMRuntime_runFinalizersOnExit    },
83         { "runFinalizationForExit", "()V",                                          (void *) (ptrint) &Java_java_lang_VMRuntime_runFinalizationForExit },
84         { "traceInstructions",      "(Z)V",                                         (void *) (ptrint) &Java_java_lang_VMRuntime_traceInstructions      },
85         { "traceMethodCalls",       "(Z)V",                                         (void *) (ptrint) &Java_java_lang_VMRuntime_traceMethodCalls       },
86         { "availableProcessors",    "()I",                                          (void *) (ptrint) &Java_java_lang_VMRuntime_availableProcessors    },
87         { "nativeLoad",             "(Ljava/lang/String;Ljava/lang/ClassLoader;)I", (void *) (ptrint) &Java_java_lang_VMRuntime_nativeLoad             },
88         { "mapLibraryName",         "(Ljava/lang/String;)Ljava/lang/String;",       (void *) (ptrint) &Java_java_lang_VMRuntime_mapLibraryName         },
89 };
90
91
92 /* _Jv_java_lang_VMRuntime_init ************************************************
93
94    Register native functions.
95
96 *******************************************************************************/
97
98 void _Jv_java_lang_VMRuntime_init(void)
99 {
100         utf *u;
101
102         u = utf_new_char("java/lang/VMRuntime");
103
104         native_method_register(u, methods, NATIVE_METHODS_COUNT);
105 }
106
107
108 /*
109  * Class:     java/lang/VMRuntime
110  * Method:    exit
111  * Signature: (I)V
112  */
113 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_exit(JNIEnv *env, jclass clazz, s4 status)
114 {
115         _Jv_java_lang_Runtime_exit(status);
116 }
117
118
119 /*
120  * Class:     java/lang/VMRuntime
121  * Method:    freeMemory
122  * Signature: ()J
123  */
124 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_freeMemory(JNIEnv *env, jclass clazz)
125 {
126         return _Jv_java_lang_Runtime_freeMemory();
127 }
128
129
130 /*
131  * Class:     java/lang/VMRuntime
132  * Method:    totalMemory
133  * Signature: ()J
134  */
135 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_totalMemory(JNIEnv *env, jclass clazz)
136 {
137         return _Jv_java_lang_Runtime_totalMemory();
138 }
139
140
141 /*
142  * Class:     java/lang/VMRuntime
143  * Method:    maxMemory
144  * Signature: ()J
145  */
146 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_maxMemory(JNIEnv *env, jclass clazz)
147 {
148         return gc_get_max_heap_size();
149 }
150
151
152 /*
153  * Class:     java/lang/VMRuntime
154  * Method:    gc
155  * Signature: ()V
156  */
157 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_gc(JNIEnv *env, jclass clazz)
158 {
159         _Jv_java_lang_Runtime_gc();
160 }
161
162
163 /*
164  * Class:     java/lang/VMRuntime
165  * Method:    runFinalization
166  * Signature: ()V
167  */
168 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalization(JNIEnv *env, jclass clazz)
169 {
170         gc_invoke_finalizers();
171 }
172
173
174 /*
175  * Class:     java/lang/VMRuntime
176  * Method:    runFinalizersOnExit
177  * Signature: (Z)V
178  */
179 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizersOnExit(JNIEnv *env, jclass clazz, s4 value)
180 {
181         _Jv_java_lang_Runtime_runFinalizersOnExit(value);
182 }
183
184
185 /*
186  * Class:     java/lang/VMRuntime
187  * Method:    runFinalizationsForExit
188  * Signature: ()V
189  */
190 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizationForExit(JNIEnv *env, jclass clazz)
191 {
192 /*      if (finalizeOnExit) { */
193 /*              gc_call(); */
194         /* gc_finalize_all(); */
195 /*      } */
196 /*      log_text("Java_java_lang_VMRuntime_runFinalizationForExit called"); */
197         /*gc_finalize_all();*/
198         /*gc_invoke_finalizers();*/
199         /*gc_call();*/
200 }
201
202
203 /*
204  * Class:     java/lang/VMRuntime
205  * Method:    traceInstructions
206  * Signature: (Z)V
207  */
208 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceInstructions(JNIEnv *env, jclass clazz, s4 par1)
209 {
210         /* not supported */
211 }
212
213
214 /*
215  * Class:     java/lang/VMRuntime
216  * Method:    traceMethodCalls
217  * Signature: (Z)V
218  */
219 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceMethodCalls(JNIEnv *env, jclass clazz, s4 par1)
220 {
221         /* not supported */
222 }
223
224
225 /*
226  * Class:     java/lang/VMRuntime
227  * Method:    availableProcessors
228  * Signature: ()I
229  */
230 JNIEXPORT s4 JNICALL Java_java_lang_VMRuntime_availableProcessors(JNIEnv *env, jclass clazz)
231 {
232 #if defined(_SC_NPROC_ONLN)
233         return (s4) sysconf(_SC_NPROC_ONLN);
234
235 #elif defined(_SC_NPROCESSORS_ONLN)
236         return (s4) sysconf(_SC_NPROCESSORS_ONLN);
237
238 #elif defined(__DARWIN__)
239         /* this should work in BSD */
240         /*
241         int ncpu, mib[2], rc;
242         size_t len;
243
244         mib[0] = CTL_HW;
245         mib[1] = HW_NCPU;
246         len = sizeof(ncpu);
247         rc = sysctl(mib, 2, &ncpu, &len, NULL, 0);
248
249         return (s4) ncpu;
250         */
251
252         host_basic_info_data_t hinfo;
253         mach_msg_type_number_t hinfo_count = HOST_BASIC_INFO_COUNT;
254         kern_return_t rc;
255
256         rc = host_info(mach_host_self(), HOST_BASIC_INFO,
257                                    (host_info_t) &hinfo, &hinfo_count);
258  
259         if (rc != KERN_SUCCESS) {
260                 return -1;
261         }
262
263     return (s4) hinfo.avail_cpus;
264
265 #else
266         return 1;
267 #endif
268 }
269
270
271 /*
272  * Class:     java/lang/VMRuntime
273  * Method:    nativeLoad
274  * Signature: (Ljava/lang/String;Ljava/lang/ClassLoader;)I
275  */
276 JNIEXPORT s4 JNICALL Java_java_lang_VMRuntime_nativeLoad(JNIEnv *env, jclass clazz, java_lang_String *libname, java_lang_ClassLoader *loader)
277 {
278         java_objectheader *cl;
279
280         cl = (java_objectheader *) loader;
281
282 #if defined(ENABLE_JNI)
283         return _Jv_java_lang_Runtime_loadLibrary(env, libname, cl);
284 #else
285         return _Jv_java_lang_Runtime_loadLibrary(libname, cl);
286 #endif
287 }
288
289
290 /*
291  * Class:     java/lang/VMRuntime
292  * Method:    mapLibraryName
293  * Signature: (Ljava/lang/String;)Ljava/lang/String;
294  */
295 JNIEXPORT java_lang_String* JNICALL Java_java_lang_VMRuntime_mapLibraryName(JNIEnv *env, jclass clazz, java_lang_String *libname)
296 {
297         utf               *u;
298         char              *buffer;
299         s4                 buffer_len;
300         s4                 dumpsize;
301         java_objectheader *o;
302
303         if (libname == NULL) {
304                 exceptions_throw_nullpointerexception();
305                 return NULL;
306         }
307
308         u = javastring_toutf((java_objectheader *) libname, false);
309
310         /* calculate length of library name */
311
312         buffer_len = strlen("lib");
313
314         buffer_len += utf_bytes(u);
315
316 #if defined(__DARWIN__)
317         buffer_len += strlen(".dylib");
318 #else
319         buffer_len += strlen(".so");
320 #endif
321
322         buffer_len += strlen("0");
323
324         dumpsize = dump_size();
325         buffer = DMNEW(char, buffer_len);
326
327         /* generate library name */
328
329         strcpy(buffer, "lib");
330         utf_cat(buffer, u);
331
332 #if defined(__DARWIN__)
333         strcat(buffer, ".dylib");
334 #else
335         strcat(buffer, ".so");
336 #endif
337
338         o = javastring_new_from_utf_string(buffer);
339
340         /* release memory */
341
342         dump_release(dumpsize);
343
344         return (java_lang_String *) o;
345 }
346
347
348 /*
349  * These are local overrides for various environment variables in Emacs.
350  * Please do not remove this and leave it at the end of the file, where
351  * Emacs will automagically detect them.
352  * ---------------------------------------------------------------------
353  * Local variables:
354  * mode: c
355  * indent-tabs-mode: t
356  * c-basic-offset: 4
357  * tab-width: 4
358  * End:
359  * vim:noexpandtab:sw=4:ts=4:
360  */