Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
653998658c1cdcd7a5f0de7ebc27d0b383d5c364
[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
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix,
14                                 "Logging specific to SIMIX (process)");
15
16 static unsigned long simix_process_count = 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   if(simix_global)
28     return SIMIX_context_self();
29   return 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   DEBUG1("Cleanup process %s", process->name);
38   xbt_swag_remove(process, simix_global->process_to_run);
39   xbt_swag_remove(process, simix_global->process_list);
40   xbt_swag_remove(process, process->smx_host->process_list);
41   xbt_swag_insert(process, simix_global->process_to_destroy);
42 }
43
44 /** 
45  * Garbage collection
46  *
47  * Should be called some time to time to free the memory allocated for processes
48  * that have finished (or killed).
49  */
50 void SIMIX_process_empty_trash(void)
51 {
52   smx_process_t process = NULL;
53
54   while ((process = xbt_swag_extract(simix_global->process_to_destroy))) {
55     SIMIX_context_free(process->context);
56
57     /* Free the exception allocated at creation time */
58     if (process->running_ctx)
59       free(process->running_ctx);
60     if (process->properties)
61       xbt_dict_free(&process->properties);
62
63     free(process->name);
64     process->name = NULL;
65     free(process);
66   }
67 }
68
69 /**
70  * \brief Creates and runs the maestro process
71  */
72 void SIMIX_create_maestro_process()
73 {
74   smx_process_t maestro = NULL;
75   
76   /* Create maestro process and intilialize it */
77   maestro = xbt_new0(s_smx_process_t, 1);
78   maestro->pid = simix_process_count++;
79   maestro->name = (char *) "";
80   maestro->running_ctx = xbt_new(xbt_running_ctx_t, 1);
81   XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
82   maestro->context = SIMIX_context_new(NULL, 0, NULL, NULL, maestro);
83
84   simix_global->maestro_process = maestro;
85   
86   return;
87 }
88
89 /**
90  * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
91  * \return the process created
92  */
93 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
94
95   smx_process_t process;
96
97   if (simix_global->create_process_function) {
98     process = simix_global->create_process_function(args->name,
99         args->code,
100         args->data,
101         args->hostname,
102         args->argc,
103         args->argv,
104         args->properties);
105   }
106   else {
107     process = SIMIX_process_create(args->name,
108         args->code,
109         args->data,
110         args->hostname,
111         args->argc,
112         args->argv,
113         args->properties);
114   }
115   // FIXME: to simplify this, simix_global->create_process_function could just
116   // be SIMIX_process_create() by default (and the same thing in smx_deployment.c)
117
118   return process;
119 }
120
121 /**
122  * \brief Internal function to create a process.
123  *
124  * This function actually creates the process.
125  * It may be called when a REQ_PROCESS_CREATE request occurs,
126  * or directly for SIMIX internal purposes.
127  *
128  * \return the process created
129  */
130 smx_process_t SIMIX_process_create(const char *name,
131                                    xbt_main_func_t code,
132                                                      void *data,
133                                                            const char *hostname,
134                                                            int argc, char **argv,
135                                                            xbt_dict_t properties) {
136
137   smx_process_t process = NULL;
138   smx_host_t host = SIMIX_host_get_by_name(hostname);
139
140   DEBUG2("Start process %s on host %s", name, hostname);
141
142   if (!SIMIX_host_get_state(host)) {
143     WARN2("Cannot launch process '%s' on failed host '%s'", name,
144           hostname);
145   }
146   else {
147     process = xbt_new0(s_smx_process_t, 1);
148
149     xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
150
151     /* Process data */
152     process->pid = simix_process_count++;
153     process->name = xbt_strdup(name);
154     process->smx_host = host;
155     process->iwannadie = 0;
156     process->data = data;
157
158     VERB1("Create context %s", process->name);
159     process->context = SIMIX_context_new(code, argc, argv,
160         simix_global->cleanup_process_function, process);
161
162     process->running_ctx = xbt_new(xbt_running_ctx_t, 1);
163     XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
164
165     /* Add properties */
166     process->properties = properties;
167
168     /* Add the process to it's host process list */
169     xbt_swag_insert(process, host->process_list);
170
171     DEBUG1("Start context '%s'", process->name);
172
173     /* Now insert it in the global process list and in the process to run list */
174     xbt_swag_insert(process, simix_global->process_list);
175     DEBUG2("Inserting %s(%s) in the to_run list", process->name, host->name);
176     xbt_swag_insert(process, simix_global->process_to_run);
177   }
178
179   return process;
180 }
181
182 /**
183  * \brief Internal function to kill a SIMIX process.
184  *
185  * This function may be called when a REQ_PROCESS_KILL request occurs,
186  * or directly for SIMIX internal purposes.
187  *
188  * \param process poor victim
189  */
190 void SIMIX_process_kill(smx_process_t process, smx_process_t killer) {
191
192   DEBUG2("Killing process %s on %s", process->name, process->smx_host->name);
193
194   process->iwannadie = 1;
195   process->blocked = 0;
196   process->suspended = 0;
197   /* FIXME: set doexception to 0 also? */
198   
199   if (process->waiting_action) {
200
201     switch (process->waiting_action->type) {
202
203       case SIMIX_ACTION_EXECUTE:          
204       case SIMIX_ACTION_PARALLEL_EXECUTE:
205         SIMIX_host_execution_destroy(process->waiting_action);
206         break;
207
208       case SIMIX_ACTION_COMMUNICATE:
209         SIMIX_comm_destroy(process->waiting_action);
210         break;
211
212       case SIMIX_ACTION_SLEEP:
213         SIMIX_process_sleep_destroy(process->waiting_action);
214         break;
215
216       case SIMIX_ACTION_SYNCHRO:
217         SIMIX_synchro_stop_waiting(process, process->request);
218         SIMIX_synchro_destroy(process->waiting_action);
219         break;
220
221       case SIMIX_ACTION_IO:
222         THROW_UNIMPLEMENTED;
223         break;
224     }
225   }
226
227   /* If I'm killing myself then stop, otherwise schedule the process to kill. */
228   if (process == killer) {
229     SIMIX_context_stop(process->context);
230   }
231   else {
232     xbt_swag_insert(process, simix_global->process_to_run);
233   }
234 }
235
236 /**
237  * \brief Kills all running processes.
238  *
239  * Only maestro can kill everyone.
240  */
241 void SIMIX_process_killall(void)
242 {
243   smx_process_t p = NULL;
244
245   while ((p = xbt_swag_extract(simix_global->process_list)))
246     SIMIX_process_kill(p, SIMIX_process_self());
247
248   SIMIX_process_empty_trash();
249 }
250
251 void SIMIX_process_change_host(smx_process_t process,
252     const char *source, const char *dest)
253 {
254   smx_host_t h1 = NULL;
255   smx_host_t h2 = NULL;
256   xbt_assert0((process != NULL), "Invalid parameters");
257   h1 = SIMIX_host_get_by_name(source);
258   h2 = SIMIX_host_get_by_name(dest);
259   process->smx_host = h2;
260   xbt_swag_remove(process, h1->process_list);
261   xbt_swag_insert(process, h2->process_list);
262 }
263
264 void SIMIX_pre_process_suspend(smx_req_t req)
265 {
266   smx_process_t process = req->process_suspend.process;
267   SIMIX_process_suspend(process, req->issuer);
268
269   if (process != req->issuer) {
270     SIMIX_request_answer(req);
271   }
272   /* If we are suspending ourselves, then just do not replay the request. */
273 }
274
275 void SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
276 {
277   xbt_assert0(process, "Invalid parameters");
278
279   process->suspended = 1;
280
281   /* If we are suspending another process, and it is waiting on an action,
282      suspend it's action. */
283   if (process != issuer) {
284
285     if (process->waiting_action) {
286
287       switch (process->waiting_action->type) {
288
289         case SIMIX_ACTION_EXECUTE:
290         case SIMIX_ACTION_PARALLEL_EXECUTE:
291           SIMIX_host_execution_suspend(process->waiting_action);
292           break;
293
294         case SIMIX_ACTION_COMMUNICATE:
295           SIMIX_comm_suspend(process->waiting_action);
296           break;
297
298         case SIMIX_ACTION_SLEEP:
299           SIMIX_process_sleep_suspend(process->waiting_action);
300           break;
301
302         default:
303           THROW_IMPOSSIBLE;
304       }
305     }
306   }
307 }
308
309 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
310 {
311   xbt_assert0((process != NULL), "Invalid parameters");
312
313   process->suspended = 0;
314
315   /* If we are resuming another process, resume the action it was waiting for
316      if any. Otherwise add it to the list of process to run in the next round. */
317   if (process != issuer) {
318
319     if (process->waiting_action) {
320
321       switch(process->waiting_action->type) {
322
323         case SIMIX_ACTION_EXECUTE:          
324         case SIMIX_ACTION_PARALLEL_EXECUTE:
325           SIMIX_host_execution_resume(process->waiting_action);
326           break;
327           
328         case SIMIX_ACTION_COMMUNICATE:
329           SIMIX_comm_resume(process->waiting_action);
330           break;
331           
332         case SIMIX_ACTION_SLEEP:
333           SIMIX_process_sleep_resume(process->waiting_action);
334           break;
335           
336         default:
337           THROW_IMPOSSIBLE;
338       }
339     } else {
340       xbt_swag_insert(process, simix_global->process_to_run);
341     }
342   }
343 }
344
345 int SIMIX_process_count(void)
346 {
347   return xbt_swag_size(simix_global->process_list);
348 }
349
350 void* SIMIX_process_self_get_data(void)
351 {
352   smx_process_t me = SIMIX_process_self();
353   if (!me) {
354     return NULL;
355   }
356   return SIMIX_process_get_data(me);
357 }
358
359 void SIMIX_process_self_set_data(void *data)
360 {
361   SIMIX_process_set_data(SIMIX_process_self(), data);
362 }
363
364 void* SIMIX_process_get_data(smx_process_t process)
365 {
366   return process->data;
367 }
368
369 void SIMIX_process_set_data(smx_process_t process, void *data)
370 {
371   process->data = data;
372 }
373
374 smx_host_t SIMIX_process_get_host(smx_process_t process)
375 {
376   return process->smx_host;
377 }
378
379 /* needs to be public and without request because it is called
380    by exceptions and logging events */
381 const char* SIMIX_process_self_get_name(void) {
382
383   smx_process_t process = SIMIX_process_self();
384   if (process == NULL || process == simix_global->maestro_process)
385     return "";
386
387   return SIMIX_process_get_name(process);
388 }
389
390 const char* SIMIX_process_get_name(smx_process_t process)
391 {
392   return process->name;
393 }
394
395 int SIMIX_process_is_suspended(smx_process_t process)
396 {
397   return process->suspended;
398 }
399
400 int SIMIX_process_is_enabled(smx_process_t process)
401 {
402   if(process->request && SIMIX_request_isEnabled(process->request))
403     return TRUE;
404
405   return FALSE;
406 }
407
408 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
409 {
410   return process->properties;
411 }
412
413 void SIMIX_pre_process_sleep(smx_req_t req)
414 {
415 #ifdef HAVE_MC
416   if(_surf_do_model_check){
417     req->process_sleep.result = SIMIX_DONE;
418     SIMIX_request_answer(req);
419   }
420 #endif
421   smx_action_t action = SIMIX_process_sleep(req->issuer, req->process_sleep.duration);
422   xbt_fifo_push(action->request_list, req);
423   req->issuer->waiting_action = action;
424 }
425
426 smx_action_t SIMIX_process_sleep(smx_process_t process, double duration)
427 {
428   smx_action_t action;
429   smx_host_t host = process->smx_host;
430
431   /* check if the host is active */
432   if (surf_workstation_model->extension.
433       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
434     THROW1(host_error, 0, "Host %s failed, you cannot call this function",
435            host->name);
436   }
437
438   action = xbt_new0(s_smx_action_t, 1);
439   action->type = SIMIX_ACTION_SLEEP;
440   action->request_list = xbt_fifo_new();
441   action->name = xbt_strdup("sleep");
442 #ifdef HAVE_TRACING
443   action->category = NULL;
444 #endif
445
446   action->sleep.host = host;
447   action->sleep.surf_sleep =
448       surf_workstation_model->extension.workstation.sleep(host->host, duration);
449
450   surf_workstation_model->action_data_set(action->sleep.surf_sleep, action);
451   DEBUG1("Create sleep action %p", action);
452
453   return action;
454 }
455
456 void SIMIX_post_process_sleep(smx_action_t action)
457 {
458   e_smx_state_t state = SIMIX_action_map_state(surf_workstation_model->action_state_get(action->sleep.surf_sleep));
459   smx_req_t req;
460
461   while ((req = xbt_fifo_shift(action->request_list))) {
462     req->process_sleep.result = state;
463     req->issuer->waiting_action = NULL;
464     SIMIX_request_answer(req);
465   }
466
467   SIMIX_process_sleep_destroy(action);
468 }
469
470 void SIMIX_process_sleep_destroy(smx_action_t action)
471 {
472   DEBUG1("Destroy action %p", action);
473   if (action->name)
474     xbt_free(action->name);
475   if (action->sleep.surf_sleep)
476     action->sleep.surf_sleep->model_type->action_unref(action->sleep.surf_sleep);
477 #ifdef HAVE_TRACING
478   TRACE_smx_action_destroy(action);
479 #endif
480   xbt_fifo_free(action->request_list);
481   xbt_free(action);
482 }
483
484 void SIMIX_process_sleep_suspend(smx_action_t action)
485 {
486   surf_workstation_model->suspend(action->sleep.surf_sleep);
487 }
488
489 void SIMIX_process_sleep_resume(smx_action_t action)
490 {
491   surf_workstation_model->resume(action->sleep.surf_sleep);
492 }
493
494 /**
495  * \brief Returns the current agent.
496  *
497  * This functions returns the currently running SIMIX process.
498  *
499  * \return The SIMIX process
500  */
501 XBT_INLINE smx_process_t SIMIX_process_self(void)
502 {
503   if(simix_global)
504     return SIMIX_context_get_data(SIMIX_context_self());
505
506   return NULL;
507 }
508
509 /** 
510  * Calling this function makes the process to yield.
511  * Only the processes can call this function, giving back the control to maestro
512  */
513 void SIMIX_process_yield(void)
514 {
515   smx_process_t self = SIMIX_process_self();
516   
517   DEBUG1("Yield process '%s'", self->name);
518   
519   /* Go into sleep and return control to maestro */
520   SIMIX_context_suspend(self->context);
521
522   /* Ok, maestro returned control to us */
523   DEBUG1("Maestro returned control to me: '%s'", self->name);
524   
525   if (self->iwannadie)
526     SIMIX_context_stop(self->context);
527
528   if (self->doexception) {
529     DEBUG0("Wait, maestro left me an exception");
530     self->doexception = 0;
531     RETHROW;
532   }
533 }
534
535 /* callback: context fetching */
536 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
537 {
538   return SIMIX_process_self()->running_ctx;
539 }
540
541 /* callback: termination */
542 void SIMIX_process_exception_terminate(xbt_ex_t * e)
543 {
544   xbt_ex_display(e);
545   abort();
546 }