Logo AND Algorithmique Numérique Distribuée

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