Logo AND Algorithmique Numérique Distribuée

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