Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
dc3d26271b2ee3fc6037d64fa5c1d9a18ffe77f3
[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 #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                 m_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(m_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         m_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 m_process_t jprocess_to_native_process(jobject jprocess, JNIEnv * env)
50 {
51   return (m_process_t) (long) (*env)->GetLongField(env, jprocess, jprocess_field_Process_bind);
52 }
53
54 void jprocess_bind(jobject jprocess, m_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   m_process_t process;          /* the native process to create                         */
108   m_host_t host;                /* Where that process lives */
109
110   hostname = (*env)->GetStringUTFChars(env, jhostname, 0);
111
112   XBT_DEBUG("Java_org_simgrid_msg_MsgNative_processCreate(env=%p,jproc=%p,host=%s)",
113          env, jprocess_arg, hostname);
114
115
116   /* get the name of the java process */
117   jname = jprocess_get_name(jprocess_arg, env);
118   if (!jname) {
119     jxbt_throw_null(env,
120             xbt_strdup("Internal error: Process name cannot be NULL"));
121     return;
122   }
123
124   /* bind/retrieve the msg host */
125   host = MSG_get_host_by_name(hostname);
126
127   if (!(host)) {    /* not binded */
128     jxbt_throw_host_not_found(env, hostname);
129     return;
130   }
131
132   /* create a global java process instance */
133   jprocess = jprocess_new_global_ref(jprocess_arg, env);
134   if (!jprocess) {
135     jxbt_throw_jni(env, "Can't get a global ref to the java process");
136     return;
137   }
138
139   /* build the C name of the process */
140   name = (*env)->GetStringUTFChars(env, jname, 0);
141   name = xbt_strdup(name);
142
143   /* Retrieve the kill time from the process */
144   jdouble jkill = (*env)->GetDoubleField(env, jprocess, jprocess_field_Process_killTime);
145   /* Actually build the MSG process */
146   process = MSG_process_create_with_environment(name,
147                                                 (xbt_main_func_t) jprocess,
148                                                 /*data*/ jprocess,
149                                                 host,
150                                                 /*argc, argv, properties*/
151                                                 0,NULL,NULL);
152   MSG_process_set_kill_time(process, (double)jkill);
153   MSG_process_set_data(process,&process);
154   /* bind the java process instance to the native process */
155   jprocess_bind(jprocess, process, env);
156
157   /* release our reference to the process name (variable name becomes invalid) */
158   //FIXME : This line should be uncommented but with mac it doesn't work. BIG WARNING
159   //(*env)->ReleaseStringUTFChars(env, jname, name);
160   (*env)->ReleaseStringUTFChars(env, jhostname, hostname);
161
162   /* sets the PID and the PPID of the process */
163   (*env)->SetIntField(env, jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
164   (*env)->SetIntField(env, jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
165   /* sets the Host of the process */
166   jobject jhost = Java_org_simgrid_msg_Host_getByName(env,NULL,jhostname);
167
168   (*env)->SetObjectField(env, jprocess, jprocess_field_Process_host, jhost);
169 }
170
171 JNIEXPORT jint JNICALL
172 Java_org_simgrid_msg_Process_killAll(JNIEnv * env, jclass cls,
173                                      jint jresetPID)
174 {
175   return (jint) MSG_process_killall((int) jresetPID);
176 }
177
178 JNIEXPORT jobject JNICALL
179 Java_org_simgrid_msg_Process_fromPID(JNIEnv * env, jclass cls,
180                                      jint PID)
181 {
182   m_process_t process = MSG_process_from_PID(PID);
183
184   if (!process) {
185     jxbt_throw_process_not_found(env, bprintf("PID = %d",(int) PID));
186     return NULL;
187   }
188
189   jobject jprocess = native_to_java_process(process);
190
191   if (!jprocess) {
192     jxbt_throw_jni(env, "SIMIX_process_get_jprocess() failed");
193     return NULL;
194   }
195
196   return jprocess;
197 }
198
199 JNIEXPORT jobject JNICALL
200 Java_org_simgrid_msg_Process_currentProcess(JNIEnv * env, jclass cls)
201 {
202   m_process_t process = MSG_process_self();
203   jobject jprocess;
204
205   if (!process) {
206     jxbt_throw_jni(env, xbt_strdup("MSG_process_self() failed"));
207     return NULL;
208   }
209
210   jprocess = native_to_java_process(process);
211
212   if (!jprocess)
213     jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
214
215   return jprocess;
216 }
217
218 JNIEXPORT void JNICALL
219 Java_org_simgrid_msg_Process_suspend(JNIEnv * env,
220                                    jobject jprocess)
221 {
222   m_process_t process = jprocess_to_native_process(jprocess, env);
223
224   if (!process) {
225     jxbt_throw_notbound(env, "process", jprocess);
226     return;
227   }
228
229   /* try to suspend the process */
230   MSG_error_t rv = MSG_process_suspend(process);
231
232   jxbt_check_res("MSG_process_suspend()", rv, MSG_OK,
233                  bprintf("unexpected error , please report this bug"));
234
235 }
236 JNIEXPORT void JNICALL
237 Java_org_simgrid_msg_Process_resume(JNIEnv * env,
238                                      jobject jprocess)
239 {
240   m_process_t process = jprocess_to_native_process(jprocess, env);
241
242   if (!process) {
243     jxbt_throw_notbound(env, "process", jprocess);
244     return;
245   }
246
247   /* try to resume the process */
248   MSG_error_t rv = MSG_process_resume(process);
249
250   jxbt_check_res("MSG_process_resume()", rv, MSG_OK,
251                  bprintf("unexpected error , please report this bug"));
252 }
253 JNIEXPORT jboolean JNICALL
254 Java_org_simgrid_msg_Process_isSuspended(JNIEnv * env,
255                                          jobject jprocess)
256 {
257   m_process_t process = jprocess_to_native_process(jprocess, env);
258
259   if (!process) {
260     jxbt_throw_notbound(env, "process", jprocess);
261     return 0;
262   }
263
264   /* true is the process is suspended, false otherwise */
265   return (jboolean) MSG_process_is_suspended(process);
266 }
267
268 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_sleep
269         (JNIEnv *env, jclass cls, jlong jmillis, jint jnanos) {
270
271         double time =  jmillis / 1000 + jnanos / 1000;
272         MSG_error_t rv;
273         TRY {
274                 rv = MSG_process_sleep(time);
275         }
276         CATCH_ANONYMOUS {
277                 return;
278         }
279   if (rv != MSG_OK) {
280         jmsg_throw_status(env,rv);
281   }
282 }
283 JNIEXPORT void JNICALL
284 Java_org_simgrid_msg_Process_waitFor(JNIEnv * env, jobject jprocess,
285                                      jdouble jseconds)
286 {
287         MSG_error_t rv;
288   TRY {
289          rv = MSG_process_sleep((double)jseconds);
290   }
291   CATCH_ANONYMOUS {
292         return;
293   }
294   if (rv != MSG_OK) {
295         XBT_INFO("Status NOK");
296         jmsg_throw_status(env,rv);
297   }
298 }
299
300 JNIEXPORT void JNICALL
301 Java_org_simgrid_msg_Process_kill(JNIEnv * env,
302                                   jobject jprocess)
303 {
304         /* get the native instances from the java ones */
305   m_process_t process = jprocess_to_native_process(jprocess, env);
306   if (!process) {
307     jxbt_throw_notbound(env, "process", jprocess);
308     return;
309   }
310
311         MSG_process_kill(process);
312 }
313 JNIEXPORT void JNICALL
314 Java_org_simgrid_msg_Process_migrate(JNIEnv * env,
315                                      jobject jprocess, jobject jhost)
316 {
317   m_process_t process = jprocess_to_native_process(jprocess, env);
318
319   if (!process) {
320     jxbt_throw_notbound(env, "process", jprocess);
321     return;
322   }
323
324   m_host_t host = jhost_get_native(env, jhost);
325
326   if (!host) {
327     jxbt_throw_notbound(env, "host", jhost);
328     return;
329   }
330
331   /* try to change the host of the process */
332   MSG_error_t rv = MSG_process_migrate(process, host);
333   jxbt_check_res("MSG_process_migrate()", rv, MSG_OK,
334                  bprintf("unexpected error , please report this bug"));
335   /* change the host java side */
336   (*env)->SetObjectField(env, jprocess, jprocess_field_Process_host, jhost);
337 }
338 JNIEXPORT void JNICALL
339 Java_org_simgrid_msg_Process_setKillTime (JNIEnv *env , jobject jprocess, jdouble jkilltime) {
340         m_process_t process = jprocess_to_native_process(jprocess, env);
341         MSG_process_set_kill_time(process, (double)jkilltime);
342 }