Logo AND Algorithmique Numérique Distribuée

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