Logo AND Algorithmique Numérique Distribuée

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