Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use new style logging macros.
[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   MSG_process_sleep(2);
23
24   while (1){ // I am an eternal emigrant
25     MSG_task_receive(&(task), "master_mailbox");
26     destination = (char*)MSG_task_get_data (task);
27     if (!destination) break; //there is no destination, die
28     XBT_INFO("Migrating to %s", destination);
29     MSG_process_change_host(MSG_get_host_by_name(destination));
30     MSG_process_sleep(2); // I am tired, have to sleep for 2 seconds
31     xbt_free (destination);
32     MSG_task_destroy (task);
33     task = NULL;
34   }
35   return 0;
36 }
37
38 static int master(int argc, char *argv[])
39 {
40   m_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);
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     TRACE_msg_set_task_category(task, "migration_order");
63     MSG_task_send (task, "master_mailbox");
64     task = NULL;
65   }
66   return 0;
67 }
68
69 /** Main function */
70 int main(int argc, char *argv[])
71 {
72   MSG_error_t res = MSG_OK;
73
74   /* Argument checking */
75   MSG_global_init(&argc, argv);
76   if (argc < 3) {
77     XBT_CRITICAL("Usage: %s platform_file deployment_file\n", argv[0]);
78     XBT_CRITICAL("example: %s msg_platform.xml msg_deployment_suspend.xml\n",
79               argv[0]);
80     exit(1);
81   }
82
83   /* Simulation setting */
84   MSG_create_environment(argv[1]);
85
86   TRACE_category ("migration_order");
87
88   /* Application deployment */
89   MSG_function_register("emigrant", emigrant);
90   MSG_function_register("master", master);
91   MSG_launch_application(argv[2]);
92
93   /* Run the simulation */
94   res = MSG_main();
95   XBT_INFO("Simulation time %g", MSG_get_clock());
96   if (res == MSG_OK)
97     res = MSG_clean();
98
99   if (res == MSG_OK)
100     return 0;
101   else
102     return 1;
103 }                               /* end_of_main */