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. */
7 #include "simgrid/msg.h" /* Yeah! If you want to use msg, you need to include simgrid/msg.h */
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
14 static int process_daemon(int argc, char *argv[])
16 msg_task_t task = NULL;
17 XBT_INFO(" Start daemon on %s (%f)", MSG_host_get_name(MSG_host_self()), MSG_get_host_speed(MSG_host_self()));
19 task = MSG_task_create("daemon", MSG_get_host_speed(MSG_host_self()), 0, NULL);
20 XBT_INFO(" Execute daemon");
21 MSG_task_execute(task);
22 MSG_task_destroy(task);
25 XBT_INFO(" daemon done. See you!");
29 static int process_sleep(int argc, char *argv[])
32 XBT_INFO(" I'm alive but I should sleep");
33 MSG_process_sleep(10);
35 XBT_INFO(" I'm done. See you!");
39 static int commTX(int argc, char *argv[])
41 msg_task_t task = NULL;
43 sprintf(mailbox, "comm");
44 XBT_INFO(" Start TX");
45 task = MSG_task_create("COMM", 0, 100000000, NULL);
46 MSG_task_isend(task, mailbox);
47 // We should wait a bit (if not the process will end before the communication, hence an exception on the other side).
48 MSG_process_sleep(30);
53 static int commRX(int argc, char *argv[])
55 msg_task_t task = NULL;
57 sprintf(mailbox, "comm");
58 XBT_INFO(" Start RX");
59 msg_error_t error = MSG_task_receive(&(task), mailbox);
61 XBT_INFO(" Receive message: %s", task->name);
62 } else if (error==MSG_HOST_FAILURE) {
63 XBT_INFO(" Receive message: HOST_FAILURE");
64 } else if (error==MSG_TRANSFER_FAILURE) {
65 XBT_INFO(" Receive message: TRANSFERT_FAILURE");
67 XBT_INFO(" Receive message: %d", error);
73 static int test_launcher(int argc, char *argv[])
77 argvF = xbt_new(char*, 2);
78 argvF[0] = xbt_strdup("process_daemon");
79 msg_host_t jupiter = MSG_host_by_name("Jupiter");
82 // Create a process running a simple task on a host and turn the host off during the execution of the process.
83 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
85 XBT_INFO(" Create a process on Jupiter");
86 argvF = xbt_new(char*, 2);
87 argvF[0] = xbt_strdup("process_daemon");
88 MSG_process_create_with_arguments("process_daemon", process_daemon, NULL, jupiter, 1, argvF);
90 XBT_INFO(" Turn off Jupiter");
91 MSG_host_off(jupiter);
92 MSG_process_sleep(10);
93 XBT_INFO("Test 1 seems ok, cool !(#Processes: %d, it should be 1; #tasks: %d)", MSG_process_get_number(), tasks_done);
97 // Create a process that on a host that is turned off (this should not be possible)
98 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
100 XBT_INFO(" Turn off Jupiter");
101 // adsein: Jupiter is already, hence nothing should happen
102 // adsein: This can be one additional test, to check that you cannot shutdown twice a host
103 MSG_host_off(jupiter);
104 argvF = xbt_new(char*, 2);
105 argvF[0] = xbt_strdup("process_daemon");
106 MSG_process_create_with_arguments("process_daemon", process_daemon, NULL, jupiter, 1, argvF);
107 MSG_process_sleep(10);
108 XBT_INFO(" Test 2 does not crash, WTF ?!(number of Process : %d, it should be 1)", MSG_process_get_number());
109 XBT_INFO(" Ok so let's turn on/off the node to see whether the process is correctly bound to Jupiter");
110 MSG_host_on(jupiter);
111 XBT_INFO(" Turn off");
112 MSG_host_off(jupiter);
114 MSG_process_sleep(10);
115 XBT_INFO("number of Process : %d it should be 1. The daemon that has been created for test2 has been correctly "
116 "destroyed....ok at least it looks rigorous, cool ! You just have to disallow the possibility to create "
117 "a new process on a node when the node is off.)", MSG_process_get_number());
121 // Create a process running sucessive sleeps on a host and turn the host off during the execution of the process.
122 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
124 MSG_host_on(jupiter);
125 argvF = xbt_new(char*, 2);
126 argvF[0] = xbt_strdup("process_sleep");
127 MSG_process_create_with_arguments("process_sleep", process_sleep, NULL, jupiter, 1, argvF);
128 MSG_process_sleep(100);
129 XBT_INFO(" Turn off");
130 MSG_host_off(jupiter);
131 XBT_INFO(" sleep for 10 seconds");
132 MSG_process_sleep(10000);
133 XBT_INFO("number of Process : %d it should be 1 (i.e. the Test one))", MSG_process_get_number());
137 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
138 XBT_INFO("Test 4 (turn off src during a communication) : Create a Process/task to make a communication between "
139 "Jupiter and Tremblay and turn off Jupiter during the communication");
140 MSG_host_on(jupiter);
141 MSG_process_sleep(10);
142 argvF = xbt_new(char*, 2);
143 argvF[0] = xbt_strdup("commRX");
144 MSG_process_create_with_arguments("commRX", commRX, NULL, MSG_host_by_name("Tremblay"), 1, argvF);
145 argvF = xbt_new(char*, 2);
146 argvF[0] = xbt_strdup("commTX");
147 MSG_process_create_with_arguments("commTX", commTX, NULL, jupiter, 1, argvF);
148 XBT_INFO(" number of processes: %d", MSG_process_get_number());
149 MSG_process_sleep(10);
150 XBT_INFO(" Turn Jupiter off");
151 MSG_host_off(jupiter);
152 XBT_INFO("Test 4 seems ok (number of Process : %d, it should be 1 or 2 if RX has not been satisfied) cool, you "
153 "can now turn off a node that has a process paused by a sleep call", MSG_process_get_number());
157 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
158 XBT_INFO("Test 5 (turn off dest during a communication : Create a Process/task to make a communication between "
159 "Tremblay and Jupiter and turn off Jupiter during the communication");
160 MSG_host_on(jupiter);
161 MSG_process_sleep(10);
162 argvF = xbt_new(char*, 2);
163 argvF[0] = xbt_strdup("commRX");
164 MSG_process_create_with_arguments("commRX", commRX, NULL, jupiter, 1, argvF);
165 argvF = xbt_new(char*, 2);
166 argvF[0] = xbt_strdup("commTX");
167 MSG_process_create_with_arguments("commTX", commTX, NULL, MSG_host_by_name("Tremblay"), 1, argvF);
168 XBT_INFO(" number of processes: %d", MSG_process_get_number());
169 MSG_process_sleep(10);
170 XBT_INFO(" Turn Jupiter off");
171 MSG_host_off(jupiter);
172 XBT_INFO("Test 5 seems ok, cool !(number of Process : %d, it should be 2", MSG_process_get_number());
176 if (xbt_dynar_search_or_negative(tests, &test)!=-1){
177 XBT_INFO("Test 6: Turn on Jupiter, assign a VM on Jupiter, launch a process inside the VM, and turn off the node");
182 msg_process_t daemon;
184 vm0 = MSG_vm_create (jupiter, "vm0", 1, 2048, 125, NULL, -1, 125, dpRate);
187 argvF = xbt_new(char*, 2);
188 argvF[0] = xbt_strdup("process_daemon");
189 daemon = MSG_process_create_with_arguments("process_daemon", process_daemon, NULL, vm0, 1, argvF);
191 argvF = xbt_new(char*, 2);
192 argvF[0] = xbt_strdup("process_daemonJUPI");
193 MSG_process_create_with_arguments("process_daemonJUPI", process_daemon, NULL, jupiter, 1, argvF);
195 MSG_process_suspend(daemon);
196 MSG_vm_set_bound(vm0, 90);
197 MSG_process_resume(daemon);
199 MSG_process_sleep(10);
201 XBT_INFO(" Turn Jupiter off");
202 MSG_host_off(jupiter);
203 XBT_INFO(" Shutdown vm0");
204 MSG_vm_shutdown(vm0);
205 XBT_INFO(" Destroy vm0");
207 XBT_INFO("Test 6 is also weird: when the node Jupiter is turned off once again, the VM and its daemon are not "
208 "killed. However, the issue regarding the shutdown of hosted VMs can be seen a feature not a bug ;)");
211 XBT_INFO(" Test done. See you!");
215 int main(int argc, char *argv[])
219 MSG_init(&argc, argv);
220 xbt_assert(argc > 3,"Usage: %s platform_file deployment_file test_number\n"
221 "\tExample: %s msg_platform.xml msg_deployment.xml 1\n", argv[0], argv[0]);
225 xbt_dynar_t s_tests = xbt_str_split(argv[3], ",");
227 tests = xbt_dynar_new(sizeof(int), NULL);
228 xbt_dynar_foreach(s_tests, iter, groups) {
229 sscanf(xbt_dynar_get_as(s_tests, iter, char *), "%d", &tmp_test);
230 xbt_dynar_set_as(tests, iter, int, tmp_test);
232 xbt_dynar_free(&s_tests);
234 MSG_create_environment(argv[1]);
236 MSG_function_register("test_launcher", test_launcher);
237 MSG_function_register("process_daemon", process_daemon);
238 MSG_function_register("process_sleep", process_sleep);
240 MSG_launch_application(argv[2]);
244 XBT_INFO("Simulation time %g", MSG_get_clock());
246 return res != MSG_OK;