1 /* Copyright (c) 2007-2012. 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. */
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");
19 * ./examples/msg/cloud/scale ../examples/platforms/g5k.xml
21 * 1. valgrind --tool=callgrind ./examples/msg/cloud/scale ../examples/platforms/g5k.xml
25 static double time_precise(void)
28 int ret = gettimeofday(&tv, NULL);
30 xbt_die("gettimeofday");
32 double now = (double) tv.tv_sec + tv.tv_usec * 0.001 * 0.001;
37 static int computation_fun(int argc, char *argv[])
40 // double clock_sta = time_precise();
42 msg_task_t task = MSG_task_create("Task", 10000000, 0, NULL);
43 MSG_task_execute(task);
44 MSG_task_destroy(task);
46 // double clock_end = time_precise();
48 // XBT_INFO("%f", clock_end - clock_sta);
55 static void launch_computation_worker(msg_host_t host)
57 MSG_process_create("compute", computation_fun, NULL, host);
63 msg_process_t tx_proc;
67 static int communication_tx_fun(int argc, char *argv[])
69 xbt_assert(argc == 2);
70 const char *mbox = argv[1];
72 msg_task_t task = MSG_task_create("Task", 1000000, 1000000, NULL);
74 struct task_priv *priv = xbt_new(struct task_priv, 1);
75 priv->tx_proc = MSG_process_self();
76 priv->tx_host = MSG_host_self();
77 priv->clock_sta = MSG_get_clock();
79 MSG_task_set_data(task, priv);
81 MSG_task_send(task, mbox);
86 static int communication_rx_fun(int argc, char *argv[])
88 const char *pr_name = MSG_process_get_name(MSG_process_self());
89 const char *host_name = MSG_host_get_name(MSG_host_self());
90 xbt_assert(argc == 2);
91 const char *mbox = argv[1];
93 msg_task_t task = NULL;
94 MSG_task_recv(&task, mbox);
96 struct task_priv *priv = MSG_task_get_data(task);
97 double clock_end = MSG_get_clock();
99 XBT_INFO("%s:%s to %s:%s => %g sec",
100 MSG_host_get_name(priv->tx_host),
101 MSG_process_get_name(priv->tx_proc),
102 host_name, pr_name, clock_end - priv->clock_sta);
104 MSG_task_destroy(task);
109 static void launch_communication_worker(msg_host_t tx_host, msg_host_t rx_host)
111 char *mbox = bprintf("MBOX:%s-%s",
112 MSG_host_get_name(tx_host),
113 MSG_host_get_name(rx_host));
116 const char *pr_name_tx = "comm_tx";
117 argv = xbt_new(char *, 3);
118 argv[0] = xbt_strdup(pr_name_tx);
119 argv[1] = xbt_strdup(mbox);
122 MSG_process_create_with_arguments(pr_name_tx, communication_tx_fun, NULL, tx_host, 2, argv);
124 const char *pr_name_rx = "comm_rx";
125 argv = xbt_new(char *, 3);
126 argv[0] = xbt_strdup(pr_name_rx);
127 argv[1] = xbt_strdup(mbox);
130 MSG_process_create_with_arguments(pr_name_rx, communication_rx_fun, NULL, rx_host, 2, argv);
141 static int master_main(int argc, char *argv[])
143 xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
148 msg_host_t *pm = xbt_new(msg_host_t, npm);
149 msg_vm_t *vm = xbt_new(msg_vm_t, nvm);
152 for (i = 0; i < npm; i++) {
153 pm[i] = xbt_dynar_get_as(hosts_dynar, i, msg_host_t);
156 for (i = 0; i < nvm; i++) {
157 int pm_index = i % npm;
158 char *vm_name = bprintf("vm%d", i);
159 vm[i] = MSG_vm_create_core(pm[pm_index], vm_name);
162 launch_computation_worker(vm[i]);
168 XBT_INFO("## Test (start)");
170 for (i = 0; i < 10; i++) {
171 double clock_sta = time_precise();
172 MSG_process_sleep(1);
173 double clock_end = time_precise();
174 XBT_INFO("duration %f", clock_end - clock_sta);
178 for (i = 0; i < nvm; i++) {
179 MSG_vm_destroy(vm[i]);
182 XBT_INFO("## Test (ended)");
187 static void launch_master(msg_host_t host)
189 const char *pr_name = "master_";
190 char **argv = xbt_new(char *, 2);
191 argv[0] = xbt_strdup(pr_name);
194 MSG_process_create_with_arguments(pr_name, master_main, NULL, host, 1, argv);
198 int main(int argc, char *argv[])
200 /* Get the arguments */
201 MSG_init(&argc, argv);
203 /* load the platform file */
204 xbt_assert(argc == 2);
205 MSG_create_environment(argv[1]);
207 xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
208 msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
211 int res = MSG_main();
212 XBT_INFO("Bye (simulation time %g)", MSG_get_clock());
215 return !(res == MSG_OK);