Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
eb6b46359e8c8cbf04282f33fec67c8da3e6bca9
[simgrid.git] / teshsuite / msg / host_on_off_processes / host_on_off_processes.cpp
1 /* Copyright (c) 2010-2019. 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/Exception.hpp"
7 #include "simgrid/msg.h"
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 has 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 commTX(int /*argc*/, char** /*argv*/)
43 {
44   const char* mailbox = "comm";
45   XBT_INFO("  Start TX");
46   msg_task_t task = MSG_task_create("COMM", 0, 100000000, NULL);
47   MSG_task_dsend(task, mailbox, task_cleanup_handler);
48   // We should wait a bit (if not the process will end before the communication, hence an exception on the other side).
49   int res = MSG_process_sleep(30);
50   if (res == MSG_HOST_FAILURE) {
51     XBT_INFO("The host has died ... as expected.");
52   }
53   XBT_INFO("  TX done");
54   return 0;
55 }
56
57 static int commRX(int /*argc*/, char** /*argv*/)
58 {
59   msg_task_t task     = NULL;
60   const char* mailbox = "comm";
61   XBT_INFO("  Start RX");
62   msg_error_t error = MSG_task_receive(&(task), mailbox);
63   if (error == MSG_OK) {
64     XBT_INFO("  Receive message: %s", MSG_task_get_name(task));
65     MSG_task_destroy(task);
66   } else if (error == MSG_HOST_FAILURE) {
67     XBT_INFO("  Receive message: HOST_FAILURE");
68   } else if (error == MSG_TRANSFER_FAILURE) {
69     XBT_INFO("  Receive message: TRANSFER_FAILURE");
70   } else {
71     XBT_INFO("  Receive message: %u", static_cast<unsigned int>(error));
72   }
73   XBT_INFO("  RX Done");
74   return 0;
75 }
76
77 static int test_launcher(int /*argc*/, char** /*argv*/)
78 {
79   int test = 0;
80   char** argvF;
81   msg_host_t jupiter = MSG_host_by_name("Jupiter");
82
83   test = 1;
84   // Create a process running a simple task on a host and turn the host off during the execution of the process.
85   if (xbt_dynar_search_or_negative(tests, &test) != -1) {
86     XBT_INFO("Test 1:");
87     XBT_INFO("  Create a process on Jupiter");
88     argvF    = xbt_new(char*, 2);
89     argvF[0] = xbt_strdup("process_daemon");
90     MSG_process_create_with_arguments("process_daemon", process_daemon, NULL, jupiter, 1, argvF);
91     MSG_process_sleep(3);
92     XBT_INFO("  Turn off Jupiter");
93     MSG_host_off(jupiter);
94     MSG_process_sleep(10);
95     XBT_INFO("Test 1 seems ok, cool !(#Processes: %d, it should be 1; #tasks: %d)", MSG_process_get_number(),
96              tasks_done);
97   }
98
99   test = 2;
100   // Create a process that on a host that is turned off (this should not be possible)
101   if (xbt_dynar_search_or_negative(tests, &test) != -1) {
102     XBT_INFO("Test 2:");
103     XBT_INFO("  Turn off Jupiter");
104     // adsein: Jupiter is already, hence nothing should happen
105     // adsein: This can be one additional test, to check that you cannot shutdown twice a host
106     MSG_host_off(jupiter);
107     argvF    = xbt_new(char*, 2);
108     argvF[0] = xbt_strdup("process_daemon");
109     MSG_process_create_with_arguments("process_daemon", process_daemon, NULL, jupiter, 1, argvF);
110     MSG_process_sleep(10);
111     XBT_INFO("  Test 2 does not crash as it should (number of Process : %d, it should be 1)", MSG_process_get_number());
112     XBT_INFO("  Ok so let's turn on/off the node to see whether the process is correctly bound to Jupiter");
113     MSG_host_on(jupiter);
114     XBT_INFO("  Turn off");
115     MSG_host_off(jupiter);
116     XBT_INFO("  sleep");
117     MSG_process_sleep(10);
118     XBT_INFO("number of Process : %d it should be 1. The daemon that has been created for test2 has been correctly "
119              "destroyed....ok at least it looks rigorous, cool ! You just have to disallow the possibility to create "
120              "a new process on a node when the node is off.)",
121              MSG_process_get_number());
122   }
123
124   test = 3;
125   // Create a process running sucessive sleeps on a host and turn the host off during the execution of the process.
126   if (xbt_dynar_search_or_negative(tests, &test) != -1) {
127     xbt_die("Test 3 is superseeded by activity-lifecycle");
128   }
129
130   test = 4;
131   if (xbt_dynar_search_or_negative(tests, &test) != -1) {
132     XBT_INFO("Test 4 (turn off src during a communication) : Create a Process/task to make a communication between "
133              "Jupiter and Tremblay and turn off Jupiter during the communication");
134     MSG_host_on(jupiter);
135     MSG_process_sleep(10);
136     argvF    = xbt_new(char*, 2);
137     argvF[0] = xbt_strdup("commRX");
138     MSG_process_create_with_arguments("commRX", commRX, NULL, MSG_host_by_name("Tremblay"), 1, argvF);
139     argvF    = xbt_new(char*, 2);
140     argvF[0] = xbt_strdup("commTX");
141     MSG_process_create_with_arguments("commTX", commTX, NULL, jupiter, 1, argvF);
142     XBT_INFO("  number of processes: %d", MSG_process_get_number());
143     MSG_process_sleep(10);
144     XBT_INFO("  Turn Jupiter off");
145     MSG_host_off(jupiter);
146     XBT_INFO("Test 4 is ok.  (number of Process : %d, it should be 1 or 2 if RX has not been satisfied)."
147              " An exception is raised when we turn off a node that has a process sleeping",
148              MSG_process_get_number());
149   }
150
151   test = 5;
152   if (xbt_dynar_search_or_negative(tests, &test) != -1) {
153     XBT_INFO("Test 5 (turn off dest during a communication : Create a Process/task to make a communication between "
154              "Tremblay and Jupiter and turn off Jupiter during the communication");
155     MSG_host_on(jupiter);
156     MSG_process_sleep(10);
157     argvF    = xbt_new(char*, 2);
158     argvF[0] = xbt_strdup("commRX");
159     MSG_process_create_with_arguments("commRX", commRX, NULL, jupiter, 1, argvF);
160     argvF    = xbt_new(char*, 2);
161     argvF[0] = xbt_strdup("commTX");
162     MSG_process_create_with_arguments("commTX", commTX, NULL, MSG_host_by_name("Tremblay"), 1, argvF);
163     XBT_INFO("  number of processes: %d", MSG_process_get_number());
164     MSG_process_sleep(10);
165     XBT_INFO("  Turn Jupiter off");
166     MSG_host_off(jupiter);
167     XBT_INFO("Test 5 seems ok (number of Process: %d, it should be 2)", MSG_process_get_number());
168   }
169
170   test = 6;
171   if (xbt_dynar_search_or_negative(tests, &test) != -1) {
172     XBT_INFO("Test 6: Turn on Jupiter, assign a VM on Jupiter, launch a process inside the VM, and turn off the node");
173     MSG_process_set_data_cleanup(nullptr); // If set for test 6, cleanup handler gives double-free errors.
174
175     // Create VM0
176     msg_vm_t vm0 = MSG_vm_create_core(jupiter, "vm0");
177     MSG_vm_start(vm0);
178
179     argvF    = xbt_new(char*, 2);
180     argvF[0] = xbt_strdup("process_daemon");
181     msg_process_t daemon =
182         MSG_process_create_with_arguments("process_daemon", process_daemon, NULL, (msg_host_t)vm0, 1, argvF);
183
184     argvF    = xbt_new(char*, 2);
185     argvF[0] = xbt_strdup("process_daemonJUPI");
186     MSG_process_create_with_arguments("process_daemonJUPI", process_daemon, NULL, jupiter, 1, argvF);
187
188     MSG_process_suspend(daemon);
189     MSG_vm_set_bound(vm0, 90);
190     MSG_process_resume(daemon);
191
192     MSG_process_sleep(10);
193
194     XBT_INFO("  Turn Jupiter off");
195     MSG_host_off(jupiter);
196     XBT_INFO("  Shutdown vm0");
197     MSG_vm_shutdown(vm0);
198     XBT_INFO("  Destroy vm0");
199     MSG_vm_destroy(vm0);
200     XBT_INFO("Test 6 is also weird: when the node Jupiter is turned off once again, the VM and its daemon are not "
201              "killed. However, the issue regarding the shutdown of hosted VMs can be seen a feature not a bug ;)");
202   }
203
204   XBT_INFO("  Test done. See you!");
205   return 0;
206 }
207
208 int main(int argc, char* argv[])
209 {
210   msg_error_t res;
211
212   MSG_init(&argc, argv);
213   xbt_assert(argc == 3, "Usage: %s platform_file test_number\n\tExample: %s msg_platform.xml 1\n", argv[0], argv[0]);
214
215   unsigned int iter;
216   char* groups;
217   xbt_dynar_t s_tests = xbt_str_split(argv[2], ",");
218   int tmp_test        = 0;
219   tests               = xbt_dynar_new(sizeof(int), NULL);
220   xbt_dynar_foreach (s_tests, iter, groups) {
221     sscanf(xbt_dynar_get_as(s_tests, iter, char*), "%d", &tmp_test);
222     xbt_dynar_set_as(tests, iter, int, tmp_test);
223   }
224   xbt_dynar_free(&s_tests);
225
226   MSG_create_environment(argv[1]);
227
228   MSG_process_set_data_cleanup(task_cleanup_handler);
229   MSG_process_create("test_launcher", test_launcher, NULL, MSG_get_host_by_name("Tremblay"));
230
231   res = MSG_main();
232
233   XBT_INFO("Simulation time %g", MSG_get_clock());
234   xbt_dynar_free(&tests);
235
236   return res != MSG_OK;
237 }