Logo AND Algorithmique Numérique Distribuée

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