Logo AND Algorithmique Numérique Distribuée

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