1 /* Copyright (c) 2007-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. */
10 #include "xbt/sysdep.h" /* calloc, printf */
12 /* Create a log channel to have nice outputs. */
14 #include "xbt/asserts.h"
15 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
16 "Messages specific for this msg example");
20 * ./examples/msg/cloud/scale ../examples/platforms/g5k.xml
22 * 1. valgrind --tool=callgrind ./examples/msg/cloud/scale ../examples/platforms/g5k.xml
26 static double time_precise(void)
29 int ret = gettimeofday(&tv, NULL);
31 xbt_die("gettimeofday");
33 double now = (double) tv.tv_sec + tv.tv_usec * 0.001 * 0.001;
38 static int computation_fun(int argc, char *argv[])
41 // double clock_sta = time_precise();
43 msg_task_t task = MSG_task_create("Task", 10000000, 0, NULL);
44 MSG_task_execute(task);
45 MSG_task_destroy(task);
47 // double clock_end = time_precise();
49 // XBT_INFO("%f", clock_end - clock_sta);
56 static void launch_computation_worker(msg_host_t host)
58 MSG_process_create("compute", computation_fun, NULL, host);
64 msg_process_t tx_proc;
68 static int communication_tx_fun(int argc, char *argv[])
70 xbt_assert(argc == 2);
71 const char *mbox = argv[1];
73 msg_task_t task = MSG_task_create("Task", 1000000, 1000000, NULL);
75 struct task_priv *priv = xbt_new(struct task_priv, 1);
76 priv->tx_proc = MSG_process_self();
77 priv->tx_host = MSG_host_self();
78 priv->clock_sta = MSG_get_clock();
80 MSG_task_set_data(task, priv);
82 MSG_task_send(task, mbox);
87 static int communication_rx_fun(int argc, char *argv[])
89 const char *pr_name = MSG_process_get_name(MSG_process_self());
90 const char *host_name = MSG_host_get_name(MSG_host_self());
91 xbt_assert(argc == 2);
92 const char *mbox = argv[1];
94 msg_task_t task = NULL;
95 MSG_task_recv(&task, mbox);
97 struct task_priv *priv = MSG_task_get_data(task);
98 double clock_end = MSG_get_clock();
100 XBT_INFO("%s:%s to %s:%s => %g sec",
101 MSG_host_get_name(priv->tx_host),
102 MSG_process_get_name(priv->tx_proc),
103 host_name, pr_name, clock_end - priv->clock_sta);
105 MSG_task_destroy(task);
110 static void launch_communication_worker(msg_host_t tx_host, msg_host_t rx_host)
112 char *mbox = bprintf("MBOX:%s-%s",
113 MSG_host_get_name(tx_host),
114 MSG_host_get_name(rx_host));
117 const char *pr_name_tx = "comm_tx";
118 argv = xbt_new(char *, 3);
119 argv[0] = xbt_strdup(pr_name_tx);
120 argv[1] = xbt_strdup(mbox);
123 MSG_process_create_with_arguments(pr_name_tx, communication_tx_fun, NULL, tx_host, 2, argv);
125 const char *pr_name_rx = "comm_rx";
126 argv = xbt_new(char *, 3);
127 argv[0] = xbt_strdup(pr_name_rx);
128 argv[1] = xbt_strdup(mbox);
131 MSG_process_create_with_arguments(pr_name_rx, communication_rx_fun, NULL, rx_host, 2, argv);
142 static int master_main(int argc, char *argv[])
144 xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
149 msg_host_t *pm = xbt_new(msg_host_t, npm);
150 msg_vm_t *vm = xbt_new(msg_vm_t, nvm);
153 for (i = 0; i < npm; i++) {
154 pm[i] = xbt_dynar_get_as(hosts_dynar, i, msg_host_t);
157 for (i = 0; i < nvm; i++) {
158 int pm_index = i % npm;
159 char *vm_name = bprintf("vm%d", i);
160 vm[i] = MSG_vm_create_core(pm[pm_index], vm_name);
163 launch_computation_worker(vm[i]);
169 XBT_INFO("## Test (start)");
171 for (i = 0; i < 10; i++) {
172 double clock_sta = time_precise();
173 MSG_process_sleep(1);
174 double clock_end = time_precise();
175 XBT_INFO("duration %f", clock_end - clock_sta);
179 for (i = 0; i < nvm; i++) {
180 MSG_vm_destroy(vm[i]);
183 XBT_INFO("## Test (ended)");
188 static void launch_master(msg_host_t host)
190 const char *pr_name = "master_";
191 char **argv = xbt_new(char *, 2);
192 argv[0] = xbt_strdup(pr_name);
195 MSG_process_create_with_arguments(pr_name, master_main, NULL, host, 1, argv);
199 int main(int argc, char *argv[])
201 /* Get the arguments */
202 MSG_init(&argc, argv);
204 /* load the platform file */
205 xbt_assert(argc == 2);
206 MSG_create_environment(argv[1]);
208 xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
209 msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
212 int res = MSG_main();
213 XBT_INFO("Bye (simulation time %g)", MSG_get_clock());
216 return !(res == MSG_OK);