Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'hypervisor' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid...
[simgrid.git] / examples / msg / migration / migration.c
1 /* Copyright (c) 2009-2013. 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 #include "msg/msg.h"            /* core library */
8 #include "xbt/sysdep.h"         /* calloc */
9 #include "xbt/synchro_core.h"
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 /** @addtogroup MSG_examples
17  *  
18  *  - <b>migration/migration.c</b> Demonstrates how to use the @ref
19  *    MSG_process_migrate function to let processes change the host they 
20  *    run on after their start. 
21  */
22
23 xbt_mutex_t mutex = NULL;
24 xbt_cond_t cond = NULL;
25 static msg_process_t process_to_migrate = NULL;
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;
31   XBT_INFO
32       ("I'll look for a new job on another machine where the grass is greener.");
33   MSG_process_migrate(MSG_process_self(), MSG_get_host_by_name("Boivin"));
34   
35   XBT_INFO("Yeah, found something to do");
36   task = MSG_task_create("job", 98095000, 0, NULL);
37   MSG_task_execute(task);
38   MSG_task_destroy(task);
39   MSG_process_sleep(2);
40   XBT_INFO("Moving back home after work");
41   MSG_process_migrate(MSG_process_self(), MSG_get_host_by_name("Jacquelin"));
42   MSG_process_migrate(MSG_process_self(), MSG_get_host_by_name("Boivin"));
43   MSG_process_sleep(4);
44   xbt_mutex_acquire(mutex);
45   process_to_migrate = MSG_process_self();
46   xbt_cond_broadcast(cond);
47   xbt_mutex_release(mutex);
48   MSG_process_suspend(MSG_process_self());
49   msg_host_t h = MSG_process_get_host(MSG_process_self());
50   XBT_INFO("I've been moved on this new host: %s", MSG_host_get_name(h));
51   XBT_INFO("Uh, nothing to do here. Stopping now");
52   return 0;
53 }                               /* end_of_emigrant */
54
55
56 /* This function move the emigrant on Jacquelin */
57 static int policeman(int argc, char *argv[])
58 {
59
60   xbt_mutex_acquire(mutex);
61   XBT_INFO("Wait a bit before migrating the emigrant.");
62   while (process_to_migrate == NULL) xbt_cond_wait(cond, mutex);
63   MSG_process_migrate(process_to_migrate, MSG_get_host_by_name("Jacquelin"));
64   XBT_INFO("I moved the emigrant");
65   MSG_process_resume(process_to_migrate);
66   xbt_mutex_release(mutex);
67
68   return 0;
69 }                               /* end_of_policeman */
70
71
72 /** Main function */
73 int main(int argc, char *argv[])
74 {
75   msg_error_t res = MSG_OK;
76
77   /* Argument checking */
78   MSG_init(&argc, argv);
79   if (argc < 3) {
80     XBT_CRITICAL("Usage: %s platform_file deployment_file\n", argv[0]);
81     XBT_CRITICAL("example: %s msg_platform.xml msg_deployment_suspend.xml\n",
82               argv[0]);
83     exit(1);
84   }
85
86   /* Simulation setting */
87   MSG_create_environment(argv[1]);
88
89   /* Application deployment */
90   MSG_function_register("emigrant", emigrant);
91   MSG_function_register("policeman", policeman);
92   MSG_launch_application(argv[2]);
93
94   /* Run the simulation */
95   mutex = xbt_mutex_init();
96   cond = xbt_cond_init();
97   res = MSG_main();
98   XBT_INFO("Simulation time %g", MSG_get_clock());
99   xbt_cond_destroy(cond);
100   xbt_mutex_destroy(mutex);
101
102   if (res == MSG_OK)
103     return 0;
104   else
105     return 1;
106 }                               /* end_of_main */