Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
65788507cad7d537d56b77ba0e7bd6cc5da51716
[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   simix_global->current_process = maestro;
86   
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_count++;
154     process->name = xbt_strdup(name);
155     process->smx_host = host;
156     process->iwannadie = 0;
157     process->data = data;
158
159     VERB1("Create context %s", process->name);
160     process->context = SIMIX_context_new(code, argc, argv,
161         simix_global->cleanup_process_function, process);
162
163     process->running_ctx = xbt_new(xbt_running_ctx_t, 1);
164     XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
165
166     /* Add properties */
167     process->properties = properties;
168
169     /* Add the process to it's host process list */
170     xbt_swag_insert(process, host->process_list);
171
172     DEBUG1("Start context '%s'", process->name);
173
174     /* Now insert it in the global process list and in the process to run list */
175     xbt_swag_insert(process, simix_global->process_list);
176     DEBUG2("Inserting %s(%s) in the to_run list", process->name, host->name);
177     xbt_swag_insert(process, simix_global->process_to_run);
178   }
179
180   return process;
181 }
182
183 /**
184  * \brief Internal function to kill a SIMIX process.
185  *
186  * This function may be called when a REQ_PROCESS_KILL request occurs,
187  * or directly for SIMIX internal purposes.
188  *
189  * \param process poor victim
190  */
191 void SIMIX_process_kill(smx_process_t process, smx_process_t killer) {
192
193   DEBUG2("Killing process %s on %s", process->name, process->smx_host->name);
194
195   process->iwannadie = 1;
196   process->blocked = 0;
197   process->suspended = 0;
198   /* FIXME: set doexception to 0 also? */
199   
200   if (process->waiting_action) {
201
202     switch (process->waiting_action->type) {
203
204       case SIMIX_ACTION_EXECUTE:          
205       case SIMIX_ACTION_PARALLEL_EXECUTE:
206         SIMIX_host_execution_destroy(process->waiting_action);
207         break;
208
209       case SIMIX_ACTION_COMMUNICATE:
210         SIMIX_comm_destroy(process->waiting_action);
211         break;
212
213       case SIMIX_ACTION_SLEEP:
214         SIMIX_process_sleep_destroy(process->waiting_action);
215         break;
216
217       case SIMIX_ACTION_SYNCHRO:
218         SIMIX_synchro_stop_waiting(process, process->request);
219         SIMIX_synchro_destroy(process->waiting_action);
220         break;
221
222       case SIMIX_ACTION_IO:
223         THROW_UNIMPLEMENTED;
224         break;
225     }
226   }
227
228   /* If I'm killing myself then stop, otherwise schedule the process to kill. */
229   if (process == killer) {
230     SIMIX_context_stop(process->context);
231   }
232   else {
233     xbt_swag_insert(process, simix_global->process_to_run);
234   }
235 }
236
237 /**
238  * \brief Kills all running processes.
239  *
240  * Only maestro can kill everyone.
241  */
242 void SIMIX_process_killall(void)
243 {
244   smx_process_t p = NULL;
245
246   while ((p = xbt_swag_extract(simix_global->process_list)))
247     SIMIX_process_kill(p, SIMIX_process_self());
248
249   SIMIX_process_empty_trash();
250 }
251
252 void SIMIX_process_change_host(smx_process_t process,
253     const char *source, const char *dest)
254 {
255   smx_host_t h1 = NULL;
256   smx_host_t h2 = NULL;
257   xbt_assert0((process != NULL), "Invalid parameters");
258   h1 = SIMIX_host_get_by_name(source);
259   h2 = SIMIX_host_get_by_name(dest);
260   process->smx_host = h2;
261   xbt_swag_remove(process, h1->process_list);
262   xbt_swag_insert(process, h2->process_list);
263 }
264
265 void SIMIX_pre_process_suspend(smx_req_t req)
266 {
267   smx_process_t process = req->process_suspend.process;
268   SIMIX_process_suspend(process, req->issuer);
269
270   if (process != req->issuer) {
271     SIMIX_request_answer(req);
272   }
273   /* If we are suspending ourselves, then just do not replay the request. */
274 }
275
276 void SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
277 {
278   xbt_assert0(process, "Invalid parameters");
279
280   process->suspended = 1;
281
282   /* If we are suspending another process, and it is waiting on an action,
283      suspend it's action. */
284   if (process != issuer) {
285
286     if (process->waiting_action) {
287
288       switch (process->waiting_action->type) {
289
290         case SIMIX_ACTION_EXECUTE:
291         case SIMIX_ACTION_PARALLEL_EXECUTE:
292           SIMIX_host_execution_suspend(process->waiting_action);
293           break;
294
295         case SIMIX_ACTION_COMMUNICATE:
296           SIMIX_comm_suspend(process->waiting_action);
297           break;
298
299         case SIMIX_ACTION_SLEEP:
300           SIMIX_process_sleep_suspend(process->waiting_action);
301           break;
302
303         default:
304           THROW_IMPOSSIBLE;
305       }
306     }
307   }
308 }
309
310 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
311 {
312   xbt_assert0((process != NULL), "Invalid parameters");
313
314   process->suspended = 0;
315
316   /* If we are resuming another process, resume the action it was waiting for
317      if any. Otherwise add it to the list of process to run in the next round. */
318   if (process != issuer) {
319
320     if (process->waiting_action) {
321
322       switch(process->waiting_action->type) {
323
324         case SIMIX_ACTION_EXECUTE:          
325         case SIMIX_ACTION_PARALLEL_EXECUTE:
326           SIMIX_host_execution_resume(process->waiting_action);
327           break;
328           
329         case SIMIX_ACTION_COMMUNICATE:
330           SIMIX_comm_resume(process->waiting_action);
331           break;
332           
333         case SIMIX_ACTION_SLEEP:
334           SIMIX_process_sleep_resume(process->waiting_action);
335           break;
336           
337         default:
338           THROW_IMPOSSIBLE;
339       }
340     } else {
341       xbt_swag_insert(process, simix_global->process_to_run);
342     }
343   }
344 }
345
346 int SIMIX_process_count(void)
347 {
348   return xbt_swag_size(simix_global->process_list);
349 }
350
351 void* SIMIX_process_self_get_data(void)
352 {
353   smx_process_t me = SIMIX_process_self();
354   if (!me) {
355     return NULL;
356   }
357   return SIMIX_process_get_data(me);
358 }
359
360 void SIMIX_process_self_set_data(void *data)
361 {
362   SIMIX_process_set_data(SIMIX_process_self(), data);
363 }
364
365 void* SIMIX_process_get_data(smx_process_t process)
366 {
367   return process->data;
368 }
369
370 void SIMIX_process_set_data(smx_process_t process, void *data)
371 {
372   process->data = data;
373 }
374
375 smx_host_t SIMIX_process_get_host(smx_process_t process)
376 {
377   return process->smx_host;
378 }
379
380 /* needs to be public and without request because it is called
381    by exceptions and logging events */
382 const char* SIMIX_process_self_get_name(void) {
383
384   smx_process_t process = SIMIX_process_self();
385   if (process == NULL || process == simix_global->maestro_process)
386     return "";
387
388   return SIMIX_process_get_name(process);
389 }
390
391 const char* SIMIX_process_get_name(smx_process_t process)
392 {
393   return process->name;
394 }
395
396 int SIMIX_process_is_suspended(smx_process_t process)
397 {
398   return process->suspended;
399 }
400
401 int SIMIX_process_is_enabled(smx_process_t process)
402 {
403   if(process->request && SIMIX_request_isEnabled(process->request))
404     return TRUE;
405
406   return FALSE;
407 }
408
409 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
410 {
411   return process->properties;
412 }
413
414 void SIMIX_pre_process_sleep(smx_req_t req)
415 {
416 #ifdef HAVE_MC
417   if(_surf_do_model_check){
418     req->process_sleep.result = SIMIX_DONE;
419     SIMIX_request_answer(req);
420   }
421 #endif
422   smx_action_t action = SIMIX_process_sleep(req->issuer, req->process_sleep.duration);
423   xbt_fifo_push(action->request_list, req);
424   req->issuer->waiting_action = action;
425 }
426
427 smx_action_t SIMIX_process_sleep(smx_process_t process, double duration)
428 {
429   smx_action_t action;
430   smx_host_t host = process->smx_host;
431
432   /* check if the host is active */
433   if (surf_workstation_model->extension.
434       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
435     THROW1(host_error, 0, "Host %s failed, you cannot call this function",
436            host->name);
437   }
438
439   action = xbt_new0(s_smx_action_t, 1);
440   action->type = SIMIX_ACTION_SLEEP;
441   action->request_list = xbt_fifo_new();
442   action->name = xbt_strdup("sleep");
443 #ifdef HAVE_TRACING
444   action->category = NULL;
445 #endif
446
447   action->sleep.host = host;
448   action->sleep.surf_sleep =
449       surf_workstation_model->extension.workstation.sleep(host->host, duration);
450
451   surf_workstation_model->action_data_set(action->sleep.surf_sleep, action);
452   DEBUG1("Create sleep action %p", action);
453
454   return action;
455 }
456
457 void SIMIX_post_process_sleep(smx_action_t action)
458 {
459   e_smx_state_t state = SIMIX_action_map_state(surf_workstation_model->action_state_get(action->sleep.surf_sleep));
460   smx_req_t req;
461
462   while ((req = xbt_fifo_shift(action->request_list))) {
463     req->process_sleep.result = state;
464     req->issuer->waiting_action = NULL;
465     SIMIX_request_answer(req);
466   }
467
468   SIMIX_process_sleep_destroy(action);
469 }
470
471 void SIMIX_process_sleep_destroy(smx_action_t action)
472 {
473   DEBUG1("Destroy action %p", action);
474   if (action->name)
475     xbt_free(action->name);
476   if (action->sleep.surf_sleep)
477     action->sleep.surf_sleep->model_type->action_unref(action->sleep.surf_sleep);
478 #ifdef HAVE_TRACING
479   TRACE_smx_action_destroy(action);
480 #endif
481   xbt_fifo_free(action->request_list);
482   xbt_free(action);
483 }
484
485 void SIMIX_process_sleep_suspend(smx_action_t action)
486 {
487   surf_workstation_model->suspend(action->sleep.surf_sleep);
488 }
489
490 void SIMIX_process_sleep_resume(smx_action_t action)
491 {
492   surf_workstation_model->resume(action->sleep.surf_sleep);
493 }
494
495 /** 
496  * Calling this function makes the process to yield.
497  * Only the processes can call this function, giving back the control to maestro
498  */
499 void SIMIX_process_yield(void)
500 {
501   smx_process_t self = SIMIX_process_self();
502   
503   DEBUG1("Yield process '%s'", self->name);
504   
505   /* Go into sleep and return control to maestro */
506   SIMIX_context_suspend(self->context);
507
508   /* Ok, maestro returned control to us */
509   DEBUG1("Maestro returned control to me: '%s'", self->name);
510   
511   if (self->iwannadie)
512     SIMIX_context_stop(self->context);
513
514   if (self->doexception) {
515     DEBUG0("Wait, maestro left me an exception");
516     self->doexception = 0;
517     RETHROW;
518   }
519 }
520
521 /* callback: context fetching */
522 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
523 {
524   return simix_global->current_process->running_ctx;
525 }
526
527 /* callback: termination */
528 void SIMIX_process_exception_terminate(xbt_ex_t * e)
529 {
530   xbt_ex_display(e);
531   abort();
532 }