Logo AND Algorithmique Numérique Distribuée

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