Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] use of xbt functions inside instr
[simgrid.git] / src / instr / instr_resource_utilization.c
1 /* Copyright (c) 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 "instr/instr_private.h"
8
9 #ifdef HAVE_TRACING
10
11 //to check if variables were previously set to 0, otherwise paje won't simulate them
12 static xbt_dict_t platform_variables;   /* host or link name -> array of categories */
13
14 //B
15 static xbt_dict_t method_b_dict;
16
17 //C
18 static xbt_dict_t method_c_dict;
19
20 //resource utilization tracing method
21 static void (*TRACE_method_alloc) (void) = NULL;
22 static void (*TRACE_method_release) (void) = NULL;
23 static void (*TRACE_method_start) (smx_action_t action) = NULL;
24 static void (*TRACE_method_event) (smx_action_t action, double now,
25                                    double delta, const char *variable,
26                                    const char *resource, double value) =
27     NULL;
28 static void (*TRACE_method_end) (smx_action_t action) = NULL;
29
30 //used by all methods
31 static void __TRACE_surf_check_variable_set_to_zero(double now,
32                                                     const char *variable,
33                                                     const char *resource)
34 {
35   /* check if we have to set it to 0 */
36   if (!xbt_dict_get_or_null(platform_variables, resource)) {
37     xbt_dynar_t array = xbt_dynar_new(sizeof(char *), xbt_free);
38     char *var_cpy = xbt_strdup(variable);
39     xbt_dynar_push(array, &var_cpy);
40     if (TRACE_platform_is_enabled())
41       pajeSetVariable(now, variable, resource, "0");
42     xbt_dict_set(platform_variables, resource, array,
43                  xbt_dynar_free_voidp);
44   } else {
45     xbt_dynar_t array = xbt_dict_get(platform_variables, resource);
46     unsigned int i;
47     char *cat;
48     int flag = 0;
49     xbt_dynar_foreach(array, i, cat) {
50       if (strcmp(variable, cat) == 0) {
51         flag = 1;
52       }
53     }
54     if (flag == 0) {
55       char *var_cpy = xbt_strdup(variable);
56       xbt_dynar_push(array, &var_cpy);
57       if (TRACE_platform_is_enabled())
58         pajeSetVariable(now, variable, resource, "0");
59     }
60   }
61   /* end of check */
62 }
63
64 #define A_METHOD
65 //A
66 static void __TRACE_A_alloc(void)
67 {
68 }
69
70 static void __TRACE_A_release(void)
71 {
72 }
73
74 static void __TRACE_A_start(smx_action_t action)
75 {
76 }
77
78 static void __TRACE_A_event(smx_action_t action, double now, double delta,
79                             const char *variable, const char *resource,
80                             double value)
81 {
82   char valuestr[100];
83   snprintf(valuestr, 100, "%f", value);
84
85   __TRACE_surf_check_variable_set_to_zero(now, variable, resource);
86   pajeAddVariable(now, variable, resource, valuestr);
87   pajeSubVariable(now + delta, variable, resource, valuestr);
88 }
89
90 static void __TRACE_A_end(smx_action_t action)
91 {
92 }
93
94 #define B_METHOD
95 //B
96
97 static void __TRACE_B_alloc(void)
98 {
99   method_b_dict = xbt_dict_new();
100 }
101
102 static void __TRACE_B_release(void)
103 {
104   char *key, *time;
105   xbt_dict_cursor_t cursor = NULL;
106   xbt_dict_foreach(method_b_dict, cursor, key, time) {
107     char resource[INSTR_DEFAULT_STR_SIZE];
108     char variable[INSTR_DEFAULT_STR_SIZE];
109     char what[INSTR_DEFAULT_STR_SIZE];
110     sscanf (key, "%s %s %s", resource, variable, what);
111     if (strcmp(what, "time")==0){
112       char key_value[INSTR_DEFAULT_STR_SIZE];
113       snprintf (key_value, INSTR_DEFAULT_STR_SIZE, "%s %s value", resource, variable);
114       char *value = xbt_dict_get_or_null (method_b_dict, key_value);
115       pajeSubVariable(atof(time), variable, resource, value);
116     }
117   }
118   xbt_dict_free(&method_b_dict);
119 }
120
121 static void __TRACE_B_start(smx_action_t action)
122 {
123 }
124
125 static void __TRACE_B_event(smx_action_t action, double now, double delta,
126                             const char *variable, const char *resource,
127                             double value)
128 {
129   char key_time[INSTR_DEFAULT_STR_SIZE];
130   char key_value[INSTR_DEFAULT_STR_SIZE];
131   char nowstr[INSTR_DEFAULT_STR_SIZE];
132   char valuestr[INSTR_DEFAULT_STR_SIZE];
133   char nowdeltastr[INSTR_DEFAULT_STR_SIZE];
134
135   snprintf (key_time, INSTR_DEFAULT_STR_SIZE, "%s %s time", resource, variable);
136   snprintf (key_value, INSTR_DEFAULT_STR_SIZE, "%s %s value", resource, variable);
137   snprintf (nowstr, INSTR_DEFAULT_STR_SIZE, "%f", now);
138   snprintf (valuestr, INSTR_DEFAULT_STR_SIZE, "%f", value);
139   snprintf (nowdeltastr, INSTR_DEFAULT_STR_SIZE, "%f", now+delta);
140
141   char *lasttimestr = xbt_dict_get_or_null(method_b_dict, key_time);
142   char *lastvaluestr = xbt_dict_get_or_null(method_b_dict, key_value);
143   if (lasttimestr == NULL){
144     __TRACE_surf_check_variable_set_to_zero(now, variable, resource);
145     pajeAddVariable(now, variable, resource, valuestr);
146     xbt_dict_set(method_b_dict, key_time, xbt_strdup(nowdeltastr), xbt_free);
147     xbt_dict_set(method_b_dict, key_value, xbt_strdup(valuestr), xbt_free);
148   }else{
149     double lasttime = atof (lasttimestr);
150     double lastvalue = atof (lastvaluestr);
151
152     if (lastvalue == value){
153       double dif = fabs(now - lasttime);
154       if (dif < 0.000001){
155         //perfect, just go on
156       }else{
157         //time changed, have to update
158         pajeSubVariable(lasttime, variable, resource, lastvaluestr);
159         pajeAddVariable(now, variable, resource, valuestr);
160       }
161     }else{
162       //value changed, have to update
163       pajeSubVariable(lasttime, variable, resource, lastvaluestr);
164       pajeAddVariable(now, variable, resource, valuestr);
165     }
166     xbt_dict_set(method_b_dict, key_time, xbt_strdup(nowdeltastr), xbt_free);
167     xbt_dict_set(method_b_dict, key_value, xbt_strdup(valuestr), xbt_free);
168   }
169   return;
170 }
171
172 static void __TRACE_B_end(smx_action_t action)
173 {
174 }
175
176 #define C_METHOD
177 //C
178 static void __TRACE_C_alloc(void)
179 {
180   method_c_dict = xbt_dict_new();
181 }
182
183 static void __TRACE_C_release(void)
184 {
185   xbt_dict_free(&method_c_dict);
186 }
187
188 static void __TRACE_C_start(smx_action_t action)
189 {
190   char key[100];
191   snprintf(key, 100, "%p", action);
192
193   //check if exists
194   if (xbt_dict_get_or_null(method_c_dict, key)) {
195     xbt_dict_remove(method_c_dict, key);        //should never execute here, but it does
196   }
197   xbt_dict_set(method_c_dict, key, xbt_dict_new(), xbt_free);
198 }
199
200 static void __TRACE_C_event(smx_action_t action, double now, double delta,
201                             const char *variable, const char *resource,
202                             double value)
203 {
204   char key[100];
205   snprintf(key, 100, "%p", action);
206
207   xbt_dict_t action_dict = xbt_dict_get(method_c_dict, key);
208   //setting start time
209   if (!xbt_dict_get_or_null(action_dict, "start")) {
210     char start_time[100];
211     snprintf(start_time, 100, "%f", now);
212     xbt_dict_set(action_dict, "start", xbt_strdup(start_time), xbt_free);
213   }
214   //updating end time
215   char end_time[100];
216   snprintf(end_time, 100, "%f", now + delta);
217   xbt_dict_set(action_dict, "end", xbt_strdup(end_time), xbt_free);
218
219   //accumulate the value resource-variable
220   char res_var[300];
221   snprintf(res_var, 300, "%s %s", resource, variable);
222   double current_value_f;
223   char *current_value = xbt_dict_get_or_null(action_dict, res_var);
224   if (current_value) {
225     current_value_f = atof(current_value);
226     current_value_f += value * delta;
227   } else {
228     current_value_f = value * delta;
229   }
230   char new_current_value[100];
231   snprintf(new_current_value, 100, "%f", current_value_f);
232   xbt_dict_set(action_dict, res_var, xbt_strdup(new_current_value),
233                xbt_free);
234 }
235
236 static void __TRACE_C_end(smx_action_t action)
237 {
238   char key[100];
239   snprintf(key, 100, "%p", action);
240
241   xbt_dict_t action_dict = xbt_dict_get(method_c_dict, key);
242   double start_time = atof(xbt_dict_get(action_dict, "start"));
243   double end_time = atof(xbt_dict_get(action_dict, "end"));
244
245   xbt_dict_cursor_t cursor = NULL;
246   char *action_dict_key, *action_dict_value;
247   xbt_dict_foreach(action_dict, cursor, action_dict_key, action_dict_value) {
248     char resource[100], variable[100];
249     if (sscanf(action_dict_key, "%s %s", resource, variable) != 2)
250       continue;
251     __TRACE_surf_check_variable_set_to_zero(start_time, variable,
252                                             resource);
253     char value_str[100];
254     if (end_time - start_time != 0) {
255       snprintf(value_str, 100, "%f",
256                atof(action_dict_value) / (end_time - start_time));
257       pajeAddVariable(start_time, variable, resource, value_str);
258       pajeSubVariable(end_time, variable, resource, value_str);
259     }
260   }
261   xbt_dict_remove(method_c_dict, key);
262 }
263
264 #define RESOURCE_UTILIZATION_INTERFACE
265 /*
266  * TRACE_surf_link_set_utilization: entry point from SimGrid
267  */
268 void TRACE_surf_link_set_utilization(void *link, smx_action_t smx_action,
269                                      surf_action_t surf_action,
270                                      double value, double now,
271                                      double delta)
272 {
273   if (!TRACE_is_active())
274     return;
275   if (!value)
276     return;
277   //only trace link utilization if link is known by tracing mechanism
278   if (!TRACE_surf_link_is_traced(link))
279     return;
280   if (!value)
281     return;
282
283   char resource[100];
284   snprintf(resource, 100, "%p", link);
285
286   //trace uncategorized link utilization
287   if (TRACE_uncategorized()){
288     TRACE_surf_resource_utilization_event(smx_action, now, delta,
289                                         "bandwidth_used", resource, value);
290   }
291
292   //trace categorized utilization
293   if (!surf_action->category)
294     return;
295   char type[100];
296   snprintf(type, 100, "b%s", surf_action->category);
297   TRACE_surf_resource_utilization_event(smx_action, now, delta, type,
298                                         resource, value);
299   return;
300 }
301
302 /*
303  * TRACE_surf_host_set_utilization: entry point from SimGrid
304  */
305 void TRACE_surf_host_set_utilization(const char *name,
306                                      smx_action_t smx_action,
307                                      surf_action_t surf_action,
308                                      double value, double now,
309                                      double delta)
310 {
311   if (!TRACE_is_active())
312     return;
313   if (!value)
314     return;
315
316   //trace uncategorized host utilization
317   if (TRACE_uncategorized()){
318     TRACE_surf_resource_utilization_event(smx_action, now, delta,
319                                         "power_used", name, value);
320   }
321
322   //trace categorized utilization
323   if (!surf_action->category)
324     return;
325   char type[100];
326   snprintf(type, 100, "p%s", surf_action->category);
327   TRACE_surf_resource_utilization_event(smx_action, now, delta, type, name,
328                                         value);
329   return;
330 }
331
332 /*
333  * __TRACE_surf_resource_utilization_*: entry points from tracing functions
334  */
335 void TRACE_surf_resource_utilization_start(smx_action_t action)
336 {
337   if (!TRACE_is_active())
338     return;
339   TRACE_method_start(action);
340 }
341
342 void TRACE_surf_resource_utilization_event(smx_action_t action, double now,
343                                            double delta,
344                                            const char *variable,
345                                            const char *resource,
346                                            double value)
347 {
348   if (!TRACE_is_active())
349     return;
350   TRACE_method_event(action, now, delta, variable, resource, value);
351 }
352
353 void TRACE_surf_resource_utilization_end(smx_action_t action)
354 {
355   if (!TRACE_is_active())
356     return;
357   TRACE_method_end(action);
358 }
359
360 void TRACE_surf_resource_utilization_release()
361 {
362   if (!TRACE_is_active())
363     return;
364   TRACE_method_release();
365 }
366
367 static void __TRACE_define_method(char *method)
368 {
369   if (!strcmp(method, "a")) {
370     TRACE_method_alloc = __TRACE_A_alloc;
371     TRACE_method_release = __TRACE_A_release;
372     TRACE_method_start = __TRACE_A_start;
373     TRACE_method_event = __TRACE_A_event;
374     TRACE_method_end = __TRACE_A_end;
375   } else if (!strcmp(method, "c")) {
376     TRACE_method_alloc = __TRACE_C_alloc;
377     TRACE_method_release = __TRACE_C_release;
378     TRACE_method_start = __TRACE_C_start;
379     TRACE_method_event = __TRACE_C_event;
380     TRACE_method_end = __TRACE_C_end;
381   } else {                      //default is B
382     TRACE_method_alloc = __TRACE_B_alloc;
383     TRACE_method_release = __TRACE_B_release;
384     TRACE_method_start = __TRACE_B_start;
385     TRACE_method_event = __TRACE_B_event;
386     TRACE_method_end = __TRACE_B_end;
387   }
388 }
389
390 void TRACE_surf_resource_utilization_alloc()
391 {
392   platform_variables = xbt_dict_new();
393   __TRACE_define_method(TRACE_get_platform_method());
394   TRACE_method_alloc();
395 }
396 #endif /* HAVE_TRACING */