Logo AND Algorithmique Numérique Distribuée

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