Logo AND Algorithmique Numérique Distribuée

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