Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #2 from mquinson/master
[simgrid.git] / examples / msg / energy / pstate / pstate.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 "xbt/sysdep.h"         /* calloc */
9
10 /* Create a log channel to have nice outputs. */
11 #include "xbt/log.h"
12 #include "xbt/asserts.h"
13
14
15 /** @addtogroup MSG_examples
16  *
17  * - <b>energy/e1/e1.c</b> Shows how a set of pstates can be defined
18  *     for a host and how the current pstate can be accessed/changed
19  *     with @ref MSG_get_host_current_power_peak and @ref
20  *     MSG_set_host_pstate.
21  *     Make sure to read the platform XML file for details on how
22  *     to declare the CPU capacity for each pstate.
23  *
24  */
25
26 XBT_LOG_NEW_DEFAULT_CATEGORY(test,
27                              "Pstate properties test");
28
29 int dvfs(int argc, char *argv[]);
30
31
32 int dvfs(int argc, char *argv[])
33 {
34   msg_host_t host = NULL;
35   msg_task_t task1 = NULL;
36   double task_time = 0;
37   double workload = 100E6;
38   int new_peak_index=2;
39   host = MSG_host_self();; //MSG_get_host_by_name("MyHost1");
40
41   int nb = MSG_host_get_nb_pstates(host);
42   XBT_INFO("Count of Processor states=%d", nb);
43
44   double current_peak = MSG_host_get_current_power_peak(host);
45   XBT_INFO("Current power peak=%f", current_peak);
46
47   // Run a task
48   task1 = MSG_task_create ("t1", workload, 0, NULL);
49   MSG_task_execute (task1);
50   MSG_task_destroy(task1);
51
52   task_time = MSG_get_clock();
53   XBT_INFO("Task1 simulation time: %e", task_time);
54
55   // Change power peak
56   if ((new_peak_index >= nb) || (new_peak_index < 0))
57     {
58     XBT_INFO("Cannot set pstate %d, host supports only %d pstates", new_peak_index, nb);
59     return 0;
60     }
61
62   double peak_at = MSG_host_get_power_peak_at(host, new_peak_index);
63   XBT_INFO("Changing power peak value to %f (at index %d)", peak_at, new_peak_index);
64
65   MSG_host_set_pstate(host, new_peak_index);
66
67   current_peak = MSG_host_get_current_power_peak(host);
68   XBT_INFO("Current power peak=%f", current_peak);
69
70   // Run a second task
71   task1 = MSG_task_create ("t1", workload, 0, NULL);
72   MSG_task_execute (task1);
73   MSG_task_destroy(task1);
74
75   task_time = MSG_get_clock() - task_time;
76   XBT_INFO("Task2 simulation time: %e", task_time);
77
78
79   // Verify the default pstate is set to 0
80   host = MSG_host_by_name("MyHost2");
81   int nb2 = MSG_host_get_nb_pstates(host);
82   XBT_INFO("Count of Processor states=%d", nb2);
83
84   double current_peak2 = MSG_host_get_current_power_peak(host);
85   XBT_INFO("Current power peak=%f", current_peak2);
86   return 0;
87 }
88
89 int main(int argc, char *argv[])
90 {
91   msg_error_t res = MSG_OK;
92
93   MSG_init(&argc, argv);
94
95   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
96             "\tExample: %s msg_platform.xml msg_deployment.xml\n", 
97             argv[0], argv[0]);
98   
99   MSG_create_environment(argv[1]);
100
101   /*   Application deployment */
102   MSG_function_register("dvfs_test", dvfs);
103
104   MSG_launch_application(argv[2]);
105
106   res = MSG_main();
107
108   XBT_INFO("Total simulation time: %e", MSG_get_clock());
109
110   return res != MSG_OK;
111 }
112