Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
12975d2b3a2d10c05b6a0e17bda13c4802f698d8
[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, 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 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());
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("commRX");
110     MSG_process_create_with_arguments("commRX", commRX, NULL, jupiter, 1, argvF);
111     argvF = xbt_new(char*, 2);
112     argvF[0] = xbt_strdup("commTX");
113     MSG_process_create_with_arguments("commTX", commTX, NULL, MSG_get_host_by_name("Tremblay"), 1, argvF);
114     XBT_INFO("  number of processes: %d", MSG_process_get_number());
115     MSG_process_sleep(10);
116     XBT_INFO("  Turn Jupiter off");
117     MSG_host_off(jupiter);
118     XBT_INFO("Test 4 seems ok, cool !(number of Process : %d, it should be 2", MSG_process_get_number());
119   }
120
121   test =6;
122   if (xbt_dynar_member(tests, &test)){
123
124   }
125
126   test = 7;
127   if (xbt_dynar_member(tests, &test)){
128
129   }
130
131   test = 8;
132   if (xbt_dynar_member(tests, &test)){
133
134   }
135
136   test = 9;
137   if (xbt_dynar_member(tests, &test)){
138
139   }
140   XBT_INFO("  Test done. See you!");
141   return 0;
142 }
143
144 // adsein: Is this really a daemon ? it ran only one task ? I just added a stupid loop 
145 int process_daemon(int argc, char *argv[])
146 {
147   msg_task_t task = NULL;
148   for(;;){
149     task = MSG_task_create("deamon", 100*MSG_get_host_speed(MSG_host_self()), 0, NULL);
150     XBT_INFO("  Execute deamon");
151     MSG_task_destroy(task);
152   }
153   MSG_task_execute(task);
154   XBT_INFO("  Deamon done. See you!");
155   return 0;
156 }
157
158 int process_sleep(int argc, char *argv[])
159 {
160   for(;;){
161     XBT_INFO("  I'm alive but I should sleep");
162     MSG_process_sleep(10);
163   }
164   XBT_INFO("  I'm done. See you!");
165   return 0;
166 }
167
168 int commTX(int argc, char *argv[])
169 {
170   msg_task_t task = NULL;
171   char mailbox[80];
172   sprintf(mailbox, "comm");
173   XBT_INFO("  Start TX");
174   task = MSG_task_create("COMM", 0, 100000000, NULL);
175   MSG_task_isend(task, mailbox);
176   // We should wait a bit (if not the process will end before the communication, leading to an exception at the other side).
177   MSG_process_sleep(30);
178   XBT_INFO("  TX done");
179   return 0;
180 }
181
182 int commRX(int argc, char *argv[])
183 {
184   msg_task_t task = NULL;
185   char mailbox[80];
186   sprintf(mailbox, "comm");
187   XBT_INFO("  Start RX");
188   msg_error_t error = MSG_task_receive(&(task), mailbox);
189   if (error==MSG_OK) {
190     XBT_INFO("  Receive message: %s", task->name);
191   } else if (error==MSG_HOST_FAILURE) {
192     XBT_INFO("  Receive message: HOST_FAILURE");
193   } else if (error==MSG_TRANSFER_FAILURE) {
194     XBT_INFO("  Receive message: TRANSFERT_FAILURE");
195   } else {
196     XBT_INFO("  Receive message: %d", error);
197   }
198   XBT_INFO("  RX Done");
199   return 0;
200 }
201
202 /** Main function */
203 int main(int argc, char *argv[])
204 {
205   msg_error_t res;
206   const char *platform_file;
207   const char *application_file;
208
209   MSG_init(&argc, argv);
210   if (argc != 4) {
211     printf("Usage: %s platform_file deployment_file test_number\n", argv[0]);
212     printf("example: %s msg_platform.xml msg_deployment.xml 1\n", argv[0]);
213     exit(1);
214   }
215
216   unsigned int iter;
217   char *groups;
218   xbt_dynar_t s_tests = xbt_str_split(argv[3], ",");
219   int tmp_test = 0;
220   tests = xbt_dynar_new(sizeof(int), NULL);
221   xbt_dynar_foreach(s_tests, iter, groups) {
222      sscanf(xbt_dynar_get_as(s_tests, iter, char *), "%d", &tmp_test);
223      xbt_dynar_set_as(tests, iter, int, tmp_test);
224   }
225
226   platform_file = argv[1];
227   application_file = argv[2];
228
229   /* MSG_config("workstation/model","KCCFLN05"); */
230   {                             /*  Simulation setting */
231     MSG_create_environment(platform_file);
232   }
233   {                             /*   Application deployment */
234     MSG_function_register("test_launcher", test_launcher);
235     MSG_function_register("process_daemon", process_daemon);
236     MSG_function_register("process_sleep", process_sleep);
237
238     MSG_launch_application(application_file);
239   }
240   res = MSG_main();
241
242   XBT_INFO("Simulation time %g", MSG_get_clock());
243
244   if (res == MSG_OK)
245     return 0;
246   else
247     return 1;
248 }                               /* end_of_main */