Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : change log category name
[simgrid.git] / examples / msg / migration / migration.c
1 /* Copyright (c) 2009, 2010. 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
10 /* Create a log channel to have nice outputs. */
11 #include "xbt/log.h"
12 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
13                              "Messages specific for this msg example");
14
15 xbt_mutex_t mutex = NULL;
16 xbt_cond_t cond = NULL;
17 static m_process_t process_to_migrate = NULL;
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   m_task_t task;
23   XBT_INFO
24       ("I'll look for a new job on another machine where the grass is greener.");
25   MSG_process_migrate(MSG_process_self(), MSG_get_host_by_name("Boivin"));
26   
27   XBT_INFO("Yeah, found something to do");
28   task = MSG_task_create("job", 98095000, 0, NULL);
29   MSG_task_execute(task);
30   MSG_task_destroy(task);
31   MSG_process_sleep(2);
32   XBT_INFO("Moving back home after work");
33   MSG_process_migrate(MSG_process_self(), MSG_get_host_by_name("Jacquelin"));
34   MSG_process_migrate(MSG_process_self(), MSG_get_host_by_name("Boivin"));
35   MSG_process_sleep(4);
36   xbt_mutex_acquire(mutex);
37   process_to_migrate = MSG_process_self();
38   xbt_cond_broadcast(cond);
39   xbt_mutex_release(mutex);
40   MSG_process_suspend(MSG_process_self());
41   m_host_t h = MSG_process_get_host(MSG_process_self());
42   XBT_INFO("I've been moved on this new host: %s", h->name);
43   XBT_INFO("Uh, nothing to do here. Stopping now");
44   return 0;
45 }                               /* end_of_emigrant */
46
47
48 /* This function move the emigrant on Jacquelin */
49 static int policeman(int argc, char *argv[])
50 {
51
52   xbt_mutex_acquire(mutex);
53   XBT_INFO("Wait a bit before migrating the emigrant.");
54   while (process_to_migrate == NULL) xbt_cond_wait(cond, mutex);
55   MSG_process_migrate(process_to_migrate, MSG_get_host_by_name("Jacquelin"));
56   XBT_INFO("I moved the emigrant");
57   MSG_process_resume(process_to_migrate);
58   xbt_mutex_release(mutex);
59
60   return 0;
61 }                               /* end_of_policeman */
62
63
64 /** Main function */
65 int main(int argc, char *argv[])
66 {
67   MSG_error_t res = MSG_OK;
68
69   /* Argument checking */
70   MSG_global_init(&argc, argv);
71   if (argc < 3) {
72     XBT_CRITICAL("Usage: %s platform_file deployment_file\n", argv[0]);
73     XBT_CRITICAL("example: %s msg_platform.xml msg_deployment_suspend.xml\n",
74               argv[0]);
75     exit(1);
76   }
77
78   /* Simulation setting */
79   MSG_create_environment(argv[1]);
80
81   /* Application deployment */
82   MSG_function_register("emigrant", emigrant);
83   MSG_function_register("policeman", policeman);
84   MSG_launch_application(argv[2]);
85
86   /* Run the simulation */
87   mutex = xbt_mutex_init();
88   cond = xbt_cond_init();
89   res = MSG_main();
90   XBT_INFO("Simulation time %g", MSG_get_clock());
91   xbt_cond_destroy(cond);
92   xbt_mutex_destroy(mutex);
93
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 */