Logo AND Algorithmique Numérique Distribuée

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