Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
17ae54ad404d9c74e441ff2ad9c11f4699a74976
[simgrid.git] / examples / msg / energy-onoff / energy-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 "simgrid/msg.h"
8 #include "simgrid/plugins/energy.h"
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
11
12 static void simulate_bootup(msg_host_t host) {
13   int previous_pstate = MSG_host_get_pstate(host);
14
15   XBT_INFO("Switch to virtual pstate 3, that encodes the shutting down state in the XML file of that example");
16   MSG_host_set_pstate(host,3);
17
18   msg_host_t host_list[1] = {host};
19   double flops_amount[1] = {1};
20   double bytes_amount[1] = {0};
21
22   XBT_INFO("Actually start the host");
23   MSG_host_on(host);
24
25   XBT_INFO("Simulate the boot up by executing one flop on that host");
26   // We use a parallel task to run some task on a remote host.
27   msg_task_t bootup = MSG_parallel_task_create("boot up", 1, host_list, flops_amount, bytes_amount, NULL);
28   MSG_task_execute(bootup);
29   MSG_task_destroy(bootup);
30
31   XBT_INFO("Switch back to previously selected pstate %d", previous_pstate);
32   MSG_host_set_pstate(host, previous_pstate);
33 }
34
35 static void simulate_shutdown(msg_host_t host) {
36   int previous_pstate = MSG_host_get_pstate(host);
37
38   XBT_INFO("Switch to virtual pstate 4, that encodes the shutting down state in the XML file of that example");
39   MSG_host_set_pstate(host,4);
40
41   msg_host_t host_list[1] = {host};
42   double flops_amount[1] = {1};
43   double bytes_amount[1] = {0};
44
45   XBT_INFO("Simulate the shutdown by executing one flop on that remote host (using a parallel task)");
46   msg_task_t shutdown = MSG_parallel_task_create("shutdown", 1, host_list, flops_amount, bytes_amount, NULL);
47   MSG_task_execute(shutdown);
48   MSG_task_destroy(shutdown);
49
50   XBT_INFO("Switch back to previously selected pstate %d", previous_pstate);
51   MSG_host_set_pstate(host, previous_pstate);
52
53   XBT_INFO("Actually shutdown the host");
54   MSG_host_off(host);
55 }
56
57 static int onoff(int argc, char *argv[]) {
58   msg_host_t host1 = MSG_host_by_name("MyHost1");
59
60   XBT_INFO("Energetic profile: %s", MSG_host_get_property_value(host1,"watt_per_state"));
61   XBT_INFO("Initial peak speed=%.0E flop/s; Energy dissipated =%.0E J",
62      MSG_host_get_current_power_peak(host1), sg_host_get_consumed_energy(host1));
63
64   XBT_INFO("Sleep for 10 seconds");
65   MSG_process_sleep(10);
66   XBT_INFO("Done sleeping. Current peak speed=%.0E; Energy dissipated=%.2f J",
67      MSG_host_get_current_power_peak(host1), sg_host_get_consumed_energy(host1));
68
69   simulate_shutdown(host1);
70   XBT_INFO("Host1 is now OFF. Current peak speed=%.0E flop/s; Energy dissipated=%.0f J",
71      MSG_host_get_current_power_peak(host1), sg_host_get_consumed_energy(host1));
72
73   XBT_INFO("Sleep for 10 seconds");
74   MSG_process_sleep(10);
75   XBT_INFO("Done sleeping. Current peak speed=%.0E; Energy dissipated=%.2f J",
76      MSG_host_get_current_power_peak(host1), sg_host_get_consumed_energy(host1));
77
78   simulate_bootup(host1);
79   XBT_INFO("Host1 is now ON again. Current peak speed=%.0E flop/s; Energy dissipated=%.0f J",
80      MSG_host_get_current_power_peak(host1), sg_host_get_consumed_energy(host1));
81
82   return 0;
83 }
84
85 int main(int argc, char *argv[])
86 {
87   sg_host_energy_plugin_init();
88   MSG_init(&argc, argv);
89
90   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
91
92   MSG_create_environment(argv[1]);
93
94   MSG_process_create("onoff_test", onoff, NULL, MSG_get_host_by_name("MyHost2"));
95
96   msg_error_t res = MSG_main();
97
98   XBT_INFO("Total simulation time: %.2f", MSG_get_clock());
99
100   return res != MSG_OK;
101 }