Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add energy VM example (simple but should be sufficient) - Adrien
[simgrid.git] / examples / msg / energy / vm / vm.c
1 /* Copyright (c) 2007-2010, 2013-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
9 #include "simgrid/msg.h"
10 #include "xbt/sysdep.h"         /* calloc */
11 #include "simgrid/plugins.h"
12
13 /* Create a log channel to have nice outputs. */
14 #include "xbt/log.h"
15 #include "xbt/asserts.h"
16
17 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
18                              "Messages specific for this msg example");
19
20 static msg_host_t host1 = NULL;
21 static msg_host_t host2 = NULL;
22 static msg_vm_t vm1 = NULL;
23 static double start;
24
25 int dvfs(int argc, char *argv[]);
26 int shutdown_vm(void *ignored1, void *ignored2);
27
28 int worker_func() {
29         //MSG_process_on_exit(shutdown_vm, NULL);
30
31         msg_task_t task1 = MSG_task_create("t1", 300E6, 0, NULL);
32   MSG_task_execute (task1);
33   MSG_task_destroy(task1);
34   XBT_INFO("Worker fun done");
35         return 0;
36 }
37
38 /*
39 int shutdown_vm(void *ignored1, void *ignored2) {
40   start = MSG_get_clock();
41   XBT_INFO("Shutting down the VM");
42         MSG_vm_shutdown(vm1);
43
44
45   XBT_INFO("VM is now down (duration: %.2f s). Current peak speed=%.0E flop/s; Energy dissipated=%.0f J",
46                   MSG_get_clock()-start,
47                   MSG_host_get_current_power_peak(host), MSG_host_get_consumed_energy(host));
48
49         return 0;
50 }
51 */
52
53 int dvfs(int argc, char *argv[])
54 {
55   host1 = MSG_host_by_name("MyHost1");
56   host2 = MSG_host_by_name("MyHost2");
57
58   double start = MSG_get_clock();
59
60   /* Host 1 */
61   XBT_INFO("Creating and starting a VM");
62         vm1 = MSG_vm_create(host1,
63                       "vm1", /* name */
64                       4, /* number of cpus */
65                       2048, /* ram size */
66                       100, /* net cap */
67                       "", /* disk path */
68                       1024 * 20, /* disk size */
69                       10, /* mig netspeed */
70                       50 /* dp intensity */
71                       );
72
73   // on Host1 create two tasks, one inside a VM  the other one  directly on the host 
74         MSG_vm_start(vm1);
75   msg_process_t p11 = MSG_process_create("p11", worker_func, NULL, vm1); 
76   msg_process_t p12 = MSG_process_create("p12", worker_func, NULL, host1);
77   //XBT_INFO("Task on Host started");
78
79
80   // on Host2, create two tasks directlu on the host: Energy of host 1 and host 2 should be the same. 
81   msg_process_t p21 = MSG_process_create("p21", worker_func, NULL, host2);
82   msg_process_t p22 = MSG_process_create("p22", worker_func, NULL, host2);
83
84   /* Wait and see */
85         MSG_process_sleep(5);
86         MSG_process_sleep(5);
87         MSG_vm_shutdown(vm1);
88
89   return 0;
90 }
91
92 int main(int argc, char *argv[])
93 {
94   msg_error_t res = MSG_OK;
95   sg_energy_plugin_init();
96   MSG_init(&argc, argv);
97
98   if (argc != 3) {
99     XBT_CRITICAL("Usage: %s platform_file deployment_file\n",
100               argv[0]);
101     XBT_CRITICAL
102         ("example: %s msg_platform.xml msg_deployment.xml\n",
103          argv[0]);
104     exit(1);
105   }
106
107   MSG_create_environment(argv[1]);
108
109   /*   Application deployment */
110   MSG_function_register("dvfs_test", dvfs);
111
112   MSG_launch_application(argv[2]);
113
114   res = MSG_main();
115
116   XBT_INFO("Total simulation time: %.2f", MSG_get_clock());
117
118   if (res == MSG_OK)
119     return 0;
120   else
121     return 1;
122 }
123