Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright notices
[simgrid.git] / examples / msg / energy / onoff / onoff.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 void simulate_bootup(msg_host_t host) {
21
22         int previous_pstate = MSG_host_get_pstate(host);
23
24         XBT_INFO("Switch to virtual pstate 3, that encodes the shutting down state in the XML file of that example");
25         MSG_host_set_pstate(host,3);
26
27         msg_host_t host_list[1] = {host};
28         double flops_amount[1] = {1};
29         double bytes_amount[1] = {0};
30
31         XBT_INFO("Actually start the host");
32         MSG_host_on(host);
33
34         XBT_INFO("Simulate the boot up by executing one flop on that host");
35         // We use a parallel task to run some task on a remote host.
36         msg_task_t bootup = MSG_parallel_task_create("boot up", 1, host_list, flops_amount, bytes_amount, NULL);
37         MSG_task_execute(bootup);
38         MSG_task_destroy(bootup);
39
40         XBT_INFO("Switch back to previously selected pstate %d", previous_pstate);
41         MSG_host_set_pstate(host, previous_pstate);
42 }
43
44 static void simulate_shutdown(msg_host_t host) {
45
46         int previous_pstate = MSG_host_get_pstate(host);
47
48         XBT_INFO("Switch to virtual pstate 4, that encodes the shutting down state in the XML file of that example");
49         MSG_host_set_pstate(host,4);
50
51         msg_host_t host_list[1] = {host};
52         double flops_amount[1] = {1};
53         double bytes_amount[1] = {0};
54
55         XBT_INFO("Simulate the shutdown by executing one flop on that remote host (using a parallel task)");
56         msg_task_t shutdown = MSG_parallel_task_create("shutdown", 1, host_list, flops_amount, bytes_amount, NULL);
57         MSG_task_execute(shutdown);
58         MSG_task_destroy(shutdown);
59
60         XBT_INFO("Switch back to previously selected pstate %d", previous_pstate);
61         MSG_host_set_pstate(host, previous_pstate);
62
63         XBT_INFO("Actually shutdown the host");
64         MSG_host_off(host);
65 }
66
67 static int onoff(int argc, char *argv[]) {
68         msg_host_t host1 = MSG_host_by_name("MyHost1");
69
70         XBT_INFO("Energetic profile: %s",
71                         MSG_host_get_property_value(host1,"watt_per_state"));
72         XBT_INFO("Initial peak speed=%.0E flop/s; Energy dissipated =%.0E J",
73                         MSG_host_get_current_power_peak(host1), MSG_host_get_consumed_energy(host1));
74
75         XBT_INFO("Sleep for 10 seconds");
76         MSG_process_sleep(10);
77         XBT_INFO("Done sleeping. Current peak speed=%.0E; Energy dissipated=%.2f J",
78                         MSG_host_get_current_power_peak(host1), MSG_host_get_consumed_energy(host1));
79
80         simulate_shutdown(host1);
81         XBT_INFO("Host1 is now OFF. Current peak speed=%.0E flop/s; Energy dissipated=%.0f J",
82                         MSG_host_get_current_power_peak(host1), MSG_host_get_consumed_energy(host1));
83
84         XBT_INFO("Sleep for 10 seconds");
85         MSG_process_sleep(10);
86         XBT_INFO("Done sleeping. Current peak speed=%.0E; Energy dissipated=%.2f J",
87                         MSG_host_get_current_power_peak(host1), MSG_host_get_consumed_energy(host1));
88
89         simulate_bootup(host1);
90         XBT_INFO("Host1 is now ON again. Current peak speed=%.0E flop/s; Energy dissipated=%.0f J",
91                         MSG_host_get_current_power_peak(host1), MSG_host_get_consumed_energy(host1));
92
93
94         return 0;
95 }
96
97 int main(int argc, char *argv[])
98 {
99         msg_error_t res = MSG_OK;
100         sg_energy_plugin_init();
101         MSG_init(&argc, argv);
102
103         if (argc != 3) {
104                 XBT_CRITICAL("Usage: %s platform_file deployment_file\n",
105                                 argv[0]);
106                 XBT_CRITICAL
107                 ("example: %s msg_platform.xml msg_deployment.xml\n",
108                                 argv[0]);
109                 exit(1);
110         }
111
112         MSG_create_environment(argv[1]);
113
114         /*   Application deployment */
115         MSG_function_register("onoff_test", onoff);
116
117         MSG_launch_application(argv[2]);
118
119         res = MSG_main();
120
121         XBT_INFO("Total simulation time: %.2f", MSG_get_clock());
122
123         if (res == MSG_OK)
124                 return 0;
125         else
126                 return 1;
127 }
128