Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0b19a419b441f20ce803f3d5fa04ea70753cb41a
[simgrid.git] / teshsuite / msg / host_on_off_processes / host_on_off_processes.c
1 /* Copyright (c) 2010-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <stdio.h>
8 #include "simgrid/msg.h"            /* Yeah! If you want to use msg, you need to include simgrid/msg.h */
9 #include "xbt/sysdep.h"         /* calloc, printf */
10
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15                              "Messages specific for this msg example");
16
17 int test_launcher(int argc, char *argv[]);
18 int process_daemon(int argc, char *argv[]);
19 int process_sleep(int argc, char *argv[]);
20 int commRX(int argc, char *argv[]);
21 int commTX(int argc, char *argv[]);
22
23 xbt_dynar_t tests;
24 int tasks_done = 0;
25
26 int test_launcher(int argc, char *argv[])
27 {
28   int test = 0;
29   char **argvF;
30   argvF = xbt_new(char*, 2);
31   argvF[0] = xbt_strdup("process_daemon");
32   msg_host_t jupiter = MSG_host_by_name("Jupiter");
33
34   test = 1;
35   // Create a process running a simple task on a host and turn the host off during the execution of the process.
36   if (xbt_dynar_search_or_negative(tests, &test)!=-1){
37     XBT_INFO("Test 1:");
38     XBT_INFO("  Create a process on Jupiter");
39     argvF = xbt_new(char*, 2);
40     argvF[0] = xbt_strdup("process_daemon");
41     MSG_process_create_with_arguments("process_daemon", process_daemon, NULL, jupiter, 1, argvF);
42     MSG_process_sleep(3);
43     XBT_INFO("  Turn off Jupiter");
44     MSG_host_off(jupiter);
45     MSG_process_sleep(10);
46     XBT_INFO("Test 1 seems ok, cool !(#Processes: %d, it should be 1; #tasks: %d)", MSG_process_get_number(), tasks_done);
47   }
48
49   test = 2;
50   // Create a process that on a host that is turned off (this should not be possible)
51   if (xbt_dynar_search_or_negative(tests, &test)!=-1){
52     XBT_INFO("Test 2:");
53     XBT_INFO("  Turn off Jupiter");
54     // adsein: Jupiter is already, hence nothing should happen
55     // adsein: This can be one additional test, to check that you cannot shutdown twice a host
56     MSG_host_off(jupiter);
57     argvF = xbt_new(char*, 2);
58     argvF[0] = xbt_strdup("process_daemon");
59     MSG_process_create_with_arguments("process_daemon", process_daemon, NULL, jupiter, 1, argvF);
60     MSG_process_sleep(10);
61     XBT_INFO("  Test 2 does not crash, WTF ?!(number of Process : %d, it should be 1)", MSG_process_get_number());
62     XBT_INFO("  Ok so let's turn on/off the node to see whether the process is correctly bound to Jupiter");
63     MSG_host_on(jupiter);
64     XBT_INFO("  Turn off");
65     MSG_host_off(jupiter);
66     XBT_INFO("  sleep");
67     MSG_process_sleep(10);
68     XBT_INFO("number of Process : %d it should be 1. The daemon that has been created for test2 has been correctly destroyed....ok at least it looks rigorous, cool ! You just have to disallow the possibility to create a new process on a node when the node is off.)", MSG_process_get_number());
69   }
70
71    test = 3;
72   // Create a process running sucessive sleeps on a host and turn the host off during the execution of the process.
73   if (xbt_dynar_search_or_negative(tests, &test)!=-1){
74     XBT_INFO("Test 3:");
75     MSG_host_on(jupiter);
76     argvF = xbt_new(char*, 2);
77     argvF[0] = xbt_strdup("process_sleep");
78     MSG_process_create_with_arguments("process_sleep", process_sleep, NULL, jupiter, 1, argvF);
79     MSG_process_sleep(100);
80     XBT_INFO("  Turn off");
81     MSG_host_off(jupiter);
82     XBT_INFO("  sleep for 10 seconds");
83     MSG_process_sleep(10000);
84     XBT_INFO("number of Process : %d it should be 1 (i.e. the Test one))", MSG_process_get_number());
85   }
86
87   test = 4;
88   if (xbt_dynar_search_or_negative(tests, &test)!=-1){
89     XBT_INFO("Test 4 (turn off src during a communication) : Create a Process/task to make a communication between Jupiter and Tremblay and turn off Jupiter during the communication");
90     MSG_host_on(jupiter);
91     MSG_process_sleep(10);
92     argvF = xbt_new(char*, 2);
93     argvF[0] = xbt_strdup("commRX");
94     MSG_process_create_with_arguments("commRX", commRX, NULL, MSG_host_by_name("Tremblay"), 1, argvF);
95     argvF = xbt_new(char*, 2);
96     argvF[0] = xbt_strdup("commTX");
97     MSG_process_create_with_arguments("commTX", commTX, NULL, jupiter, 1, argvF);
98     XBT_INFO("  number of processes: %d", MSG_process_get_number());
99     MSG_process_sleep(10);
100     XBT_INFO("  Turn Jupiter off");
101     MSG_host_off(jupiter);
102     XBT_INFO("Test 4 seems ok  (number of Process : %d, it should be 1 or 2 if RX has not been satisfied) cool, you can now turn off a node that has a process paused by a sleep call", MSG_process_get_number());
103   }
104
105   test = 5;
106   if (xbt_dynar_search_or_negative(tests, &test)!=-1){
107     XBT_INFO("Test 5 (turn off dest during a communication : Create a Process/task to make a communication between Tremblay and Jupiter and turn off Jupiter during the communication");
108     MSG_host_on(jupiter);
109     MSG_process_sleep(10);
110     argvF = xbt_new(char*, 2);
111     argvF[0] = xbt_strdup("commRX");
112     MSG_process_create_with_arguments("commRX", commRX, NULL, jupiter, 1, argvF);
113     argvF = xbt_new(char*, 2);
114     argvF[0] = xbt_strdup("commTX");
115     MSG_process_create_with_arguments("commTX", commTX, NULL, MSG_host_by_name("Tremblay"), 1, argvF);
116     XBT_INFO("  number of processes: %d", MSG_process_get_number());
117     MSG_process_sleep(10);
118     XBT_INFO("  Turn Jupiter off");
119     MSG_host_off(jupiter);
120     XBT_INFO("Test 5 seems ok, cool !(number of Process : %d, it should be 2", MSG_process_get_number());
121   }
122
123   test =6;
124   if (xbt_dynar_search_or_negative(tests, &test)!=-1){
125     XBT_INFO("Test 6: Turn on Jupiter, assign a VM on Jupiter, launch a process inside the VM, and turn off the node");
126
127     // Create VM0
128     int dpRate = 70;
129     msg_vm_t vm0;
130     msg_process_t daemon;
131
132     vm0 = MSG_vm_create (jupiter, "vm0", 1, 2048, 125, NULL, -1, 125, dpRate);
133     MSG_vm_start(vm0);
134
135     argvF = xbt_new(char*, 2);
136     argvF[0] = xbt_strdup("process_daemon");
137     daemon = MSG_process_create_with_arguments("process_daemon", process_daemon, NULL, vm0, 1, argvF);
138
139     argvF = xbt_new(char*, 2);
140     argvF[0] = xbt_strdup("process_daemonJUPI");
141     MSG_process_create_with_arguments("process_daemonJUPI", process_daemon, NULL, jupiter, 1, argvF);
142
143     MSG_process_suspend(daemon);
144     MSG_vm_set_bound(vm0, 90);
145     MSG_process_resume(daemon);
146
147     MSG_process_sleep(10);
148
149     XBT_INFO("  Turn Jupiter off");
150     MSG_host_off(jupiter);
151     XBT_INFO("  Shutdown vm0");
152     MSG_vm_shutdown(vm0);
153     XBT_INFO("  Destroy vm0");
154     MSG_vm_destroy(vm0);
155     XBT_INFO("Test 6 is also weird: when the node Jupiter is turned off once again, the VM and its daemon are not killed. However, the issue regarding the shutdown of hosted VMs can be seen a feature not a bug ;)");
156   }
157
158   test = 7;
159   if (xbt_dynar_search_or_negative(tests, &test)!=-1){
160
161   }
162
163   test = 8;
164   if (xbt_dynar_search_or_negative(tests, &test)!=-1){
165
166   }
167
168   test = 9;
169   if (xbt_dynar_search_or_negative(tests, &test)!=-1){
170
171   }
172   XBT_INFO("  Test done. See you!");
173   return 0;
174 }
175
176 int process_daemon(int argc, char *argv[])
177 {
178   msg_task_t task = NULL;
179   XBT_INFO("  Start daemon on %s (%f)",  MSG_host_get_name(MSG_host_self()), MSG_get_host_speed(MSG_host_self()));
180   for(;;){
181     task = MSG_task_create("daemon", MSG_get_host_speed(MSG_host_self()), 0, NULL);
182     XBT_INFO("  Execute daemon");
183     MSG_task_execute(task);
184     MSG_task_destroy(task);
185     tasks_done ++;
186   }
187   XBT_INFO("  daemon done. See you!");
188   return 0;
189 }
190
191 int process_sleep(int argc, char *argv[])
192 {
193   for(;;){
194     XBT_INFO("  I'm alive but I should sleep");
195     MSG_process_sleep(10);
196   }
197   XBT_INFO("  I'm done. See you!");
198   return 0;
199 }
200
201 int commTX(int argc, char *argv[])
202 {
203   msg_task_t task = NULL;
204   char mailbox[80];
205   sprintf(mailbox, "comm");
206   XBT_INFO("  Start TX");
207   task = MSG_task_create("COMM", 0, 100000000, NULL);
208   MSG_task_isend(task, mailbox);
209   // We should wait a bit (if not the process will end before the communication, leading to an exception at the other side).
210   MSG_process_sleep(30);
211   XBT_INFO("  TX done");
212   return 0;
213 }
214
215 int commRX(int argc, char *argv[])
216 {
217   msg_task_t task = NULL;
218   char mailbox[80];
219   sprintf(mailbox, "comm");
220   XBT_INFO("  Start RX");
221   msg_error_t error = MSG_task_receive(&(task), mailbox);
222   if (error==MSG_OK) {
223     XBT_INFO("  Receive message: %s", task->name);
224   } else if (error==MSG_HOST_FAILURE) {
225     XBT_INFO("  Receive message: HOST_FAILURE");
226   } else if (error==MSG_TRANSFER_FAILURE) {
227     XBT_INFO("  Receive message: TRANSFERT_FAILURE");
228   } else {
229     XBT_INFO("  Receive message: %d", error);
230   }
231   XBT_INFO("  RX Done");
232   return 0;
233 }
234
235 /** Main function */
236 int main(int argc, char *argv[])
237 {
238   msg_error_t res;
239   const char *platform_file;
240   const char *application_file;
241
242   MSG_init(&argc, argv);
243   if (argc != 4) {
244     printf("Usage: %s platform_file deployment_file test_number\n", argv[0]);
245     printf("example: %s msg_platform.xml msg_deployment.xml 1\n", argv[0]);
246     exit(1);
247   }
248
249   unsigned int iter;
250   char *groups;
251   xbt_dynar_t s_tests = xbt_str_split(argv[3], ",");
252   int tmp_test = 0;
253   tests = xbt_dynar_new(sizeof(int), NULL);
254   xbt_dynar_foreach(s_tests, iter, groups) {
255      sscanf(xbt_dynar_get_as(s_tests, iter, char *), "%d", &tmp_test);
256      xbt_dynar_set_as(tests, iter, int, tmp_test);
257   }
258   xbt_dynar_free(&s_tests);
259
260   platform_file = argv[1];
261   application_file = argv[2];
262
263   {                             /*  Simulation setting */
264     MSG_create_environment(platform_file);
265   }
266   {                             /*   Application deployment */
267     MSG_function_register("test_launcher", test_launcher);
268     MSG_function_register("process_daemon", process_daemon);
269     MSG_function_register("process_sleep", process_sleep);
270
271     MSG_launch_application(application_file);
272   }
273   res = MSG_main();
274
275   XBT_INFO("Simulation time %g", MSG_get_clock());
276
277   if (res == MSG_OK)
278     return 0;
279   else
280     return 1;
281 }                               /* end_of_main */