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