Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
85eab76b4f4a9ce021ff3d33a9513022b6dee0a1
[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 is not allowed)
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 crash as it should. This message will not be displayed.");
112   }
113
114   test = 3;
115   // Create a process running sucessive sleeps on a host and turn the host off during the execution of the process.
116   if (xbt_dynar_search_or_negative(tests, &test) != -1) {
117     xbt_die("Test 3 is superseeded by activity-lifecycle");
118   }
119
120   test = 4;
121   if (xbt_dynar_search_or_negative(tests, &test) != -1) {
122     XBT_INFO("Test 4 (turn off src during a communication) : Create a Process/task to make a communication between "
123              "Jupiter and Tremblay and turn off Jupiter during the communication");
124     MSG_host_on(jupiter);
125     MSG_process_sleep(10);
126     argvF    = xbt_new(char*, 2);
127     argvF[0] = xbt_strdup("commRX");
128     MSG_process_create_with_arguments("commRX", commRX, NULL, MSG_host_by_name("Tremblay"), 1, argvF);
129     argvF    = xbt_new(char*, 2);
130     argvF[0] = xbt_strdup("commTX");
131     MSG_process_create_with_arguments("commTX", commTX, NULL, jupiter, 1, argvF);
132     XBT_INFO("  number of processes: %d", MSG_process_get_number());
133     MSG_process_sleep(10);
134     XBT_INFO("  Turn Jupiter off");
135     MSG_host_off(jupiter);
136     XBT_INFO("Test 4 is ok.  (number of Process : %d, it should be 1 or 2 if RX has not been satisfied)."
137              " An exception is raised when we turn off a node that has a process sleeping",
138              MSG_process_get_number());
139   }
140
141   test = 5;
142   if (xbt_dynar_search_or_negative(tests, &test) != -1) {
143     XBT_INFO("Test 5 (turn off dest during a communication : Create a Process/task to make a communication between "
144              "Tremblay and Jupiter and turn off Jupiter during the communication");
145     MSG_host_on(jupiter);
146     MSG_process_sleep(10);
147     argvF    = xbt_new(char*, 2);
148     argvF[0] = xbt_strdup("commRX");
149     MSG_process_create_with_arguments("commRX", commRX, NULL, jupiter, 1, argvF);
150     argvF    = xbt_new(char*, 2);
151     argvF[0] = xbt_strdup("commTX");
152     MSG_process_create_with_arguments("commTX", commTX, NULL, MSG_host_by_name("Tremblay"), 1, argvF);
153     XBT_INFO("  number of processes: %d", MSG_process_get_number());
154     MSG_process_sleep(10);
155     XBT_INFO("  Turn Jupiter off");
156     MSG_host_off(jupiter);
157     XBT_INFO("Test 5 seems ok (number of Process: %d, it should be 2)", MSG_process_get_number());
158   }
159
160   test = 6;
161   if (xbt_dynar_search_or_negative(tests, &test) != -1) {
162     XBT_INFO("Test 6: Turn on Jupiter, assign a VM on Jupiter, launch a process inside the VM, and turn off the node");
163     MSG_process_set_data_cleanup(nullptr); // If set for test 6, cleanup handler gives double-free errors.
164
165     // Create VM0
166     msg_vm_t vm0 = MSG_vm_create_core(jupiter, "vm0");
167     MSG_vm_start(vm0);
168
169     argvF    = xbt_new(char*, 2);
170     argvF[0] = xbt_strdup("process_daemon");
171     msg_process_t daemon =
172         MSG_process_create_with_arguments("process_daemon", process_daemon, NULL, (msg_host_t)vm0, 1, argvF);
173
174     argvF    = xbt_new(char*, 2);
175     argvF[0] = xbt_strdup("process_daemonJUPI");
176     MSG_process_create_with_arguments("process_daemonJUPI", process_daemon, NULL, jupiter, 1, argvF);
177
178     MSG_process_suspend(daemon);
179     MSG_vm_set_bound(vm0, 90);
180     MSG_process_resume(daemon);
181
182     MSG_process_sleep(10);
183
184     XBT_INFO("  Turn Jupiter off");
185     MSG_host_off(jupiter);
186     XBT_INFO("  Shutdown vm0");
187     MSG_vm_shutdown(vm0);
188     XBT_INFO("  Destroy vm0");
189     MSG_vm_destroy(vm0);
190     XBT_INFO("Test 6 is also weird: when the node Jupiter is turned off once again, the VM and its daemon are not "
191              "killed. However, the issue regarding the shutdown of hosted VMs can be seen a feature not a bug ;)");
192   }
193
194   XBT_INFO("  Test done. See you!");
195   return 0;
196 }
197
198 int main(int argc, char* argv[])
199 {
200   msg_error_t res;
201
202   MSG_init(&argc, argv);
203   xbt_assert(argc == 3, "Usage: %s platform_file test_number\n\tExample: %s msg_platform.xml 1\n", argv[0], argv[0]);
204
205   unsigned int iter;
206   char* groups;
207   xbt_dynar_t s_tests = xbt_str_split(argv[2], ",");
208   int tmp_test        = 0;
209   tests               = xbt_dynar_new(sizeof(int), NULL);
210   xbt_dynar_foreach (s_tests, iter, groups) {
211     sscanf(xbt_dynar_get_as(s_tests, iter, char*), "%d", &tmp_test);
212     xbt_dynar_set_as(tests, iter, int, tmp_test);
213   }
214   xbt_dynar_free(&s_tests);
215
216   MSG_create_environment(argv[1]);
217
218   MSG_process_set_data_cleanup(task_cleanup_handler);
219   MSG_process_create("test_launcher", test_launcher, NULL, MSG_get_host_by_name("Tremblay"));
220
221   res = MSG_main();
222
223   XBT_INFO("Simulation time %g", MSG_get_clock());
224   xbt_dynar_free(&tests);
225
226   return res != MSG_OK;
227 }