Logo AND Algorithmique Numérique Distribuée

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