Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add a function SIMIX_process_get_by_name()
[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_assert0(((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     const char *source, const char *dest)
238 {
239   smx_host_t h1 = NULL;
240   smx_host_t h2 = NULL;
241   xbt_assert0((process != NULL), "Invalid parameters");
242   h1 = SIMIX_host_get_by_name(source);
243   h2 = SIMIX_host_get_by_name(dest);
244   process->smx_host = h2;
245   xbt_swag_remove(process, h1->process_list);
246   xbt_swag_insert(process, h2->process_list);
247 }
248
249 void SIMIX_pre_process_suspend(smx_req_t req)
250 {
251   smx_process_t process = req->process_suspend.process;
252   SIMIX_process_suspend(process, req->issuer);
253
254   if (process != req->issuer) {
255     SIMIX_request_answer(req);
256   }
257   /* If we are suspending ourselves, then just do not replay the request. */
258 }
259
260 void SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
261 {
262   process->suspended = 1;
263
264   /* If we are suspending another process, and it is waiting on an action,
265      suspend it's action. */
266   if (process != issuer) {
267
268     if (process->waiting_action) {
269
270       switch (process->waiting_action->type) {
271
272         case SIMIX_ACTION_EXECUTE:
273         case SIMIX_ACTION_PARALLEL_EXECUTE:
274           SIMIX_host_execution_suspend(process->waiting_action);
275           break;
276
277         case SIMIX_ACTION_COMMUNICATE:
278           SIMIX_comm_suspend(process->waiting_action);
279           break;
280
281         case SIMIX_ACTION_SLEEP:
282           SIMIX_process_sleep_suspend(process->waiting_action);
283           break;
284
285         default:
286           THROW_IMPOSSIBLE;
287       }
288     }
289   }
290 }
291
292 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
293 {
294   xbt_assert0((process != NULL), "Invalid parameters");
295
296   process->suspended = 0;
297
298   /* If we are resuming another process, resume the action it was waiting for
299      if any. Otherwise add it to the list of process to run in the next round. */
300   if (process != issuer) {
301
302     if (process->waiting_action) {
303
304       switch (process->waiting_action->type) {
305
306         case SIMIX_ACTION_EXECUTE:          
307         case SIMIX_ACTION_PARALLEL_EXECUTE:
308           SIMIX_host_execution_resume(process->waiting_action);
309           break;
310
311         case SIMIX_ACTION_COMMUNICATE:
312           SIMIX_comm_resume(process->waiting_action);
313           break;
314
315         case SIMIX_ACTION_SLEEP:
316           SIMIX_process_sleep_resume(process->waiting_action);
317           break;
318
319         default:
320           THROW_IMPOSSIBLE;
321       }
322     }
323     else {
324       xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
325     }
326   }
327 }
328
329 int SIMIX_process_get_maxpid(void) {
330   return simix_process_maxpid;
331 }
332
333 int SIMIX_process_count(void)
334 {
335   return xbt_swag_size(simix_global->process_list);
336 }
337
338 void* SIMIX_process_self_get_data(void)
339 {
340   smx_process_t me = SIMIX_process_self();
341   if (!me) {
342     return NULL;
343   }
344   return SIMIX_process_get_data(me);
345 }
346
347 void SIMIX_process_self_set_data(void *data)
348 {
349   SIMIX_process_set_data(SIMIX_process_self(), data);
350 }
351
352 void* SIMIX_process_get_data(smx_process_t process)
353 {
354   return process->data;
355 }
356
357 void SIMIX_process_set_data(smx_process_t process, void *data)
358 {
359   process->data = data;
360 }
361
362 smx_host_t SIMIX_process_get_host(smx_process_t process)
363 {
364   return process->smx_host;
365 }
366
367 /* needs to be public and without request because it is called
368    by exceptions and logging events */
369 const char* SIMIX_process_self_get_name(void) {
370
371   smx_process_t process = SIMIX_process_self();
372   if (process == NULL || process == simix_global->maestro_process)
373     return "";
374
375   return SIMIX_process_get_name(process);
376 }
377
378 const char* SIMIX_process_get_name(smx_process_t process)
379 {
380   return process->name;
381 }
382
383 smx_process_t SIMIX_process_get_by_name(const char* name)
384 {
385   smx_process_t proc;
386
387   xbt_swag_foreach(proc, simix_global->process_list)
388   {
389     if(!strcmp(name, proc->name))
390       return proc;
391   }
392   return NULL;
393 }
394
395 int SIMIX_process_is_suspended(smx_process_t process)
396 {
397   return process->suspended;
398 }
399
400 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
401 {
402   return process->properties;
403 }
404
405 void SIMIX_pre_process_sleep(smx_req_t req)
406 {
407   if (MC_IS_ENABLED) {
408     MC_process_clock_add(req->issuer, req->process_sleep.duration);
409     req->process_sleep.result = SIMIX_DONE;
410     SIMIX_request_answer(req);
411     return;
412   }
413   smx_action_t action = SIMIX_process_sleep(req->issuer, req->process_sleep.duration);
414   xbt_fifo_push(action->request_list, req);
415   req->issuer->waiting_action = action;
416 }
417
418 smx_action_t SIMIX_process_sleep(smx_process_t process, double duration)
419 {
420   smx_action_t action;
421   smx_host_t host = process->smx_host;
422
423   /* check if the host is active */
424   if (surf_workstation_model->extension.
425       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
426     THROW1(host_error, 0, "Host %s failed, you cannot call this function",
427            host->name);
428   }
429
430   action = xbt_mallocator_get(simix_global->action_mallocator);
431   action->type = SIMIX_ACTION_SLEEP;
432   action->name = NULL;
433 #ifdef HAVE_TRACING
434   action->category = NULL;
435 #endif
436
437   action->sleep.host = host;
438   action->sleep.surf_sleep =
439       surf_workstation_model->extension.workstation.sleep(host->host, duration);
440
441   surf_workstation_model->action_data_set(action->sleep.surf_sleep, action);
442   XBT_DEBUG("Create sleep action %p", action);
443
444   return action;
445 }
446
447 void SIMIX_post_process_sleep(smx_action_t action)
448 {
449   smx_req_t req;
450   e_smx_state_t state;
451
452   while ((req = xbt_fifo_shift(action->request_list))) {
453
454     switch(surf_workstation_model->action_state_get(action->sleep.surf_sleep)){
455       case SURF_ACTION_FAILED:
456         state = SIMIX_SRC_HOST_FAILURE;
457         break;
458
459       case SURF_ACTION_DONE:
460         state = SIMIX_DONE;
461         break;
462
463       default:
464         THROW_IMPOSSIBLE;
465         break;
466     }
467     req->process_sleep.result = state;
468     req->issuer->waiting_action = NULL;
469     SIMIX_request_answer(req);
470   }
471   SIMIX_process_sleep_destroy(action);
472 }
473
474 void SIMIX_process_sleep_destroy(smx_action_t action)
475 {
476   XBT_DEBUG("Destroy action %p", action);
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_mallocator_release(simix_global->action_mallocator, 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   XBT_DEBUG("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   XBT_DEBUG("Maestro returned control to me: '%s'", self->name);
510
511   if (self->context->iwannadie){
512     XBT_DEBUG("I wanna die!");
513     SIMIX_context_stop(self->context);
514   }
515
516   if (self->doexception) {
517     XBT_DEBUG("Wait, maestro left me an exception");
518     self->doexception = 0;
519     RETHROW;
520   }
521 }
522
523 /* callback: context fetching */
524 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
525 {
526   return SIMIX_process_self()->running_ctx;
527 }
528
529 /* callback: termination */
530 void SIMIX_process_exception_terminate(xbt_ex_t * e)
531 {
532   xbt_ex_display(e);
533   abort();
534 }
535
536 smx_context_t SIMIX_process_get_context(smx_process_t p) {
537   return p->context;
538 }
539
540 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
541   p->context = c;
542 }