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