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 "simgrid/msg.h" /* Yeah! If you want to use msg, you need to include simgrid/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_search_or_negative(tests, &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);
42 XBT_INFO(" Turn off Jupiter");
43 MSG_host_off(jupiter);
44 MSG_process_sleep(10);
45 XBT_INFO("Test 1 seems ok, cool !(number of Process : %d, it should be 1 (i.e. the Test one))", MSG_process_get_number());
49 // Create a process that on a host that is turned off (this should not be possible)
50 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
52 XBT_INFO(" Turn off Jupiter");
53 // adsein: Jupiter is already, hence nothing should happen
54 // adsein: This can be one additional test, to check that you cannot shutdown twice a host
55 MSG_host_off(jupiter);
56 argvF = xbt_new(char*, 2);
57 argvF[0] = xbt_strdup("process_daemon");
58 MSG_process_create_with_arguments("process_daemon", process_daemon, NULL, jupiter, 1, argvF);
59 MSG_process_sleep(10);
60 XBT_INFO(" Test 2 does not crash, WTF ?!(number of Process : %d, it should be 1)", MSG_process_get_number());
61 XBT_INFO(" Ok so let's turn on/off the node to see whether the process is correctly bound to Jupiter");
63 XBT_INFO(" Turn off");
64 MSG_host_off(jupiter);
66 MSG_process_sleep(10);
67 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());
71 // Create a process running sucessive sleeps on a host and turn the host off during the execution of the process.
72 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
75 argvF = xbt_new(char*, 2);
76 argvF[0] = xbt_strdup("process_sleep");
77 MSG_process_create_with_arguments("process_sleep", process_sleep, NULL, jupiter, 1, argvF);
78 MSG_process_sleep(100);
79 XBT_INFO(" Turn off");
80 MSG_host_off(jupiter);
81 XBT_INFO(" sleep for 10 seconds");
82 MSG_process_sleep(10000);
83 XBT_INFO("number of Process : %d it should be 1 (i.e. the Test one))", MSG_process_get_number());
87 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
88 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");
90 MSG_process_sleep(10);
91 argvF = xbt_new(char*, 2);
92 argvF[0] = xbt_strdup("commRX");
93 MSG_process_create_with_arguments("commRX", commRX, NULL, MSG_get_host_by_name("Tremblay"), 1, argvF);
94 argvF = xbt_new(char*, 2);
95 argvF[0] = xbt_strdup("commTX");
96 MSG_process_create_with_arguments("commTX", commTX, NULL, jupiter, 1, argvF);
97 XBT_INFO(" number of processes: %d", MSG_process_get_number());
98 MSG_process_sleep(10);
99 XBT_INFO(" Turn Jupiter off");
100 MSG_host_off(jupiter);
101 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());
105 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
106 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");
107 XBT_INFO("Warning! I think this test is completely broken and it was revealed by exception/exception test.");
108 XBT_INFO("At time 20, Jupiter should wake up with a HOST_FAILURE and it gets a TRANSFERT_FAILURE. This is because when turning off Jupiter, its processes are killed, which cancels/destroys the corresponding surf communication instead of canceling a src_ or dst_timeout.");
109 MSG_host_on(jupiter);
110 MSG_process_sleep(10);
111 argvF = xbt_new(char*, 2);
112 argvF[0] = xbt_strdup("commRX");
113 MSG_process_create_with_arguments("commRX", commRX, NULL, jupiter, 1, argvF);
114 argvF = xbt_new(char*, 2);
115 argvF[0] = xbt_strdup("commTX");
116 MSG_process_create_with_arguments("commTX", commTX, NULL, MSG_get_host_by_name("Tremblay"), 1, argvF);
117 XBT_INFO(" number of processes: %d", MSG_process_get_number());
118 MSG_process_sleep(10);
119 XBT_INFO(" Turn Jupiter off");
120 MSG_host_off(jupiter);
121 XBT_INFO("Test 5 seems ok, cool !(number of Process : %d, it should be 2", MSG_process_get_number());
125 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
126 XBT_INFO("Test 6: Turn on Jupiter, assign a VM on Jupiter, launch a process inside the VM, and turn off the node");
131 msg_process_t daemon;
133 vm0 = MSG_vm_create (jupiter, "vm0", 1, 2048, 125, NULL, -1, 125, dpRate);
136 argvF = xbt_new(char*, 2);
137 argvF[0] = xbt_strdup("process_daemon");
138 daemon = MSG_process_create_with_arguments("process_daemon", process_daemon, NULL, vm0, 1, argvF);
140 argvF = xbt_new(char*, 2);
141 argvF[0] = xbt_strdup("process_daemonJUPI");
142 MSG_process_create_with_arguments("process_daemonJUPI", process_daemon, NULL, jupiter, 1, argvF);
144 MSG_process_suspend(daemon);
145 MSG_vm_set_bound(vm0, 90);
146 MSG_process_resume(daemon);
148 MSG_process_sleep(10);
150 XBT_INFO(" Turn Jupiter off");
151 MSG_host_off(jupiter);
152 XBT_INFO(" Shutdown vm0");
153 MSG_vm_shutdown(vm0);
154 XBT_INFO(" Destroy vm0");
156 XBT_INFO("Test 6 is also weird: when the node Jupiter is turned off once again, the VM and its daemon are not killed. However, the issue regarding the shutdown of hosted VMs can be seen a feature not a bug ;)");
160 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
165 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
170 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
173 XBT_INFO(" Test done. See you!");
177 // adsein: Is this really a daemon ? it ran only one task ? I just added a stupid loop
178 int process_daemon(int argc, char *argv[])
180 msg_task_t task = NULL;
181 XBT_INFO(" Start daemon on %s (%f)", MSG_host_get_name(MSG_host_self()), MSG_get_host_speed(MSG_host_self()));
183 task = MSG_task_create("deamon", MSG_get_host_speed(MSG_host_self()), 0, NULL);
184 XBT_INFO(" Execute deamon");
185 MSG_task_execute(task);
186 MSG_task_destroy(task);
188 XBT_INFO(" Deamon done. See you!");
192 int process_sleep(int argc, char *argv[])
195 XBT_INFO(" I'm alive but I should sleep");
196 MSG_process_sleep(10);
198 XBT_INFO(" I'm done. See you!");
202 int commTX(int argc, char *argv[])
204 msg_task_t task = NULL;
206 sprintf(mailbox, "comm");
207 XBT_INFO(" Start TX");
208 task = MSG_task_create("COMM", 0, 100000000, NULL);
209 MSG_task_isend(task, mailbox);
210 // We should wait a bit (if not the process will end before the communication, leading to an exception at the other side).
211 MSG_process_sleep(30);
212 XBT_INFO(" TX done");
216 int commRX(int argc, char *argv[])
218 msg_task_t task = NULL;
220 sprintf(mailbox, "comm");
221 XBT_INFO(" Start RX");
222 msg_error_t error = MSG_task_receive(&(task), mailbox);
224 XBT_INFO(" Receive message: %s", task->name);
225 } else if (error==MSG_HOST_FAILURE) {
226 XBT_INFO(" Receive message: HOST_FAILURE");
227 } else if (error==MSG_TRANSFER_FAILURE) {
228 XBT_INFO(" Receive message: TRANSFERT_FAILURE");
230 XBT_INFO(" Receive message: %d", error);
232 XBT_INFO(" RX Done");
237 int main(int argc, char *argv[])
240 const char *platform_file;
241 const char *application_file;
243 MSG_init(&argc, argv);
245 printf("Usage: %s platform_file deployment_file test_number\n", argv[0]);
246 printf("example: %s msg_platform.xml msg_deployment.xml 1\n", argv[0]);
252 xbt_dynar_t s_tests = xbt_str_split(argv[3], ",");
254 tests = xbt_dynar_new(sizeof(int), NULL);
255 xbt_dynar_foreach(s_tests, iter, groups) {
256 sscanf(xbt_dynar_get_as(s_tests, iter, char *), "%d", &tmp_test);
257 xbt_dynar_set_as(tests, iter, int, tmp_test);
260 platform_file = argv[1];
261 application_file = argv[2];
263 /* MSG_config("workstation/model","KCCFLN05"); */
264 { /* Simulation setting */
265 MSG_create_environment(platform_file);
267 { /* Application deployment */
268 MSG_function_register("test_launcher", test_launcher);
269 MSG_function_register("process_daemon", process_daemon);
270 MSG_function_register("process_sleep", process_sleep);
272 MSG_launch_application(application_file);
276 XBT_INFO("Simulation time %g", MSG_get_clock());