Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Biggest commit ever (SIMIX2): the user processes can now run in parallel
[simgrid.git] / src / simix / smx_process.c
1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "private.h"
8 #include "xbt/sysdep.h"
9 #include "xbt/log.h"
10 #include "xbt/dict.h"
11 #include "msg/mailbox.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix,
14                                 "Logging specific to SIMIX (process)");
15
16 static unsigned long simix_process_count = 0;
17
18 /**
19  * \brief Move a process to the list of processes to destroy.
20  */
21 void SIMIX_process_cleanup(smx_process_t process)
22 {
23   DEBUG1("Cleanup process %s", process->name);
24   xbt_swag_remove(process, simix_global->process_to_run);
25   xbt_swag_remove(process, simix_global->process_list);
26   xbt_swag_remove(process, process->smx_host->process_list);
27   xbt_swag_insert(process, simix_global->process_to_destroy);
28 }
29
30 /** 
31  * Garbage collection
32  *
33  * Should be called some time to time to free the memory allocated for processes
34  * that have finished (or killed).
35  */
36 void SIMIX_process_empty_trash(void)
37 {
38   smx_process_t process = NULL;
39
40   while ((process = xbt_swag_extract(simix_global->process_to_destroy))) {
41     SIMIX_context_free(process->context);
42
43     /* Free the exception allocated at creation time */
44     if (process->running_ctx)
45       free(process->running_ctx);
46     if (process->properties)
47       xbt_dict_free(&process->properties);
48
49     free(process->name);
50     process->name = NULL;
51     free(process);
52   }
53 }
54
55 /**
56  * \brief Creates and runs the maestro process
57  */
58 void SIMIX_create_maestro_process()
59 {
60   smx_process_t maestro = NULL;
61   
62   /* Create maestro process and intilialize it */
63   maestro = xbt_new0(s_smx_process_t, 1);
64   maestro->pid = simix_process_count++;
65   maestro->name = (char *) "";
66   maestro->running_ctx = xbt_new(xbt_running_ctx_t, 1);
67   XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
68   maestro->context = SIMIX_context_new(NULL, 0, NULL, NULL, maestro);
69
70   simix_global->maestro_process = maestro;
71   simix_global->current_process = maestro;
72   
73   return;
74 }
75
76 /**
77  * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
78  * \return the process created
79  */
80 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
81
82   smx_process_t process;
83
84   if (simix_global->create_process_function) {
85     process = simix_global->create_process_function(args->name,
86         args->code,
87         args->data,
88         args->hostname,
89         args->argc,
90         args->argv,
91         args->properties);
92   }
93   else {
94     process = SIMIX_process_create(args->name,
95         args->code,
96         args->data,
97         args->hostname,
98         args->argc,
99         args->argv,
100         args->properties);
101   }
102   // FIXME: to simplify this, simix_global->create_process_function could just
103   // be SIMIX_process_create() by default (and the same thing in smx_deployment.c)
104
105   return process;
106 }
107
108 /**
109  * \brief Internal function to create a process.
110  *
111  * This function actually creates the process.
112  * It may be called when a REQ_PROCESS_CREATE request occurs,
113  * or directly for SIMIX internal purposes.
114  *
115  * \return the process created
116  */
117 smx_process_t SIMIX_process_create(const char *name,
118                                    xbt_main_func_t code,
119                                                      void *data,
120                                                            const char *hostname,
121                                                            int argc, char **argv,
122                                                            xbt_dict_t properties) {
123
124   smx_process_t process = NULL;
125   smx_host_t host = SIMIX_host_get_by_name(hostname);
126
127   DEBUG2("Start process %s on host %s", name, hostname);
128
129   if (!SIMIX_host_get_state(host)) {
130     WARN2("Cannot launch process '%s' on failed host '%s'", name,
131           hostname);
132   }
133   else {
134     process = xbt_new0(s_smx_process_t, 1);
135
136     xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
137
138     /* Process data */
139     process->pid = simix_process_count++;
140     process->name = xbt_strdup(name);
141     process->smx_host = host;
142     process->iwannadie = 0;
143     process->data = data;
144
145     VERB1("Create context %s", process->name);
146     process->context = SIMIX_context_new(code, argc, argv,
147         simix_global->cleanup_process_function, process);
148
149     process->running_ctx = xbt_new(xbt_running_ctx_t, 1);
150     XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
151
152     /* Add properties */
153     process->properties = properties;
154
155     /* Add the process to it's host process list */
156     xbt_swag_insert(process, host->process_list);
157
158     DEBUG1("Start context '%s'", process->name);
159
160     /* Now insert it in the global process list and in the process to run list */
161     xbt_swag_insert(process, simix_global->process_list);
162     DEBUG2("Inserting %s(%s) in the to_run list", process->name, host->name);
163     xbt_swag_insert(process, simix_global->process_to_run);
164   }
165
166   return process;
167 }
168
169 /**
170  * \brief Internal function to kill a SIMIX process.
171  *
172  * This function may be called when a REQ_PROCESS_KILL request occurs,
173  * or directly for SIMIX internal purposes.
174  *
175  * \param process poor victim
176  */
177 void SIMIX_process_kill(smx_process_t process, smx_process_t killer) {
178
179   DEBUG2("Killing process %s on %s", process->name, process->smx_host->name);
180
181   process->iwannadie = 1;
182   process->blocked = 0;
183   process->suspended = 0;
184   /* FIXME: set doexception to 0 also? */
185   
186   if (process->waiting_action) {
187
188     switch (process->waiting_action->type) {
189
190       case SIMIX_ACTION_EXECUTE:          
191       case SIMIX_ACTION_PARALLEL_EXECUTE:
192         SIMIX_host_execution_destroy(process->waiting_action);
193         break;
194
195       case SIMIX_ACTION_COMMUNICATE:
196         SIMIX_comm_destroy(process->waiting_action);
197         break;
198
199       case SIMIX_ACTION_SLEEP:
200         SIMIX_process_sleep_destroy(process->waiting_action);
201         break;
202
203       case SIMIX_ACTION_SYNCHRO:
204         SIMIX_synchro_stop_waiting(process, process->request);
205         SIMIX_synchro_destroy(process->waiting_action);
206         break;
207
208       case SIMIX_ACTION_IO:
209         THROW_UNIMPLEMENTED;
210         break;
211     }
212   }
213
214   /* If I'm killing myself then stop, otherwise schedule the process to kill. */
215   if (process == killer) {
216     SIMIX_context_stop(process->context);
217   }
218   else {
219     xbt_swag_insert(process, simix_global->process_to_run);
220   }
221 }
222
223 /**
224  * \brief Kills all running processes.
225  *
226  * Only maestro can kill everyone.
227  */
228 void SIMIX_process_killall(void)
229 {
230   smx_process_t p = NULL;
231
232   while ((p = xbt_swag_extract(simix_global->process_list)))
233     SIMIX_process_kill(p, SIMIX_process_self());
234
235   SIMIX_process_empty_trash();
236 }
237
238 void SIMIX_process_change_host(smx_process_t process,
239     const char *source, const char *dest)
240 {
241   smx_host_t h1 = NULL;
242   smx_host_t h2 = NULL;
243   xbt_assert0((process != NULL), "Invalid parameters");
244   h1 = SIMIX_host_get_by_name(source);
245   h2 = SIMIX_host_get_by_name(dest);
246   process->smx_host = h2;
247   xbt_swag_remove(process, h1->process_list);
248   xbt_swag_insert(process, h2->process_list);
249 }
250
251 void SIMIX_pre_process_suspend(smx_req_t req)
252 {
253   smx_process_t process = req->process_suspend.process;
254   SIMIX_process_suspend(process, req->issuer);
255
256   if (process != req->issuer) {
257     SIMIX_request_answer(req);
258   }
259   /* If we are suspending ourselves, then just do not replay the request. */
260 }
261
262 void SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
263 {
264   xbt_assert0(process, "Invalid parameters");
265
266   process->suspended = 1;
267
268   /* If we are suspending another process, and it is waiting on an action,
269      suspend it's action. */
270   if (process != issuer) {
271
272     if (process->waiting_action) {
273
274       switch (process->waiting_action->type) {
275
276         case SIMIX_ACTION_EXECUTE:
277         case SIMIX_ACTION_PARALLEL_EXECUTE:
278           SIMIX_host_execution_suspend(process->waiting_action);
279           break;
280
281         case SIMIX_ACTION_COMMUNICATE:
282           SIMIX_comm_suspend(process->waiting_action);
283           break;
284
285         case SIMIX_ACTION_SLEEP:
286           SIMIX_process_sleep_suspend(process->waiting_action);
287           break;
288
289         default:
290           THROW_IMPOSSIBLE;
291       }
292     }
293   }
294 }
295
296 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
297 {
298   xbt_assert0((process != NULL), "Invalid parameters");
299
300   process->suspended = 0;
301
302   /* If we are resuming another process, resume the action it was waiting for
303      if any. Otherwise add it to the list of process to run in the next round. */
304   if (process != issuer) {
305
306     if (process->waiting_action) {
307
308       switch(process->waiting_action->type) {
309
310         case SIMIX_ACTION_EXECUTE:          
311         case SIMIX_ACTION_PARALLEL_EXECUTE:
312           SIMIX_host_execution_resume(process->waiting_action);
313           break;
314           
315         case SIMIX_ACTION_COMMUNICATE:
316           SIMIX_comm_resume(process->waiting_action);
317           break;
318           
319         case SIMIX_ACTION_SLEEP:
320           SIMIX_process_sleep_resume(process->waiting_action);
321           break;
322           
323         default:
324           THROW_IMPOSSIBLE;
325       }
326     } else {
327       xbt_swag_insert(process, simix_global->process_to_run);
328     }
329   }
330 }
331
332 int SIMIX_process_count(void)
333 {
334   return xbt_swag_size(simix_global->process_list);
335 }
336
337 void* SIMIX_process_self_get_data(void)
338 {
339   smx_process_t me = SIMIX_process_self();
340   if (!me) {
341     return NULL;
342   }
343   return SIMIX_process_get_data(me);
344 }
345
346 void SIMIX_process_self_set_data(void *data)
347 {
348   SIMIX_process_set_data(SIMIX_process_self(), data);
349 }
350
351 void* SIMIX_process_get_data(smx_process_t process)
352 {
353   return process->data;
354 }
355
356 void SIMIX_process_set_data(smx_process_t process, void *data)
357 {
358   process->data = data;
359 }
360
361 smx_host_t SIMIX_process_get_host(smx_process_t process)
362 {
363   return process->smx_host;
364 }
365
366 /* needs to be public and without request because it is called
367    by exceptions and logging events */
368 const char* SIMIX_process_self_get_name(void) {
369
370   smx_process_t process = SIMIX_process_self();
371   if (process == NULL || process == simix_global->maestro_process)
372     return "";
373
374   return SIMIX_process_get_name(process);
375 }
376
377 const char* SIMIX_process_get_name(smx_process_t process)
378 {
379   return process->name;
380 }
381
382 int SIMIX_process_is_suspended(smx_process_t process)
383 {
384   return process->suspended;
385 }
386
387 int SIMIX_process_is_enabled(smx_process_t process)
388 {
389   if(process->request && SIMIX_request_isEnabled(process->request))
390     return TRUE;
391
392   return FALSE;
393 }
394
395 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
396 {
397   return process->properties;
398 }
399
400 void SIMIX_pre_process_sleep(smx_req_t req)
401 {
402 #ifdef HAVE_MC
403   if(_surf_do_model_check){
404     req->process_sleep.result = SIMIX_DONE;
405     SIMIX_request_answer(req);
406   }
407 #endif
408   smx_action_t action = SIMIX_process_sleep(req->issuer, req->process_sleep.duration);
409   xbt_fifo_push(action->request_list, req);
410   req->issuer->waiting_action = action;
411 }
412
413 smx_action_t SIMIX_process_sleep(smx_process_t process, double duration)
414 {
415   smx_action_t action;
416   smx_host_t host = process->smx_host;
417
418   /* check if the host is active */
419   if (surf_workstation_model->extension.
420       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
421     THROW1(host_error, 0, "Host %s failed, you cannot call this function",
422            host->name);
423   }
424
425   action = xbt_new0(s_smx_action_t, 1);
426   action->type = SIMIX_ACTION_SLEEP;
427   action->request_list = xbt_fifo_new();
428   action->name = xbt_strdup("sleep");
429 #ifdef HAVE_TRACING
430   action->category = NULL;
431 #endif
432
433   action->sleep.host = host;
434   action->sleep.surf_sleep =
435       surf_workstation_model->extension.workstation.sleep(host->host, duration);
436
437   surf_workstation_model->action_data_set(action->sleep.surf_sleep, action);
438   DEBUG1("Create sleep action %p", action);
439
440   return action;
441 }
442
443 void SIMIX_post_process_sleep(smx_action_t action)
444 {
445   e_smx_state_t state = SIMIX_action_map_state(surf_workstation_model->action_state_get(action->sleep.surf_sleep));
446   smx_req_t req;
447
448   while ((req = xbt_fifo_shift(action->request_list))) {
449     req->process_sleep.result = state;
450     req->issuer->waiting_action = NULL;
451     SIMIX_request_answer(req);
452   }
453
454   SIMIX_process_sleep_destroy(action);
455 }
456
457 void SIMIX_process_sleep_destroy(smx_action_t action)
458 {
459   DEBUG1("Destroy action %p", action);
460   if (action->name)
461     xbt_free(action->name);
462   if (action->sleep.surf_sleep)
463     action->sleep.surf_sleep->model_type->action_unref(action->sleep.surf_sleep);
464 #ifdef HAVE_TRACING
465   TRACE_smx_action_destroy(act);
466 #endif
467   xbt_fifo_free(action->request_list);
468   xbt_free(action);
469 }
470
471 void SIMIX_process_sleep_suspend(smx_action_t action)
472 {
473   surf_workstation_model->suspend(action->sleep.surf_sleep);
474 }
475
476 void SIMIX_process_sleep_resume(smx_action_t action)
477 {
478   surf_workstation_model->resume(action->sleep.surf_sleep);
479 }
480
481 /**
482  * \brief Returns the current agent.
483  *
484  * This functions returns the currently running SIMIX process.
485  *
486  * \return The SIMIX process
487  */
488 XBT_INLINE smx_process_t SIMIX_process_self(void)
489 {
490   if(simix_global)
491     return SIMIX_context_self();
492   return NULL;
493 }
494
495 /** 
496  * Calling this function makes the process to yield.
497  * Only the processes can call this function, giving back the control to maestro
498  */
499 void SIMIX_process_yield(void)
500 {
501   smx_process_t self = SIMIX_process_self();
502   
503   DEBUG1("Yield process '%s'", self->name);
504   
505   /* Go into sleep and return control to maestro */
506   SIMIX_context_suspend(self->context);
507
508   /* Ok, maestro returned control to us */
509   DEBUG1("Maestro returned control to me: '%s'", self->name);
510   
511   if (self->iwannadie)
512     SIMIX_context_stop(self->context);
513
514   if (self->doexception) {
515     DEBUG0("Wait, maestro left me an exception");
516     self->doexception = 0;
517     RETHROW;
518   }
519 }
520
521 /* callback: context fetching */
522 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
523 {
524   return simix_global->current_process->running_ctx;
525 }
526
527 /* callback: termination */
528 void SIMIX_process_exception_terminate(xbt_ex_t * e)
529 {
530   xbt_ex_display(e);
531   abort();
532 }