Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move the actor lifecycle markers from Context to ActorImpl
[simgrid.git] / src / bindings / java / jmsg_task.cpp
1 /* Functions related to the java task instances.                            */
2
3 /* Copyright (c) 2007-2022. The SimGrid Team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "simgrid/Exception.hpp"
9 #include "simgrid/s4u/Host.hpp"
10
11 #include "jmsg.hpp"
12 #include "jmsg_host.h"
13 #include "jmsg_process.h"
14 #include "jmsg_task.h"
15 #include "jxbt_utilities.hpp"
16
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(java);
18
19 static jmethodID jtask_method_Comm_constructor;
20
21 static jfieldID jtask_field_Task_bind;
22 static jfieldID jtask_field_Task_name;
23 static jfieldID jtask_field_Task_messageSize;
24 static jfieldID jtask_field_Comm_bind;
25 static jfieldID jtask_field_Comm_taskBind;
26 static jfieldID jtask_field_Comm_receiving;
27
28 void jtask_bind(jobject jtask, msg_task_t task, JNIEnv * env)
29 {
30   env->SetLongField(jtask, jtask_field_Task_bind, (intptr_t)task);
31 }
32
33 msg_task_t jtask_to_native(jobject jtask, JNIEnv* env)
34 {
35   return (msg_task_t)(intptr_t)env->GetLongField(jtask, jtask_field_Task_bind);
36 }
37
38 JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_nativeInit(JNIEnv *env, jclass cls) {
39   jclass jtask_class_Comm = env->FindClass("org/simgrid/msg/Comm");
40   jclass jtask_class_Task = env->FindClass("org/simgrid/msg/Task");
41   xbt_assert(jtask_class_Comm && jtask_class_Task,
42              "Native initialization of msg/Comm or msg/Task failed. Please report that bug");
43
44   jtask_method_Comm_constructor = env->GetMethodID(jtask_class_Comm, "<init>", "()V");
45   jtask_field_Task_bind = jxbt_get_jfield(env, jtask_class_Task, "bind", "J");
46   jtask_field_Task_name = jxbt_get_jfield(env, jtask_class_Task, "name", "Ljava/lang/String;");
47   jtask_field_Task_messageSize = jxbt_get_jfield(env, jtask_class_Task, "messageSize", "D");
48   jtask_field_Comm_bind = jxbt_get_jfield(env, jtask_class_Comm, "bind", "J");
49   jtask_field_Comm_taskBind = jxbt_get_jfield(env, jtask_class_Comm, "taskBind", "J");
50   jtask_field_Comm_receiving = jxbt_get_jfield(env, jtask_class_Comm, "receiving", "Z");
51   xbt_assert(jtask_field_Task_bind && jtask_field_Comm_bind && jtask_field_Comm_taskBind &&
52                  jtask_field_Comm_receiving && jtask_method_Comm_constructor,
53              "Native initialization of msg/Task failed. Please report that bug");
54 }
55
56 JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_create(JNIEnv * env, jobject jtask, jstring jname,
57                                       jdouble jflopsAmount, jdouble jbytesAmount)
58 {
59   jstring_wrapper task_name(env, jname);
60   msg_task_t task = MSG_task_create(task_name, jflopsAmount, jbytesAmount, jtask);
61
62   /* bind & store the task */
63   jtask_bind(jtask, task, env);
64 }
65
66 JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_parallelCreate(JNIEnv * env, jobject jtask, jstring jname,
67                                          jobjectArray jhosts, jdoubleArray jcomputeDurations_arg,
68                                          jdoubleArray jmessageSizes_arg)
69 {
70   int host_count = env->GetArrayLength(jhosts);
71
72   jdouble* jcomputeDurations = env->GetDoubleArrayElements(jcomputeDurations_arg, nullptr);
73   auto* hosts                = new msg_host_t[host_count];
74   auto* computeDurations     = new double[host_count];
75   for (int index = 0; index < host_count; index++) {
76     jobject jhost           = env->GetObjectArrayElement(jhosts, index);
77     hosts[index] = jhost_get_native(env, jhost);
78     computeDurations[index] = jcomputeDurations[index];
79   }
80   env->ReleaseDoubleArrayElements(jcomputeDurations_arg, jcomputeDurations, 0);
81
82   jdouble* jmessageSizes = env->GetDoubleArrayElements(jmessageSizes_arg, nullptr);
83   auto* messageSizes     = new double[host_count * host_count];
84   for (int index = 0; index < host_count * host_count; index++) {
85     messageSizes[index] = jmessageSizes[index];
86   }
87   env->ReleaseDoubleArrayElements(jmessageSizes_arg, jmessageSizes, 0);
88
89   /* get the C string from the java string */
90   jstring_wrapper name(env, jname);
91   msg_task_t task  = MSG_parallel_task_create(name, host_count, hosts, computeDurations, messageSizes, jtask);
92
93   /* associate the java task object and the native task */
94   jtask_bind(jtask, task, env);
95
96   delete[] hosts;
97   delete[] computeDurations;
98   delete[] messageSizes;
99 }
100
101 JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_cancel(JNIEnv * env, jobject jtask)
102 {
103   msg_task_t ptask = jtask_to_native(jtask, env);
104
105   if (not ptask) {
106     jxbt_throw_notbound(env, "task", jtask);
107     return;
108   }
109
110   msg_error_t rv = MSG_task_cancel(ptask);
111   xbt_assert(rv == MSG_OK, "MSG_task_cancel() unexpectedly failed with error code %d. Please report this bug", rv);
112 }
113
114 JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_execute(JNIEnv * env, jobject jtask)
115 {
116   msg_task_t task = jtask_to_native(jtask, env);
117
118   if (not task) {
119     jxbt_throw_notbound(env, "task", jtask);
120     return;
121   }
122   msg_error_t rv;
123   if (not simgrid::ForcefulKillException::try_n_catch([&rv, &task]() { rv = MSG_task_execute(task); })) {
124     jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", "Process killed");
125   }
126
127   if (env->ExceptionOccurred())
128     return;
129   if (rv != MSG_OK) {
130     jmsg_throw_status(env, rv);
131   }
132 }
133
134 JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_setBound(JNIEnv * env, jobject jtask, jdouble bound)
135 {
136   msg_task_t task = jtask_to_native(jtask, env);
137
138   if (not task) {
139     jxbt_throw_notbound(env, "task", jtask);
140     return;
141   }
142   MSG_task_set_bound(task, bound);
143 }
144
145 JNIEXPORT jstring JNICALL Java_org_simgrid_msg_Task_getName(JNIEnv * env, jobject jtask) {
146   const_msg_task_t task = jtask_to_native(jtask, env);
147
148   if (not task) {
149     jxbt_throw_notbound(env, "task", jtask);
150     return nullptr;
151   }
152
153   return env->NewStringUTF(MSG_task_get_name(task));
154 }
155
156 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Task_getSender(JNIEnv * env, jobject jtask) {
157   const_msg_task_t task = jtask_to_native(jtask, env);
158
159   if (not task) {
160     jxbt_throw_notbound(env, "task", jtask);
161     return nullptr;
162   }
163
164   auto const* process = MSG_task_get_sender(task);
165   if (process == nullptr) {
166     return nullptr;
167   }
168   return (jobject)jprocess_from_native(process);
169 }
170
171 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Task_getSource(JNIEnv * env, jobject jtask)
172 {
173   const_msg_task_t task = jtask_to_native(jtask, env);
174
175   if (not task) {
176     jxbt_throw_notbound(env, "task", jtask);
177     return nullptr;
178   }
179
180   auto const* host = MSG_task_get_source(task);
181   if (host == nullptr) {
182     return nullptr;
183   }
184   if (not host->extension(JAVA_HOST_LEVEL)) {
185     jxbt_throw_jni(env, "MSG_task_get_source() failed");
186     return nullptr;
187   }
188
189   return (jobject) host->extension(JAVA_HOST_LEVEL);
190 }
191
192 JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Task_getFlopsAmount(JNIEnv * env, jobject jtask)
193 {
194   const_msg_task_t ptask = jtask_to_native(jtask, env);
195
196   if (not ptask) {
197     jxbt_throw_notbound(env, "task", jtask);
198     return -1;
199   }
200   return (jdouble)MSG_task_get_flops_amount(ptask);
201 }
202
203 JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_setName(JNIEnv *env, jobject jtask, jobject jname) {
204   msg_task_t task = jtask_to_native(jtask, env);
205
206   if (not task) {
207     jxbt_throw_notbound(env, "task", jtask);
208     return;
209   }
210   jstring_wrapper name(env, static_cast<jstring>(jname));
211
212   env->SetObjectField(jtask, jtask_field_Task_name, jname);
213   MSG_task_set_name(task, name);
214 }
215
216 JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_setPriority(JNIEnv * env, jobject jtask, jdouble priority)
217 {
218   msg_task_t task = jtask_to_native(jtask, env);
219
220   if (not task) {
221     jxbt_throw_notbound(env, "task", jtask);
222     return;
223   }
224   MSG_task_set_priority(task, priority);
225 }
226
227 JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_setFlopsAmount (JNIEnv *env, jobject jtask, jdouble computationAmount)
228 {
229   msg_task_t task = jtask_to_native(jtask, env);
230
231   if (not task) {
232     jxbt_throw_notbound(env, "task", jtask);
233     return;
234   }
235   MSG_task_set_flops_amount(task, computationAmount);
236 }
237
238 JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_setBytesAmount (JNIEnv *env, jobject jtask, jdouble dataSize)
239 {
240   msg_task_t task = jtask_to_native(jtask, env);
241
242   if (not task) {
243     jxbt_throw_notbound(env, "task", jtask);
244     return;
245   }
246   env->SetDoubleField(jtask, jtask_field_Task_messageSize, dataSize);
247   MSG_task_set_bytes_amount(task, dataSize);
248 }
249
250 JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_sendBounded(JNIEnv * env,jobject jtask, jstring jalias,
251                                                              jdouble jtimeout,jdouble maxrate)
252 {
253   msg_task_t task = jtask_to_native(jtask, env);
254   if (not task) {
255     jxbt_throw_notbound(env, "task", jtask);
256     return;
257   }
258
259   /* Add a global ref into the Ctask so that the receiver can use it */
260   MSG_task_set_data(task, env->NewGlobalRef(jtask));
261
262   jstring_wrapper alias(env, jalias);
263   msg_error_t res   = MSG_task_send_with_timeout_bounded(task, alias, jtimeout, maxrate);
264
265   if (res != MSG_OK)
266     jmsg_throw_status(env, res);
267 }
268
269 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Task_receive(JNIEnv* env, jclass cls, jstring jalias, jdouble jtimeout)
270 {
271   msg_task_t task = nullptr;
272
273   jstring_wrapper alias(env, jalias);
274   msg_error_t rv;
275   if (not simgrid::ForcefulKillException::try_n_catch(
276           [&rv, &task, &alias, &jtimeout]() { rv = MSG_task_receive_with_timeout(&task, alias, (double)jtimeout); })) {
277     jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", "Process killed");
278   }
279   if (env->ExceptionOccurred())
280     return nullptr;
281   if (rv != MSG_OK) {
282     jmsg_throw_status(env, rv);
283     return nullptr;
284   }
285   auto jtask_global = (jobject)MSG_task_get_data(task);
286
287   /* Convert the global ref into a local ref so that the JVM can free the stuff */
288   jobject jtask_local = env->NewLocalRef(jtask_global);
289   env->DeleteGlobalRef(jtask_global);
290   MSG_task_set_data(task, nullptr);
291
292   return (jobject) jtask_local;
293 }
294
295 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Task_irecv(JNIEnv * env, jclass cls, jstring jmailbox) {
296   jclass comm_class = env->FindClass("org/simgrid/msg/Comm");
297   if (not comm_class)
298     return nullptr;
299
300   //pointer to store the task object pointer.
301   auto* task = new msg_task_t(nullptr);
302   /* There should be a cache here */
303
304   jobject jcomm = env->NewObject(comm_class, jtask_method_Comm_constructor);
305   if (not jcomm) {
306     jxbt_throw_jni(env, "Can't create a Comm object.");
307     return nullptr;
308   }
309
310   jstring_wrapper mailbox(env, jmailbox);
311   msg_comm_t comm     = MSG_task_irecv(task, mailbox);
312
313   env->SetLongField(jcomm, jtask_field_Comm_bind, (jlong) (uintptr_t)(comm));
314   env->SetLongField(jcomm, jtask_field_Comm_taskBind, (jlong) (uintptr_t)(task));
315   env->SetBooleanField(jcomm, jtask_field_Comm_receiving, JNI_TRUE);
316
317   return jcomm;
318 }
319
320 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Task_receiveBounded(JNIEnv* env, jclass cls, jstring jalias,
321                                                                    jdouble jtimeout, jdouble rate)
322 {
323   msg_task_t task = nullptr;
324
325   jstring_wrapper alias(env, jalias);
326   msg_error_t res   = MSG_task_receive_with_timeout_bounded(&task, alias, jtimeout, rate);
327   if (env->ExceptionOccurred())
328     return nullptr;
329   if (res != MSG_OK) {
330     jmsg_throw_status(env, res);
331     return nullptr;
332   }
333   auto jtask_global = (jobject)MSG_task_get_data(task);
334
335   /* Convert the global ref into a local ref so that the JVM can free the stuff */
336   jobject jtask_local = env->NewLocalRef(jtask_global);
337   env->DeleteGlobalRef(jtask_global);
338   MSG_task_set_data(task, nullptr);
339
340   return (jobject) jtask_local;
341 }
342
343 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Task_irecvBounded(JNIEnv * env, jclass cls, jstring jmailbox,
344                                                                  jdouble rate)
345 {
346   jclass comm_class = env->FindClass("org/simgrid/msg/Comm");
347   if (not comm_class)
348     return nullptr;
349
350   // pointer to store the task object pointer.
351   auto* task = new msg_task_t(nullptr);
352
353   jobject jcomm = env->NewObject(comm_class, jtask_method_Comm_constructor);
354   if (not jcomm) {
355     jxbt_throw_jni(env, "Can't create a Comm object.");
356     return nullptr;
357   }
358
359   jstring_wrapper mailbox(env, jmailbox);
360   msg_comm_t comm     = MSG_task_irecv_bounded(task, mailbox, rate);
361
362   env->SetLongField(jcomm, jtask_field_Comm_bind, (jlong) (uintptr_t)(comm));
363   env->SetLongField(jcomm, jtask_field_Comm_taskBind, (jlong) (uintptr_t)(task));
364   env->SetBooleanField(jcomm, jtask_field_Comm_receiving, JNI_TRUE);
365
366   return jcomm;
367 }
368
369 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Task_isend(JNIEnv *env, jobject jtask, jstring jmailbox)
370 {
371   msg_comm_t comm;
372
373   jclass comm_class = env->FindClass("org/simgrid/msg/Comm");
374
375   if (not comm_class)
376     return nullptr;
377
378   jobject jcomm       = env->NewObject(comm_class, jtask_method_Comm_constructor);
379   jstring_wrapper mailbox(env, jmailbox);
380
381   msg_task_t task = jtask_to_native(jtask, env);
382
383   if (not task) {
384     env->DeleteLocalRef(jcomm);
385     jxbt_throw_notbound(env, "task", jtask);
386         return nullptr;
387   }
388
389   MSG_task_set_data(task, env->NewGlobalRef(jtask));
390   comm = MSG_task_isend(task,mailbox);
391
392   env->SetLongField(jcomm, jtask_field_Comm_bind, (jlong) (uintptr_t)(comm));
393   env->SetLongField(jcomm, jtask_field_Comm_taskBind, (jlong) (uintptr_t)(nullptr));
394   env->SetBooleanField(jcomm, jtask_field_Comm_receiving, JNI_FALSE);
395
396   return jcomm;
397 }
398
399 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Task_isendBounded(JNIEnv *env, jobject jtask, jstring jmailbox,
400                                                                  jdouble maxrate)
401 {
402   msg_task_t task;
403   jobject jcomm;
404   msg_comm_t comm;
405
406   jclass comm_class = env->FindClass("org/simgrid/msg/Comm");
407   if (not comm_class)
408     return nullptr;
409
410   jcomm = env->NewObject(comm_class, jtask_method_Comm_constructor);
411   jstring_wrapper mailbox(env, jmailbox);
412
413   task = jtask_to_native(jtask, env);
414
415   if (not task) {
416     env->DeleteLocalRef(jcomm);
417     jxbt_throw_notbound(env, "task", jtask);
418         return nullptr;
419   }
420
421   MSG_task_set_data(task, env->NewGlobalRef(jtask));
422   comm = MSG_task_isend_bounded(task,mailbox,maxrate);
423
424   env->SetLongField(jcomm, jtask_field_Comm_bind, (jlong) (uintptr_t)(comm));
425   env->SetLongField(jcomm, jtask_field_Comm_taskBind, (jlong) (uintptr_t)(nullptr));
426   env->SetBooleanField(jcomm, jtask_field_Comm_receiving, JNI_FALSE);
427
428   return jcomm;
429 }
430
431 JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_nativeFinalize(JNIEnv * env, jobject jtask)
432 {
433   msg_task_t task = jtask_to_native(jtask, env);
434
435   if (not task) {
436     jxbt_throw_notbound(env, "task", jtask);
437     return;
438     }
439
440     MSG_task_destroy(task);
441 }
442
443 static void msg_task_cancel_on_failed_dsend(void*t) {
444   auto task       = (msg_task_t)t;
445   JNIEnv* env     = get_current_thread_env();
446   if (env) {
447     auto jtask_global = (jobject)MSG_task_get_data(task);
448     /* Destroy the global ref so that the JVM can free the stuff */
449     env->DeleteGlobalRef(jtask_global);
450     /* Don't free the C data here, to avoid a race condition with the GC also sometimes doing so.
451      * A rare memleak is seen as preferable to a rare "free(): invalid pointer" failure that
452      * proves really hard to debug.
453      */
454   }
455   MSG_task_set_data(task, nullptr);
456 }
457
458 JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_dsend(JNIEnv * env, jobject jtask, jstring jalias)
459 {
460   jstring_wrapper alias(env, jalias);
461
462   msg_task_t task = jtask_to_native(jtask, env);
463
464   if (not task) {
465     jxbt_throw_notbound(env, "task", jtask);
466     return;
467   }
468
469   /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
470   MSG_task_set_data(task, env->NewGlobalRef(jtask));
471   MSG_task_dsend(task, alias, msg_task_cancel_on_failed_dsend);
472 }
473
474 JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_dsendBounded(JNIEnv * env, jobject jtask, jstring jalias,
475                                                               jdouble maxrate)
476 {
477   jstring_wrapper alias(env, jalias);
478
479   msg_task_t task = jtask_to_native(jtask, env);
480
481   if (not task) {
482     jxbt_throw_notbound(env, "task", jtask);
483     return;
484   }
485
486   /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
487   MSG_task_set_data(task, env->NewGlobalRef(jtask));
488   MSG_task_dsend_bounded(task, alias, msg_task_cancel_on_failed_dsend, maxrate);
489 }
490
491 JNIEXPORT jboolean JNICALL Java_org_simgrid_msg_Task_listen(JNIEnv * env, jclass cls, jstring jalias)
492 {
493   jstring_wrapper alias(env, jalias);
494   return (jboolean)MSG_task_listen(alias);
495 }
496
497 JNIEXPORT jint JNICALL Java_org_simgrid_msg_Task_listenFrom(JNIEnv * env, jclass cls, jstring jalias)
498 {
499   jstring_wrapper alias(env, jalias);
500   return (jint)MSG_task_listen_from(alias);
501 }