1 /* Copyright (c) 2007-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"
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");
18 static int computation_fun(int argc, char *argv[])
20 const char *pr_name = MSG_process_get_name(MSG_process_self());
21 const char *host_name = MSG_host_get_name(MSG_host_self());
23 msg_task_t task = MSG_task_create("Task", 1000000, 1000000, NULL);
25 double clock_sta = MSG_get_clock();
26 MSG_task_execute(task);
27 double clock_end = MSG_get_clock();
29 XBT_INFO("%s:%s task executed %g", host_name, pr_name, clock_end - clock_sta);
31 MSG_task_destroy(task);
36 static void launch_computation_worker(msg_host_t host)
38 const char *pr_name = "compute";
39 char **argv = xbt_new(char *, 2);
40 argv[0] = xbt_strdup(pr_name);
43 MSG_process_create_with_arguments(pr_name, computation_fun, NULL, host, 1, argv);
48 msg_process_t tx_proc;
52 static int communication_tx_fun(int argc, char *argv[])
54 xbt_assert(argc == 2);
55 const char *mbox = argv[1];
57 msg_task_t task = MSG_task_create("Task", 1000000, 1000000, NULL);
59 struct task_priv *priv = xbt_new(struct task_priv, 1);
60 priv->tx_proc = MSG_process_self();
61 priv->tx_host = MSG_host_self();
62 priv->clock_sta = MSG_get_clock();
64 MSG_task_set_data(task, priv);
66 MSG_task_send(task, mbox);
71 static int communication_rx_fun(int argc, char *argv[])
73 const char *pr_name = MSG_process_get_name(MSG_process_self());
74 const char *host_name = MSG_host_get_name(MSG_host_self());
75 xbt_assert(argc == 2);
76 const char *mbox = argv[1];
78 msg_task_t task = NULL;
79 MSG_task_recv(&task, mbox);
81 struct task_priv *priv = MSG_task_get_data(task);
82 double clock_end = MSG_get_clock();
84 XBT_INFO("%s:%s to %s:%s => %g sec",
85 MSG_host_get_name(priv->tx_host),
86 MSG_process_get_name(priv->tx_proc),
87 host_name, pr_name, clock_end - priv->clock_sta);
90 MSG_task_destroy(task);
95 static void launch_communication_worker(msg_host_t tx_host, msg_host_t rx_host)
97 char *mbox = bprintf("MBOX:%s-%s",
98 MSG_host_get_name(tx_host),
99 MSG_host_get_name(rx_host));
102 const char *pr_name_tx = "comm_tx";
103 argv = xbt_new(char *, 3);
104 argv[0] = xbt_strdup(pr_name_tx);
105 argv[1] = xbt_strdup(mbox);
108 MSG_process_create_with_arguments(pr_name_tx, communication_tx_fun, NULL, tx_host, 2, argv);
110 const char *pr_name_rx = "comm_rx";
111 argv = xbt_new(char *, 3);
112 argv[0] = xbt_strdup(pr_name_rx);
113 argv[1] = xbt_strdup(mbox);
116 MSG_process_create_with_arguments(pr_name_rx, communication_rx_fun, NULL, rx_host, 2, argv);
122 static int master_main(int argc, char *argv[])
124 xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
125 msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
126 msg_host_t pm1 = xbt_dynar_get_as(hosts_dynar, 1, msg_host_t);
127 msg_host_t pm2 = xbt_dynar_get_as(hosts_dynar, 2, msg_host_t);
131 XBT_INFO("## Test 1 (started): check computation on normal PMs");
133 XBT_INFO("### Put a task on a PM");
134 launch_computation_worker(pm0);
135 MSG_process_sleep(2);
137 XBT_INFO("### Put two tasks on a PM");
138 launch_computation_worker(pm0);
139 launch_computation_worker(pm0);
140 MSG_process_sleep(2);
142 XBT_INFO("### Put a task on each PM");
143 launch_computation_worker(pm0);
144 launch_computation_worker(pm1);
145 MSG_process_sleep(2);
147 XBT_INFO("## Test 1 (ended)");
150 XBT_INFO("## Test 2 (started): check impact of running a task inside a VM (there is no degradation for the moment)");
152 XBT_INFO("### Put a VM on a PM, and put a task to the VM");
153 vm0 = MSG_vm_create_core(pm0, "VM0");
155 launch_computation_worker(vm0);
156 MSG_process_sleep(2);
159 XBT_INFO("## Test 2 (ended)");
162 XBT_INFO("## Test 3 (started): check impact of running a task collocated with a VM (there is no VM noise for the moment)");
164 XBT_INFO("### Put a VM on a PM, and put a task to the PM");
165 vm0 = MSG_vm_create_core(pm0, "VM0");
167 launch_computation_worker(pm0);
168 MSG_process_sleep(2);
171 XBT_INFO("## Test 3 (ended)");
174 XBT_INFO("## Test 4 (started): compare the cost of running two tasks inside two different VMs collocated or not (for the moment, there is no degradation for the VMs. Hence, the time should be equals to the time of test 1");
176 XBT_INFO("### Put two VMs on a PM, and put a task to each VM");
177 vm0 = MSG_vm_create_core(pm0, "VM0");
178 vm1 = MSG_vm_create_core(pm0, "VM1");
181 launch_computation_worker(vm0);
182 launch_computation_worker(vm1);
183 MSG_process_sleep(2);
187 XBT_INFO("### Put a VM on each PM, and put a task to each VM");
188 vm0 = MSG_vm_create_core(pm0, "VM0");
189 vm1 = MSG_vm_create_core(pm1, "VM1");
192 launch_computation_worker(vm0);
193 launch_computation_worker(vm1);
194 MSG_process_sleep(2);
197 XBT_INFO("## Test 4 (ended)");
200 XBT_INFO("## Test 5 (started): Analyse network impact");
201 XBT_INFO("### Make a connection between PM0 and PM1");
202 launch_communication_worker(pm0, pm1);
203 MSG_process_sleep(5);
205 XBT_INFO("### Make two connection between PM0 and PM1");
206 launch_communication_worker(pm0, pm1);
207 launch_communication_worker(pm0, pm1);
208 MSG_process_sleep(5);
210 XBT_INFO("### Make a connection between PM0 and VM0@PM0");
211 vm0 = MSG_vm_create_core(pm0, "VM0");
213 launch_communication_worker(pm0, vm0);
214 MSG_process_sleep(5);
217 XBT_INFO("### Make a connection between PM0 and VM0@PM1");
218 vm0 = MSG_vm_create_core(pm1, "VM0");
220 launch_communication_worker(pm0, vm0);
221 MSG_process_sleep(5);
224 XBT_INFO("### Make two connections between PM0 and VM0@PM1");
225 vm0 = MSG_vm_create_core(pm1, "VM0");
227 launch_communication_worker(pm0, vm0);
228 launch_communication_worker(pm0, vm0);
229 MSG_process_sleep(5);
232 XBT_INFO("### Make a connection between PM0 and VM0@PM1, and also make a connection between PM0 and PM1");
233 vm0 = MSG_vm_create_core(pm1, "VM0");
235 launch_communication_worker(pm0, vm0);
236 launch_communication_worker(pm0, pm1);
237 MSG_process_sleep(5);
240 XBT_INFO("### Make a connection between VM0@PM0 and PM1@PM1, and also make a connection between VM0@PM0 and VM1@PM1");
241 vm0 = MSG_vm_create_core(pm0, "VM0");
242 vm1 = MSG_vm_create_core(pm1, "VM1");
245 launch_communication_worker(vm0, vm1);
246 launch_communication_worker(vm0, vm1);
247 MSG_process_sleep(5);
251 XBT_INFO("## Test 5 (ended)");
254 XBT_INFO("## Test 6 (started): Check migration impact (not yet implemented neither on the CPU resource nor on the network one");
255 XBT_INFO("### Relocate VM0 between PM0 and PM1");
256 vm0 = MSG_vm_create_core(pm0, "VM0");
258 s_vm_params_t params;
259 memset(¶ms, 0, sizeof(params));
260 params.ramsize = 1L * 1024 * 1024 * 1024; // 1Gbytes
261 MSG_host_set_params(vm0, ¶ms);
264 launch_communication_worker(vm0, pm2);
265 MSG_process_sleep(0.01);
266 MSG_vm_migrate(vm0, pm1);
267 MSG_process_sleep(0.01);
268 MSG_vm_migrate(vm0, pm0);
269 MSG_process_sleep(5);
271 XBT_INFO("## Test 6 (ended)");
273 xbt_dynar_free(&hosts_dynar);
277 static void launch_master(msg_host_t host)
279 const char *pr_name = "master_";
280 char **argv = xbt_new(char *, 2);
281 argv[0] = xbt_strdup(pr_name);
284 MSG_process_create_with_arguments(pr_name, master_main, NULL, host, 1, argv);
288 int main(int argc, char *argv[])
290 /* Get the arguments */
291 MSG_init(&argc, argv);
293 /* load the platform file */
294 xbt_assert(argc == 2);
295 MSG_create_environment(argv[1]);
297 xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
298 msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
301 int res = MSG_main();
302 XBT_INFO("Bye (simulation time %g)", MSG_get_clock());
303 xbt_dynar_free(&hosts_dynar);
305 return !(res == MSG_OK);