Logo AND Algorithmique Numérique Distribuée

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