Logo AND Algorithmique Numérique Distribuée

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