Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] bye bye triva, welcome viva
[simgrid.git] / examples / msg / tracing / procmig.c
1 /* Copyright (c) 2010 The SimGrid team. All rights reserved.                */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 /** @addtogroup MSG_examples
7  * 
8  * - <b>tracing/procmig.c</b> This program shows a process migration. Tracing
9  * this program with the options below enables a gantt-chart visualization
10  * of where the process has been during its execution. Migrations are represented by
11  * arrows from the origin to the destination host.
12  * You might want to run this program with the following parameters:
13  * --cfg=tracing:1
14  * --cfg=tracing/msg/process:1
15  * (See \ref tracing_tracing_options for details)
16  */
17
18 #include "msg/msg.h"            /* core library */
19 #include "xbt/sysdep.h"         /* calloc */
20
21 /* Create a log channel to have nice outputs. */
22 #include "xbt/log.h"
23 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
24                              "Messages specific for this msg example");
25
26 /** The guy we will move from host to host. It move alone and then is moved by policeman back  */
27 static int emigrant(int argc, char *argv[])
28 {
29   msg_task_t task = NULL;
30   char *destination = NULL;
31
32   MSG_process_sleep(2);
33
34   while (1){ // I am an eternal emigrant
35     MSG_task_receive(&(task), "master_mailbox");
36     destination = (char*)MSG_task_get_data (task);
37     MSG_task_destroy (task);
38     if (!destination) break; //there is no destination, die
39     MSG_process_migrate(MSG_process_self(), MSG_get_host_by_name(destination));
40     MSG_process_sleep(2); // I am tired, have to sleep for 2 seconds
41     free (destination);
42     task = NULL;
43   }
44   return 0;
45 }
46
47 static int master(int argc, char *argv[])
48 {
49   msg_task_t task = NULL;
50
51   // I am the master of emigrant process,
52   // I tell it where it must emigrate to.
53   xbt_dynar_t destinations = xbt_dynar_new (sizeof(char*), &xbt_free_ref);
54   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Tremblay"));
55   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Jupiter"));
56   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Fafard"));
57   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Ginette"));
58   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Bourassa"));
59   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Fafard"));
60   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Tremblay"));
61   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Ginette"));
62   xbt_dynar_push_as (destinations, char*, NULL);
63
64   char *destination;
65   unsigned int i;
66   xbt_dynar_foreach(destinations, i, destination){
67     task = MSG_task_create("task", 0, 0, NULL);
68     if (destination){
69       MSG_task_set_data(task, xbt_strdup (destination));
70     }
71     MSG_task_set_category(task, "migration_order");
72     MSG_task_send (task, "master_mailbox");
73     task = NULL;
74   }
75   xbt_dynar_free (&destinations);
76   return 0;
77 }
78
79 /** Main function */
80 int main(int argc, char *argv[])
81 {
82   /* Argument checking */
83   MSG_init(&argc, argv);
84   if (argc < 3) {
85     XBT_CRITICAL("Usage: %s platform_file deployment_file\n", argv[0]);
86     exit(1);
87   }
88
89   char *platform_file = argv[1];
90   char *deployment_file = argv[2];
91   MSG_create_environment(platform_file);
92
93   TRACE_category ("migration_order");
94
95   /* Application deployment */
96   MSG_function_register("emigrant", emigrant);
97   MSG_function_register("master", master);
98   MSG_launch_application(deployment_file);
99
100   MSG_main();
101   return 0;
102 }                               /* end_of_main */