Logo AND Algorithmique Numérique Distribuée

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