Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill old CVS $Id$ lines.
[simgrid.git] / examples / msg / tracing / procmig.c
1 /* Copyright (c) 2009 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 #include "msg/msg.h"            /* core library */
7 #include "xbt/sysdep.h"         /* calloc */
8
9 /* Create a log channel to have nice outputs. */
10 #include "xbt/log.h"
11 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
12                              "Messages specific for this msg example");
13
14 /** The guy we will move from host to host. It move alone and then is moved by policeman back  */
15 static int emigrant(int argc, char *argv[])
16 {
17   m_task_t task = NULL;
18   char *destination = NULL;
19
20   MSG_process_sleep(2);
21
22   while (1){ // I am an eternal emigrant
23     MSG_task_receive(&(task), "master_mailbox");
24     destination = (char*)MSG_task_get_data (task);
25     if (!destination) break; //there is no destination, die
26     XBT_INFO("Migrating to %s", destination);
27     MSG_process_migrate(MSG_process_self(), MSG_get_host_by_name(destination));
28     MSG_process_sleep(2); // I am tired, have to sleep for 2 seconds
29     MSG_task_destroy (task);
30     task = NULL;
31   }
32   return 0;
33 }
34
35 static int master(int argc, char *argv[])
36 {
37   m_task_t task = NULL;
38
39   // I am the master of emigrant process,
40   // I tell it where it must emigrate to.
41   xbt_dynar_t destinations = xbt_dynar_new (sizeof(char*), &xbt_free_ref);
42   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Tremblay"));
43   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Jupiter"));
44   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Fafard"));
45   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Ginette"));
46   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Bourassa"));
47   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Fafard"));
48   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Tremblay"));
49   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Ginette"));
50   xbt_dynar_push_as (destinations, char*, NULL);
51
52   char *destination;
53   unsigned int i;
54   xbt_dynar_foreach(destinations, i, destination){
55     task = MSG_task_create("task", 0, 0, NULL);
56     if (destination){
57       MSG_task_set_data(task, xbt_strdup (destination));
58     }
59     TRACE_msg_set_task_category(task, "migration_order");
60     MSG_task_send (task, "master_mailbox");
61     task = NULL;
62   }
63   xbt_dynar_free (&destinations);
64   return 0;
65 }
66
67 /** Main function */
68 int main(int argc, char *argv[])
69 {
70   MSG_error_t res = MSG_OK;
71
72   /* Argument checking */
73   MSG_global_init(&argc, argv);
74   if (argc < 3) {
75     XBT_CRITICAL("Usage: %s platform_file deployment_file\n", argv[0]);
76     XBT_CRITICAL("example: %s msg_platform.xml msg_deployment_suspend.xml\n",
77               argv[0]);
78     exit(1);
79   }
80
81   /* Simulation setting */
82   MSG_create_environment(argv[1]);
83
84   TRACE_category ("migration_order");
85
86   /* Application deployment */
87   MSG_function_register("emigrant", emigrant);
88   MSG_function_register("master", master);
89   MSG_launch_application(argv[2]);
90
91   /* Run the simulation */
92   res = MSG_main();
93   XBT_INFO("Simulation time %g", MSG_get_clock());
94   if (res == MSG_OK)
95     res = MSG_clean();
96
97   if (res == MSG_OK)
98     return 0;
99   else
100     return 1;
101 }                               /* end_of_main */