Logo AND Algorithmique Numérique Distribuée

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