1 /* Copyright (c) 2010-2017. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "simgrid/msg.h"
8 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
13 static void task_cleanup_handler(void *task)
16 MSG_task_destroy(task);
19 static int process_daemon(int argc, char *argv[])
21 msg_process_t self = MSG_process_self();
22 XBT_INFO(" Start daemon on %s (%f)", MSG_host_get_name(MSG_host_self()), MSG_host_get_speed(MSG_host_self()));
24 msg_task_t task = MSG_task_create("daemon", MSG_host_get_speed(MSG_host_self()), 0, NULL);
25 MSG_process_set_data(self, task);
26 XBT_INFO(" Execute daemon");
27 MSG_task_execute(task);
28 MSG_process_set_data(self, NULL);
29 MSG_task_destroy(task);
32 XBT_INFO(" daemon done. See you!");
36 static int process_sleep(int argc, char *argv[])
39 XBT_INFO(" I'm alive but I should sleep");
40 MSG_process_sleep(10);
42 XBT_INFO(" I'm done. See you!");
46 static int commTX(int argc, char *argv[])
48 const char * mailbox = "comm";
49 XBT_INFO(" Start TX");
50 msg_task_t task = MSG_task_create("COMM", 0, 100000000, NULL);
51 MSG_task_dsend(task, mailbox, task_cleanup_handler);
52 // We should wait a bit (if not the process will end before the communication, hence an exception on the other side).
53 MSG_process_sleep(30);
58 static int commRX(int argc, char *argv[])
60 msg_task_t task = NULL;
61 const char * mailbox = "comm";
62 XBT_INFO(" Start RX");
63 msg_error_t error = MSG_task_receive(&(task), mailbox);
65 XBT_INFO(" Receive message: %s", task->name);
66 MSG_task_destroy(task);
67 } else if (error==MSG_HOST_FAILURE) {
68 XBT_INFO(" Receive message: HOST_FAILURE");
69 } else if (error==MSG_TRANSFER_FAILURE) {
70 XBT_INFO(" Receive message: TRANSFERT_FAILURE");
72 XBT_INFO(" Receive message: %u", error);
78 static int test_launcher(int argc, char *argv[])
82 msg_host_t jupiter = MSG_host_by_name("Jupiter");
85 // Create a process running a simple task on a host and turn the host off during the execution of the process.
86 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
88 XBT_INFO(" Create a process on Jupiter");
89 argvF = xbt_new(char*, 2);
90 argvF[0] = xbt_strdup("process_daemon");
91 MSG_process_create_with_arguments("process_daemon", process_daemon, NULL, jupiter, 1, argvF);
93 XBT_INFO(" Turn off Jupiter");
94 MSG_host_off(jupiter);
95 MSG_process_sleep(10);
96 XBT_INFO("Test 1 seems ok, cool !(#Processes: %d, it should be 1; #tasks: %d)", MSG_process_get_number(), tasks_done);
100 // Create a process that on a host that is turned off (this should not be possible)
101 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
103 XBT_INFO(" Turn off Jupiter");
104 // adsein: Jupiter is already, hence nothing should happen
105 // adsein: This can be one additional test, to check that you cannot shutdown twice a host
106 MSG_host_off(jupiter);
107 argvF = xbt_new(char*, 2);
108 argvF[0] = xbt_strdup("process_daemon");
109 MSG_process_create_with_arguments("process_daemon", process_daemon, NULL, jupiter, 1, argvF);
110 MSG_process_sleep(10);
111 XBT_INFO(" Test 2 does not crash as it should (number of Process : %d, it should be 1)", MSG_process_get_number());
112 XBT_INFO(" Ok so let's turn on/off the node to see whether the process is correctly bound to Jupiter");
113 MSG_host_on(jupiter);
114 XBT_INFO(" Turn off");
115 MSG_host_off(jupiter);
117 MSG_process_sleep(10);
118 XBT_INFO("number of Process : %d it should be 1. The daemon that has been created for test2 has been correctly "
119 "destroyed....ok at least it looks rigorous, cool ! You just have to disallow the possibility to create "
120 "a new process on a node when the node is off.)", MSG_process_get_number());
124 // Create a process running sucessive sleeps on a host and turn the host off during the execution of the process.
125 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
127 MSG_host_on(jupiter);
128 argvF = xbt_new(char*, 2);
129 argvF[0] = xbt_strdup("process_sleep");
130 MSG_process_create_with_arguments("process_sleep", process_sleep, NULL, jupiter, 1, argvF);
131 MSG_process_sleep(100);
132 XBT_INFO(" Turn off");
133 MSG_host_off(jupiter);
134 XBT_INFO(" sleep for 10 seconds");
135 MSG_process_sleep(10000);
136 XBT_INFO("number of Process : %d it should be 1 (i.e. the Test one))", MSG_process_get_number());
140 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
141 XBT_INFO("Test 4 (turn off src during a communication) : Create a Process/task to make a communication between "
142 "Jupiter and Tremblay and turn off Jupiter during the communication");
143 MSG_host_on(jupiter);
144 MSG_process_sleep(10);
145 argvF = xbt_new(char*, 2);
146 argvF[0] = xbt_strdup("commRX");
147 MSG_process_create_with_arguments("commRX", commRX, NULL, MSG_host_by_name("Tremblay"), 1, argvF);
148 argvF = xbt_new(char*, 2);
149 argvF[0] = xbt_strdup("commTX");
150 MSG_process_create_with_arguments("commTX", commTX, NULL, jupiter, 1, argvF);
151 XBT_INFO(" number of processes: %d", MSG_process_get_number());
152 MSG_process_sleep(10);
153 XBT_INFO(" Turn Jupiter off");
154 MSG_host_off(jupiter);
155 XBT_INFO("Test 4 seems ok (number of Process : %d, it should be 1 or 2 if RX has not been satisfied) cool, you "
156 "can now turn off a node that has a process paused by a sleep call", MSG_process_get_number());
160 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
161 XBT_INFO("Test 5 (turn off dest during a communication : Create a Process/task to make a communication between "
162 "Tremblay and Jupiter and turn off Jupiter during the communication");
163 MSG_host_on(jupiter);
164 MSG_process_sleep(10);
165 argvF = xbt_new(char*, 2);
166 argvF[0] = xbt_strdup("commRX");
167 MSG_process_create_with_arguments("commRX", commRX, NULL, jupiter, 1, argvF);
168 argvF = xbt_new(char*, 2);
169 argvF[0] = xbt_strdup("commTX");
170 MSG_process_create_with_arguments("commTX", commTX, NULL, MSG_host_by_name("Tremblay"), 1, argvF);
171 XBT_INFO(" number of processes: %d", MSG_process_get_number());
172 MSG_process_sleep(10);
173 XBT_INFO(" Turn Jupiter off");
174 MSG_host_off(jupiter);
175 XBT_INFO("Test 5 seems ok (number of Process: %d, it should be 2)", MSG_process_get_number());
179 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
180 XBT_INFO("Test 6: Turn on Jupiter, assign a VM on Jupiter, launch a process inside the VM, and turn off the node");
185 msg_process_t daemon;
187 vm0 = MSG_vm_create(jupiter, "vm0", 1, 2048, 125, dpRate);
190 argvF = xbt_new(char*, 2);
191 argvF[0] = xbt_strdup("process_daemon");
192 daemon = MSG_process_create_with_arguments("process_daemon", process_daemon, NULL, (msg_host_t)vm0, 1, argvF);
194 argvF = xbt_new(char*, 2);
195 argvF[0] = xbt_strdup("process_daemonJUPI");
196 MSG_process_create_with_arguments("process_daemonJUPI", process_daemon, NULL, jupiter, 1, argvF);
198 MSG_process_suspend(daemon);
199 MSG_vm_set_bound(vm0, 90);
200 MSG_process_resume(daemon);
202 MSG_process_sleep(10);
204 XBT_INFO(" Turn Jupiter off");
205 MSG_host_off(jupiter);
206 XBT_INFO(" Shutdown vm0");
207 MSG_vm_shutdown(vm0);
208 XBT_INFO(" Destroy vm0");
210 XBT_INFO("Test 6 is also weird: when the node Jupiter is turned off once again, the VM and its daemon are not "
211 "killed. However, the issue regarding the shutdown of hosted VMs can be seen a feature not a bug ;)");
214 XBT_INFO(" Test done. See you!");
218 int main(int argc, char *argv[])
222 MSG_init(&argc, argv);
223 xbt_assert(argc == 3,"Usage: %s platform_file test_number\n\tExample: %s msg_platform.xml 1\n", argv[0], argv[0]);
227 xbt_dynar_t s_tests = xbt_str_split(argv[2], ",");
229 tests = xbt_dynar_new(sizeof(int), NULL);
230 xbt_dynar_foreach(s_tests, iter, groups) {
231 sscanf(xbt_dynar_get_as(s_tests, iter, char *), "%d", &tmp_test);
232 xbt_dynar_set_as(tests, iter, int, tmp_test);
234 xbt_dynar_free(&s_tests);
236 MSG_create_environment(argv[1]);
238 MSG_process_set_data_cleanup(task_cleanup_handler);
239 MSG_process_create("test_launcher", test_launcher, NULL, MSG_get_host_by_name("Tremblay"));
243 XBT_INFO("Simulation time %g", MSG_get_clock());
244 xbt_dynar_free(&tests);
246 return res != MSG_OK;