Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
typo (s/deamon/daemon/)
[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
25 int test_launcher(int argc, char *argv[])
26 {
27   int test = 0;
28   char **argvF;
29   argvF = xbt_new(char*, 2);
30   argvF[0] = xbt_strdup("process_daemon");
31   msg_host_t jupiter = MSG_host_by_name("Jupiter");
32
33   test = 1;
34   // Create a process running a simple task on a host and turn the host off during the execution of the process.
35   if (xbt_dynar_search_or_negative(tests, &test)!=-1){
36     XBT_INFO("Test 1:");
37     XBT_INFO("  Create a process on Jupiter");
38     argvF = xbt_new(char*, 2);
39     argvF[0] = xbt_strdup("process_daemon");
40     MSG_process_create_with_arguments("process_daemon", process_daemon, NULL, jupiter, 1, argvF);
41     MSG_process_sleep(3);
42     XBT_INFO("  Turn off Jupiter");
43     MSG_host_off(jupiter);
44     MSG_process_sleep(10);
45     XBT_INFO("Test 1 seems ok, cool !(number of Process : %d, it should be 1 (i.e. the Test one))", MSG_process_get_number());
46   }
47
48   test = 2;
49   // Create a process that on a host that is turned off (this should not be possible)
50   if (xbt_dynar_search_or_negative(tests, &test)!=-1){
51     XBT_INFO("Test 2:");
52     XBT_INFO("  Turn off Jupiter");
53     // adsein: Jupiter is already, hence nothing should happen
54     // adsein: This can be one additional test, to check that you cannot shutdown twice a host
55     MSG_host_off(jupiter);
56     argvF = xbt_new(char*, 2);
57     argvF[0] = xbt_strdup("process_daemon");
58     MSG_process_create_with_arguments("process_daemon", process_daemon, NULL, jupiter, 1, argvF);
59     MSG_process_sleep(10);
60     XBT_INFO("  Test 2 does not crash, WTF ?!(number of Process : %d, it should be 1)", MSG_process_get_number());
61     XBT_INFO("  Ok so let's turn on/off the node to see whether the process is correctly bound to Jupiter");
62     MSG_host_on(jupiter);
63     XBT_INFO("  Turn off");
64     MSG_host_off(jupiter);
65     XBT_INFO("  sleep");
66     MSG_process_sleep(10);
67     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());
68   }
69
70    test = 3;
71   // Create a process running sucessive sleeps on a host and turn the host off during the execution of the process.
72   if (xbt_dynar_search_or_negative(tests, &test)!=-1){
73     XBT_INFO("Test 3:");
74     MSG_host_on(jupiter);
75     argvF = xbt_new(char*, 2);
76     argvF[0] = xbt_strdup("process_sleep");
77     MSG_process_create_with_arguments("process_sleep", process_sleep, NULL, jupiter, 1, argvF);
78     MSG_process_sleep(100);
79     XBT_INFO("  Turn off");
80     MSG_host_off(jupiter);
81     XBT_INFO("  sleep for 10 seconds");
82     MSG_process_sleep(10000);
83     XBT_INFO("number of Process : %d it should be 1 (i.e. the Test one))", MSG_process_get_number());
84   }
85
86   test = 4;
87   if (xbt_dynar_search_or_negative(tests, &test)!=-1){
88     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");
89     MSG_host_on(jupiter);
90     MSG_process_sleep(10);
91     argvF = xbt_new(char*, 2);
92     argvF[0] = xbt_strdup("commRX");
93     MSG_process_create_with_arguments("commRX", commRX, NULL, MSG_host_by_name("Tremblay"), 1, argvF);
94     argvF = xbt_new(char*, 2);
95     argvF[0] = xbt_strdup("commTX");
96     MSG_process_create_with_arguments("commTX", commTX, NULL, jupiter, 1, argvF);
97     XBT_INFO("  number of processes: %d", MSG_process_get_number());
98     MSG_process_sleep(10);
99     XBT_INFO("  Turn Jupiter off");
100     MSG_host_off(jupiter);
101     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());
102   }
103
104   test = 5;
105   if (xbt_dynar_search_or_negative(tests, &test)!=-1){
106     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");
107     MSG_host_on(jupiter);
108     MSG_process_sleep(10);
109     argvF = xbt_new(char*, 2);
110     argvF[0] = xbt_strdup("commRX");
111     MSG_process_create_with_arguments("commRX", commRX, NULL, jupiter, 1, argvF);
112     argvF = xbt_new(char*, 2);
113     argvF[0] = xbt_strdup("commTX");
114     MSG_process_create_with_arguments("commTX", commTX, NULL, MSG_host_by_name("Tremblay"), 1, argvF);
115     XBT_INFO("  number of processes: %d", MSG_process_get_number());
116     MSG_process_sleep(10);
117     XBT_INFO("  Turn Jupiter off");
118     MSG_host_off(jupiter);
119     XBT_INFO("Test 5 seems ok, cool !(number of Process : %d, it should be 2", MSG_process_get_number());
120   }
121
122   test =6;
123   if (xbt_dynar_search_or_negative(tests, &test)!=-1){
124     XBT_INFO("Test 6: Turn on Jupiter, assign a VM on Jupiter, launch a process inside the VM, and turn off the node");
125
126     // Create VM0
127     int dpRate = 70;
128     msg_vm_t vm0;
129     msg_process_t daemon;
130
131     vm0 = MSG_vm_create (jupiter, "vm0", 1, 2048, 125, NULL, -1, 125, dpRate);
132     MSG_vm_start(vm0);
133
134     argvF = xbt_new(char*, 2);
135     argvF[0] = xbt_strdup("process_daemon");
136     daemon = MSG_process_create_with_arguments("process_daemon", process_daemon, NULL, vm0, 1, argvF);
137
138     argvF = xbt_new(char*, 2);
139     argvF[0] = xbt_strdup("process_daemonJUPI");
140     MSG_process_create_with_arguments("process_daemonJUPI", process_daemon, NULL, jupiter, 1, argvF);
141
142     MSG_process_suspend(daemon);
143     MSG_vm_set_bound(vm0, 90);
144     MSG_process_resume(daemon);
145
146     MSG_process_sleep(10);
147
148     XBT_INFO("  Turn Jupiter off");
149     MSG_host_off(jupiter);
150     XBT_INFO("  Shutdown vm0");
151     MSG_vm_shutdown(vm0);
152     XBT_INFO("  Destroy vm0");
153     MSG_vm_destroy(vm0);
154     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 ;)");
155   }
156
157   test = 7;
158   if (xbt_dynar_search_or_negative(tests, &test)!=-1){
159
160   }
161
162   test = 8;
163   if (xbt_dynar_search_or_negative(tests, &test)!=-1){
164
165   }
166
167   test = 9;
168   if (xbt_dynar_search_or_negative(tests, &test)!=-1){
169
170   }
171   XBT_INFO("  Test done. See you!");
172   return 0;
173 }
174
175 // adsein: Is this really a daemon ? it ran only one task ? I just added a stupid loop
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   }
186   XBT_INFO("  daemon done. See you!");
187   return 0;
188 }
189
190 int process_sleep(int argc, char *argv[])
191 {
192   for(;;){
193     XBT_INFO("  I'm alive but I should sleep");
194     MSG_process_sleep(10);
195   }
196   XBT_INFO("  I'm done. See you!");
197   return 0;
198 }
199
200 int commTX(int argc, char *argv[])
201 {
202   msg_task_t task = NULL;
203   char mailbox[80];
204   sprintf(mailbox, "comm");
205   XBT_INFO("  Start TX");
206   task = MSG_task_create("COMM", 0, 100000000, NULL);
207   MSG_task_isend(task, mailbox);
208   // We should wait a bit (if not the process will end before the communication, leading to an exception at the other side).
209   MSG_process_sleep(30);
210   XBT_INFO("  TX done");
211   return 0;
212 }
213
214 int commRX(int argc, char *argv[])
215 {
216   msg_task_t task = NULL;
217   char mailbox[80];
218   sprintf(mailbox, "comm");
219   XBT_INFO("  Start RX");
220   msg_error_t error = MSG_task_receive(&(task), mailbox);
221   if (error==MSG_OK) {
222     XBT_INFO("  Receive message: %s", task->name);
223   } else if (error==MSG_HOST_FAILURE) {
224     XBT_INFO("  Receive message: HOST_FAILURE");
225   } else if (error==MSG_TRANSFER_FAILURE) {
226     XBT_INFO("  Receive message: TRANSFERT_FAILURE");
227   } else {
228     XBT_INFO("  Receive message: %d", error);
229   }
230   XBT_INFO("  RX Done");
231   return 0;
232 }
233
234 /** Main function */
235 int main(int argc, char *argv[])
236 {
237   msg_error_t res;
238   const char *platform_file;
239   const char *application_file;
240
241   MSG_init(&argc, argv);
242   if (argc != 4) {
243     printf("Usage: %s platform_file deployment_file test_number\n", argv[0]);
244     printf("example: %s msg_platform.xml msg_deployment.xml 1\n", argv[0]);
245     exit(1);
246   }
247
248   unsigned int iter;
249   char *groups;
250   xbt_dynar_t s_tests = xbt_str_split(argv[3], ",");
251   int tmp_test = 0;
252   tests = xbt_dynar_new(sizeof(int), NULL);
253   xbt_dynar_foreach(s_tests, iter, groups) {
254      sscanf(xbt_dynar_get_as(s_tests, iter, char *), "%d", &tmp_test);
255      xbt_dynar_set_as(tests, iter, int, tmp_test);
256   }
257   xbt_dynar_free(&s_tests);
258
259   platform_file = argv[1];
260   application_file = argv[2];
261
262   {                             /*  Simulation setting */
263     MSG_create_environment(platform_file);
264   }
265   {                             /*   Application deployment */
266     MSG_function_register("test_launcher", test_launcher);
267     MSG_function_register("process_daemon", process_daemon);
268     MSG_function_register("process_sleep", process_sleep);
269
270     MSG_launch_application(application_file);
271   }
272   res = MSG_main();
273
274   XBT_INFO("Simulation time %g", MSG_get_clock());
275
276   if (res == MSG_OK)
277     return 0;
278   else
279     return 1;
280 }                               /* end_of_main */