Logo AND Algorithmique Numérique Distribuée

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