Logo AND Algorithmique Numérique Distribuée

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