1 /* Copyright (c) 2010-2015. 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, "Messages specific for this msg example");
16 int test_launcher(int argc, char *argv[]);
17 int process_daemon(int argc, char *argv[]);
18 int process_sleep(int argc, char *argv[]);
19 int commRX(int argc, char *argv[]);
20 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_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 !(#Processes: %d, it should be 1; #tasks: %d)", MSG_process_get_number(), tasks_done);
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_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 MSG_host_on(jupiter);
108 MSG_process_sleep(10);
109 argvF = xbt_new(char*, 2);
110 argvF[0] = xbt_strdup("commRX");
111 MSG_process_create_with_arguments("commRX", commRX, NULL, jupiter, 1, argvF);
112 argvF = xbt_new(char*, 2);
113 argvF[0] = xbt_strdup("commTX");
114 MSG_process_create_with_arguments("commTX", commTX, NULL, MSG_host_by_name("Tremblay"), 1, argvF);
115 XBT_INFO(" number of processes: %d", MSG_process_get_number());
116 MSG_process_sleep(10);
117 XBT_INFO(" Turn Jupiter off");
118 MSG_host_off(jupiter);
119 XBT_INFO("Test 5 seems ok, cool !(number of Process : %d, it should be 2", MSG_process_get_number());
123 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
124 XBT_INFO("Test 6: Turn on Jupiter, assign a VM on Jupiter, launch a process inside the VM, and turn off the node");
129 msg_process_t daemon;
131 vm0 = MSG_vm_create (jupiter, "vm0", 1, 2048, 125, NULL, -1, 125, dpRate);
134 argvF = xbt_new(char*, 2);
135 argvF[0] = xbt_strdup("process_daemon");
136 daemon = MSG_process_create_with_arguments("process_daemon", process_daemon, NULL, vm0, 1, argvF);
138 argvF = xbt_new(char*, 2);
139 argvF[0] = xbt_strdup("process_daemonJUPI");
140 MSG_process_create_with_arguments("process_daemonJUPI", process_daemon, NULL, jupiter, 1, argvF);
142 MSG_process_suspend(daemon);
143 MSG_vm_set_bound(vm0, 90);
144 MSG_process_resume(daemon);
146 MSG_process_sleep(10);
148 XBT_INFO(" Turn Jupiter off");
149 MSG_host_off(jupiter);
150 XBT_INFO(" Shutdown vm0");
151 MSG_vm_shutdown(vm0);
152 XBT_INFO(" Destroy vm0");
154 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 ;)");
158 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
163 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
168 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
171 XBT_INFO(" Test done. See you!");
175 int process_daemon(int argc, char *argv[])
177 msg_task_t task = NULL;
178 XBT_INFO(" Start daemon on %s (%f)", MSG_host_get_name(MSG_host_self()), MSG_get_host_speed(MSG_host_self()));
180 task = MSG_task_create("daemon", MSG_get_host_speed(MSG_host_self()), 0, NULL);
181 XBT_INFO(" Execute daemon");
182 MSG_task_execute(task);
183 MSG_task_destroy(task);
186 XBT_INFO(" daemon done. See you!");
190 int process_sleep(int argc, char *argv[])
193 XBT_INFO(" I'm alive but I should sleep");
194 MSG_process_sleep(10);
196 XBT_INFO(" I'm done. See you!");
200 int commTX(int argc, char *argv[])
202 msg_task_t task = NULL;
204 sprintf(mailbox, "comm");
205 XBT_INFO(" Start TX");
206 task = MSG_task_create("COMM", 0, 100000000, NULL);
207 MSG_task_isend(task, mailbox);
208 // We should wait a bit (if not the process will end before the communication, leading to an exception at the other side).
209 MSG_process_sleep(30);
210 XBT_INFO(" TX done");
214 int commRX(int argc, char *argv[])
216 msg_task_t task = NULL;
218 sprintf(mailbox, "comm");
219 XBT_INFO(" Start RX");
220 msg_error_t error = MSG_task_receive(&(task), mailbox);
222 XBT_INFO(" Receive message: %s", task->name);
223 } else if (error==MSG_HOST_FAILURE) {
224 XBT_INFO(" Receive message: HOST_FAILURE");
225 } else if (error==MSG_TRANSFER_FAILURE) {
226 XBT_INFO(" Receive message: TRANSFERT_FAILURE");
228 XBT_INFO(" Receive message: %d", error);
230 XBT_INFO(" RX Done");
235 int main(int argc, char *argv[])
238 const char *platform_file;
239 const char *application_file;
241 MSG_init(&argc, argv);
243 printf("Usage: %s platform_file deployment_file test_number\n", argv[0]);
244 printf("example: %s msg_platform.xml msg_deployment.xml 1\n", argv[0]);
250 xbt_dynar_t s_tests = xbt_str_split(argv[3], ",");
252 tests = xbt_dynar_new(sizeof(int), NULL);
253 xbt_dynar_foreach(s_tests, iter, groups) {
254 sscanf(xbt_dynar_get_as(s_tests, iter, char *), "%d", &tmp_test);
255 xbt_dynar_set_as(tests, iter, int, tmp_test);
257 xbt_dynar_free(&s_tests);
259 platform_file = argv[1];
260 application_file = argv[2];
262 { /* Simulation setting */
263 MSG_create_environment(platform_file);
265 { /* Application deployment */
266 MSG_function_register("test_launcher", test_launcher);
267 MSG_function_register("process_daemon", process_daemon);
268 MSG_function_register("process_sleep", process_sleep);
270 MSG_launch_application(application_file);
274 XBT_INFO("Simulation time %g", MSG_get_clock());
276 return res != MSG_OK;