Logo AND Algorithmique Numérique Distribuée

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