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,
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[]);
26 int test_launcher(int argc, char *argv[])
30 argvF = xbt_new(char*, 2);
31 argvF[0] = xbt_strdup("process_daemon");
32 msg_host_t jupiter = MSG_host_by_name("Jupiter");
35 // Create a process running a simple task on a host and turn the host off during the execution of the process.
36 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
38 XBT_INFO(" Create a process on Jupiter");
39 argvF = xbt_new(char*, 2);
40 argvF[0] = xbt_strdup("process_daemon");
41 MSG_process_create_with_arguments("process_daemon", process_daemon, NULL, jupiter, 1, argvF);
43 XBT_INFO(" Turn off Jupiter");
44 MSG_host_off(jupiter);
45 MSG_process_sleep(10);
46 XBT_INFO("Test 1 seems ok, cool !(#Processes: %d, it should be 1; #tasks: %d)", MSG_process_get_number(), tasks_done);
50 // Create a process that on a host that is turned off (this should not be possible)
51 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
53 XBT_INFO(" Turn off Jupiter");
54 // adsein: Jupiter is already, hence nothing should happen
55 // adsein: This can be one additional test, to check that you cannot shutdown twice a host
56 MSG_host_off(jupiter);
57 argvF = xbt_new(char*, 2);
58 argvF[0] = xbt_strdup("process_daemon");
59 MSG_process_create_with_arguments("process_daemon", process_daemon, NULL, jupiter, 1, argvF);
60 MSG_process_sleep(10);
61 XBT_INFO(" Test 2 does not crash, WTF ?!(number of Process : %d, it should be 1)", MSG_process_get_number());
62 XBT_INFO(" Ok so let's turn on/off the node to see whether the process is correctly bound to Jupiter");
64 XBT_INFO(" Turn off");
65 MSG_host_off(jupiter);
67 MSG_process_sleep(10);
68 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());
72 // Create a process running sucessive sleeps on a host and turn the host off during the execution of the process.
73 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
76 argvF = xbt_new(char*, 2);
77 argvF[0] = xbt_strdup("process_sleep");
78 MSG_process_create_with_arguments("process_sleep", process_sleep, NULL, jupiter, 1, argvF);
79 MSG_process_sleep(100);
80 XBT_INFO(" Turn off");
81 MSG_host_off(jupiter);
82 XBT_INFO(" sleep for 10 seconds");
83 MSG_process_sleep(10000);
84 XBT_INFO("number of Process : %d it should be 1 (i.e. the Test one))", MSG_process_get_number());
88 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
89 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");
91 MSG_process_sleep(10);
92 argvF = xbt_new(char*, 2);
93 argvF[0] = xbt_strdup("commRX");
94 MSG_process_create_with_arguments("commRX", commRX, NULL, MSG_host_by_name("Tremblay"), 1, argvF);
95 argvF = xbt_new(char*, 2);
96 argvF[0] = xbt_strdup("commTX");
97 MSG_process_create_with_arguments("commTX", commTX, NULL, jupiter, 1, argvF);
98 XBT_INFO(" number of processes: %d", MSG_process_get_number());
99 MSG_process_sleep(10);
100 XBT_INFO(" Turn Jupiter off");
101 MSG_host_off(jupiter);
102 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());
106 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
107 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");
108 MSG_host_on(jupiter);
109 MSG_process_sleep(10);
110 argvF = xbt_new(char*, 2);
111 argvF[0] = xbt_strdup("commRX");
112 MSG_process_create_with_arguments("commRX", commRX, NULL, jupiter, 1, argvF);
113 argvF = xbt_new(char*, 2);
114 argvF[0] = xbt_strdup("commTX");
115 MSG_process_create_with_arguments("commTX", commTX, NULL, MSG_host_by_name("Tremblay"), 1, argvF);
116 XBT_INFO(" number of processes: %d", MSG_process_get_number());
117 MSG_process_sleep(10);
118 XBT_INFO(" Turn Jupiter off");
119 MSG_host_off(jupiter);
120 XBT_INFO("Test 5 seems ok, cool !(number of Process : %d, it should be 2", MSG_process_get_number());
124 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
125 XBT_INFO("Test 6: Turn on Jupiter, assign a VM on Jupiter, launch a process inside the VM, and turn off the node");
130 msg_process_t daemon;
132 vm0 = MSG_vm_create (jupiter, "vm0", 1, 2048, 125, NULL, -1, 125, dpRate);
135 argvF = xbt_new(char*, 2);
136 argvF[0] = xbt_strdup("process_daemon");
137 daemon = MSG_process_create_with_arguments("process_daemon", process_daemon, NULL, vm0, 1, argvF);
139 argvF = xbt_new(char*, 2);
140 argvF[0] = xbt_strdup("process_daemonJUPI");
141 MSG_process_create_with_arguments("process_daemonJUPI", process_daemon, NULL, jupiter, 1, argvF);
143 MSG_process_suspend(daemon);
144 MSG_vm_set_bound(vm0, 90);
145 MSG_process_resume(daemon);
147 MSG_process_sleep(10);
149 XBT_INFO(" Turn Jupiter off");
150 MSG_host_off(jupiter);
151 XBT_INFO(" Shutdown vm0");
152 MSG_vm_shutdown(vm0);
153 XBT_INFO(" Destroy vm0");
155 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 ;)");
159 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
164 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
169 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
172 XBT_INFO(" Test done. See you!");
176 int process_daemon(int argc, char *argv[])
178 msg_task_t task = NULL;
179 XBT_INFO(" Start daemon on %s (%f)", MSG_host_get_name(MSG_host_self()), MSG_get_host_speed(MSG_host_self()));
181 task = MSG_task_create("daemon", MSG_get_host_speed(MSG_host_self()), 0, NULL);
182 XBT_INFO(" Execute daemon");
183 MSG_task_execute(task);
184 MSG_task_destroy(task);
187 XBT_INFO(" daemon done. See you!");
191 int process_sleep(int argc, char *argv[])
194 XBT_INFO(" I'm alive but I should sleep");
195 MSG_process_sleep(10);
197 XBT_INFO(" I'm done. See you!");
201 int commTX(int argc, char *argv[])
203 msg_task_t task = NULL;
205 sprintf(mailbox, "comm");
206 XBT_INFO(" Start TX");
207 task = MSG_task_create("COMM", 0, 100000000, NULL);
208 MSG_task_isend(task, mailbox);
209 // We should wait a bit (if not the process will end before the communication, leading to an exception at the other side).
210 MSG_process_sleep(30);
211 XBT_INFO(" TX done");
215 int commRX(int argc, char *argv[])
217 msg_task_t task = NULL;
219 sprintf(mailbox, "comm");
220 XBT_INFO(" Start RX");
221 msg_error_t error = MSG_task_receive(&(task), mailbox);
223 XBT_INFO(" Receive message: %s", task->name);
224 } else if (error==MSG_HOST_FAILURE) {
225 XBT_INFO(" Receive message: HOST_FAILURE");
226 } else if (error==MSG_TRANSFER_FAILURE) {
227 XBT_INFO(" Receive message: TRANSFERT_FAILURE");
229 XBT_INFO(" Receive message: %d", error);
231 XBT_INFO(" RX Done");
236 int main(int argc, char *argv[])
239 const char *platform_file;
240 const char *application_file;
242 MSG_init(&argc, argv);
244 printf("Usage: %s platform_file deployment_file test_number\n", argv[0]);
245 printf("example: %s msg_platform.xml msg_deployment.xml 1\n", argv[0]);
251 xbt_dynar_t s_tests = xbt_str_split(argv[3], ",");
253 tests = xbt_dynar_new(sizeof(int), NULL);
254 xbt_dynar_foreach(s_tests, iter, groups) {
255 sscanf(xbt_dynar_get_as(s_tests, iter, char *), "%d", &tmp_test);
256 xbt_dynar_set_as(tests, iter, int, tmp_test);
258 xbt_dynar_free(&s_tests);
260 platform_file = argv[1];
261 application_file = argv[2];
263 { /* Simulation setting */
264 MSG_create_environment(platform_file);
266 { /* Application deployment */
267 MSG_function_register("test_launcher", test_launcher);
268 MSG_function_register("process_daemon", process_daemon);
269 MSG_function_register("process_sleep", process_sleep);
271 MSG_launch_application(application_file);
275 XBT_INFO("Simulation time %g", MSG_get_clock());