Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Code refactor: move the functions associated to the request enabledness to mc_request.c
[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 /* FIXME: Ugly hack!*/
19 extern double NOW;
20
21 /**
22  * \brief Returns the current agent.
23  *
24  * This functions returns the currently running SIMIX process.
25  *
26  * \return The SIMIX process
27  */
28 XBT_INLINE smx_process_t SIMIX_process_self(void)
29 {
30   smx_context_t self_context = SIMIX_context_self();
31
32   return self_context ? SIMIX_context_get_data(self_context) : NULL;
33 }
34
35 /**
36  * \brief Move a process to the list of processes to destroy.
37  */
38 void SIMIX_process_cleanup(smx_process_t process)
39 {
40   DEBUG1("Cleanup process %s", process->name);
41   /*xbt_swag_remove(process, simix_global->process_to_run);*/
42   xbt_swag_remove(process, simix_global->process_list);
43   xbt_swag_remove(process, process->smx_host->process_list);
44   xbt_swag_insert(process, simix_global->process_to_destroy);
45 }
46
47 /** 
48  * Garbage collection
49  *
50  * Should be called some time to time to free the memory allocated for processes
51  * that have finished (or killed).
52  */
53 void SIMIX_process_empty_trash(void)
54 {
55   smx_process_t process = NULL;
56
57   while ((process = xbt_swag_extract(simix_global->process_to_destroy))) {
58     SIMIX_context_free(process->context);
59
60     /* Free the exception allocated at creation time */
61     if (process->running_ctx)
62       free(process->running_ctx);
63     if (process->properties)
64       xbt_dict_free(&process->properties);
65
66     free(process->name);
67     process->name = NULL;
68     free(process);
69   }
70 }
71
72 /**
73  * \brief Creates and runs the maestro process
74  */
75 void SIMIX_create_maestro_process()
76 {
77   smx_process_t maestro = NULL;
78
79   /* Create maestro process and intilialize it */
80   maestro = xbt_new0(s_smx_process_t, 1);
81   maestro->pid = simix_process_maxpid++;
82   maestro->name = (char *) "";
83   maestro->running_ctx = xbt_new(xbt_running_ctx_t, 1);
84   XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
85   maestro->context = SIMIX_context_new(NULL, 0, NULL, NULL, maestro);
86   maestro->request.issuer = maestro;
87
88   simix_global->maestro_process = maestro;
89   return;
90 }
91
92 /**
93  * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
94  * \return the process created
95  */
96 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
97
98   smx_process_t process;
99
100   if (simix_global->create_process_function) {
101     process = simix_global->create_process_function(args->name,
102         args->code,
103         args->data,
104         args->hostname,
105         args->argc,
106         args->argv,
107         args->properties);
108   }
109   else {
110     process = SIMIX_process_create(args->name,
111         args->code,
112         args->data,
113         args->hostname,
114         args->argc,
115         args->argv,
116         args->properties);
117   }
118   // FIXME: to simplify this, simix_global->create_process_function could just
119   // be SIMIX_process_create() by default (and the same thing in smx_deployment.c)
120
121   return process;
122 }
123
124 /**
125  * \brief Internal function to create a process.
126  *
127  * This function actually creates the process.
128  * It may be called when a REQ_PROCESS_CREATE request occurs,
129  * or directly for SIMIX internal purposes.
130  *
131  * \return the process created
132  */
133 smx_process_t SIMIX_process_create(const char *name,
134                                    xbt_main_func_t code,
135                                    void *data,
136                                    const char *hostname,
137                                    int argc, char **argv,
138                                    xbt_dict_t properties) {
139
140   smx_process_t process = NULL;
141   smx_host_t host = SIMIX_host_get_by_name(hostname);
142
143   DEBUG2("Start process %s on host %s", name, hostname);
144
145   if (!SIMIX_host_get_state(host)) {
146     WARN2("Cannot launch process '%s' on failed host '%s'", name,
147           hostname);
148   }
149   else {
150     process = xbt_new0(s_smx_process_t, 1);
151
152     xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
153
154     /* Process data */
155     process->pid = simix_process_maxpid++;
156     process->name = xbt_strdup(name);
157     process->smx_host = host;
158     process->data = data;
159
160     VERB1("Create context %s", process->name);
161     process->context = SIMIX_context_new(code, argc, argv,
162         simix_global->cleanup_process_function, process);
163
164     process->running_ctx = xbt_new(xbt_running_ctx_t, 1);
165     XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
166
167     /* Add properties */
168     process->properties = properties;
169
170     /* Add the process to it's host process list */
171     xbt_swag_insert(process, host->process_list);
172
173     DEBUG1("Start context '%s'", process->name);
174
175     /* Now insert it in the global process list and in the process to run list */
176     xbt_swag_insert(process, simix_global->process_list);
177     DEBUG2("Inserting %s(%s) in the to_run list", process->name, host->name);
178     xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
179   }
180
181   return process;
182 }
183
184 /**
185  * \brief Internal function to kill a SIMIX process.
186  *
187  * This function may be called when a REQ_PROCESS_KILL request occurs,
188  * or directly for SIMIX internal purposes.
189  *
190  * \param process poor victim
191  */
192 void SIMIX_process_kill(smx_process_t process, smx_process_t killer) {
193
194   DEBUG2("Killing process %s on %s", process->name, process->smx_host->name);
195
196   process->context->iwannadie = 1;
197   process->blocked = 0;
198   process->suspended = 0;
199   /* FIXME: set doexception to 0 also? */
200
201   if (process->waiting_action) {
202
203     switch (process->waiting_action->type) {
204
205       case SIMIX_ACTION_EXECUTE:          
206       case SIMIX_ACTION_PARALLEL_EXECUTE:
207         SIMIX_host_execution_destroy(process->waiting_action);
208         break;
209
210       case SIMIX_ACTION_COMMUNICATE:
211         SIMIX_comm_destroy(process->waiting_action);
212         break;
213
214       case SIMIX_ACTION_SLEEP:
215         SIMIX_process_sleep_destroy(process->waiting_action);
216         break;
217
218       case SIMIX_ACTION_SYNCHRO:
219         SIMIX_synchro_stop_waiting(process, &process->request);
220         SIMIX_synchro_destroy(process->waiting_action);
221         break;
222
223       case SIMIX_ACTION_IO:
224         THROW_UNIMPLEMENTED;
225         break;
226     }
227   }
228
229   /* If I'm killing myself then stop, otherwise schedule the process to kill. */
230   if (process == killer) {
231     SIMIX_context_stop(process->context);
232   }
233   else {
234     xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
235   }
236 }
237
238 /**
239  * \brief Kills all running processes.
240  *
241  * Only maestro can kill everyone.
242  */
243 void SIMIX_process_killall(void)
244 {
245   smx_process_t p = NULL;
246
247   while ((p = xbt_swag_extract(simix_global->process_list)))
248     SIMIX_process_kill(p, SIMIX_process_self());
249
250   SIMIX_context_runall(simix_global->process_to_run);
251
252   SIMIX_process_empty_trash();
253 }
254
255 void SIMIX_process_change_host(smx_process_t process,
256     const char *source, const char *dest)
257 {
258   smx_host_t h1 = NULL;
259   smx_host_t h2 = NULL;
260   xbt_assert0((process != NULL), "Invalid parameters");
261   h1 = SIMIX_host_get_by_name(source);
262   h2 = SIMIX_host_get_by_name(dest);
263   process->smx_host = h2;
264   xbt_swag_remove(process, h1->process_list);
265   xbt_swag_insert(process, h2->process_list);
266 }
267
268 void SIMIX_pre_process_suspend(smx_req_t req)
269 {
270   smx_process_t process = req->process_suspend.process;
271   SIMIX_process_suspend(process, req->issuer);
272
273   if (process != req->issuer) {
274     SIMIX_request_answer(req);
275   }
276   /* If we are suspending ourselves, then just do not replay the request. */
277 }
278
279 void SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
280 {
281   xbt_assert0(process, "Invalid parameters");
282
283   process->suspended = 1;
284
285   /* If we are suspending another process, and it is waiting on an action,
286      suspend it's action. */
287   if (process != issuer) {
288
289     if (process->waiting_action) {
290
291       switch (process->waiting_action->type) {
292
293         case SIMIX_ACTION_EXECUTE:
294         case SIMIX_ACTION_PARALLEL_EXECUTE:
295           SIMIX_host_execution_suspend(process->waiting_action);
296           break;
297
298         case SIMIX_ACTION_COMMUNICATE:
299           SIMIX_comm_suspend(process->waiting_action);
300           break;
301
302         case SIMIX_ACTION_SLEEP:
303           SIMIX_process_sleep_suspend(process->waiting_action);
304           break;
305
306         default:
307           THROW_IMPOSSIBLE;
308       }
309     }
310   }
311 }
312
313 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
314 {
315   xbt_assert0((process != NULL), "Invalid parameters");
316
317   process->suspended = 0;
318
319   /* If we are resuming another process, resume the action it was waiting for
320      if any. Otherwise add it to the list of process to run in the next round. */
321   if (process != issuer) {
322
323     if (process->waiting_action) {
324
325       switch (process->waiting_action->type) {
326
327         case SIMIX_ACTION_EXECUTE:          
328         case SIMIX_ACTION_PARALLEL_EXECUTE:
329           SIMIX_host_execution_resume(process->waiting_action);
330           break;
331
332         case SIMIX_ACTION_COMMUNICATE:
333           SIMIX_comm_resume(process->waiting_action);
334           break;
335
336         case SIMIX_ACTION_SLEEP:
337           SIMIX_process_sleep_resume(process->waiting_action);
338           break;
339
340         default:
341           THROW_IMPOSSIBLE;
342       }
343     }
344     else {
345       xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
346     }
347   }
348 }
349
350 int SIMIX_process_get_maxpid(void) {
351   return simix_process_maxpid;
352 }
353 int SIMIX_process_count(void)
354 {
355   return xbt_swag_size(simix_global->process_list);
356 }
357
358 void* SIMIX_process_self_get_data(void)
359 {
360   smx_process_t me = SIMIX_process_self();
361   if (!me) {
362     return NULL;
363   }
364   return SIMIX_process_get_data(me);
365 }
366
367 void SIMIX_process_self_set_data(void *data)
368 {
369   SIMIX_process_set_data(SIMIX_process_self(), data);
370 }
371
372 void* SIMIX_process_get_data(smx_process_t process)
373 {
374   return process->data;
375 }
376
377 void SIMIX_process_set_data(smx_process_t process, void *data)
378 {
379   process->data = data;
380 }
381
382 smx_host_t SIMIX_process_get_host(smx_process_t process)
383 {
384   return process->smx_host;
385 }
386
387 /* needs to be public and without request because it is called
388    by exceptions and logging events */
389 const char* SIMIX_process_self_get_name(void) {
390
391   smx_process_t process = SIMIX_process_self();
392   if (process == NULL || process == simix_global->maestro_process)
393     return "";
394
395   return SIMIX_process_get_name(process);
396 }
397
398 const char* SIMIX_process_get_name(smx_process_t process)
399 {
400   return process->name;
401 }
402
403 int SIMIX_process_is_suspended(smx_process_t process)
404 {
405   return process->suspended;
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   if (MC_IS_ENABLED) {
416     NOW += req->process_sleep.duration;
417     req->process_sleep.result = SIMIX_DONE;
418     SIMIX_request_answer(req);
419     return;
420   }
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  * Calling this function makes the process to yield.
496  * Only the processes can call this function, giving back the control to maestro
497  */
498 void SIMIX_process_yield(void)
499 {
500   smx_process_t self = SIMIX_process_self();
501
502   DEBUG1("Yield process '%s'", self->name);
503
504   /* Go into sleep and return control to maestro */
505   SIMIX_context_suspend(self->context);
506
507   /* Ok, maestro returned control to us */
508   DEBUG1("Maestro returned control to me: '%s'", self->name);
509
510   if (self->context->iwannadie){
511     DEBUG0("I wanna die!");
512     SIMIX_context_stop(self->context);
513   }
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 }