Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
sed -i -e 's/\t/ /g' [sources] Please people, stop using tabs
[simgrid.git] / examples / msg / cloud / scale.c
1 /* Copyright (c) 2007-2015. 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 "simgrid/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   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   for (;;) {
39     // double clock_sta = time_precise();
40
41     msg_task_t task = MSG_task_create("Task", 10000000, 0, NULL);
42     MSG_task_execute(task);
43     MSG_task_destroy(task);
44
45     // double clock_end = time_precise();
46
47     // XBT_INFO("%f", clock_end - clock_sta);
48   }
49
50
51   return 0;
52 }
53
54 static void launch_computation_worker(msg_host_t host) {
55   MSG_process_create("compute", computation_fun, NULL, host);
56 }
57
58 static int master_main(int argc, char *argv[])
59 {
60   xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
61
62   int npm = 10;
63   int nvm = 1000;
64
65   msg_host_t *pm = xbt_new(msg_host_t, npm);
66   msg_vm_t   *vm = xbt_new(msg_vm_t, nvm);
67
68   int i = 0;
69   for (i = 0; i < npm; i++) {
70     pm[i] = xbt_dynar_get_as(hosts_dynar, i, msg_host_t);
71   }
72
73   for (i = 0; i < nvm; i++) {
74     int pm_index = i % npm;
75     char *vm_name = bprintf("vm%d", i);
76     vm[i] = MSG_vm_create_core(pm[pm_index], vm_name);
77     MSG_vm_start(vm[i]);
78
79     launch_computation_worker(vm[i]);
80
81     xbt_free(vm_name);
82   }
83
84
85   XBT_INFO("## Test (start)");
86
87   for (i = 0; i < 10; i++) {
88     double clock_sta = time_precise();
89     MSG_process_sleep(1);
90     double clock_end = time_precise();
91     XBT_INFO("duration %f", clock_end - clock_sta);
92   }
93
94
95   for (i = 0; i < nvm; i++) {
96     MSG_vm_destroy(vm[i]);
97   }
98
99   XBT_INFO("## Test (ended)");
100   
101   return 0;
102 }
103
104 static void launch_master(msg_host_t host)
105 {
106   const char *pr_name = "master_";
107   char **argv = xbt_new(char *, 2);
108   argv[0] = xbt_strdup(pr_name);
109   argv[1] = NULL;
110
111   MSG_process_create_with_arguments(pr_name, master_main, NULL, host, 1, argv);
112 }
113
114
115 int main(int argc, char *argv[])
116 {
117   /* Get the arguments */
118   MSG_init(&argc, argv);
119
120   /* load the platform file */
121   xbt_assert(argc == 2);
122   MSG_create_environment(argv[1]);
123
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   launch_master(pm0);
127
128   int res = MSG_main();
129   XBT_INFO("Bye (simulation time %g)", MSG_get_clock());
130
131
132   return !(res == MSG_OK);
133 }