1 /* Copyright (c) 2010-2014. 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 "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/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(int argc, char *argv[]);
18 int daemon(int argc, char *argv[]);
19 int commRX(int argc, char *argv[]);
20 int commTX(int argc, char *argv[]);
24 int test(int argc, char *argv[])
28 argvF = xbt_new(char*, 2);
29 argvF[0] = xbt_strdup("daemon");
30 msg_host_t jupiter = MSG_get_host_by_name("Jupiter");
33 if (xbt_dynar_member(tests, &test)){
35 XBT_INFO(" Create a process on Jupiter");
36 argvF = xbt_new(char*, 2);
37 argvF[0] = xbt_strdup("daemon");
38 MSG_process_create_with_arguments("daemon", daemon, NULL, jupiter, 1, argvF);
39 XBT_INFO(" Turn off Jupiter");
40 MSG_host_off(jupiter);
41 MSG_process_sleep(10);
42 XBT_INFO("Test 1 seems ok, cool !(number of Process : %d, it should be 1 (i.e. the Test one))", MSG_process_get_number());
46 if (xbt_dynar_member(tests, &test)){
48 XBT_INFO(" Turn off Jupiter");
49 MSG_host_off(jupiter);
50 argvF = xbt_new(char*, 2);
51 argvF[0] = xbt_strdup("daemon");
52 MSG_process_create_with_arguments("daemon", daemon, NULL, jupiter, 1, argvF);
53 MSG_process_sleep(10);
54 XBT_INFO(" Test 2 does not crash, WTF ?!(number of Process : %d, it should be 1)", MSG_process_get_number());
55 XBT_INFO(" Ok so let's turn on/off the node to see whether the process is correctly bound to Jupiter");
57 XBT_INFO(" Turn off");
58 MSG_host_off(jupiter);
60 MSG_process_sleep(10);
61 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());
65 if (xbt_dynar_member(tests, &test)){
66 XBT_INFO("Test 3 (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");
68 MSG_process_sleep(10);
69 argvF = xbt_new(char*, 2);
70 argvF[0] = xbt_strdup("commRX");
71 MSG_process_create_with_arguments("commRX", commRX, NULL, MSG_get_host_by_name("Tremblay"), 1, argvF);
72 argvF = xbt_new(char*, 2);
73 argvF[0] = xbt_strdup("commTX");
74 MSG_process_create_with_arguments("commTX", commTX, NULL, MSG_get_host_by_name("Jupiter"), 1, argvF);
75 XBT_INFO(" number of processes: %d", MSG_process_get_number());
76 MSG_process_sleep(10);
77 XBT_INFO(" Turn Jupiter off");
78 MSG_host_off(jupiter);
79 XBT_INFO("Test 3 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());
83 if (xbt_dynar_member(tests, &test)){
84 XBT_INFO("Test 4 (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");
86 MSG_process_sleep(10);
87 argvF = xbt_new(char*, 2);
88 argvF[0] = xbt_strdup("commTX");
89 MSG_process_create_with_arguments("commTX", commTX, NULL, MSG_get_host_by_name("Tremblay"), 1, argvF);
90 argvF = xbt_new(char*, 2);
91 argvF[0] = xbt_strdup("commRX");
92 MSG_process_create_with_arguments("commRX", commRX, NULL, MSG_get_host_by_name("Jupiter"), 1, argvF);
93 MSG_host_off(jupiter);
94 XBT_INFO("Test 4 seems ok, cool !(number of Process : %d, it should be 1", MSG_process_get_number());
98 if (xbt_dynar_member(tests, &test)){
103 if (xbt_dynar_member(tests, &test)){
108 if (xbt_dynar_member(tests, &test)){
113 if (xbt_dynar_member(tests, &test)){
120 int daemon(int argc, char *argv[])
122 msg_task_t task = NULL;
123 task = MSG_task_create("deamon", 100*MSG_get_host_speed(MSG_host_self()), 0, NULL);
124 XBT_INFO(" Execute deamon");
125 MSG_task_execute(task);
126 XBT_INFO(" I'm done. See you!");
130 int commTX(int argc, char *argv[])
132 msg_task_t task = NULL;
134 sprintf(mailbox, "comm");
135 XBT_INFO(" Start TX");
136 task = MSG_task_create("COMM", 0, 100000000, NULL);
137 MSG_task_isend(task, mailbox);
138 // We should wait a bit (if not the process will end before the communication, leading to an exception at the other side).
139 MSG_process_sleep(30);
140 XBT_INFO(" TX done");
144 int commRX(int argc, char *argv[])
146 msg_task_t task = NULL;
148 sprintf(mailbox, "comm");
149 XBT_INFO(" Start RX");
150 msg_error_t error = MSG_task_receive(&(task), mailbox);
152 XBT_INFO(" Receive message: %s", task->name);
153 } else if (error==MSG_HOST_FAILURE) {
154 XBT_INFO(" Receive message: HOST_FAILURE");
155 } else if (error==MSG_TRANSFER_FAILURE) {
156 XBT_INFO(" Receive message: TRANSFERT_FAILURE");
158 XBT_INFO(" Receive message: %d", error);
160 XBT_INFO(" RX Done");
165 int main(int argc, char *argv[])
168 const char *platform_file;
169 const char *application_file;
171 MSG_init(&argc, argv);
173 printf("Usage: %s platform_file deployment_file test_number\n", argv[0]);
174 printf("example: %s msg_platform.xml msg_deployment.xml 1\n", argv[0]);
180 xbt_dynar_t s_tests = xbt_str_split(argv[3], ",");
182 tests = xbt_dynar_new(sizeof(int), NULL);
183 xbt_dynar_foreach(s_tests, iter, groups) {
184 sscanf(xbt_dynar_get_as(s_tests, iter, char *), "%d", &tmp_test);
185 xbt_dynar_set_as(tests, iter, int, tmp_test);
188 platform_file = argv[1];
189 application_file = argv[2];
191 /* MSG_config("workstation/model","KCCFLN05"); */
192 { /* Simulation setting */
193 MSG_create_environment(platform_file);
195 { /* Application deployment */
196 MSG_function_register("test", test);
197 MSG_function_register("daemon", daemon);
199 MSG_launch_application(application_file);
203 XBT_INFO("Simulation time %g", MSG_get_clock());