Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8f9462a9ab58282005e364ead75f52c1267401fc
[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(int argc, char *argv[]);
18 int test_daemon(int argc, char *argv[]);
19 int commRX(int argc, char *argv[]);
20 int commTX(int argc, char *argv[]);
21
22 xbt_dynar_t tests;
23
24 int test(int argc, char *argv[])
25 {
26   int test = 0;
27   char **argvF;
28   argvF = xbt_new(char*, 2);
29   argvF[0] = xbt_strdup("test_daemon");
30   msg_host_t jupiter = MSG_get_host_by_name("Jupiter");
31
32   test = 1;
33   if (xbt_dynar_member(tests, &test)){
34     XBT_INFO("Test 1:");
35     XBT_INFO("  Create a process on Jupiter");
36     argvF = xbt_new(char*, 2);
37     argvF[0] = xbt_strdup("test_daemon");
38     MSG_process_create_with_arguments("test_daemon", test_daemon, NULL, jupiter, 1, argvF);
39     XBT_INFO("  Turn off Jupiter");
40     MSG_host_off(jupiter);
41     MSG_process_sleep(10);
42     XBT_INFO("Test 1 seems ok, cool !(number of Process : %d, it should be 1 (i.e. the Test one))", MSG_process_get_number());
43   }
44
45   test = 2;
46   if (xbt_dynar_member(tests, &test)){
47     XBT_INFO("Test 2:");
48     XBT_INFO("  Turn off Jupiter");
49     MSG_host_off(jupiter);
50     argvF = xbt_new(char*, 2);
51     argvF[0] = xbt_strdup("test_daemon");
52     MSG_process_create_with_arguments("test_daemon", test_daemon, NULL, jupiter, 1, argvF);
53     MSG_process_sleep(10);
54     XBT_INFO("  Test 2 does not crash, WTF ?!(number of Process : %d, it should be 1)", MSG_process_get_number());
55     XBT_INFO("  Ok so let's turn on/off the node to see whether the process is correctly bound to Jupiter");
56     MSG_host_on(jupiter);
57     XBT_INFO("  Turn off");
58     MSG_host_off(jupiter);
59     XBT_INFO("  sleep");
60     MSG_process_sleep(10);
61     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());
62   }
63
64   test = 3;
65   if (xbt_dynar_member(tests, &test)){
66     XBT_INFO("Test 3 (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");
67     MSG_host_on(jupiter);
68     MSG_process_sleep(10);
69     argvF = xbt_new(char*, 2);
70     argvF[0] = xbt_strdup("commRX");
71     MSG_process_create_with_arguments("commRX", commRX, NULL, MSG_get_host_by_name("Tremblay"), 1, argvF);
72     argvF = xbt_new(char*, 2);
73     argvF[0] = xbt_strdup("commTX");
74     MSG_process_create_with_arguments("commTX", commTX, NULL, MSG_get_host_by_name("Jupiter"), 1, argvF);
75     XBT_INFO("  number of processes: %d", MSG_process_get_number());
76     MSG_process_sleep(10);
77     XBT_INFO("  Turn Jupiter off");
78     MSG_host_off(jupiter);
79     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());
80   }
81
82   test = 4;
83   if (xbt_dynar_member(tests, &test)){
84     XBT_INFO("Test 4 (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");
85     MSG_host_on(jupiter);
86     MSG_process_sleep(10);
87     argvF = xbt_new(char*, 2);
88     argvF[0] = xbt_strdup("commTX");
89     MSG_process_create_with_arguments("commTX", commTX, NULL, MSG_get_host_by_name("Tremblay"), 1, argvF);
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("Jupiter"), 1, argvF);
93     MSG_host_off(jupiter);
94     XBT_INFO("Test 4 seems ok, cool !(number of Process : %d, it should be 1", MSG_process_get_number());
95   }
96
97   test =5;
98   if (xbt_dynar_member(tests, &test)){
99
100   }
101
102   test = 6;
103   if (xbt_dynar_member(tests, &test)){
104
105   }
106
107   test = 7;
108   if (xbt_dynar_member(tests, &test)){
109
110   }
111
112   test = 8;
113   if (xbt_dynar_member(tests, &test)){
114
115   }
116
117   return 0;
118 }
119
120 int test_daemon(int argc, char *argv[])
121 {
122   msg_task_t task = NULL;
123   task = MSG_task_create("deamon", 100*MSG_get_host_speed(MSG_host_self()), 0, NULL);
124   XBT_INFO("  Execute deamon");
125   MSG_task_execute(task);
126   XBT_INFO("  I'm done. See you!");
127   return 0;
128 }
129
130 int commTX(int argc, char *argv[])
131 {
132   msg_task_t task = NULL;
133   char mailbox[80];
134   sprintf(mailbox, "comm");
135   XBT_INFO("  Start TX");
136   task = MSG_task_create("COMM", 0, 100000000, NULL);
137   MSG_task_isend(task, mailbox);
138   // We should wait a bit (if not the process will end before the communication, leading to an exception at the other side).
139   MSG_process_sleep(30);
140   XBT_INFO("  TX done");
141   return 0;
142 }
143
144 int commRX(int argc, char *argv[])
145 {
146   msg_task_t task = NULL;
147   char mailbox[80];
148   sprintf(mailbox, "comm");
149   XBT_INFO("  Start RX");
150   msg_error_t error = MSG_task_receive(&(task), mailbox);
151   if (error==MSG_OK) {
152     XBT_INFO("  Receive message: %s", task->name);
153   } else if (error==MSG_HOST_FAILURE) {
154     XBT_INFO("  Receive message: HOST_FAILURE");
155   } else if (error==MSG_TRANSFER_FAILURE) {
156     XBT_INFO("  Receive message: TRANSFERT_FAILURE");
157   } else {
158     XBT_INFO("  Receive message: %d", error);
159   }
160   XBT_INFO("  RX Done");
161   return 0;
162 }
163
164 /** Main function */
165 int main(int argc, char *argv[])
166 {
167   msg_error_t res;
168   const char *platform_file;
169   const char *application_file;
170
171   MSG_init(&argc, argv);
172   if (argc != 4) {
173     printf("Usage: %s platform_file deployment_file test_number\n", argv[0]);
174     printf("example: %s msg_platform.xml msg_deployment.xml 1\n", argv[0]);
175     exit(1);
176   }
177
178   unsigned int iter;
179   char *groups;
180   xbt_dynar_t s_tests = xbt_str_split(argv[3], ",");
181   int tmp_test = 0;
182   tests = xbt_dynar_new(sizeof(int), NULL);
183   xbt_dynar_foreach(s_tests, iter, groups) {
184      sscanf(xbt_dynar_get_as(s_tests, iter, char *), "%d", &tmp_test);
185      xbt_dynar_set_as(tests, iter, int, tmp_test);
186   }
187
188   platform_file = argv[1];
189   application_file = argv[2];
190
191   /* MSG_config("workstation/model","KCCFLN05"); */
192   {                             /*  Simulation setting */
193     MSG_create_environment(platform_file);
194   }
195   {                             /*   Application deployment */
196     MSG_function_register("test", test);
197     MSG_function_register("test_daemon", test_daemon);
198
199     MSG_launch_application(application_file);
200   }
201   res = MSG_main();
202
203   XBT_INFO("Simulation time %g", MSG_get_clock());
204
205   if (res == MSG_OK)
206     return 0;
207   else
208     return 1;
209 }                               /* end_of_main */