Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / teshsuite / msg / host_on_off_processes / host_on_off_processes.c
1 /* Copyright (c) 2010-2014. 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 "msg/msg.h"            /* Yeah! If you want to use msg, you need to include msg/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_get_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_member(tests, &test)){
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     XBT_INFO("  Turn off Jupiter");
42     MSG_host_off(jupiter);
43     MSG_process_sleep(10);
44     XBT_INFO("Test 1 seems ok, cool !(number of Process : %d, it should be 1 (i.e. the Test one))", MSG_process_get_number());
45   }
46
47   test = 2;
48   // Create a process that on a host that is turned off (this should not be possible)
49   if (xbt_dynar_member(tests, &test)){
50     XBT_INFO("Test 2:");
51     XBT_INFO("  Turn off Jupiter");
52     // adsein: Jupiter is already, hence nothing should happen
53     // adsein: This can be one additional test, to check that you cannot shutdown twice a host
54     MSG_host_off(jupiter);
55     argvF = xbt_new(char*, 2);
56     argvF[0] = xbt_strdup("process_daemon");
57     MSG_process_create_with_arguments("process_daemon", process_daemon, NULL, jupiter, 1, argvF);
58     MSG_process_sleep(10);
59     XBT_INFO("  Test 2 does not crash, WTF ?!(number of Process : %d, it should be 1)", MSG_process_get_number());
60     XBT_INFO("  Ok so let's turn on/off the node to see whether the process is correctly bound to Jupiter");
61     MSG_host_on(jupiter);
62     XBT_INFO("  Turn off");
63     MSG_host_off(jupiter);
64     XBT_INFO("  sleep");
65     MSG_process_sleep(10);
66     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());
67   }
68
69    test = 3;
70   // Create a process running sucessive sleeps on a host and turn the host off during the execution of the process.
71   if (xbt_dynar_member(tests, &test)){
72     XBT_INFO("Test 3:");
73     MSG_host_on(jupiter);
74     argvF = xbt_new(char*, 2);
75     argvF[0] = xbt_strdup("process_sleep");
76     MSG_process_create_with_arguments("process_sleep", process_sleep, NULL, jupiter, 1, argvF);
77     MSG_process_sleep(100);
78     XBT_INFO("  Turn off");
79     MSG_host_off(jupiter);
80     XBT_INFO("  sleep for 10 seconds");
81     MSG_process_sleep(10000);
82     XBT_INFO("number of Process : %d it should be 1 (i.e. the Test one))", MSG_process_get_number());
83   }
84
85   test = 4;
86   if (xbt_dynar_member(tests, &test)){
87     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");
88     MSG_host_on(jupiter);
89     MSG_process_sleep(10);
90     argvF = xbt_new(char*, 2);
91     argvF[0] = xbt_strdup("commRX");
92     MSG_process_create_with_arguments("commRX", commRX, NULL, MSG_get_host_by_name("Tremblay"), 1, argvF);
93     argvF = xbt_new(char*, 2);
94     argvF[0] = xbt_strdup("commTX");
95     MSG_process_create_with_arguments("commTX", commTX, NULL, MSG_get_host_by_name("Jupiter"), 1, argvF);
96     XBT_INFO("  number of processes: %d", MSG_process_get_number());
97     MSG_process_sleep(10);
98     XBT_INFO("  Turn Jupiter off");
99     MSG_host_off(jupiter);
100     XBT_INFO("Test 3 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());
101   }
102
103   test = 5;
104   if (xbt_dynar_member(tests, &test)){
105     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");
106     MSG_host_on(jupiter);
107     MSG_process_sleep(10);
108     argvF = xbt_new(char*, 2);
109     argvF[0] = xbt_strdup("commTX");
110     MSG_process_create_with_arguments("commTX", commTX, NULL, MSG_get_host_by_name("Tremblay"), 1, argvF);
111     argvF = xbt_new(char*, 2);
112     argvF[0] = xbt_strdup("commRX");
113     MSG_process_create_with_arguments("commRX", commRX, NULL, MSG_get_host_by_name("Jupiter"), 1, argvF);
114     MSG_host_off(jupiter);
115     XBT_INFO("Test 4 seems ok, cool !(number of Process : %d, it should be 1", MSG_process_get_number());
116   }
117
118   test =6;
119   if (xbt_dynar_member(tests, &test)){
120
121   }
122
123   test = 7;
124   if (xbt_dynar_member(tests, &test)){
125
126   }
127
128   test = 8;
129   if (xbt_dynar_member(tests, &test)){
130
131   }
132
133   test = 9;
134   if (xbt_dynar_member(tests, &test)){
135
136   }
137
138   return 0;
139 }
140
141 // adsein: Is this really a daemon ? it ran only one task ? I just added a stupid loop 
142 int process_daemon(int argc, char *argv[])
143 {
144   msg_task_t task = NULL;
145   for(;;){
146     task = MSG_task_create("deamon", 100*MSG_get_host_speed(MSG_host_self()), 0, NULL);
147     XBT_INFO("  Execute deamon");
148     MSG_task_destroy(task);
149   }
150   MSG_task_execute(task);
151   XBT_INFO("  I'm done. See you!");
152   return 0;
153 }
154
155 int process_sleep(int argc, char *argv[])
156 {
157   for(;;){
158     XBT_INFO("  I'm alive but I should sleep");
159     MSG_process_sleep(10);
160   }
161   XBT_INFO("  I'm done. See you!");
162   return 0;
163 }
164
165 int commTX(int argc, char *argv[])
166 {
167   msg_task_t task = NULL;
168   char mailbox[80];
169   sprintf(mailbox, "comm");
170   XBT_INFO("  Start TX");
171   task = MSG_task_create("COMM", 0, 100000000, NULL);
172   MSG_task_isend(task, mailbox);
173   // We should wait a bit (if not the process will end before the communication, leading to an exception at the other side).
174   MSG_process_sleep(30);
175   XBT_INFO("  TX done");
176   return 0;
177 }
178
179 int commRX(int argc, char *argv[])
180 {
181   msg_task_t task = NULL;
182   char mailbox[80];
183   sprintf(mailbox, "comm");
184   XBT_INFO("  Start RX");
185   msg_error_t error = MSG_task_receive(&(task), mailbox);
186   if (error==MSG_OK) {
187     XBT_INFO("  Receive message: %s", task->name);
188   } else if (error==MSG_HOST_FAILURE) {
189     XBT_INFO("  Receive message: HOST_FAILURE");
190   } else if (error==MSG_TRANSFER_FAILURE) {
191     XBT_INFO("  Receive message: TRANSFERT_FAILURE");
192   } else {
193     XBT_INFO("  Receive message: %d", error);
194   }
195   XBT_INFO("  RX Done");
196   return 0;
197 }
198
199 /** Main function */
200 int main(int argc, char *argv[])
201 {
202   msg_error_t res;
203   const char *platform_file;
204   const char *application_file;
205
206   MSG_init(&argc, argv);
207   if (argc != 4) {
208     printf("Usage: %s platform_file deployment_file test_number\n", argv[0]);
209     printf("example: %s msg_platform.xml msg_deployment.xml 1\n", argv[0]);
210     exit(1);
211   }
212
213   unsigned int iter;
214   char *groups;
215   xbt_dynar_t s_tests = xbt_str_split(argv[3], ",");
216   int tmp_test = 0;
217   tests = xbt_dynar_new(sizeof(int), NULL);
218   xbt_dynar_foreach(s_tests, iter, groups) {
219      sscanf(xbt_dynar_get_as(s_tests, iter, char *), "%d", &tmp_test);
220      xbt_dynar_set_as(tests, iter, int, tmp_test);
221   }
222
223   platform_file = argv[1];
224   application_file = argv[2];
225
226   /* MSG_config("workstation/model","KCCFLN05"); */
227   {                             /*  Simulation setting */
228     MSG_create_environment(platform_file);
229   }
230   {                             /*   Application deployment */
231     MSG_function_register("test_launcher", test_launcher);
232     MSG_function_register("process_daemon", process_daemon);
233     MSG_function_register("process_sleep", process_sleep);
234
235     MSG_launch_application(application_file);
236   }
237   res = MSG_main();
238
239   XBT_INFO("Simulation time %g", MSG_get_clock());
240
241   if (res == MSG_OK)
242     return 0;
243   else
244     return 1;
245 }                               /* end_of_main */