Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright notices
[simgrid.git] / examples / msg / energy / consumption / energy_consumption.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 int dvfs(int argc, char *argv[]);
21
22
23 int dvfs(int argc, char *argv[])
24 {
25   msg_host_t host = NULL;
26   msg_task_t task1 = NULL;
27   host = MSG_host_by_name("MyHost1");
28
29
30   XBT_INFO("Energetic profile: %s",
31                   MSG_host_get_property_value(host,"watt_per_state"));
32   XBT_INFO("Initial peak speed=%.0E flop/s; Energy dissipated =%.0E J",
33                   MSG_host_get_current_power_peak(host), MSG_host_get_consumed_energy(host));
34
35   double start = MSG_get_clock();
36   XBT_INFO("Sleep for 10 seconds");
37   MSG_process_sleep(10);
38   XBT_INFO("Done sleeping (duration: %.2f s). Current peak speed=%.0E; Energy dissipated=%.2f J",
39                   MSG_get_clock()-start,
40                   MSG_host_get_current_power_peak(host), MSG_host_get_consumed_energy(host));
41
42   // Run a task
43   start = MSG_get_clock();
44   XBT_INFO("Run a task for 100E6 flops");
45   task1 = MSG_task_create ("t1", 100E6, 0, NULL);
46   MSG_task_execute (task1);
47   MSG_task_destroy(task1);
48   XBT_INFO("Task done (duration: %.2f s). Current peak speed=%.0E flop/s; Current consumption: from %.0fW to %.0fW depending on load; Energy dissipated=%.0f J",
49                   MSG_get_clock()-start,
50                   MSG_host_get_current_power_peak(host), MSG_host_get_wattmin_at(host,MSG_host_get_pstate(host)),
51                   MSG_host_get_wattmax_at(host,MSG_host_get_pstate(host)),
52                   MSG_host_get_consumed_energy(host));
53
54   // ========= Change power peak =========
55   int pstate=2;
56   MSG_host_set_pstate(host, pstate);
57   XBT_INFO("========= Requesting pstate %d (speed should be of %.2f flop/s and is of %.2f flop/s)",
58                   pstate,
59                   MSG_host_get_power_peak_at(host, pstate),
60                   MSG_host_get_current_power_peak(host));
61
62   // Run a second task
63   start = MSG_get_clock();
64   XBT_INFO("Run a task for 100E6 flops");
65   task1 = MSG_task_create ("t2", 100E6, 0, NULL);
66   MSG_task_execute (task1);
67   MSG_task_destroy(task1);
68   XBT_INFO("Task done (duration: %.2f s). Current peak speed=%.0E flop/s; Energy dissipated=%.0f J",
69                   MSG_get_clock()-start,
70                   MSG_host_get_current_power_peak(host), MSG_host_get_consumed_energy(host));
71
72   start = MSG_get_clock();
73   XBT_INFO("Sleep for 4 seconds");
74   MSG_process_sleep(4);
75   XBT_INFO("Done sleeping (duration: %.2f s). Current peak speed=%.0E flop/s; Energy dissipated=%.0f J",
76                   MSG_get_clock()-start,
77                   MSG_host_get_current_power_peak(host), MSG_host_get_consumed_energy(host));
78
79   // =========== Turn the other host off ==========
80   XBT_INFO("Turning MyHost2 off, and sleeping another 10 seconds. MyHost2 dissipated %.0f J so far.",
81                   MSG_host_get_consumed_energy(MSG_host_by_name("MyHost2")) );
82   MSG_host_off(MSG_host_by_name("MyHost2"));
83   start = MSG_get_clock();
84   MSG_process_sleep(10);
85   XBT_INFO("Done sleeping (duration: %.2f s). Current peak speed=%.0E flop/s; Energy dissipated=%.0f J",
86                   MSG_get_clock()-start,
87                   MSG_host_get_current_power_peak(host), MSG_host_get_consumed_energy(host));
88   return 0;
89 }
90
91 int main(int argc, char *argv[])
92 {
93   msg_error_t res = MSG_OK;
94   sg_energy_plugin_init();
95   MSG_init(&argc, argv);
96
97   if (argc != 3) {
98     XBT_CRITICAL("Usage: %s platform_file deployment_file\n",
99               argv[0]);
100     XBT_CRITICAL
101         ("example: %s msg_platform.xml msg_deployment.xml\n",
102          argv[0]);
103     exit(1);
104   }
105
106   MSG_create_environment(argv[1]);
107
108   /*   Application deployment */
109   MSG_function_register("dvfs_test", dvfs);
110
111   MSG_launch_application(argv[2]);
112
113   res = MSG_main();
114
115   XBT_INFO("Total simulation time: %.2f", MSG_get_clock());
116
117   if (res == MSG_OK)
118     return 0;
119   else
120     return 1;
121 }
122