Logo AND Algorithmique Numérique Distribuée

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