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, 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("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());
122 if (xbt_dynar_member(tests, &test)){
127 if (xbt_dynar_member(tests, &test)){
132 if (xbt_dynar_member(tests, &test)){
137 if (xbt_dynar_member(tests, &test)){
140 XBT_INFO(" Test done. See you!");
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[])
147 msg_task_t task = NULL;
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);
153 MSG_task_execute(task);
154 XBT_INFO(" Deamon done. See you!");
158 int process_sleep(int argc, char *argv[])
161 XBT_INFO(" I'm alive but I should sleep");
162 MSG_process_sleep(10);
164 XBT_INFO(" I'm done. See you!");
168 int commTX(int argc, char *argv[])
170 msg_task_t task = NULL;
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");
182 int commRX(int argc, char *argv[])
184 msg_task_t task = NULL;
186 sprintf(mailbox, "comm");
187 XBT_INFO(" Start RX");
188 msg_error_t error = MSG_task_receive(&(task), mailbox);
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");
196 XBT_INFO(" Receive message: %d", error);
198 XBT_INFO(" RX Done");
203 int main(int argc, char *argv[])
206 const char *platform_file;
207 const char *application_file;
209 MSG_init(&argc, argv);
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]);
218 xbt_dynar_t s_tests = xbt_str_split(argv[3], ",");
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);
226 platform_file = argv[1];
227 application_file = argv[2];
229 /* MSG_config("workstation/model","KCCFLN05"); */
230 { /* Simulation setting */
231 MSG_create_environment(platform_file);
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);
238 MSG_launch_application(application_file);
242 XBT_INFO("Simulation time %g", MSG_get_clock());