1 /* Copyright (c) 2010-2019. 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/Exception.hpp"
7 #include "simgrid/msg.h"
9 #include <stdio.h> /* sscanf */
11 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
16 static void task_cleanup_handler(void* task)
19 MSG_task_destroy(static_cast<msg_task_t>(task));
22 static int process_daemon(int /*argc*/, char** /*argv*/)
24 msg_process_t self = MSG_process_self();
25 XBT_INFO(" Start daemon on %s (%f)", MSG_host_get_name(MSG_host_self()), MSG_host_get_speed(MSG_host_self()));
27 msg_task_t task = MSG_task_create("daemon", MSG_host_get_speed(MSG_host_self()), 0, NULL);
28 MSG_process_set_data(self, task);
29 XBT_INFO(" Execute daemon");
30 msg_error_t res = MSG_task_execute(task);
31 MSG_task_destroy(task);
33 if (res == MSG_HOST_FAILURE) {
34 XBT_INFO("Host has died as expected, do nothing else");
38 XBT_INFO(" daemon done. See you!");
42 static int commTX(int /*argc*/, char** /*argv*/)
44 const char* mailbox = "comm";
45 XBT_INFO(" Start TX");
46 msg_task_t task = MSG_task_create("COMM", 0, 100000000, NULL);
47 MSG_task_dsend(task, mailbox, task_cleanup_handler);
48 // We should wait a bit (if not the process will end before the communication, hence an exception on the other side).
50 MSG_process_sleep(30);
51 } catch (simgrid::HostFailureException& e) {
52 XBT_INFO("The host has died ... as expected.");
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);
64 if (error == MSG_OK) {
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: TRANSFER_FAILURE");
72 XBT_INFO(" Receive message: %u", static_cast<unsigned int>(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(),
101 // Create a process that on a host that is turned off (this should not be possible)
102 if (xbt_dynar_search_or_negative(tests, &test) != -1) {
104 XBT_INFO(" Turn off Jupiter");
105 // adsein: Jupiter is already, hence nothing should happen
106 // adsein: This can be one additional test, to check that you cannot shutdown twice a host
107 MSG_host_off(jupiter);
108 argvF = xbt_new(char*, 2);
109 argvF[0] = xbt_strdup("process_daemon");
110 MSG_process_create_with_arguments("process_daemon", process_daemon, NULL, jupiter, 1, argvF);
111 MSG_process_sleep(10);
112 XBT_INFO(" Test 2 does not crash as it should (number of Process : %d, it should be 1)", MSG_process_get_number());
113 XBT_INFO(" Ok so let's turn on/off the node to see whether the process is correctly bound to Jupiter");
114 MSG_host_on(jupiter);
115 XBT_INFO(" Turn off");
116 MSG_host_off(jupiter);
118 MSG_process_sleep(10);
119 XBT_INFO("number of Process : %d it should be 1. The daemon that has been created for test2 has been correctly "
120 "destroyed....ok at least it looks rigorous, cool ! You just have to disallow the possibility to create "
121 "a new process on a node when the node is off.)",
122 MSG_process_get_number());
126 // Create a process running sucessive sleeps on a host and turn the host off during the execution of the process.
127 if (xbt_dynar_search_or_negative(tests, &test) != -1) {
128 xbt_die("Test 3 is superseeded by activity-lifecycle");
132 if (xbt_dynar_search_or_negative(tests, &test) != -1) {
133 XBT_INFO("Test 4 (turn off src during a communication) : Create a Process/task to make a communication between "
134 "Jupiter and Tremblay and turn off Jupiter during the communication");
135 MSG_host_on(jupiter);
136 MSG_process_sleep(10);
137 argvF = xbt_new(char*, 2);
138 argvF[0] = xbt_strdup("commRX");
139 MSG_process_create_with_arguments("commRX", commRX, NULL, MSG_host_by_name("Tremblay"), 1, argvF);
140 argvF = xbt_new(char*, 2);
141 argvF[0] = xbt_strdup("commTX");
142 MSG_process_create_with_arguments("commTX", commTX, NULL, jupiter, 1, argvF);
143 XBT_INFO(" number of processes: %d", MSG_process_get_number());
144 MSG_process_sleep(10);
145 XBT_INFO(" Turn Jupiter off");
146 MSG_host_off(jupiter);
147 XBT_INFO("Test 4 is ok. (number of Process : %d, it should be 1 or 2 if RX has not been satisfied)."
148 " An exception is raised when we turn off a node that has a process sleeping",
149 MSG_process_get_number());
153 if (xbt_dynar_search_or_negative(tests, &test) != -1) {
154 XBT_INFO("Test 5 (turn off dest during a communication : Create a Process/task to make a communication between "
155 "Tremblay and Jupiter and turn off Jupiter during the communication");
156 MSG_host_on(jupiter);
157 MSG_process_sleep(10);
158 argvF = xbt_new(char*, 2);
159 argvF[0] = xbt_strdup("commRX");
160 MSG_process_create_with_arguments("commRX", commRX, NULL, jupiter, 1, argvF);
161 argvF = xbt_new(char*, 2);
162 argvF[0] = xbt_strdup("commTX");
163 MSG_process_create_with_arguments("commTX", commTX, NULL, MSG_host_by_name("Tremblay"), 1, argvF);
164 XBT_INFO(" number of processes: %d", MSG_process_get_number());
165 MSG_process_sleep(10);
166 XBT_INFO(" Turn Jupiter off");
167 MSG_host_off(jupiter);
168 XBT_INFO("Test 5 seems ok (number of Process: %d, it should be 2)", MSG_process_get_number());
172 if (xbt_dynar_search_or_negative(tests, &test) != -1) {
173 XBT_INFO("Test 6: Turn on Jupiter, assign a VM on Jupiter, launch a process inside the VM, and turn off the node");
176 msg_vm_t vm0 = MSG_vm_create_core(jupiter, "vm0");
179 argvF = xbt_new(char*, 2);
180 argvF[0] = xbt_strdup("process_daemon");
181 msg_process_t daemon =
182 MSG_process_create_with_arguments("process_daemon", process_daemon, NULL, (msg_host_t)vm0, 1, argvF);
184 argvF = xbt_new(char*, 2);
185 argvF[0] = xbt_strdup("process_daemonJUPI");
186 MSG_process_create_with_arguments("process_daemonJUPI", process_daemon, NULL, jupiter, 1, argvF);
188 MSG_process_suspend(daemon);
189 MSG_vm_set_bound(vm0, 90);
190 MSG_process_resume(daemon);
192 MSG_process_sleep(10);
194 XBT_INFO(" Turn Jupiter off");
195 MSG_host_off(jupiter);
196 XBT_INFO(" Shutdown vm0");
197 MSG_vm_shutdown(vm0);
198 XBT_INFO(" Destroy vm0");
200 XBT_INFO("Test 6 is also weird: when the node Jupiter is turned off once again, the VM and its daemon are not "
201 "killed. However, the issue regarding the shutdown of hosted VMs can be seen a feature not a bug ;)");
204 XBT_INFO(" Test done. See you!");
208 int main(int argc, char* argv[])
212 MSG_init(&argc, argv);
213 xbt_assert(argc == 3, "Usage: %s platform_file test_number\n\tExample: %s msg_platform.xml 1\n", argv[0], argv[0]);
217 xbt_dynar_t s_tests = xbt_str_split(argv[2], ",");
219 tests = xbt_dynar_new(sizeof(int), NULL);
220 xbt_dynar_foreach (s_tests, iter, groups) {
221 sscanf(xbt_dynar_get_as(s_tests, iter, char*), "%d", &tmp_test);
222 xbt_dynar_set_as(tests, iter, int, tmp_test);
224 xbt_dynar_free(&s_tests);
226 MSG_create_environment(argv[1]);
228 MSG_process_create("test_launcher", test_launcher, NULL, MSG_get_host_by_name("Tremblay"));
232 XBT_INFO("Simulation time %g", MSG_get_clock());
233 xbt_dynar_free(&tests);
235 return res != MSG_OK;