Logo AND Algorithmique Numérique Distribuée

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