Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
all DHT examples are now called dht-<protocol>
[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 <sys/time.h>
8 #include "simgrid/msg.h"
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
11
12 /*
13  * Usage:
14  * ./examples/msg/cloud/scale ../examples/platforms/g5k.xml
15  *
16  * 1. valgrind --tool=callgrind ./examples/msg/cloud/scale ../examples/platforms/g5k.xml
17  * 2. kcachegrind
18  **/
19
20 static double time_precise(void) {
21   struct timeval tv;
22   int ret = gettimeofday(&tv, NULL);
23   xbt_assert(ret >= 0, "gettimeofday");
24   return (double) tv.tv_sec + tv.tv_usec * 0.001 * 0.001;
25 }
26
27 static int computation_fun(int argc, char *argv[]) {
28   for (;;) {
29     // double clock_sta = time_precise();
30
31     msg_task_t task = MSG_task_create("Task", 10000000, 0, NULL);
32     MSG_task_execute(task);
33     MSG_task_destroy(task);
34
35     // double clock_end = time_precise();
36
37     // XBT_INFO("%f", clock_end - clock_sta);
38   }
39   return 0;
40 }
41
42 static void launch_computation_worker(msg_host_t host) {
43   MSG_process_create("compute", computation_fun, NULL, host);
44 }
45
46 static int master_main(int argc, char *argv[])
47 {
48   xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
49
50   int npm = 10;
51   int nvm = 1000;
52
53   msg_host_t *pm = xbt_new(msg_host_t, npm);
54   msg_vm_t   *vm = xbt_new(msg_vm_t, nvm);
55
56   int i = 0;
57   for (i = 0; i < npm; i++) {
58     pm[i] = xbt_dynar_get_as(hosts_dynar, i, msg_host_t);
59   }
60
61   for (i = 0; i < nvm; i++) {
62     int pm_index = i % npm;
63     char *vm_name = bprintf("vm%d", i);
64     vm[i] = MSG_vm_create_core(pm[pm_index], vm_name);
65     MSG_vm_start(vm[i]);
66
67     launch_computation_worker(vm[i]);
68
69     xbt_free(vm_name);
70   }
71
72   XBT_INFO("## Test (start)");
73
74   for (i = 0; i < 10; i++) {
75     double clock_sta = time_precise();
76     MSG_process_sleep(1);
77     double clock_end = time_precise();
78     XBT_INFO("duration %f", clock_end - clock_sta);
79   }
80
81   for (i = 0; i < nvm; i++) {
82     MSG_vm_destroy(vm[i]);
83   }
84
85   XBT_INFO("## Test (ended)");
86   return 0;
87 }
88
89 static void launch_master(msg_host_t host)
90 {
91   const char *pr_name = "master_";
92   char **argv = xbt_new(char *, 2);
93   argv[0] = xbt_strdup(pr_name);
94   argv[1] = NULL;
95
96   MSG_process_create_with_arguments(pr_name, master_main, NULL, host, 1, argv);
97 }
98
99
100 int main(int argc, char *argv[])
101 {
102   /* Get the arguments */
103   MSG_init(&argc, argv);
104
105   /* load the platform file */
106   xbt_assert(argc == 2);
107   MSG_create_environment(argv[1]);
108
109   xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
110   msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
111   launch_master(pm0);
112
113   int res = MSG_main();
114   XBT_INFO("Bye (simulation time %g)", MSG_get_clock());
115
116   return !(res == MSG_OK);
117 }