Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix tne runall functions
[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 static 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   DEBUG1("Cleanup process %s", process->name);
39   /*xbt_swag_remove(process, simix_global->process_to_run);*/
40   xbt_swag_remove(process, simix_global->process_list);
41   xbt_swag_remove(process, process->smx_host->process_list);
42   xbt_swag_insert(process, simix_global->process_to_destroy);
43 }
44
45 /** 
46  * Garbage collection
47  *
48  * Should be called some time to time to free the memory allocated for processes
49  * that have finished (or killed).
50  */
51 void SIMIX_process_empty_trash(void)
52 {
53   smx_process_t process = NULL;
54
55   while ((process = xbt_swag_extract(simix_global->process_to_destroy))) {
56     SIMIX_context_free(process->context);
57
58     /* Free the exception allocated at creation time */
59     if (process->running_ctx)
60       free(process->running_ctx);
61     if (process->properties)
62       xbt_dict_free(&process->properties);
63
64     free(process->name);
65     process->name = NULL;
66     free(process);
67   }
68 }
69
70 /**
71  * \brief Creates and runs the maestro process
72  */
73 void SIMIX_create_maestro_process()
74 {
75   smx_process_t maestro = NULL;
76
77   /* Create maestro process and intilialize it */
78   maestro = xbt_new0(s_smx_process_t, 1);
79   maestro->pid = simix_process_maxpid++;
80   maestro->name = (char *) "";
81   maestro->running_ctx = xbt_new(xbt_running_ctx_t, 1);
82   XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
83   maestro->context = SIMIX_context_new(NULL, 0, NULL, NULL, maestro);
84   maestro->request.issuer = maestro;
85
86   simix_global->maestro_process = maestro;
87   return;
88 }
89
90 /**
91  * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
92  * \return the process created
93  */
94 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
95
96   smx_process_t process;
97
98   if (simix_global->create_process_function) {
99     process = simix_global->create_process_function(args->name,
100         args->code,
101         args->data,
102         args->hostname,
103         args->argc,
104         args->argv,
105         args->properties);
106   }
107   else {
108     process = SIMIX_process_create(args->name,
109         args->code,
110         args->data,
111         args->hostname,
112         args->argc,
113         args->argv,
114         args->properties);
115   }
116   // FIXME: to simplify this, simix_global->create_process_function could just
117   // be SIMIX_process_create() by default (and the same thing in smx_deployment.c)
118
119   return process;
120 }
121
122 /**
123  * \brief Internal function to create a process.
124  *
125  * This function actually creates the process.
126  * It may be called when a REQ_PROCESS_CREATE request occurs,
127  * or directly for SIMIX internal purposes.
128  *
129  * \return the process created
130  */
131 smx_process_t SIMIX_process_create(const char *name,
132                                    xbt_main_func_t code,
133                                    void *data,
134                                    const char *hostname,
135                                    int argc, char **argv,
136                                    xbt_dict_t properties) {
137
138   smx_process_t process = NULL;
139   smx_host_t host = SIMIX_host_get_by_name(hostname);
140
141   DEBUG2("Start process %s on host %s", name, hostname);
142
143   if (!SIMIX_host_get_state(host)) {
144     WARN2("Cannot launch process '%s' on failed host '%s'", name,
145           hostname);
146   }
147   else {
148     process = xbt_new0(s_smx_process_t, 1);
149
150     xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
151
152     /* Process data */
153     process->pid = simix_process_maxpid++;
154     process->name = xbt_strdup(name);
155     process->smx_host = host;
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_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
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->context->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_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
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     }
340     else {
341       xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
342     }
343   }
344 }
345
346 int SIMIX_process_get_maxpid(void) {
347   return simix_process_maxpid;
348 }
349 int SIMIX_process_count(void)
350 {
351   return xbt_swag_size(simix_global->process_list);
352 }
353
354 void* SIMIX_process_self_get_data(void)
355 {
356   smx_process_t me = SIMIX_process_self();
357   if (!me) {
358     return NULL;
359   }
360   return SIMIX_process_get_data(me);
361 }
362
363 void SIMIX_process_self_set_data(void *data)
364 {
365   SIMIX_process_set_data(SIMIX_process_self(), data);
366 }
367
368 void* SIMIX_process_get_data(smx_process_t process)
369 {
370   return process->data;
371 }
372
373 void SIMIX_process_set_data(smx_process_t process, void *data)
374 {
375   process->data = data;
376 }
377
378 smx_host_t SIMIX_process_get_host(smx_process_t process)
379 {
380   return process->smx_host;
381 }
382
383 /* needs to be public and without request because it is called
384    by exceptions and logging events */
385 const char* SIMIX_process_self_get_name(void) {
386
387   smx_process_t process = SIMIX_process_self();
388   if (process == NULL || process == simix_global->maestro_process)
389     return "";
390
391   return SIMIX_process_get_name(process);
392 }
393
394 const char* SIMIX_process_get_name(smx_process_t process)
395 {
396   return process->name;
397 }
398
399 int SIMIX_process_is_suspended(smx_process_t process)
400 {
401   return process->suspended;
402 }
403
404 int SIMIX_process_is_enabled(smx_process_t process)
405 {
406   if (process->request.call != REQ_NO_REQ && SIMIX_request_is_enabled(&process->request))
407     return TRUE;
408
409   return FALSE;
410 }
411
412 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
413 {
414   return process->properties;
415 }
416
417 void SIMIX_pre_process_sleep(smx_req_t req)
418 {
419   if (MC_IS_ENABLED) {
420     req->process_sleep.result = SIMIX_DONE;
421     SIMIX_request_answer(req);
422   }
423   smx_action_t action = SIMIX_process_sleep(req->issuer, req->process_sleep.duration);
424   xbt_fifo_push(action->request_list, req);
425   req->issuer->waiting_action = action;
426 }
427
428 smx_action_t SIMIX_process_sleep(smx_process_t process, double duration)
429 {
430   smx_action_t action;
431   smx_host_t host = process->smx_host;
432
433   /* check if the host is active */
434   if (surf_workstation_model->extension.
435       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
436     THROW1(host_error, 0, "Host %s failed, you cannot call this function",
437            host->name);
438   }
439
440   action = xbt_new0(s_smx_action_t, 1);
441   action->type = SIMIX_ACTION_SLEEP;
442   action->request_list = xbt_fifo_new();
443   action->name = xbt_strdup("sleep");
444 #ifdef HAVE_TRACING
445   action->category = NULL;
446 #endif
447
448   action->sleep.host = host;
449   action->sleep.surf_sleep =
450       surf_workstation_model->extension.workstation.sleep(host->host, duration);
451
452   surf_workstation_model->action_data_set(action->sleep.surf_sleep, action);
453   DEBUG1("Create sleep action %p", action);
454
455   return action;
456 }
457
458 void SIMIX_post_process_sleep(smx_action_t action)
459 {
460   e_smx_state_t state = SIMIX_action_map_state(surf_workstation_model->action_state_get(action->sleep.surf_sleep));
461   smx_req_t req;
462
463   while ((req = xbt_fifo_shift(action->request_list))) {
464     req->process_sleep.result = state;
465     req->issuer->waiting_action = NULL;
466     SIMIX_request_answer(req);
467   }
468
469   SIMIX_process_sleep_destroy(action);
470 }
471
472 void SIMIX_process_sleep_destroy(smx_action_t action)
473 {
474   DEBUG1("Destroy action %p", action);
475   if (action->name)
476     xbt_free(action->name);
477   if (action->sleep.surf_sleep)
478     action->sleep.surf_sleep->model_type->action_unref(action->sleep.surf_sleep);
479 #ifdef HAVE_TRACING
480   TRACE_smx_action_destroy(action);
481 #endif
482   xbt_fifo_free(action->request_list);
483   xbt_free(action);
484 }
485
486 void SIMIX_process_sleep_suspend(smx_action_t action)
487 {
488   surf_workstation_model->suspend(action->sleep.surf_sleep);
489 }
490
491 void SIMIX_process_sleep_resume(smx_action_t action)
492 {
493   surf_workstation_model->resume(action->sleep.surf_sleep);
494 }
495
496 /** 
497  * Calling this function makes the process to yield.
498  * Only the processes can call this function, giving back the control to maestro
499  */
500 void SIMIX_process_yield(void)
501 {
502   smx_process_t self = SIMIX_process_self();
503
504   DEBUG1("Yield process '%s'", self->name);
505
506   /* Go into sleep and return control to maestro */
507   SIMIX_context_suspend(self->context);
508
509   /* Ok, maestro returned control to us */
510   DEBUG1("Maestro returned control to me: '%s'", self->name);
511
512   if (self->context->iwannadie)
513     SIMIX_context_stop(self->context);
514
515   if (self->doexception) {
516     DEBUG0("Wait, maestro left me an exception");
517     self->doexception = 0;
518     RETHROW;
519   }
520 }
521
522 /* callback: context fetching */
523 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
524 {
525   return SIMIX_process_self()->running_ctx;
526 }
527
528 /* callback: termination */
529 void SIMIX_process_exception_terminate(xbt_ex_t * e)
530 {
531   xbt_ex_display(e);
532   abort();
533 }
534
535 smx_context_t SIMIX_process_get_context(smx_process_t p) {
536   return p->context;
537 }
538 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
539   p->context = c;
540 }