Logo AND Algorithmique Numérique Distribuée

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