Logo AND Algorithmique Numérique Distribuée

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