Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
make this test test something
[simgrid.git] / examples / msg / tracing / procmig.c
1 /* Copyright (c) 2010-2015. 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 this program with the options below
10  * enables a gantt-chart visualization of where the process has been during its execution. Migrations are represented by
11  * arrows from the origin to the destination host. You might want to run this program with the following parameters:
12  * --cfg=tracing:yes
13  * --cfg=tracing/msg/process:yes
14  * (See \ref tracing_tracing_options for details)
15  */
16
17 #include "simgrid/msg.h"
18
19 /** The guy we will move from host to host. It move alone and then is moved by policeman back  */
20 static int emigrant(int argc, char *argv[])
21 {
22   msg_task_t task = NULL;
23   char *destination = NULL;
24
25   MSG_process_sleep(2);
26
27   while (1){ // I am an eternal emigrant
28     MSG_task_receive(&(task), "master_mailbox");
29     destination = (char*)MSG_task_get_data (task);
30     MSG_task_destroy (task);
31     if (!destination) break; //there is no destination, die
32     MSG_process_migrate(MSG_process_self(), MSG_host_by_name(destination));
33     MSG_process_sleep(2); // I am tired, have to sleep for 2 seconds
34     free (destination);
35     task = NULL;
36   }
37   return 0;
38 }
39
40 static int policeman(int argc, char *argv[])
41 {
42   msg_task_t task = NULL;
43
44   // I am the master of emigrant process,
45   // I tell it where it must emigrate to.
46   xbt_dynar_t destinations = xbt_dynar_new (sizeof(char*), &xbt_free_ref);
47   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Tremblay"));
48   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Jupiter"));
49   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Fafard"));
50   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Ginette"));
51   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Bourassa"));
52   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Fafard"));
53   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Tremblay"));
54   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Ginette"));
55   xbt_dynar_push_as (destinations, char*, NULL);
56
57   char *destination;
58   unsigned int i;
59   xbt_dynar_foreach(destinations, i, destination){
60     task = MSG_task_create("task", 0, 0, NULL);
61     if (destination){
62       MSG_task_set_data(task, xbt_strdup (destination));
63     }
64     MSG_task_set_category(task, "migration_order");
65     MSG_task_send (task, "master_mailbox");
66     task = NULL;
67   }
68   xbt_dynar_free (&destinations);
69   return 0;
70 }
71
72 int main(int argc, char *argv[])
73 {
74   MSG_init(&argc, argv);
75   xbt_assert(argc > 1, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
76
77   MSG_create_environment(argv[1]);
78
79   TRACE_category ("migration_order");
80
81   MSG_process_create("emigrant", emigrant, NULL, MSG_get_host_by_name("Fafard"));
82   MSG_process_create("policeman", policeman, NULL, MSG_get_host_by_name("Tremblay"));
83
84   MSG_main();
85   return 0;
86 }