1 /* Copyright (c) 2010. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "instr/private.h"
11 #define VARIABLE_SEPARATOR '#'
16 static xbt_dict_t last_platform_variables; /* to control the amount of add/sub variables events:
17 dict with key {RESOURCE_NAME}#Time or {RESOURCE_NAME}#Value of dict with variables types == string */
21 static xbt_dict_t method_c_dict;
24 /* auxiliary function for resource utilization tracing */
25 static char *strsplit (char *input, int field, char del) //caller should free the returned string
28 int length = strlen(input), i;
29 int s = 0, e = length+1;
30 int current_field = 0;
31 for (i = 0; i < length; i++){
33 if (current_field == field){
42 //copy string from s to e (with length equal to e-s) and return
43 ret = malloc ((e-s+2)*sizeof(char));
44 strncpy (ret, input+s, e-s+1);
50 static void __TRACE_surf_resource_utilization_A (double now, double delta, const char *variable, const char *resource, double value)
52 if (!IS_TRACING_PLATFORM) return;
55 snprintf (valuestr, 100, "%f", value);
57 __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
58 pajeAddVariable (now, variable, resource, valuestr);
59 pajeSubVariable (now+delta, variable, resource, valuestr);
65 static void __TRACE_surf_resource_utilization_initialize_B ()
67 last_platform_variables = xbt_dict_new();
70 static void __TRACE_surf_resource_utilization_B (double now, double delta, const char *variable, const char *resource, double value)
72 if (!IS_TRACING_PLATFORM) return;
75 char nowstr[100], nowdeltastr[100];
76 char timekey[100], valuekey[100], variablekey[100];
77 char *lastvariable = NULL;
78 char *lasttime = NULL;
79 char *lastvalue = NULL;
80 char *nowdeltastr_cpy = NULL;
81 char *valuestr_cpy = NULL;
82 char *variable_cpy = NULL;
85 * The following code replaces the code above with the objective
86 * to decrease the size of file because of unnecessary add/sub on
87 * variables. It should be re-checked before put in production.
90 snprintf (valuestr, 100, "%f", value);
91 snprintf (nowstr, 100, "%f", now);
92 snprintf (nowdeltastr, 100, "%f", now+delta);
93 snprintf (timekey, 100, "%s%cTime", resource, VARIABLE_SEPARATOR);
94 snprintf (valuekey, 100, "%s%cValue", resource, VARIABLE_SEPARATOR);
95 snprintf (variablekey, 100, "%s%cVariable", resource, VARIABLE_SEPARATOR);
97 lastvariable = xbt_dict_get_or_null (last_platform_variables, variablekey);
98 if (lastvariable == NULL){
99 __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
100 pajeAddVariable (now, variable, resource, valuestr);
101 nowdeltastr_cpy = xbt_strdup (nowdeltastr);
102 valuestr_cpy = xbt_strdup (valuestr);
103 variable_cpy = xbt_strdup (variable);
104 xbt_dict_set (last_platform_variables, timekey, nowdeltastr_cpy, xbt_free);
105 xbt_dict_set (last_platform_variables, valuekey, valuestr_cpy, xbt_free);
106 xbt_dict_set (last_platform_variables, variablekey, variable_cpy, xbt_free);
108 lasttime = xbt_dict_get_or_null (last_platform_variables, timekey);
109 lastvalue = xbt_dict_get_or_null (last_platform_variables, valuekey);
111 /* check if it is the same variable */
112 if (strcmp(lastvariable, variable) == 0){ /* same variable */
113 /* check if lasttime equals now */
114 if (atof(lasttime) == now){ /* lastime == now */
115 /* check if lastvalue equals valuestr */
116 if (atof(lastvalue) == value){ /* lastvalue == value (good, just advance time) */
117 char *nowdeltastr_cpy = xbt_strdup (nowdeltastr);
118 xbt_dict_set (last_platform_variables, timekey, nowdeltastr_cpy, xbt_free);
119 }else{ /* value has changed */
120 /* value has changed, subtract previous value, add new one */
121 pajeSubVariable (atof(lasttime), variable, resource, lastvalue);
122 pajeAddVariable (atof(nowstr), variable, resource, valuestr);
123 nowdeltastr_cpy = xbt_strdup (nowdeltastr);
124 valuestr_cpy = xbt_strdup (valuestr);
125 xbt_dict_set (last_platform_variables, timekey, nowdeltastr_cpy, xbt_free);
126 xbt_dict_set (last_platform_variables, valuekey, valuestr_cpy, xbt_free);
128 }else{ /* lasttime != now */
129 /* the last time is different from new starting time, subtract to lasttime and add from nowstr */
130 pajeSubVariable (atof(lasttime), variable, resource, lastvalue);
131 pajeAddVariable (atof(nowstr), variable, resource, valuestr);
132 nowdeltastr_cpy = xbt_strdup (nowdeltastr);
133 valuestr_cpy = xbt_strdup (valuestr);
134 xbt_dict_set (last_platform_variables, timekey, nowdeltastr_cpy, xbt_free);
135 xbt_dict_set (last_platform_variables, valuekey, valuestr_cpy, xbt_free);
137 }else{ /* variable has changed */
138 pajeSubVariable (atof(lasttime), lastvariable, resource, lastvalue);
139 __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
140 pajeAddVariable (now, variable, resource, valuestr);
141 nowdeltastr_cpy = xbt_strdup (nowdeltastr);
142 valuestr_cpy = xbt_strdup (valuestr);
143 variable_cpy = xbt_strdup (variable);
144 xbt_dict_set (last_platform_variables, timekey, nowdeltastr_cpy, xbt_free);
145 xbt_dict_set (last_platform_variables, valuekey, valuestr_cpy, xbt_free);
146 xbt_dict_set (last_platform_variables, variablekey, variable_cpy, xbt_free);
152 static void __TRACE_surf_resource_utilization_finalize_B ()
154 xbt_dict_cursor_t cursor = NULL;
155 unsigned int cursor_ar = 0;
156 char *key, *value, *res;
159 char *var_cpy = NULL;
160 xbt_dynar_t resources = NULL;
161 if (!IS_TRACING_PLATFORM) return;
162 if (!xbt_dict_length(last_platform_variables)){
165 /* get all resources from last_platform_variables */
166 resources = xbt_dynar_new(sizeof(char*), xbt_free);
167 xbt_dict_foreach(last_platform_variables, cursor, key, value) {
168 res = strsplit (key, 0, VARIABLE_SEPARATOR);
169 aux = strsplit (key, 1, VARIABLE_SEPARATOR);
170 if (strcmp (aux, "Time") == 0){ //only need to add one of three
171 var_cpy = xbt_strdup (res);
172 xbt_dynar_push (resources, &var_cpy);
178 /* iterate through resources array */
179 xbt_dynar_foreach (resources, cursor_ar, resource) {
180 char timekey[100], valuekey[100], variablekey[100];
183 char *variable = NULL;
184 snprintf (timekey, 100, "%s%cTime", resource, VARIABLE_SEPARATOR);
185 snprintf (valuekey, 100, "%s%cValue", resource, VARIABLE_SEPARATOR);
186 snprintf (variablekey, 100, "%s%cVariable", resource, VARIABLE_SEPARATOR);
188 time = xbt_dict_get_or_null (last_platform_variables, timekey);
190 value = xbt_dict_get (last_platform_variables, valuekey);
191 variable = xbt_dict_get (last_platform_variables, variablekey);
192 pajeSubVariable (atof(time), variable, resource, value);
194 xbt_dict_remove (last_platform_variables, timekey);
195 xbt_dict_remove (last_platform_variables, valuekey);
196 xbt_dict_remove (last_platform_variables, variablekey);
204 static void __TRACE_surf_resource_utilization_start_C (smx_action_t action)
207 snprintf (key, 100, "%p", action);
210 if (xbt_dict_get_or_null (method_c_dict, key)){
211 xbt_dict_remove (method_c_dict, key); //should never execute here, but it does
213 xbt_dict_set (method_c_dict, key, xbt_dict_new(), xbt_free);
215 //fprintf (stderr, "start %p\n", action);
218 if (xbt_dict_get_or_null (start_time, key)){
219 xbt_dict_remove (start_time, key);
224 static void __TRACE_surf_resource_utilization_end_C (smx_action_t action)
227 snprintf (key, 100, "%p", action);
229 xbt_dict_t action_dict = xbt_dict_get (method_c_dict, key);
230 // fprintf (stderr, "end %p (%f - %f)\n", action, atof(xbt_dict_get_or_null(action_dict, "start")),
231 // atof(xbt_dict_get_or_null(action_dict, "end")));
233 double start_time = atof(xbt_dict_get (action_dict, "start"));
234 double end_time = atof(xbt_dict_get (action_dict, "end"));
236 xbt_dict_cursor_t cursor=NULL;
237 char *action_dict_key, *action_dict_value;
238 xbt_dict_foreach(action_dict,cursor,action_dict_key,action_dict_value) {
239 char resource[100], variable[100];
240 if (sscanf (action_dict_key, "%s %s", resource, variable) != 2) continue;
241 __TRACE_surf_check_variable_set_to_zero (start_time, variable, resource);
243 if(end_time-start_time != 0){
244 snprintf (value_str, 100, "%f", atof(action_dict_value)/(end_time-start_time));
245 pajeAddVariable (start_time, variable, resource, value_str);
246 pajeSubVariable (end_time, variable, resource, value_str);
249 //fprintf(stderr, "\t%p (key=%s) %s %s = %s\n",action, action_dict_key, resource, variable, action_dict_value);
250 //fprintf(stderr, "\t%f %f\n", start_time, end_time);
252 //fprintf (stderr, "\n");
254 xbt_dict_remove (method_c_dict, key);
257 if (xbt_dict_get_or_null (start_time_dict, key)){
258 xbt_dict_remove (start_time_dict, key);
260 if (xbt_dict_get_or_null (end_time_dict, key)){
261 xbt_dict_remove (end_time_dict, key);
267 static void __TRACE_surf_resource_utilization_C (smx_action_t action, double now, double delta, const char *variable, const char *resource, double value)
270 snprintf (key, 100, "%p", action);
272 xbt_dict_t action_dict = xbt_dict_get (method_c_dict, key);
274 if (!xbt_dict_get_or_null (action_dict, "start")){
275 char start_time[100];
276 snprintf (start_time, 100, "%f", now);
277 xbt_dict_set (action_dict, "start", xbt_strdup (start_time), xbt_free);
281 snprintf (end_time, 100, "%f", now+delta);
282 xbt_dict_set (action_dict, "end", xbt_strdup (end_time), xbt_free);
284 //accumulate the value resource-variable
286 snprintf (res_var, 300, "%s %s", resource, variable);
287 double current_value_f;
288 char *current_value = xbt_dict_get_or_null (action_dict, res_var);
290 current_value_f = atof (current_value);
291 current_value_f += value*delta;
293 current_value_f = value*delta;
295 char new_current_value[100];
296 snprintf (new_current_value, 100, "%f", current_value_f);
297 xbt_dict_set (action_dict, res_var, xbt_strdup (new_current_value), xbt_free);
299 //fprintf (stderr, "event act=%p, now=%f, delta=%f, %s - %s %f\n", action, now, delta, resource, variable, value);
302 static void __TRACE_surf_resource_utilization_initialize_C ()
304 //start_time_dict = xbt_dict_new();
305 //end_time_dict = xbt_dict_new();
306 method_c_dict = xbt_dict_new();
309 static void __TRACE_surf_resource_utilization_finalize_C ()
311 xbt_dict_free (&method_c_dict);
318 * TRACE_surf_link_set_utilization: entry point from SimGrid
320 void TRACE_surf_link_set_utilization (const char *name, smx_action_t smx_action, double value, double now, double delta)
323 if (!IS_TRACING || !IS_TRACED(smx_action)) return;
325 if (strcmp (name, "__loopback__")==0 ||
326 strcmp (name, "loopback")==0){ //ignore loopback updates
330 if (value == 0) return;
332 //if (!xbt_dict_get_or_null (created_links, name)){
333 // TRACE_surf_link_missing ();
337 snprintf (type, 100, "b%s", smx_action->category);
338 __TRACE_surf_resource_utilization_event (smx_action, now, delta, type, name, value);
339 //fprintf (stderr, "%p - (%f - %f = %f) %s %f metric = %f\n", smx_action, now, now+delta, delta, name, value, value*delta);
340 // __TRACE_surf_update_action_state_resource (now, delta, type, name, value);
345 * TRACE_surf_host_set_utilization: entry point from SimGrid
347 void TRACE_surf_host_set_utilization (const char *name, smx_action_t smx_action, double value, double now, double delta)
350 if (!IS_TRACING || !IS_TRACED(smx_action)) return;
355 snprintf (type, 100, "p%s", smx_action->category);
356 __TRACE_surf_resource_utilization_event (smx_action, now, delta, type, name, value);
357 //fprintf (stderr, "%p - (%f - %f = %f) %s %f metric = %f\n", smx_action, now, now+delta, delta, name, value, value*delta);
358 // __TRACE_surf_update_action_state_resource (now, delta, type, name, value);
363 * __TRACE_surf_resource_utilization_*: entry points from tracing functions
365 void __TRACE_surf_resource_utilization_start (smx_action_t action)
368 __TRACE_surf_resource_utilization_start_C (action);
372 void __TRACE_surf_resource_utilization_end (smx_action_t action)
375 __TRACE_surf_resource_utilization_end_C (action);
379 void __TRACE_surf_resource_utilization_event (smx_action_t action, double now, double delta, const char *variable, const char *resource, double value)
382 __TRACE_surf_resource_utilization_A (now, delta, variable, resource, value);
385 __TRACE_surf_resource_utilization_B (now, delta, variable, resource, value);
388 __TRACE_surf_resource_utilization_C (action, now, delta, variable, resource, value);
394 void __TRACE_surf_resource_utilization_initialize ()
399 __TRACE_surf_resource_utilization_initialize_B();
402 __TRACE_surf_resource_utilization_initialize_C();
408 void __TRACE_surf_resource_utilization_finalize ()
413 __TRACE_surf_resource_utilization_finalize_B();
416 __TRACE_surf_resource_utilization_finalize_C();