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/instr_private.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_resource, instr, "tracing (un)-categorized resource utilization");
13 //to check if variables were previously set to 0, otherwise paje won't simulate them
14 static xbt_dict_t platform_variables; /* host or link name -> array of categories */
17 static xbt_dict_t method_b_dict;
20 static xbt_dict_t method_c_dict;
22 //resource utilization tracing method
23 static void (*TRACE_method_alloc) (void) = NULL;
24 static void (*TRACE_method_release) (void) = NULL;
25 static void (*TRACE_method_start) (smx_action_t action) = NULL;
26 static void (*TRACE_method_event) (smx_action_t action, double now,
27 double delta, const char *variable,
28 const char *resource, double value) =
30 static void (*TRACE_method_end) (smx_action_t action) = NULL;
33 static void __TRACE_surf_check_variable_set_to_zero(double now,
37 /* check if we have to set it to 0 */
38 if (!xbt_dict_get_or_null(platform_variables, resource)) {
39 xbt_dynar_t array = xbt_dynar_new(sizeof(char *), xbt_free);
40 char *var_cpy = xbt_strdup(variable);
41 xbt_dynar_push(array, &var_cpy);
42 container_t container = getContainerByName (resource);
43 type_t type = getVariableType (variable, NULL, container->type);
44 new_pajeSetVariable (now, container, type, 0);
45 xbt_dict_set(platform_variables, resource, array,
46 xbt_dynar_free_voidp);
48 xbt_dynar_t array = xbt_dict_get(platform_variables, resource);
52 xbt_dynar_foreach(array, i, cat) {
53 if (strcmp(variable, cat) == 0) {
58 char *var_cpy = xbt_strdup(variable);
59 xbt_dynar_push(array, &var_cpy);
60 if (TRACE_categorized ()){
61 container_t container = getContainerByName (resource);
62 type_t type = getVariableType (variable, NULL, container->type);
63 new_pajeSetVariable (now, container, type, 0);
72 static void __TRACE_A_alloc(void)
76 static void __TRACE_A_release(void)
80 static void __TRACE_A_start(smx_action_t action)
84 static void __TRACE_A_event(smx_action_t action, double now, double delta,
85 const char *variable, const char *resource,
89 snprintf(valuestr, 100, "%f", value);
91 __TRACE_surf_check_variable_set_to_zero(now, variable, resource);
92 container_t container = getContainerByName (resource);
93 type_t type = getVariableType (variable, NULL, container->type);
94 new_pajeAddVariable(now, container, type, value);
95 new_pajeSubVariable(now + delta, container, type, value);
98 static void __TRACE_A_end(smx_action_t action)
105 static void __TRACE_B_alloc(void)
107 method_b_dict = xbt_dict_new();
110 static void __TRACE_B_release(void)
113 xbt_dict_cursor_t cursor = NULL;
114 xbt_dict_foreach(method_b_dict, cursor, key, time) {
115 char resource[INSTR_DEFAULT_STR_SIZE];
116 char variable[INSTR_DEFAULT_STR_SIZE];
117 char what[INSTR_DEFAULT_STR_SIZE];
118 sscanf (key, "%s %s %s", resource, variable, what);
119 if (strcmp(what, "time")==0){
120 char key_value[INSTR_DEFAULT_STR_SIZE];
121 snprintf (key_value, INSTR_DEFAULT_STR_SIZE, "%s %s value", resource, variable);
122 char *value = xbt_dict_get_or_null (method_b_dict, key_value);
123 container_t container = getContainerByName (resource);
124 type_t type = getVariableType (variable, NULL, container->type);
125 new_pajeSubVariable(atof(time), container, type, atof(value));
128 xbt_dict_free(&method_b_dict);
131 static void __TRACE_B_start(smx_action_t action)
135 static void __TRACE_B_event(smx_action_t action, double now, double delta,
136 const char *variable, const char *resource,
139 char key_time[INSTR_DEFAULT_STR_SIZE];
140 char key_value[INSTR_DEFAULT_STR_SIZE];
141 char nowstr[INSTR_DEFAULT_STR_SIZE];
142 char valuestr[INSTR_DEFAULT_STR_SIZE];
143 char nowdeltastr[INSTR_DEFAULT_STR_SIZE];
145 snprintf (key_time, INSTR_DEFAULT_STR_SIZE, "%s %s time", resource, variable);
146 snprintf (key_value, INSTR_DEFAULT_STR_SIZE, "%s %s value", resource, variable);
147 snprintf (nowstr, INSTR_DEFAULT_STR_SIZE, "%f", now);
148 snprintf (valuestr, INSTR_DEFAULT_STR_SIZE, "%f", value);
149 snprintf (nowdeltastr, INSTR_DEFAULT_STR_SIZE, "%f", now+delta);
151 char *lasttimestr = xbt_dict_get_or_null(method_b_dict, key_time);
152 char *lastvaluestr = xbt_dict_get_or_null(method_b_dict, key_value);
153 if (lasttimestr == NULL){
154 __TRACE_surf_check_variable_set_to_zero(now, variable, resource);
155 container_t container = getContainerByName (resource);
156 type_t type = getVariableType (variable, NULL, container->type);
157 new_pajeAddVariable(now, container, type, value);
158 xbt_dict_set(method_b_dict, key_time, xbt_strdup(nowdeltastr), xbt_free);
159 xbt_dict_set(method_b_dict, key_value, xbt_strdup(valuestr), xbt_free);
161 double lasttime = atof (lasttimestr);
162 double lastvalue = atof (lastvaluestr);
164 if (lastvalue == value){
165 double dif = fabs(now - lasttime);
167 //perfect, just go on
169 //time changed, have to update
170 container_t container = getContainerByName (resource);
171 type_t type = getVariableType (variable, NULL, container->type);
172 new_pajeSubVariable(lasttime, container, type, lastvalue);
173 new_pajeAddVariable(now, container, type, value);
176 //value changed, have to update
177 container_t container = getContainerByName (resource);
178 type_t type = getVariableType (variable, NULL, container->type);
179 new_pajeSubVariable(lasttime, container, type, lastvalue);
180 new_pajeAddVariable(now, container, type, value);
182 xbt_dict_set(method_b_dict, key_time, xbt_strdup(nowdeltastr), xbt_free);
183 xbt_dict_set(method_b_dict, key_value, xbt_strdup(valuestr), xbt_free);
188 static void __TRACE_B_end(smx_action_t action)
194 static void __TRACE_C_alloc(void)
196 method_c_dict = xbt_dict_new();
199 static void __TRACE_C_release(void)
201 xbt_dict_free(&method_c_dict);
204 static void __TRACE_C_start(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);
216 static void __TRACE_C_event(smx_action_t action, double now, double delta,
217 const char *variable, const char *resource,
221 snprintf(key, 100, "%p", action);
223 xbt_dict_t action_dict = xbt_dict_get(method_c_dict, key);
225 if (!xbt_dict_get_or_null(action_dict, "start")) {
226 char start_time[100];
227 snprintf(start_time, 100, "%f", now);
228 xbt_dict_set(action_dict, "start", xbt_strdup(start_time), xbt_free);
232 snprintf(end_time, 100, "%f", now + delta);
233 xbt_dict_set(action_dict, "end", xbt_strdup(end_time), xbt_free);
235 //accumulate the value resource-variable
237 snprintf(res_var, 300, "%s %s", resource, variable);
238 double current_value_f;
239 char *current_value = xbt_dict_get_or_null(action_dict, res_var);
241 current_value_f = atof(current_value);
242 current_value_f += value * delta;
244 current_value_f = value * delta;
246 char new_current_value[100];
247 snprintf(new_current_value, 100, "%f", current_value_f);
248 xbt_dict_set(action_dict, res_var, xbt_strdup(new_current_value),
252 static void __TRACE_C_end(smx_action_t action)
255 snprintf(key, 100, "%p", action);
257 xbt_dict_t action_dict = xbt_dict_get(method_c_dict, key);
258 double start_time = atof(xbt_dict_get(action_dict, "start"));
259 double end_time = atof(xbt_dict_get(action_dict, "end"));
261 xbt_dict_cursor_t cursor = NULL;
262 char *action_dict_key, *action_dict_value;
263 xbt_dict_foreach(action_dict, cursor, action_dict_key, action_dict_value) {
264 char resource[100], variable[100];
265 if (sscanf(action_dict_key, "%s %s", resource, variable) != 2)
267 __TRACE_surf_check_variable_set_to_zero(start_time, variable,
269 if (end_time - start_time != 0) {
270 container_t container = getContainerByName (resource);
271 type_t type = getVariableType (variable, NULL, container->type);
272 double val = atof(action_dict_value) / (end_time - start_time);
273 new_pajeSubVariable(start_time, container, type, val);
274 new_pajeAddVariable(end_time, container, type, val);
277 xbt_dict_remove(method_c_dict, key);
280 #define RESOURCE_UTILIZATION_INTERFACE
282 * TRACE_surf_link_set_utilization: entry point from SimGrid
284 void TRACE_surf_link_set_utilization(const char *resource, smx_action_t smx_action,
285 surf_action_t surf_action,
286 double value, double now,
289 if (!TRACE_is_active())
293 //only trace link utilization if link is known by tracing mechanism
294 if (!knownContainerWithName(resource))
299 //trace uncategorized link utilization
300 if (TRACE_uncategorized()){
301 XBT_DEBUG("UNCAT LINK [%f - %f] %s bandwidth_used %f", now, now+delta, resource, value);
302 container_t container = getContainerByName (resource);
303 type_t type = getVariableType("bandwidth_used", NULL, container->type);
304 TRACE_surf_resource_utilization_event(smx_action, now, delta, type->name, container->name, value);
307 //trace categorized utilization
308 if (TRACE_categorized()){
309 if (!surf_action->category)
311 //variable of this category starts by 'b', because we have a link here
312 char category_type[INSTR_DEFAULT_STR_SIZE];
313 snprintf (category_type, INSTR_DEFAULT_STR_SIZE, "b%s", surf_action->category);
314 XBT_DEBUG("CAT LINK [%f - %f] %s %s %f", now, now+delta, resource, category_type, value);
315 container_t container = getContainerByName (resource);
316 type_t type = getVariableType(category_type, NULL, container->type);
317 TRACE_surf_resource_utilization_event(smx_action, now, delta, type->name, container->name, value);
323 * TRACE_surf_host_set_utilization: entry point from SimGrid
325 void TRACE_surf_host_set_utilization(const char *resource,
326 smx_action_t smx_action,
327 surf_action_t surf_action,
328 double value, double now,
331 if (!TRACE_is_active())
333 //only trace host utilization if host is known by tracing mechanism
334 if (!knownContainerWithName(resource))
339 //trace uncategorized host utilization
340 if (TRACE_uncategorized()){
341 XBT_DEBUG("UNCAT HOST [%f - %f] %s power_used %f", now, now+delta, resource, value);
342 container_t container = getContainerByName (resource);
343 type_t type = getVariableType("power_used", NULL, container->type);
344 TRACE_surf_resource_utilization_event(smx_action, now, delta, type->name, container->name, value);
347 //trace categorized utilization
348 if (TRACE_categorized()){
349 if (!surf_action->category)
351 //variable of this category starts by 'p', because we have a host here
352 char category_type[INSTR_DEFAULT_STR_SIZE];
353 snprintf (category_type, INSTR_DEFAULT_STR_SIZE, "p%s", surf_action->category);
354 XBT_DEBUG("CAT HOST [%f - %f] %s %s %f", now, now+delta, resource, category_type, value);
355 container_t container = getContainerByName (resource);
356 type_t type = getVariableType(category_type, NULL, container->type);
357 TRACE_surf_resource_utilization_event(smx_action, now, delta, type->name, container->name, value);
363 * __TRACE_surf_resource_utilization_*: entry points from tracing functions
365 void TRACE_surf_resource_utilization_start(smx_action_t action)
367 if (!TRACE_is_active())
369 XBT_DEBUG("START %p", action);
370 TRACE_method_start(action);
373 void TRACE_surf_resource_utilization_event(smx_action_t action, double now,
375 const char *variable,
376 const char *resource,
379 if (!TRACE_is_active())
381 XBT_DEBUG("EVENT %p [%f - %f] %s %s %f", action, now, now+delta, resource, variable, value);
382 TRACE_method_event(action, now, delta, variable, resource, value);
385 void TRACE_surf_resource_utilization_end(smx_action_t action)
387 if (!TRACE_is_active())
389 TRACE_method_end(action);
390 XBT_DEBUG("END %p", action);
393 void TRACE_surf_resource_utilization_release()
395 if (!TRACE_is_active())
397 TRACE_method_release();
400 static void __TRACE_define_method(char *method)
402 if (!strcmp(method, "a")) {
403 TRACE_method_alloc = __TRACE_A_alloc;
404 TRACE_method_release = __TRACE_A_release;
405 TRACE_method_start = __TRACE_A_start;
406 TRACE_method_event = __TRACE_A_event;
407 TRACE_method_end = __TRACE_A_end;
408 } else if (!strcmp(method, "c")) {
409 TRACE_method_alloc = __TRACE_C_alloc;
410 TRACE_method_release = __TRACE_C_release;
411 TRACE_method_start = __TRACE_C_start;
412 TRACE_method_event = __TRACE_C_event;
413 TRACE_method_end = __TRACE_C_end;
414 } else { //default is B
415 TRACE_method_alloc = __TRACE_B_alloc;
416 TRACE_method_release = __TRACE_B_release;
417 TRACE_method_start = __TRACE_B_start;
418 TRACE_method_event = __TRACE_B_event;
419 TRACE_method_end = __TRACE_B_end;
423 void TRACE_surf_resource_utilization_alloc()
425 platform_variables = xbt_dict_new();
426 __TRACE_define_method(TRACE_get_platform_method());
427 TRACE_method_alloc();
429 #endif /* HAVE_TRACING */