Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] improved example of traced migration
[simgrid.git] / examples / msg / tracing / procmig.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2009 The SimGrid team. All rights reserved.                */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "msg/msg.h"            /* core library */
9 #include "xbt/sysdep.h"         /* calloc */
10
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
14                              "Messages specific for this msg example");
15
16 /** The guy we will move from host to host. It move alone and then is moved by policeman back  */
17 static int emigrant(int argc, char *argv[])
18 {
19   m_task_t task = NULL;
20   char *destination = NULL;
21
22   INFO0("Setting process category");
23   TRACE_msg_set_process_category(MSG_process_self(), "emigrant", "1 0 0");
24   MSG_process_sleep(2);
25
26   while (1){ // I am an eternal emigrant
27     MSG_task_receive(&(task), "master_mailbox");
28     destination = (char*)MSG_task_get_data (task);
29     if (!destination) break; //there is no destination, die
30     INFO1("Migrating to %s", destination);
31     MSG_process_change_host(MSG_get_host_by_name(destination));
32     MSG_process_sleep(2); // I am tired, have to sleep for 2 seconds
33     xbt_free (destination);
34     MSG_task_destroy (task);
35     task = NULL;
36   }
37   return 0;
38 }
39
40 static int master(int argc, char *argv[])
41 {
42   m_task_t task = NULL;
43
44   TRACE_msg_set_process_category(MSG_process_self(), "master", "1 0 0");
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);
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     TRACE_msg_set_task_category(task, "migration_order");
67     MSG_task_send (task, "master_mailbox");
68     task = NULL;
69   }
70   return 0;
71 }
72
73 /** Main function */
74 int main(int argc, char *argv[])
75 {
76   MSG_error_t res = MSG_OK;
77
78   /* Argument checking */
79   MSG_global_init(&argc, argv);
80   if (argc < 3) {
81     CRITICAL1("Usage: %s platform_file deployment_file\n", argv[0]);
82     CRITICAL1("example: %s msg_platform.xml msg_deployment_suspend.xml\n",
83               argv[0]);
84     exit(1);
85   }
86
87   /* Simulation setting */
88   MSG_create_environment(argv[1]);
89
90   TRACE_category ("migration_order");
91
92   /* Application deployment */
93   MSG_function_register("emigrant", emigrant);
94   MSG_function_register("master", master);
95   MSG_launch_application(argv[2]);
96
97   /* Run the simulation */
98   res = MSG_main();
99   INFO1("Simulation time %g", MSG_get_clock());
100   if (res == MSG_OK)
101     res = MSG_clean();
102
103   if (res == MSG_OK)
104     return 0;
105   else
106     return 1;
107 }                               /* end_of_main */