Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
new function (private to simix): SIMIX_process_get_maxpid
[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 static unsigned long simix_process_count = 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_count++;
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
85   simix_global->maestro_process = maestro;
86   return;
87 }
88
89 /**
90  * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
91  * \return the process created
92  */
93 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
94
95   smx_process_t process;
96
97   if (simix_global->create_process_function) {
98     process = simix_global->create_process_function(args->name,
99         args->code,
100         args->data,
101         args->hostname,
102         args->argc,
103         args->argv,
104         args->properties);
105   }
106   else {
107     process = SIMIX_process_create(args->name,
108         args->code,
109         args->data,
110         args->hostname,
111         args->argc,
112         args->argv,
113         args->properties);
114   }
115   // FIXME: to simplify this, simix_global->create_process_function could just
116   // be SIMIX_process_create() by default (and the same thing in smx_deployment.c)
117
118   return process;
119 }
120
121 /**
122  * \brief Internal function to create a process.
123  *
124  * This function actually creates the process.
125  * It may be called when a REQ_PROCESS_CREATE request occurs,
126  * or directly for SIMIX internal purposes.
127  *
128  * \return the process created
129  */
130 smx_process_t SIMIX_process_create(const char *name,
131                                    xbt_main_func_t code,
132                                    void *data,
133                                    const char *hostname,
134                                    int argc, char **argv,
135                                    xbt_dict_t properties) {
136
137   smx_process_t process = NULL;
138   smx_host_t host = SIMIX_host_get_by_name(hostname);
139
140   DEBUG2("Start process %s on host %s", name, hostname);
141
142   if (!SIMIX_host_get_state(host)) {
143     WARN2("Cannot launch process '%s' on failed host '%s'", name,
144           hostname);
145   }
146   else {
147     process = xbt_new0(s_smx_process_t, 1);
148
149     xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
150
151     /* Process data */
152     process->pid = simix_process_count++;
153     process->name = xbt_strdup(name);
154     process->smx_host = host;
155     process->data = data;
156
157     VERB1("Create context %s", process->name);
158     process->context = SIMIX_context_new(code, argc, argv,
159         simix_global->cleanup_process_function, process);
160
161     process->running_ctx = xbt_new(xbt_running_ctx_t, 1);
162     XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
163
164     /* Add properties */
165     process->properties = properties;
166
167     /* Add the process to it's host process list */
168     xbt_swag_insert(process, host->process_list);
169
170     DEBUG1("Start context '%s'", process->name);
171
172     /* Now insert it in the global process list and in the process to run list */
173     xbt_swag_insert(process, simix_global->process_list);
174     DEBUG2("Inserting %s(%s) in the to_run list", process->name, host->name);
175     xbt_swag_insert(process, simix_global->process_to_run);
176   }
177
178   return process;
179 }
180
181 /**
182  * \brief Internal function to kill a SIMIX process.
183  *
184  * This function may be called when a REQ_PROCESS_KILL request occurs,
185  * or directly for SIMIX internal purposes.
186  *
187  * \param process poor victim
188  */
189 void SIMIX_process_kill(smx_process_t process, smx_process_t killer) {
190
191   DEBUG2("Killing process %s on %s", process->name, process->smx_host->name);
192
193   process->context->iwannadie = 1;
194   process->blocked = 0;
195   process->suspended = 0;
196   /* FIXME: set doexception to 0 also? */
197
198   if (process->waiting_action) {
199
200     switch (process->waiting_action->type) {
201
202       case SIMIX_ACTION_EXECUTE:          
203       case SIMIX_ACTION_PARALLEL_EXECUTE:
204         SIMIX_host_execution_destroy(process->waiting_action);
205         break;
206
207       case SIMIX_ACTION_COMMUNICATE:
208         SIMIX_comm_destroy(process->waiting_action);
209         break;
210
211       case SIMIX_ACTION_SLEEP:
212         SIMIX_process_sleep_destroy(process->waiting_action);
213         break;
214
215       case SIMIX_ACTION_SYNCHRO:
216         SIMIX_synchro_stop_waiting(process, process->request);
217         SIMIX_synchro_destroy(process->waiting_action);
218         break;
219
220       case SIMIX_ACTION_IO:
221         THROW_UNIMPLEMENTED;
222         break;
223     }
224   }
225
226   /* If I'm killing myself then stop, otherwise schedule the process to kill. */
227   if (process == killer) {
228     SIMIX_context_stop(process->context);
229   }
230   else {
231     xbt_swag_insert(process, simix_global->process_to_run);
232   }
233 }
234
235 /**
236  * \brief Kills all running processes.
237  *
238  * Only maestro can kill everyone.
239  */
240 void SIMIX_process_killall(void)
241 {
242   smx_process_t p = NULL;
243
244   while ((p = xbt_swag_extract(simix_global->process_list)))
245     SIMIX_process_kill(p, SIMIX_process_self());
246
247   SIMIX_process_empty_trash();
248 }
249
250 void SIMIX_process_change_host(smx_process_t process,
251     const char *source, const char *dest)
252 {
253   smx_host_t h1 = NULL;
254   smx_host_t h2 = NULL;
255   xbt_assert0((process != NULL), "Invalid parameters");
256   h1 = SIMIX_host_get_by_name(source);
257   h2 = SIMIX_host_get_by_name(dest);
258   process->smx_host = h2;
259   xbt_swag_remove(process, h1->process_list);
260   xbt_swag_insert(process, h2->process_list);
261 }
262
263 void SIMIX_pre_process_suspend(smx_req_t req)
264 {
265   smx_process_t process = req->process_suspend.process;
266   SIMIX_process_suspend(process, req->issuer);
267
268   if (process != req->issuer) {
269     SIMIX_request_answer(req);
270   }
271   /* If we are suspending ourselves, then just do not replay the request. */
272 }
273
274 void SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
275 {
276   xbt_assert0(process, "Invalid parameters");
277
278   process->suspended = 1;
279
280   /* If we are suspending another process, and it is waiting on an action,
281      suspend it's action. */
282   if (process != issuer) {
283
284     if (process->waiting_action) {
285
286       switch (process->waiting_action->type) {
287
288         case SIMIX_ACTION_EXECUTE:
289         case SIMIX_ACTION_PARALLEL_EXECUTE:
290           SIMIX_host_execution_suspend(process->waiting_action);
291           break;
292
293         case SIMIX_ACTION_COMMUNICATE:
294           SIMIX_comm_suspend(process->waiting_action);
295           break;
296
297         case SIMIX_ACTION_SLEEP:
298           SIMIX_process_sleep_suspend(process->waiting_action);
299           break;
300
301         default:
302           THROW_IMPOSSIBLE;
303       }
304     }
305   }
306 }
307
308 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
309 {
310   xbt_assert0((process != NULL), "Invalid parameters");
311
312   process->suspended = 0;
313
314   /* If we are resuming another process, resume the action it was waiting for
315      if any. Otherwise add it to the list of process to run in the next round. */
316   if (process != issuer) {
317
318     if (process->waiting_action) {
319
320       switch (process->waiting_action->type) {
321
322         case SIMIX_ACTION_EXECUTE:          
323         case SIMIX_ACTION_PARALLEL_EXECUTE:
324           SIMIX_host_execution_resume(process->waiting_action);
325           break;
326
327         case SIMIX_ACTION_COMMUNICATE:
328           SIMIX_comm_resume(process->waiting_action);
329           break;
330
331         case SIMIX_ACTION_SLEEP:
332           SIMIX_process_sleep_resume(process->waiting_action);
333           break;
334
335         default:
336           THROW_IMPOSSIBLE;
337       }
338     }
339     else {
340       xbt_swag_insert(process, simix_global->process_to_run);
341     }
342   }
343 }
344
345 int SIMIX_process_get_maxpid(void) {
346   return simix_process_count;
347 }
348 int SIMIX_process_count(void)
349 {
350   return xbt_swag_size(simix_global->process_list);
351 }
352
353 void* SIMIX_process_self_get_data(void)
354 {
355   smx_process_t me = SIMIX_process_self();
356   if (!me) {
357     return NULL;
358   }
359   return SIMIX_process_get_data(me);
360 }
361
362 void SIMIX_process_self_set_data(void *data)
363 {
364   SIMIX_process_set_data(SIMIX_process_self(), data);
365 }
366
367 void* SIMIX_process_get_data(smx_process_t process)
368 {
369   return process->data;
370 }
371
372 void SIMIX_process_set_data(smx_process_t process, void *data)
373 {
374   process->data = data;
375 }
376
377 smx_host_t SIMIX_process_get_host(smx_process_t process)
378 {
379   return process->smx_host;
380 }
381
382 /* needs to be public and without request because it is called
383    by exceptions and logging events */
384 const char* SIMIX_process_self_get_name(void) {
385
386   smx_process_t process = SIMIX_process_self();
387   if (process == NULL || process == simix_global->maestro_process)
388     return "";
389
390   return SIMIX_process_get_name(process);
391 }
392
393 const char* SIMIX_process_get_name(smx_process_t process)
394 {
395   return process->name;
396 }
397
398 int SIMIX_process_is_suspended(smx_process_t process)
399 {
400   return process->suspended;
401 }
402
403 int SIMIX_process_is_enabled(smx_process_t process)
404 {
405   if (process->request && SIMIX_request_is_enabled(process->request))
406     return TRUE;
407
408   return FALSE;
409 }
410
411 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
412 {
413   return process->properties;
414 }
415
416 void SIMIX_pre_process_sleep(smx_req_t req)
417 {
418   if (MC_IS_ENABLED) {
419     req->process_sleep.result = SIMIX_DONE;
420     SIMIX_request_answer(req);
421   }
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->context->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_process_self()->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 }
533
534 smx_context_t SIMIX_process_get_context(smx_process_t p) {
535   return p->context;
536 }
537 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
538   p->context = c;
539 }