Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do not make an expensive call to get_route if it is not needed.
[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", 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   simix_global->create_process_function(
98       &process,
99       args->name,
100       args->code,
101       args->data,
102       args->hostname,
103       args->argc,
104       args->argv,
105       args->properties);
106
107   return process;
108 }
109
110 /**
111  * \brief Internal function to create a process.
112  *
113  * This function actually creates the process.
114  * It may be called when a REQ_PROCESS_CREATE request occurs,
115  * or directly for SIMIX internal purposes.
116  *
117  * \return the process created
118  */
119 void SIMIX_process_create(smx_process_t *process,
120                           const char *name,
121                           xbt_main_func_t code,
122                           void *data,
123                           const char *hostname,
124                           int argc, char **argv,
125                           xbt_dict_t properties) {
126
127   *process = NULL;
128   smx_host_t host = SIMIX_host_get_by_name(hostname);
129
130   XBT_DEBUG("Start process %s on host %s", name, hostname);
131
132   if (!SIMIX_host_get_state(host)) {
133     XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
134           hostname);
135   }
136   else {
137     *process = xbt_new0(s_smx_process_t, 1);
138
139     xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
140
141     /* Process data */
142     (*process)->pid = simix_process_maxpid++;
143     (*process)->name = xbt_strdup(name);
144     (*process)->smx_host = host;
145     (*process)->data = data;
146
147     XBT_VERB("Create context %s", (*process)->name);
148     (*process)->context = SIMIX_context_new(code, argc, argv,
149         simix_global->cleanup_process_function, *process);
150
151     (*process)->running_ctx = xbt_new(xbt_running_ctx_t, 1);
152     XBT_RUNNING_CTX_INITIALIZE((*process)->running_ctx);
153
154     /* Add properties */
155     (*process)->properties = properties;
156
157     /* Add the process to it's host process list */
158     xbt_swag_insert(*process, host->process_list);
159
160     XBT_DEBUG("Start context '%s'", (*process)->name);
161
162     /* Now insert it in the global process list and in the process to run list */
163     xbt_swag_insert(*process, simix_global->process_list);
164     XBT_DEBUG("Inserting %s(%s) in the to_run list", (*process)->name, host->name);
165     xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, *process);
166   }
167 }
168
169 /**
170  * \brief Internal function to kill a SIMIX process.
171  *
172  * This function may be called when a REQ_PROCESS_KILL request occurs,
173  * or directly for SIMIX internal purposes.
174  *
175  * \param process poor victim
176  */
177 void SIMIX_process_kill(smx_process_t process) {
178
179   XBT_DEBUG("Killing process %s on %s", process->name, process->smx_host->name);
180
181   process->context->iwannadie = 1;
182   process->blocked = 0;
183   process->suspended = 0;
184   /* FIXME: set doexception to 0 also? */
185
186   if (process->waiting_action) {
187
188     switch (process->waiting_action->type) {
189
190       case SIMIX_ACTION_EXECUTE:          
191       case SIMIX_ACTION_PARALLEL_EXECUTE:
192         SIMIX_host_execution_destroy(process->waiting_action);
193         break;
194
195       case SIMIX_ACTION_COMMUNICATE:
196         SIMIX_comm_destroy(process->waiting_action);
197         break;
198
199       case SIMIX_ACTION_SLEEP:
200         SIMIX_process_sleep_destroy(process->waiting_action);
201         break;
202
203       case SIMIX_ACTION_SYNCHRO:
204         SIMIX_synchro_stop_waiting(process, &process->request);
205         SIMIX_synchro_destroy(process->waiting_action);
206         break;
207
208       case SIMIX_ACTION_IO:
209         THROW_UNIMPLEMENTED;
210         break;
211     }
212   }
213
214   xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
215 }
216
217 /**
218  * \brief Kills all running processes.
219  * \param issuer this one will not be killed
220  */
221 void SIMIX_process_killall(smx_process_t issuer)
222 {
223   smx_process_t p = NULL;
224
225   while ((p = xbt_swag_extract(simix_global->process_list))) {
226     if (p != issuer) {
227       SIMIX_process_kill(p);
228     }
229   }
230
231   SIMIX_context_runall(simix_global->process_to_run);
232
233   SIMIX_process_empty_trash();
234 }
235
236 void SIMIX_process_change_host(smx_process_t process,
237                                smx_host_t dest)
238 {
239   xbt_assert((process != NULL), "Invalid parameters");
240   xbt_swag_remove(process, process->smx_host->process_list);
241   process->smx_host = dest;
242   xbt_swag_insert(process, dest->process_list);
243 }
244
245 void SIMIX_pre_process_change_host(smx_process_t process, smx_host_t dest)
246 {
247   process->new_host = dest;
248 }
249
250 void SIMIX_pre_process_suspend(smx_req_t req)
251 {
252   smx_process_t process = req->process_suspend.process;
253   SIMIX_process_suspend(process, req->issuer);
254
255   if (process != req->issuer) {
256     SIMIX_request_answer(req);
257   }
258   /* If we are suspending ourselves, then just do not replay the request. */
259 }
260
261 void SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
262 {
263   process->suspended = 1;
264
265   /* If we are suspending another process, and it is waiting on an action,
266      suspend it's action. */
267   if (process != issuer) {
268
269     if (process->waiting_action) {
270
271       switch (process->waiting_action->type) {
272
273         case SIMIX_ACTION_EXECUTE:
274         case SIMIX_ACTION_PARALLEL_EXECUTE:
275           SIMIX_host_execution_suspend(process->waiting_action);
276           break;
277
278         case SIMIX_ACTION_COMMUNICATE:
279           SIMIX_comm_suspend(process->waiting_action);
280           break;
281
282         case SIMIX_ACTION_SLEEP:
283           SIMIX_process_sleep_suspend(process->waiting_action);
284           break;
285
286         default:
287           THROW_IMPOSSIBLE;
288       }
289     }
290   }
291 }
292
293 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
294 {
295   xbt_assert((process != NULL), "Invalid parameters");
296
297   process->suspended = 0;
298
299   /* If we are resuming another process, resume the action it was waiting for
300      if any. Otherwise add it to the list of process to run in the next round. */
301   if (process != issuer) {
302
303     if (process->waiting_action) {
304
305       switch (process->waiting_action->type) {
306
307         case SIMIX_ACTION_EXECUTE:          
308         case SIMIX_ACTION_PARALLEL_EXECUTE:
309           SIMIX_host_execution_resume(process->waiting_action);
310           break;
311
312         case SIMIX_ACTION_COMMUNICATE:
313           SIMIX_comm_resume(process->waiting_action);
314           break;
315
316         case SIMIX_ACTION_SLEEP:
317           SIMIX_process_sleep_resume(process->waiting_action);
318           break;
319
320         default:
321           THROW_IMPOSSIBLE;
322       }
323     }
324     else {
325       xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
326     }
327   }
328 }
329
330 int SIMIX_process_get_maxpid(void) {
331   return simix_process_maxpid;
332 }
333
334 int SIMIX_process_count(void)
335 {
336   return xbt_swag_size(simix_global->process_list);
337 }
338
339 void* SIMIX_process_self_get_data(void)
340 {
341   smx_process_t me = SIMIX_process_self();
342   if (!me) {
343     return NULL;
344   }
345   return SIMIX_process_get_data(me);
346 }
347
348 void SIMIX_process_self_set_data(void *data)
349 {
350   SIMIX_process_set_data(SIMIX_process_self(), data);
351 }
352
353 void* SIMIX_process_get_data(smx_process_t process)
354 {
355   return process->data;
356 }
357
358 void SIMIX_process_set_data(smx_process_t process, void *data)
359 {
360   process->data = data;
361 }
362
363 smx_host_t SIMIX_process_get_host(smx_process_t process)
364 {
365   return process->smx_host;
366 }
367
368 /* needs to be public and without request because it is called
369    by exceptions and logging events */
370 const char* SIMIX_process_self_get_name(void) {
371
372   smx_process_t process = SIMIX_process_self();
373   if (process == NULL || process == simix_global->maestro_process)
374     return "";
375
376   return SIMIX_process_get_name(process);
377 }
378
379 const char* SIMIX_process_get_name(smx_process_t process)
380 {
381   return process->name;
382 }
383
384 smx_process_t SIMIX_process_get_by_name(const char* name)
385 {
386   smx_process_t proc;
387
388   xbt_swag_foreach(proc, simix_global->process_list)
389   {
390     if(!strcmp(name, proc->name))
391       return proc;
392   }
393   return NULL;
394 }
395
396 int SIMIX_process_is_suspended(smx_process_t process)
397 {
398   return process->suspended;
399 }
400
401 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
402 {
403   return process->properties;
404 }
405
406 void SIMIX_pre_process_sleep(smx_req_t req)
407 {
408   if (MC_IS_ENABLED) {
409     MC_process_clock_add(req->issuer, req->process_sleep.duration);
410     req->process_sleep.result = SIMIX_DONE;
411     SIMIX_request_answer(req);
412     return;
413   }
414   smx_action_t action = SIMIX_process_sleep(req->issuer, req->process_sleep.duration);
415   xbt_fifo_push(action->request_list, req);
416   req->issuer->waiting_action = action;
417 }
418
419 smx_action_t SIMIX_process_sleep(smx_process_t process, double duration)
420 {
421   smx_action_t action;
422   smx_host_t host = process->smx_host;
423
424   /* check if the host is active */
425   if (surf_workstation_model->extension.
426       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
427     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
428            host->name);
429   }
430
431   action = xbt_mallocator_get(simix_global->action_mallocator);
432   action->type = SIMIX_ACTION_SLEEP;
433   action->name = NULL;
434 #ifdef HAVE_TRACING
435   action->category = NULL;
436 #endif
437
438   action->sleep.host = host;
439   action->sleep.surf_sleep =
440       surf_workstation_model->extension.workstation.sleep(host->host, duration);
441
442   surf_workstation_model->action_data_set(action->sleep.surf_sleep, action);
443   XBT_DEBUG("Create sleep action %p", action);
444
445   return action;
446 }
447
448 void SIMIX_post_process_sleep(smx_action_t action)
449 {
450   smx_req_t req;
451   e_smx_state_t state;
452
453   while ((req = xbt_fifo_shift(action->request_list))) {
454
455     switch(surf_workstation_model->action_state_get(action->sleep.surf_sleep)){
456       case SURF_ACTION_FAILED:
457         state = SIMIX_SRC_HOST_FAILURE;
458         break;
459
460       case SURF_ACTION_DONE:
461         state = SIMIX_DONE;
462         break;
463
464       default:
465         THROW_IMPOSSIBLE;
466         break;
467     }
468     req->process_sleep.result = state;
469     req->issuer->waiting_action = NULL;
470     SIMIX_request_answer(req);
471   }
472   SIMIX_process_sleep_destroy(action);
473 }
474
475 void SIMIX_process_sleep_destroy(smx_action_t action)
476 {
477   XBT_DEBUG("Destroy action %p", action);
478   if (action->sleep.surf_sleep)
479     action->sleep.surf_sleep->model_type->action_unref(action->sleep.surf_sleep);
480 #ifdef HAVE_TRACING
481   TRACE_smx_action_destroy(action);
482 #endif
483   xbt_mallocator_release(simix_global->action_mallocator, 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   XBT_DEBUG("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   XBT_DEBUG("Maestro returned control to me: '%s'", self->name);
511
512   if (self->context->iwannadie){
513     XBT_DEBUG("I wanna die!");
514     SIMIX_context_stop(self->context);
515   }
516
517   if (self->doexception) {
518     XBT_DEBUG("Wait, maestro left me an exception");
519     self->doexception = 0;
520     RETHROW;
521   }
522   
523   if (self->new_host) {
524     SIMIX_process_change_host(self, self->new_host);
525     self->new_host = NULL;
526   }
527 }
528
529 /* callback: context fetching */
530 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
531 {
532   return SIMIX_process_self()->running_ctx;
533 }
534
535 /* callback: termination */
536 void SIMIX_process_exception_terminate(xbt_ex_t * e)
537 {
538   xbt_ex_display(e);
539   abort();
540 }
541
542 smx_context_t SIMIX_process_get_context(smx_process_t p) {
543   return p->context;
544 }
545
546 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
547   p->context = c;
548 }