Logo AND Algorithmique Numérique Distribuée

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