Logo AND Algorithmique Numérique Distribuée

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