Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add a test program to see scalability issue.
[simgrid.git] / examples / msg / cloud / scale.c
1 /* Copyright (c) 2007-2012. The SimGrid Team. All rights reserved. */
2
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. */
5
6 #include <stdio.h>
7 #include <sys/time.h>
8 #include "msg/msg.h"
9 #include "xbt/sysdep.h"         /* calloc, printf */
10
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15                              "Messages specific for this msg example");
16
17 /*
18  * Usage:
19  * ./examples/msg/cloud/scale ../examples/platforms/g5k.xml
20  *
21  * 1. valgrind --tool=callgrind ./examples/msg/cloud/scale ../examples/platforms/g5k.xml
22  * 2. kcachegrind
23  **/
24
25 static double time_precise(void)
26 {
27         struct timeval tv;
28         int ret = gettimeofday(&tv, NULL);
29   if (ret < 0)
30     xbt_die("gettimeofday");
31
32         double now = (double) tv.tv_sec + tv.tv_usec * 0.001 * 0.001;
33
34         return now;
35 }
36
37 static int computation_fun(int argc, char *argv[])
38 {
39   for (;;) {
40     // double clock_sta = time_precise();
41
42     msg_task_t task = MSG_task_create("Task", 10000000, 0, NULL);
43     MSG_task_execute(task);
44     MSG_task_destroy(task);
45
46     // double clock_end = time_precise();
47
48     // XBT_INFO("%f", clock_end - clock_sta);
49   }
50
51
52   return 0;
53 }
54
55 static void launch_computation_worker(msg_host_t host)
56 {
57   MSG_process_create("compute", computation_fun, NULL, host);
58 }
59
60 #if 0
61 struct task_priv {
62   msg_host_t tx_host;
63   msg_process_t tx_proc;
64   double clock_sta;
65 };
66
67 static int communication_tx_fun(int argc, char *argv[])
68 {
69   xbt_assert(argc == 2);
70   const char *mbox = argv[1];
71
72   msg_task_t task = MSG_task_create("Task", 1000000, 1000000, NULL);
73
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();
78
79   MSG_task_set_data(task, priv);
80
81   MSG_task_send(task, mbox);
82
83   return 0;
84 }
85
86 static int communication_rx_fun(int argc, char *argv[])
87 {
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];
92
93   msg_task_t task = NULL;
94   MSG_task_recv(&task, mbox);
95
96   struct task_priv *priv = MSG_task_get_data(task);
97   double clock_end = MSG_get_clock();
98
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);
103
104   MSG_task_destroy(task);
105
106   return 0;
107 }
108
109 static void launch_communication_worker(msg_host_t tx_host, msg_host_t rx_host)
110 {
111   char *mbox = bprintf("MBOX:%s-%s",
112       MSG_host_get_name(tx_host),
113       MSG_host_get_name(rx_host));
114   char **argv = NULL;
115   
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);
120   argv[2] = NULL;
121
122   MSG_process_create_with_arguments(pr_name_tx, communication_tx_fun, NULL, tx_host, 2, argv);
123
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);
128   argv[2] = NULL;
129
130   MSG_process_create_with_arguments(pr_name_rx, communication_rx_fun, NULL, rx_host, 2, argv);
131
132   xbt_free(mbox);
133 }
134 #endif
135
136
137
138
139
140
141 static int master_main(int argc, char *argv[])
142 {
143   xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
144
145   int npm = 10;
146   int nvm = 1000;
147
148   msg_host_t *pm = xbt_new(msg_host_t, npm);
149   msg_vm_t   *vm = xbt_new(msg_vm_t, nvm);
150
151   int i = 0;
152   for (i = 0; i < npm; i++) {
153           pm[i] = xbt_dynar_get_as(hosts_dynar, i, msg_host_t);
154   }
155
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);
160           MSG_vm_start(vm[i]);
161
162     launch_computation_worker(vm[i]);
163
164           xbt_free(vm_name);
165   }
166
167
168   XBT_INFO("## Test (start)");
169
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);
175   }
176
177
178   for (i = 0; i < nvm; i++) {
179           MSG_vm_destroy(vm[i]);
180   }
181
182   XBT_INFO("## Test (ended)");
183   
184   return 0;
185 }
186
187 static void launch_master(msg_host_t host)
188 {
189   const char *pr_name = "master_";
190   char **argv = xbt_new(char *, 2);
191   argv[0] = xbt_strdup(pr_name);
192   argv[1] = NULL;
193
194   MSG_process_create_with_arguments(pr_name, master_main, NULL, host, 1, argv);
195 }
196
197
198 int main(int argc, char *argv[])
199 {
200   /* Get the arguments */
201   MSG_init(&argc, argv);
202
203   /* load the platform file */
204   xbt_assert(argc == 2);
205   MSG_create_environment(argv[1]);
206
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);
209   launch_master(pm0);
210
211   int res = MSG_main();
212   XBT_INFO("Bye (simulation time %g)", MSG_get_clock());
213
214
215   return !(res == MSG_OK);
216 }