1 /* Copyright (c) 2010-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
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 */
11 /* Create a log channel to have nice outputs. */
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15 "Messages specific for this msg example");
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[]);
25 int test_launcher(int argc, char *argv[])
29 argvF = xbt_new(char*, 2);
30 argvF[0] = xbt_strdup("process_daemon");
31 msg_host_t jupiter = MSG_get_host_by_name("Jupiter");
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)){
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());
48 // Create a process that on a host that is turned off (this should not be possible)
49 if (xbt_dynar_member(tests, &test)){
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");
62 XBT_INFO(" Turn off");
63 MSG_host_off(jupiter);
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());
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)){
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());
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");
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 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());
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());
119 if (xbt_dynar_member(tests, &test)){
124 if (xbt_dynar_member(tests, &test)){
129 if (xbt_dynar_member(tests, &test)){
134 if (xbt_dynar_member(tests, &test)){
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[])
144 msg_task_t task = NULL;
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);
150 MSG_task_execute(task);
151 XBT_INFO(" I'm done. See you!");
155 int process_sleep(int argc, char *argv[])
158 XBT_INFO(" I'm alive but I should sleep");
159 MSG_process_sleep(10);
161 XBT_INFO(" I'm done. See you!");
165 int commTX(int argc, char *argv[])
167 msg_task_t task = NULL;
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");
179 int commRX(int argc, char *argv[])
181 msg_task_t task = NULL;
183 sprintf(mailbox, "comm");
184 XBT_INFO(" Start RX");
185 msg_error_t error = MSG_task_receive(&(task), mailbox);
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");
193 XBT_INFO(" Receive message: %d", error);
195 XBT_INFO(" RX Done");
200 int main(int argc, char *argv[])
203 const char *platform_file;
204 const char *application_file;
206 MSG_init(&argc, argv);
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]);
215 xbt_dynar_t s_tests = xbt_str_split(argv[3], ",");
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);
223 platform_file = argv[1];
224 application_file = argv[2];
226 /* MSG_config("workstation/model","KCCFLN05"); */
227 { /* Simulation setting */
228 MSG_create_environment(platform_file);
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);
235 MSG_launch_application(application_file);
239 XBT_INFO("Simulation time %g", MSG_get_clock());