Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
revise documentation of tracing examples
[simgrid.git] / examples / msg / trace-process-migration / trace-process-migration.c
1 /* Copyright (c) 2010-2015. 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 /** @addtogroup MSG_examples
8  * 
9  * - <b>Process migration: trace-process-migration/trace-process-migration.c</b>. Running this program with the
10  *   <i>--cfg=tracing:yes</i> and <i>--cfg=tracing/msg/process:yes</i> options  enables a Gantt-chart visualization of
11  *   where the process has been hosted during its execution. Migrations are represented by arrows from the origin to
12  *   the destination host.
13  */
14
15 #include "simgrid/msg.h"
16
17 /** The guy we will move from host to host. It move alone and then is moved by policeman back  */
18 static int emigrant(int argc, char *argv[])
19 {
20   msg_task_t task = NULL;
21   char *destination = NULL;
22
23   MSG_process_sleep(2);
24
25   while (1){ // I am an eternal emigrant
26     MSG_task_receive(&(task), "master_mailbox");
27     destination = (char*)MSG_task_get_data (task);
28     MSG_task_destroy (task);
29     if (!destination) break; //there is no destination, die
30     MSG_process_migrate(MSG_process_self(), MSG_host_by_name(destination));
31     MSG_process_sleep(2); // I am tired, have to sleep for 2 seconds
32     free (destination);
33     task = NULL;
34   }
35   return 0;
36 }
37
38 static int policeman(int argc, char *argv[])
39 {
40   msg_task_t task = NULL;
41
42   // I am the master of emigrant process,
43   // I tell it where it must emigrate to.
44   xbt_dynar_t destinations = xbt_dynar_new (sizeof(char*), &xbt_free_ref);
45   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Tremblay"));
46   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Jupiter"));
47   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Fafard"));
48   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Ginette"));
49   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Bourassa"));
50   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Fafard"));
51   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Tremblay"));
52   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Ginette"));
53   xbt_dynar_push_as (destinations, char*, NULL);
54
55   char *destination;
56   unsigned int i;
57   xbt_dynar_foreach(destinations, i, destination){
58     task = MSG_task_create("task", 0, 0, NULL);
59     if (destination){
60       MSG_task_set_data(task, xbt_strdup (destination));
61     }
62     MSG_task_set_category(task, "migration_order");
63     MSG_task_send (task, "master_mailbox");
64     task = NULL;
65   }
66   xbt_dynar_free (&destinations);
67   return 0;
68 }
69
70 int main(int argc, char *argv[])
71 {
72   MSG_init(&argc, argv);
73   xbt_assert(argc > 1, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
74
75   MSG_create_environment(argv[1]);
76
77   TRACE_category ("migration_order");
78
79   MSG_process_create("emigrant", emigrant, NULL, MSG_get_host_by_name("Fafard"));
80   MSG_process_create("policeman", policeman, NULL, MSG_get_host_by_name("Tremblay"));
81
82   MSG_main();
83   return 0;
84 }