Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add VM.setBound() and cosmetics changes - Adrien
[simgrid.git] / src / bindings / java / smx_context_cojava.c
1 /* context_cojava - implementation of context switching for java coroutines */
2
3 /* Copyright 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
9
10 #include <xbt/function_types.h>
11 #include <simgrid/simix.h>
12 #include <xbt/ex.h>
13 #include "smx_context_cojava.h"
14 #include "jxbt_utilities.h"
15 #include "xbt/dynar.h"
16
17
18 //Coroutine methodID/class cache.
19 jclass coclass;
20 jmethodID coroutine_init;
21 jmethodID coroutine_yield;
22 jmethodID coroutine_yieldTo;
23 jmethodID coroutine_stop;
24 //Maestro java coroutine
25 jobject cojava_maestro_coroutine;
26
27 JNIEnv *global_env;
28
29 static smx_context_t my_current_context = NULL;
30 static smx_context_t maestro_context = NULL;
31
32
33 xbt_dynar_t cojava_processes;
34 static unsigned long int cojava_process_index = 0;
35
36 extern JavaVM *__java_vm;
37
38 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
39
40
41 static smx_context_t
42 smx_ctx_cojava_factory_create_context(xbt_main_func_t code, int argc,
43                                     char **argv,
44                                     void_pfn_smxprocess_t cleanup_func,
45                                     void *data);
46
47 static void smx_ctx_cojava_free(smx_context_t context);
48 static void smx_ctx_cojava_suspend(smx_context_t context);
49 static void smx_ctx_cojava_resume(smx_context_t new_context);
50 static void smx_ctx_cojava_runall(void);
51 static void* smx_ctx_cojava_run(void *data);
52 static void smx_ctx_cojava_create_coroutine(smx_ctx_cojava_t context);
53 void SIMIX_ctx_cojava_factory_init(smx_context_factory_t * factory)
54 {
55   /* instantiate the context factory */
56   smx_ctx_base_factory_init(factory);
57
58   (*factory)->create_context = smx_ctx_cojava_factory_create_context;
59   /* Leave default behavior of (*factory)->finalize */
60   (*factory)->free = smx_ctx_cojava_free;
61   (*factory)->stop = smx_ctx_cojava_stop;
62   (*factory)->suspend = smx_ctx_cojava_suspend;
63   (*factory)->runall = smx_ctx_cojava_runall;
64   (*factory)->name = "ctx_cojava_factory";
65   //(*factory)->finalize = smx_ctx_base_factory_finalize;
66   (*factory)->self = smx_ctx_cojava_self;
67   (*factory)->get_data = smx_ctx_base_get_data;
68
69   global_env = get_current_thread_env();
70
71   coclass = (*global_env)->FindClass(global_env, "java/dyn/Coroutine");
72   xbt_assert((coclass != NULL), "Can't find java.dyn.Coroutine class.");
73   //Cache the method id we are going to use
74   coroutine_init = (*global_env)->GetMethodID(global_env, coclass, "<init>", "(Ljava/lang/Runnable;)V");
75   xbt_assert((coroutine_init != NULL), "Can't find <init>");
76   coroutine_stop = (*global_env)->GetMethodID(global_env, coclass, "stop", "()V");
77   xbt_assert((coroutine_stop != NULL), "Method not found...");
78   coroutine_yield = (*global_env)->GetStaticMethodID(global_env, coclass, "yield", "()V");
79   xbt_assert((coroutine_yield != NULL), "Method yield not found.");
80   coroutine_yieldTo = (*global_env)->GetStaticMethodID(global_env, coclass, "yieldTo", "(Ljava/dyn/Coroutine;)V");
81   xbt_assert((coroutine_yieldTo != NULL), "Method yieldTo not found.");
82
83   jclass class_thread = (*global_env)->FindClass(global_env, "java/lang/Thread");
84   xbt_assert((class_thread != NULL), "Can't find java.lang.Thread class");
85   
86   jclass class_coroutine_support = (*global_env)->FindClass(global_env, "java/dyn/CoroutineSupport");
87   xbt_assert((class_coroutine_support != NULL), "Can't find java.dyn.CoroutineSupport class");
88   jmethodID thread_get_current = (*global_env)->GetStaticMethodID(global_env, class_thread, "currentThread", "()Ljava/lang/Thread;");
89   xbt_assert((thread_get_current != NULL), "Can't find Thread.currentThread() method.");
90
91   /**
92    * Retrieve maetro coroutine object
93    */
94   jobject jthread;
95   jthread = (*global_env)->CallStaticObjectMethod(global_env, class_thread, thread_get_current);
96   xbt_assert((jthread != NULL), "Can't find current thread.");
97
98   jmethodID thread_get_coroutine_support = (*global_env)->GetMethodID(global_env, class_thread, "getCoroutineSupport", "()Ljava/dyn/CoroutineSupport;");
99   xbt_assert((thread_get_coroutine_support != NULL), "Can't find Thread.getCoroutineSupport method");
100
101   jobject jcoroutine_support;
102   jcoroutine_support = (*global_env)->CallObjectMethod(global_env, jthread, thread_get_coroutine_support);
103   xbt_assert((jcoroutine_support != NULL), "Can't find coroutine support object");
104   //FIXME ? Be careful, might change in the implementation (we are relying on private fields, so...).
105   jfieldID coroutine_support_thread_coroutine = (*global_env)->GetFieldID(global_env, class_coroutine_support, "threadCoroutine", "Ljava/dyn/Coroutine;");
106   xbt_assert((coroutine_support_thread_coroutine != NULL), "Can't find threadCoroutine field");
107   cojava_maestro_coroutine = (jobject)(*global_env)->GetObjectField(global_env, jcoroutine_support, coroutine_support_thread_coroutine);
108   xbt_assert((cojava_maestro_coroutine != NULL), "Can't find the thread coroutine.");
109   cojava_maestro_coroutine = (*global_env)->NewGlobalRef(global_env, cojava_maestro_coroutine);
110   xbt_assert((cojava_maestro_coroutine != NULL), "Can't get a global reference to the thread coroutine.");
111 }
112 smx_context_t smx_ctx_cojava_self(void)
113 {
114         return my_current_context;
115 }
116
117 static smx_context_t
118 smx_ctx_cojava_factory_create_context(xbt_main_func_t code, int argc,
119                                     char **argv,
120                                     void_pfn_smxprocess_t cleanup_func,
121                                     void* data)
122 {
123         smx_ctx_cojava_t context = xbt_new0(s_smx_ctx_cojava_t, 1);
124   /* If the user provided a function for the process then use it
125      otherwise is the context for maestro */
126   if (code) {
127     if (argc == 0) {
128       context->jprocess = (jobject) code;
129     }
130     else {
131       context->jprocess = NULL;
132     }
133     context->super.cleanup_func = cleanup_func;
134
135     context->super.argc = argc;
136     context->super.argv = argv;
137     context->super.code = code;
138
139     smx_ctx_cojava_run(context);
140   }
141   else {
142         context->jcoroutine = NULL;
143         my_current_context = (smx_context_t)context;
144         maestro_context = (smx_context_t)context;
145   }
146   context->bound = 0;
147   context->super.data = data;
148   return (smx_context_t) context;
149 }
150
151 static void* smx_ctx_cojava_run(void *data) {
152   smx_ctx_cojava_t context = (smx_ctx_cojava_t)data;
153   my_current_context = (smx_context_t)context;
154   //Create the "Process" object if needed.
155   if (context->super.argc <= 0) {
156     smx_ctx_cojava_create_coroutine(context);
157   }
158   my_current_context = maestro_context;
159   return NULL;
160 }
161 static void smx_ctx_cojava_free(smx_context_t context)
162 {
163   if (context) {
164     smx_ctx_cojava_t ctx_java = (smx_ctx_cojava_t) context;
165     if (ctx_java->jcoroutine) { /* We are not in maestro context */
166       JNIEnv *env = get_current_thread_env();
167       (*env)->DeleteGlobalRef(env, ctx_java->jcoroutine);
168       (*env)->DeleteGlobalRef(env, ctx_java->jprocess);
169     }
170   }
171   smx_ctx_base_free(context);
172 }
173
174
175 void smx_ctx_cojava_stop(smx_context_t context)
176 {
177   /*
178    * The java stack needs to be empty, otherwise weird stuff
179    * will happen
180    */
181   if (context->iwannadie) {
182     context->iwannadie = 0;
183     JNIEnv *env = get_current_thread_env();
184     jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", xbt_strdup("Process killed :)"));
185     THROWF(cancel_error, 0, "process cancelled");
186   }
187   else {
188     smx_ctx_base_stop(context);
189     smx_ctx_cojava_suspend(context);
190   }
191 }
192
193 static void smx_ctx_cojava_suspend(smx_context_t context)
194 {
195   smx_context_t previous_context = context;
196   unsigned long int i = cojava_process_index++;
197   jobject next_coroutine;
198
199   if (i < xbt_dynar_length(cojava_processes)) {
200     smx_context_t next_context = SIMIX_process_get_context(xbt_dynar_get_as(
201     cojava_processes,i, smx_process_t));
202     my_current_context = next_context;
203     XBT_DEBUG("Switching to %p",my_current_context);
204     smx_ctx_cojava_t java_context = (smx_ctx_cojava_t)(next_context);
205     if (!java_context->jprocess) {
206       java_context->super.code(java_context->super.argc, java_context->super.argv);
207       smx_ctx_cojava_create_coroutine(java_context);
208     }
209     else if (!java_context->bound) {
210       java_context->bound = 1;
211       smx_process_t process = SIMIX_process_self();
212       (*global_env)->SetLongField(global_env, java_context->jprocess,
213                                   jprocess_field_Process_bind,
214                                   (intptr_t)process);
215     }
216
217     next_coroutine = java_context->jcoroutine;
218   }
219   else {
220     //Give maestro the control back.
221     next_coroutine = cojava_maestro_coroutine;
222     my_current_context = maestro_context;
223   }
224   (*global_env)->CallStaticVoidMethod(global_env, coclass, coroutine_yieldTo, next_coroutine);
225   my_current_context = previous_context;
226 }
227
228 static void smx_ctx_cojava_resume(smx_context_t new_context) {
229   my_current_context = new_context;
230   smx_ctx_cojava_t java_context = (smx_ctx_cojava_t)(new_context);
231
232   if (!java_context->jprocess) {
233     java_context->super.code(java_context->super.argc, java_context->super.argv);
234     smx_ctx_cojava_create_coroutine(java_context);
235     java_context->bound = 1;
236   }
237   else if (!java_context->bound) {
238     java_context->bound = 1;
239     smx_process_t process = SIMIX_process_self();
240     (*global_env)->SetLongField(global_env, java_context->jprocess,
241                                 jprocess_field_Process_bind, (intptr_t)process);
242   }
243   (*global_env)->CallStaticVoidMethod(global_env, coclass, coroutine_yieldTo, java_context->jcoroutine);
244 }
245
246 static void smx_ctx_cojava_runall(void)
247 {
248   cojava_processes = SIMIX_process_get_runnable();
249   smx_process_t process;
250   if (!xbt_dynar_is_empty(cojava_processes)) {
251     process = xbt_dynar_get_as(cojava_processes, 0, smx_process_t);
252     cojava_process_index = 1;
253     /* Execute the first process */
254     smx_ctx_cojava_resume(SIMIX_process_get_context(process));
255   }
256 }
257
258 static void smx_ctx_cojava_create_coroutine(smx_ctx_cojava_t context) {
259   JNIEnv *env = get_current_thread_env();
260   jclass coclass = (*env)->FindClass(env, "java/dyn/Coroutine");
261   xbt_assert((coclass != NULL), "Can't find coroutine class ! :(");
262   jobject jcoroutine = (*env)->NewObject(env, coclass, coroutine_init, context->jprocess);
263   if (jcoroutine == NULL) {
264      FILE *conf= fopen("/proc/sys/vm/max_map_count","r");
265      if (conf) {
266         int limit=-1;
267         if(fscanf(conf,"%d",&limit) != 1)
268            xbt_die("Error while creating a new coroutine. Parse error.");
269         fclose(conf);
270         if (limit!=-1 && SIMIX_process_count() > (limit - 100) /2)
271            xbt_die("Error while creating a new coroutine. "
272                    "This seem due to the the vm.max_map_count system limit that is only equal to %d while we already have %d coroutines. "
273                    "Please check the install documentation to see how to increase this limit", limit, SIMIX_process_count());
274         if (limit == -1)
275           xbt_die("Error while creating a new coroutine. "
276                   "This seems to be a non-linux system, disabling the automatic verification that the system limit on the amount of memory maps is high enough.");
277         xbt_die("Error while creating a new coroutine. ");
278      }
279      
280   }
281    
282   jcoroutine = (*env)->NewGlobalRef(env, jcoroutine);
283   context->jcoroutine = jcoroutine;
284 }