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 / e3 / e3.c
1 /* Copyright (c) 2007-2010, 2013-2014. 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 "msg/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 static int dvfs(int argc, char *argv[]);
21 static int process_code(int argc, char *argv[]);
22
23 static int process_code(int argc, char *argv[])
24 {
25   msg_task_t task1 = NULL;
26   double cpu_task = 0;
27   double task_time = MSG_get_clock();
28
29   if (argc == 2)
30   {
31           /* Run a sleep task */
32           double sleep_task = atof(argv[1]);
33
34           MSG_process_sleep(sleep_task);
35           task_time = MSG_get_clock() - task_time;
36           XBT_INFO("Process %s executed task sleep cpu=%f, duration = %f",
37                           MSG_process_get_name(MSG_process_self()), 0.0, task_time);
38           XBT_INFO("==================================================");
39  }
40
41
42   // Run a task
43   cpu_task = atof(argv[0]);
44   task1 = MSG_task_create ("task", cpu_task, 0, NULL);
45   MSG_task_execute (task1);
46   MSG_task_destroy(task1);
47
48   task_time = MSG_get_clock() - task_time;
49   XBT_INFO("Process %s executed task cpu=%f, duration = %f",
50                   MSG_process_get_name(MSG_process_self()), cpu_task, task_time);
51   XBT_INFO("==================================================");
52   return 0;
53 }
54
55
56 static int dvfs(int argc, char *argv[])
57 {
58   msg_host_t host = NULL;
59   double task_time = 0;
60   host = MSG_host_self();
61
62   double current_peak = MSG_host_get_current_power_peak(host);
63
64   XBT_INFO("Current power peak=%f", current_peak);
65   double consumed_energy = MSG_host_get_consumed_energy(host);
66   XBT_INFO("Total energy (Joules): %f", consumed_energy);
67
68   // Process 1 - long CPU task
69   int argc1 = 1;
70   char** params1 = xbt_malloc0(sizeof(char *) * argc1);
71   params1[0] = xbt_strdup("400.0E6");
72   MSG_process_create_with_arguments("proc1", process_code, NULL, host, argc1, params1);
73
74   // Process 2 - sleep 2 sec + CPU task
75   int argc2 = 2;
76   char** params2 = xbt_malloc0(sizeof(char *) * argc2);
77   params2[0] = xbt_strdup("100.0E6");
78   params2[1] = xbt_strdup("2");
79   MSG_process_create_with_arguments("proc2", process_code, NULL, host, argc2, params2);
80
81   // Process 3 - sleep 2 sec + CPU task
82   int argc3 = 2;
83   char** params3 = xbt_malloc0(sizeof(char *) * argc3);
84   params3[0] = xbt_strdup("100.0E6");
85   params3[1] = xbt_strdup("2");
86   MSG_process_create_with_arguments("proc3", process_code, NULL, host, argc3, params3);
87
88
89   // Main process
90   MSG_process_sleep(8);
91
92   task_time = MSG_get_clock() - task_time;
93   XBT_INFO("Task simulation time: %e", task_time);
94   consumed_energy = MSG_host_get_consumed_energy(host);
95   XBT_INFO("Total energy (Joules): %f", consumed_energy);
96
97   return 0;
98 }
99
100 int main(int argc, char *argv[])
101 {
102   msg_error_t res = MSG_OK;
103   sg_energy_plugin_init();
104   MSG_init(&argc, argv);
105
106   if (argc != 3) {
107     XBT_CRITICAL("Usage: %s platform_file deployment_file\n",
108               argv[0]);
109     XBT_CRITICAL
110         ("example: %s msg_platform.xml msg_deployment.xml\n",
111          argv[0]);
112     exit(1);
113   }
114
115   MSG_create_environment(argv[1]);
116
117   /*   Application deployment */
118   MSG_function_register("dvfs_test", dvfs);
119
120   MSG_launch_application(argv[2]);
121
122   res = MSG_main();
123
124   XBT_INFO("Total simulation time: %e", MSG_get_clock());
125
126   if (res == MSG_OK)
127     return 0;
128   else
129     return 1;
130 }
131