Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of github.com:simgrid/simgrid
[simgrid.git] / teshsuite / msg / host_on_off_processes / host_on_off_processes.cpp
1 /* Copyright (c) 2010-2018. 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 "simgrid/msg.h"
7 #include <xbt/ex.hpp>
8
9 #include <stdio.h> /* sscanf */
10
11 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
12
13 xbt_dynar_t tests;
14 int tasks_done = 0;
15
16 static void task_cleanup_handler(void* task)
17 {
18   if (task)
19     MSG_task_destroy(static_cast<msg_task_t>(task));
20 }
21
22 static int process_daemon(int /*argc*/, char** /*argv*/)
23 {
24   msg_process_t self = MSG_process_self();
25   XBT_INFO("  Start daemon on %s (%f)", MSG_host_get_name(MSG_host_self()), MSG_host_get_speed(MSG_host_self()));
26   for (;;) {
27     msg_task_t task = MSG_task_create("daemon", MSG_host_get_speed(MSG_host_self()), 0, NULL);
28     MSG_process_set_data(self, task);
29     XBT_INFO("  Execute daemon");
30     msg_error_t res = MSG_task_execute(task);
31     MSG_task_destroy(task);
32     tasks_done++;
33     if (res == MSG_HOST_FAILURE) {
34       XBT_INFO("Host as died as expected, do nothing else");
35       return 0;
36     }
37   }
38   XBT_INFO("  daemon done. See you!");
39   return 0;
40 }
41
42 static int process_sleep(int /*argc*/, char** /*argv*/)
43 {
44   for (;;) {
45     XBT_INFO("  I'm alive but I should sleep");
46     MSG_process_sleep(10);
47   }
48   XBT_INFO("  I'm done. See you!");
49   return 0;
50 }
51
52 static int commTX(int /*argc*/, char** /*argv*/)
53 {
54   const char* mailbox = "comm";
55   XBT_INFO("  Start TX");
56   msg_task_t task = MSG_task_create("COMM", 0, 100000000, NULL);
57   MSG_task_dsend(task, mailbox, task_cleanup_handler);
58   // We should wait a bit (if not the process will end before the communication, hence an exception on the other side).
59   try {
60     MSG_process_sleep(30);
61   } catch (xbt_ex& e) {
62     if (e.category == host_error) {
63       XBT_INFO("The host has died ... as expected.");
64     } else {
65       XBT_ERROR("An unexpected exception has been raised.");
66       throw;
67     }
68   }
69   XBT_INFO("  TX done");
70   return 0;
71 }
72
73 static int commRX(int /*argc*/, char** /*argv*/)
74 {
75   msg_task_t task     = NULL;
76   const char* mailbox = "comm";
77   XBT_INFO("  Start RX");
78   msg_error_t error = MSG_task_receive(&(task), mailbox);
79   if (error == MSG_OK) {
80     XBT_INFO("  Receive message: %s", task->name);
81     MSG_task_destroy(task);
82   } else if (error == MSG_HOST_FAILURE) {
83     XBT_INFO("  Receive message: HOST_FAILURE");
84   } else if (error == MSG_TRANSFER_FAILURE) {
85     XBT_INFO("  Receive message: TRANSFER_FAILURE");
86   } else {
87     XBT_INFO("  Receive message: %u", static_cast<unsigned int>(error));
88   }
89   XBT_INFO("  RX Done");
90   return 0;
91 }
92
93 static int test_launcher(int /*argc*/, char** /*argv*/)
94 {
95   int test = 0;
96   char** argvF;
97   msg_host_t jupiter = MSG_host_by_name("Jupiter");
98
99   test = 1;
100   // Create a process running a simple task on a host and turn the host off during the execution of the process.
101   if (xbt_dynar_search_or_negative(tests, &test) != -1) {
102     XBT_INFO("Test 1:");
103     XBT_INFO("  Create a process on Jupiter");
104     argvF    = xbt_new(char*, 2);
105     argvF[0] = xbt_strdup("process_daemon");
106     MSG_process_create_with_arguments("process_daemon", process_daemon, NULL, jupiter, 1, argvF);
107     MSG_process_sleep(3);
108     XBT_INFO("  Turn off Jupiter");
109     MSG_host_off(jupiter);
110     MSG_process_sleep(10);
111     XBT_INFO("Test 1 seems ok, cool !(#Processes: %d, it should be 1; #tasks: %d)", MSG_process_get_number(),
112              tasks_done);
113   }
114
115   test = 2;
116   // Create a process that on a host that is turned off (this should not be possible)
117   if (xbt_dynar_search_or_negative(tests, &test) != -1) {
118     XBT_INFO("Test 2:");
119     XBT_INFO("  Turn off Jupiter");
120     // adsein: Jupiter is already, hence nothing should happen
121     // adsein: This can be one additional test, to check that you cannot shutdown twice a host
122     MSG_host_off(jupiter);
123     argvF    = xbt_new(char*, 2);
124     argvF[0] = xbt_strdup("process_daemon");
125     MSG_process_create_with_arguments("process_daemon", process_daemon, NULL, jupiter, 1, argvF);
126     MSG_process_sleep(10);
127     XBT_INFO("  Test 2 does not crash as it should (number of Process : %d, it should be 1)", MSG_process_get_number());
128     XBT_INFO("  Ok so let's turn on/off the node to see whether the process is correctly bound to Jupiter");
129     MSG_host_on(jupiter);
130     XBT_INFO("  Turn off");
131     MSG_host_off(jupiter);
132     XBT_INFO("  sleep");
133     MSG_process_sleep(10);
134     XBT_INFO("number of Process : %d it should be 1. The daemon that has been created for test2 has been correctly "
135              "destroyed....ok at least it looks rigorous, cool ! You just have to disallow the possibility to create "
136              "a new process on a node when the node is off.)",
137              MSG_process_get_number());
138   }
139
140   test = 3;
141   // Create a process running sucessive sleeps on a host and turn the host off during the execution of the process.
142   if (xbt_dynar_search_or_negative(tests, &test) != -1) {
143     XBT_INFO("Test 3:");
144     MSG_host_on(jupiter);
145     argvF    = xbt_new(char*, 2);
146     argvF[0] = xbt_strdup("process_sleep");
147     MSG_process_create_with_arguments("process_sleep", process_sleep, NULL, jupiter, 1, argvF);
148     MSG_process_sleep(100);
149     XBT_INFO("  Turn off");
150     MSG_host_off(jupiter);
151     XBT_INFO("  sleep for 10 seconds");
152     MSG_process_sleep(10000);
153     XBT_INFO("number of Process : %d it should be 1 (i.e. the Test one))", MSG_process_get_number());
154   }
155
156   test = 4;
157   if (xbt_dynar_search_or_negative(tests, &test) != -1) {
158     XBT_INFO("Test 4 (turn off src during a communication) : Create a Process/task to make a communication between "
159              "Jupiter and Tremblay and turn off Jupiter during the communication");
160     MSG_host_on(jupiter);
161     MSG_process_sleep(10);
162     argvF    = xbt_new(char*, 2);
163     argvF[0] = xbt_strdup("commRX");
164     MSG_process_create_with_arguments("commRX", commRX, NULL, MSG_host_by_name("Tremblay"), 1, argvF);
165     argvF    = xbt_new(char*, 2);
166     argvF[0] = xbt_strdup("commTX");
167     MSG_process_create_with_arguments("commTX", commTX, NULL, jupiter, 1, argvF);
168     XBT_INFO("  number of processes: %d", MSG_process_get_number());
169     MSG_process_sleep(10);
170     XBT_INFO("  Turn Jupiter off");
171     MSG_host_off(jupiter);
172     XBT_INFO("Test 4 is ok.  (number of Process : %d, it should be 1 or 2 if RX has not been satisfied)."
173              " An exception is raised when we turn off a node that has a process sleeping",
174              MSG_process_get_number());
175   }
176
177   test = 5;
178   if (xbt_dynar_search_or_negative(tests, &test) != -1) {
179     XBT_INFO("Test 5 (turn off dest during a communication : Create a Process/task to make a communication between "
180              "Tremblay and Jupiter and turn off Jupiter during the communication");
181     MSG_host_on(jupiter);
182     MSG_process_sleep(10);
183     argvF    = xbt_new(char*, 2);
184     argvF[0] = xbt_strdup("commRX");
185     MSG_process_create_with_arguments("commRX", commRX, NULL, jupiter, 1, argvF);
186     argvF    = xbt_new(char*, 2);
187     argvF[0] = xbt_strdup("commTX");
188     MSG_process_create_with_arguments("commTX", commTX, NULL, MSG_host_by_name("Tremblay"), 1, argvF);
189     XBT_INFO("  number of processes: %d", MSG_process_get_number());
190     MSG_process_sleep(10);
191     XBT_INFO("  Turn Jupiter off");
192     MSG_host_off(jupiter);
193     XBT_INFO("Test 5 seems ok (number of Process: %d, it should be 2)", MSG_process_get_number());
194   }
195
196   test = 6;
197   if (xbt_dynar_search_or_negative(tests, &test) != -1) {
198     XBT_INFO("Test 6: Turn on Jupiter, assign a VM on Jupiter, launch a process inside the VM, and turn off the node");
199
200     // Create VM0
201     msg_vm_t vm0 = MSG_vm_create_core(jupiter, "vm0");
202     MSG_vm_start(vm0);
203
204     argvF    = xbt_new(char*, 2);
205     argvF[0] = xbt_strdup("process_daemon");
206     msg_process_t daemon =
207         MSG_process_create_with_arguments("process_daemon", process_daemon, NULL, (msg_host_t)vm0, 1, argvF);
208
209     argvF    = xbt_new(char*, 2);
210     argvF[0] = xbt_strdup("process_daemonJUPI");
211     MSG_process_create_with_arguments("process_daemonJUPI", process_daemon, NULL, jupiter, 1, argvF);
212
213     MSG_process_suspend(daemon);
214     MSG_vm_set_bound(vm0, 90);
215     MSG_process_resume(daemon);
216
217     MSG_process_sleep(10);
218
219     XBT_INFO("  Turn Jupiter off");
220     MSG_host_off(jupiter);
221     XBT_INFO("  Shutdown vm0");
222     MSG_vm_shutdown(vm0);
223     XBT_INFO("  Destroy vm0");
224     MSG_vm_destroy(vm0);
225     XBT_INFO("Test 6 is also weird: when the node Jupiter is turned off once again, the VM and its daemon are not "
226              "killed. However, the issue regarding the shutdown of hosted VMs can be seen a feature not a bug ;)");
227   }
228
229   XBT_INFO("  Test done. See you!");
230   return 0;
231 }
232
233 int main(int argc, char* argv[])
234 {
235   msg_error_t res;
236
237   MSG_init(&argc, argv);
238   xbt_assert(argc == 3, "Usage: %s platform_file test_number\n\tExample: %s msg_platform.xml 1\n", argv[0], argv[0]);
239
240   unsigned int iter;
241   char* groups;
242   xbt_dynar_t s_tests = xbt_str_split(argv[2], ",");
243   int tmp_test        = 0;
244   tests               = xbt_dynar_new(sizeof(int), NULL);
245   xbt_dynar_foreach (s_tests, iter, groups) {
246     sscanf(xbt_dynar_get_as(s_tests, iter, char*), "%d", &tmp_test);
247     xbt_dynar_set_as(tests, iter, int, tmp_test);
248   }
249   xbt_dynar_free(&s_tests);
250
251   MSG_create_environment(argv[1]);
252
253   MSG_process_create("test_launcher", test_launcher, NULL, MSG_get_host_by_name("Tremblay"));
254
255   res = MSG_main();
256
257   XBT_INFO("Simulation time %g", MSG_get_clock());
258   xbt_dynar_free(&tests);
259
260   return res != MSG_OK;
261 }