Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b78ba98d48dab85d0d097072b0a2b164443f1343
[simgrid.git] / src / bindings / java / jmsg_process.c
1 /* Functions related to the java process instances.                         */
2
3 /* Copyright (c) 2007-2012. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7   * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "jmsg_process.h"
9
10 #include "jmsg.h"
11 #include "jmsg_host.h"
12 #include "jxbt_utilities.h"
13 #include "smx_context_java.h"
14 #include "smx_context_cojava.h"
15
16 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
17
18 jfieldID jprocess_field_Process_bind;
19 jfieldID jprocess_field_Process_host;
20 jfieldID jprocess_field_Process_killTime;
21 jfieldID jprocess_field_Process_id;
22 jfieldID jprocess_field_Process_name;
23 jfieldID jprocess_field_Process_pid;
24 jfieldID jprocess_field_Process_ppid;
25
26 JNIEXPORT void JNICALL
27 Java_org_simgrid_msg_Process_exit(JNIEnv *env, jobject jprocess) {
28   if (smx_factory_initializer_to_use == SIMIX_ctx_cojava_factory_init) {
29     msg_process_t process = jprocess_to_native_process(jprocess, env);
30     smx_context_t context = MSG_process_get_smx_ctx(process);
31     smx_ctx_cojava_stop(context);
32   }
33 }
34
35 jobject native_to_java_process(msg_process_t process)
36 {
37   return ((smx_ctx_java_t)MSG_process_get_smx_ctx(process))->jprocess;
38 }
39
40 jobject jprocess_new_global_ref(jobject jprocess, JNIEnv * env)
41 {
42   return (*env)->NewGlobalRef(env, jprocess);
43 }
44
45 void jprocess_delete_global_ref(jobject jprocess, JNIEnv * env)
46 {
47   (*env)->DeleteGlobalRef(env, jprocess);
48 }
49
50 void jprocess_join(jobject jprocess, JNIEnv * env)
51 {
52         msg_process_t process = jprocess_to_native_process(jprocess,env);
53         smx_ctx_java_t context = (smx_ctx_java_t)MSG_process_get_smx_ctx(process);
54         xbt_os_thread_join(context->thread,NULL);
55 }
56
57 msg_process_t jprocess_to_native_process(jobject jprocess, JNIEnv * env)
58 {
59   return
60     (msg_process_t)(intptr_t)(*env)->GetLongField(env, jprocess,
61                                                   jprocess_field_Process_bind);
62 }
63
64 void jprocess_bind(jobject jprocess, msg_process_t process, JNIEnv * env)
65 {
66   (*env)->SetLongField(env, jprocess, jprocess_field_Process_bind,
67                        (intptr_t)process);
68 }
69
70 jlong jprocess_get_id(jobject jprocess, JNIEnv * env)
71 {
72   return
73     (intptr_t)(*env)->GetLongField(env, jprocess, jprocess_field_Process_id);
74 }
75
76 jstring jprocess_get_name(jobject jprocess, JNIEnv * env)
77 {
78   jstring jname = (jstring) (*env)->GetObjectField(env, jprocess, jprocess_field_Process_name);
79   return (*env)->NewGlobalRef(env, jname);
80
81 }
82
83 jboolean jprocess_is_valid(jobject jprocess, JNIEnv * env)
84 {
85   jfieldID id = jxbt_get_sfield(env, "org/simgrid/msg/Process", "bind", "J");
86
87   if (!id)
88     return JNI_FALSE;
89
90   return (*env)->GetLongField(env, jprocess, id) ? JNI_TRUE : JNI_FALSE;
91 }
92 JNIEXPORT void JNICALL
93 Java_org_simgrid_msg_Process_nativeInit(JNIEnv *env, jclass cls) {
94   jclass jprocess_class_Process = (*env)->FindClass(env, "org/simgrid/msg/Process");
95
96   jprocess_field_Process_name = jxbt_get_jfield(env, jprocess_class_Process, "name", "Ljava/lang/String;");
97   jprocess_field_Process_bind = jxbt_get_jfield(env, jprocess_class_Process, "bind", "J");
98   jprocess_field_Process_id = jxbt_get_jfield(env, jprocess_class_Process, "id", "J");
99   jprocess_field_Process_pid = jxbt_get_jfield(env, jprocess_class_Process, "pid", "I");
100   jprocess_field_Process_ppid = jxbt_get_jfield(env, jprocess_class_Process, "ppid", "I");
101   jprocess_field_Process_host = jxbt_get_jfield(env, jprocess_class_Process, "host", "Lorg/simgrid/msg/Host;");
102   jprocess_field_Process_killTime = jxbt_get_jfield(env, jprocess_class_Process, "killTime", "D");
103   if (!jprocess_class_Process || !jprocess_field_Process_id || !jprocess_field_Process_name || !jprocess_field_Process_pid ||
104       !jprocess_field_Process_ppid || !jprocess_field_Process_host) {
105     jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
106   }
107 }
108 JNIEXPORT void JNICALL
109 Java_org_simgrid_msg_Process_create(JNIEnv * env,
110                                     jobject jprocess_arg,
111                                     jobject jhostname)
112 {
113
114
115   jobject jprocess;             /* the global reference to the java process instance    */
116   jstring jname;                /* the name of the java process instance                */
117   const char *name;             /* the C name of the process                            */
118   const char *hostname;
119   msg_process_t process;          /* the native process to create                         */
120   msg_host_t host;                /* Where that process lives */
121
122   hostname = (*env)->GetStringUTFChars(env, jhostname, 0);
123
124   /* get the name of the java process */
125   jname = jprocess_get_name(jprocess_arg, env);
126   if (!jname) {
127     jxbt_throw_null(env,
128             xbt_strdup("Internal error: Process name cannot be NULL"));
129     return;
130   }
131
132   /* bind/retrieve the msg host */
133   host = MSG_get_host_by_name(hostname);
134
135   if (!(host)) {    /* not binded */
136     jxbt_throw_host_not_found(env, hostname);
137     return;
138   }
139
140   /* create a global java process instance */
141   jprocess = jprocess_new_global_ref(jprocess_arg, env);
142   if (!jprocess) {
143     jxbt_throw_jni(env, "Can't get a global ref to the java process");
144     return;
145   }
146
147   /* build the C name of the process */
148   name = (*env)->GetStringUTFChars(env, jname, 0);
149   name = xbt_strdup(name);
150
151   /* Retrieve the kill time from the process */
152   jdouble jkill = (*env)->GetDoubleField(env, jprocess, jprocess_field_Process_killTime);
153   /* Actually build the MSG process */
154   process = MSG_process_create_with_environment(name,
155                                                 (xbt_main_func_t) jprocess,
156                                                 /*data*/ jprocess,
157                                                 host,
158                                                 /*argc, argv, properties*/
159                                                 0,NULL,NULL);
160   MSG_process_set_kill_time(process, (double)jkill);
161   /* bind the java process instance to the native process */
162   jprocess_bind(jprocess, process, env);
163
164   /* release our reference to the process name (variable name becomes invalid) */
165   //FIXME : This line should be uncommented but with mac it doesn't work. BIG WARNING
166   //(*env)->ReleaseStringUTFChars(env, jname, name);
167   (*env)->ReleaseStringUTFChars(env, jhostname, hostname);
168
169   /* sets the PID and the PPID of the process */
170   (*env)->SetIntField(env, jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
171   (*env)->SetIntField(env, jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
172   /* sets the Host of the process */
173   jobject jhost = Java_org_simgrid_msg_Host_getByName(env,NULL,jhostname);
174
175   (*env)->SetObjectField(env, jprocess, jprocess_field_Process_host, jhost);
176 }
177
178 JNIEXPORT jint JNICALL
179 Java_org_simgrid_msg_Process_killAll(JNIEnv * env, jclass cls,
180                                      jint jresetPID)
181 {
182   return (jint) MSG_process_killall((int) jresetPID);
183 }
184
185 JNIEXPORT jobject JNICALL
186 Java_org_simgrid_msg_Process_fromPID(JNIEnv * env, jclass cls,
187                                      jint PID)
188 {
189   msg_process_t process = MSG_process_from_PID(PID);
190
191   if (!process) {
192     jxbt_throw_process_not_found(env, bprintf("PID = %d",(int) PID));
193     return NULL;
194   }
195
196   jobject jprocess = native_to_java_process(process);
197
198   if (!jprocess) {
199     jxbt_throw_jni(env, "get process failed");
200     return NULL;
201   }
202
203   return jprocess;
204 }
205 JNIEXPORT jobject JNICALL
206 Java_org_simgrid_msg_Process_getProperty(JNIEnv *env, jobject jprocess, jobject jname) {
207   msg_process_t process = jprocess_to_native_process(jprocess, env);
208
209   if (!process) {
210     jxbt_throw_notbound(env, "process", jprocess);
211     return NULL;
212   }
213   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
214
215   const char *property = MSG_process_get_property_value(process, name);
216   if (!property) {
217     return NULL;
218   }
219
220   jobject jproperty = (*env)->NewStringUTF(env, property);
221
222   (*env)->ReleaseStringUTFChars(env, jname, name);
223
224   return jproperty;
225 }
226 JNIEXPORT jobject JNICALL
227 Java_org_simgrid_msg_Process_currentProcess(JNIEnv * env, jclass cls)
228 {
229   msg_process_t process = MSG_process_self();
230   jobject jprocess;
231
232   if (!process) {
233     jxbt_throw_jni(env, xbt_strdup("MSG_process_self() failed"));
234     return NULL;
235   }
236
237   jprocess = native_to_java_process(process);
238
239   if (!jprocess)
240     jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
241
242   return jprocess;
243 }
244
245 JNIEXPORT void JNICALL
246 Java_org_simgrid_msg_Process_suspend(JNIEnv * env,
247                                    jobject jprocess)
248 {
249   msg_process_t process = jprocess_to_native_process(jprocess, env);
250
251   if (!process) {
252     jxbt_throw_notbound(env, "process", jprocess);
253     return;
254   }
255
256   /* try to suspend the process */
257   msg_error_t rv = MSG_process_suspend(process);
258
259   jxbt_check_res("MSG_process_suspend()", rv, MSG_OK,
260                  bprintf("unexpected error , please report this bug"));
261
262 }
263
264 JNIEXPORT void JNICALL
265 Java_org_simgrid_msg_Process_resume(JNIEnv * env,
266                                      jobject jprocess)
267 {
268   msg_process_t process = jprocess_to_native_process(jprocess, env);
269
270   if (!process) {
271     jxbt_throw_notbound(env, "process", jprocess);
272     return;
273   }
274
275   /* try to resume the process */
276   msg_error_t rv = MSG_process_resume(process);
277
278   jxbt_check_res("MSG_process_resume()", rv, MSG_OK,
279                  bprintf("unexpected error , please report this bug"));
280 }
281 JNIEXPORT void JNICALL
282 Java_org_simgrid_msg_Process_setAutoRestart
283     (JNIEnv *env, jobject jprocess, jboolean jauto_restart) {
284   msg_process_t process = jprocess_to_native_process(jprocess, env);
285   xbt_ex_t e;
286
287   int auto_restart = jauto_restart == JNI_TRUE ? 1 : 0;
288
289   if (!process) {
290     jxbt_throw_notbound(env, "process", jprocess);
291     return;
292   }
293
294   TRY {
295     MSG_process_auto_restart_set(process,auto_restart);
296   }
297   CATCH (e) {
298     xbt_ex_free(e);
299   }
300 }
301 JNIEXPORT void JNICALL
302 Java_org_simgrid_msg_Process_restart
303     (JNIEnv *env, jobject jprocess) {
304   msg_process_t process = jprocess_to_native_process(jprocess, env);
305   xbt_ex_t e;
306
307   if (!process) {
308     jxbt_throw_notbound(env, "process", jprocess);
309     return;
310   }
311
312   TRY {
313     MSG_process_restart(process);
314   }
315   CATCH (e) {
316     xbt_ex_free(e);
317   }
318
319 }
320 JNIEXPORT jboolean JNICALL
321 Java_org_simgrid_msg_Process_isSuspended(JNIEnv * env,
322                                          jobject jprocess)
323 {
324   msg_process_t process = jprocess_to_native_process(jprocess, env);
325
326   if (!process) {
327     jxbt_throw_notbound(env, "process", jprocess);
328     return 0;
329   }
330
331   /* true is the process is suspended, false otherwise */
332   return (jboolean) MSG_process_is_suspended(process);
333 }
334
335 JNIEXPORT void JNICALL
336 Java_org_simgrid_msg_Process_sleep(JNIEnv *env, jclass cls, jlong jmillis, jint jnanos)
337  {
338   double time =  jmillis / 1000 + jnanos / 1000;
339   msg_error_t rv;
340   rv = MSG_process_sleep(time);
341   if (rv != MSG_OK) {
342     jmsg_throw_status(env,rv);
343   }
344 }
345 JNIEXPORT void JNICALL
346 Java_org_simgrid_msg_Process_waitFor(JNIEnv * env, jobject jprocess,
347                                      jdouble jseconds)
348 {
349   msg_error_t rv;
350   rv = MSG_process_sleep((double)jseconds);
351   if (rv != MSG_OK) {
352     XBT_DEBUG("Status NOK");
353     jmsg_throw_status(env,rv);
354   }
355 }
356
357 JNIEXPORT void JNICALL
358 Java_org_simgrid_msg_Process_kill(JNIEnv * env,
359                                   jobject jprocess)
360 {
361         /* get the native instances from the java ones */
362   msg_process_t process = jprocess_to_native_process(jprocess, env);
363   if (!process) {
364     jxbt_throw_notbound(env, "process", jprocess);
365     return;
366   }
367
368         MSG_process_kill(process);
369 }
370 JNIEXPORT void JNICALL
371 Java_org_simgrid_msg_Process_migrate(JNIEnv * env,
372                                      jobject jprocess, jobject jhost)
373 {
374   msg_process_t process = jprocess_to_native_process(jprocess, env);
375
376   if (!process) {
377     jxbt_throw_notbound(env, "process", jprocess);
378     return;
379   }
380
381   msg_host_t host = jhost_get_native(env, jhost);
382
383   if (!host) {
384     jxbt_throw_notbound(env, "host", jhost);
385     return;
386   }
387
388   /* try to change the host of the process */
389   msg_error_t rv = MSG_process_migrate(process, host);
390   if (rv != MSG_OK) {
391     jmsg_throw_status(env,rv);
392   }
393   /* change the host java side */
394   (*env)->SetObjectField(env, jprocess, jprocess_field_Process_host, jhost);
395 }
396 JNIEXPORT void JNICALL
397 Java_org_simgrid_msg_Process_setKillTime (JNIEnv *env , jobject jprocess, jdouble jkilltime) {
398         msg_process_t process = jprocess_to_native_process(jprocess, env);
399         MSG_process_set_kill_time(process, (double)jkilltime);
400 }
401
402 JNIEXPORT jint JNICALL
403 Java_org_simgrid_msg_Process_getCount(JNIEnv * env, jclass cls) {
404         /* FIXME: the next test on SimGrid version is to ensure that this still compiles with SG 3.8 while the C function were added in SG 3.9 only.
405          * This kind of pimple becomes mandatory when you get so slow to release the java version that it begins evolving further after the C release date.
406          */
407 #if SIMGRID_VERSION >= 30900
408   return (jint) MSG_process_get_number();
409 #else
410   return (jint) -1;
411 #endif
412 }