Logo AND Algorithmique Numérique Distribuée

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