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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(tracing_surf,tracing,"Tracing Surf");
13 #define VARIABLE_SEPARATOR '#'
15 static xbt_dict_t host_router_id; //name(char*) -> id(char*)
16 static xbt_dict_t host_router_name; //id(char*) -> name (char*)
18 static xbt_dict_t created_links;
19 static xbt_dict_t link_bandwidth; //name(char*) -> bandwidth(double)
20 static xbt_dict_t link_latency; //name(char*) -> latency(double)
21 static xbt_dict_t host_containers;
23 static xbt_dict_t last_platform_variables; /* to control the amount of add/sub variables events:
24 dict with key {RESOURCE_NAME}#Time or {RESOURCE_NAME}#Value of dict with variables types == string */
26 static xbt_dict_t platform_variables; /* host or link name -> array of categories */
28 static xbt_dict_t resource_variables; /* (host|link)#variable -> value */
31 static xbt_dict_t gtnets_src; /* %p (action) -> %s */
32 static xbt_dict_t gtnets_dst; /* %p (action) -> %s */
34 void __TRACE_surf_init (void)
36 host_router_id = xbt_dict_new();
37 host_router_name = xbt_dict_new();
38 created_links = xbt_dict_new();
39 link_bandwidth = xbt_dict_new();
40 link_latency = xbt_dict_new();
41 platform_variables = xbt_dict_new();
42 host_containers = xbt_dict_new();
43 last_platform_variables = xbt_dict_new();
44 resource_variables = xbt_dict_new ();
45 gtnets_src = xbt_dict_new ();
46 gtnets_dst = xbt_dict_new ();
49 static char *strsplit (char *input, int field, char del) //caller should free the returned string
52 int length = strlen(input), i;
53 int s = 0, e = length+1;
54 int current_field = 0;
55 for (i = 0; i < length; i++){
57 if (current_field == field){
66 //copy string from s to e (with length equal to e-s) and return
67 ret = malloc ((e-s+2)*sizeof(char));
68 strncpy (ret, input+s, e-s+1);
73 void __TRACE_surf_finalize (void)
75 xbt_dict_cursor_t cursor = NULL;
76 unsigned int cursor_ar = 0;
77 char *key, *value, *res;
81 xbt_dynar_t resources = NULL;
82 if (!IS_TRACING_PLATFORM) return;
83 if (!xbt_dict_length(last_platform_variables)){
86 /* get all resources from last_platform_variables */
87 resources = xbt_dynar_new(sizeof(char*), xbt_free);
88 xbt_dict_foreach(last_platform_variables, cursor, key, value) {
89 res = strsplit (key, 0, VARIABLE_SEPARATOR);
90 aux = strsplit (key, 1, VARIABLE_SEPARATOR);
91 if (strcmp (aux, "Time") == 0){ //only need to add one of three
92 var_cpy = xbt_strdup (res);
93 xbt_dynar_push (resources, &var_cpy);
99 /* iterate through resources array */
100 xbt_dynar_foreach (resources, cursor_ar, resource) {
101 char timekey[100], valuekey[100], variablekey[100];
104 char *variable = NULL;
105 snprintf (timekey, 100, "%s%cTime", resource, VARIABLE_SEPARATOR);
106 snprintf (valuekey, 100, "%s%cValue", resource, VARIABLE_SEPARATOR);
107 snprintf (variablekey, 100, "%s%cVariable", resource, VARIABLE_SEPARATOR);
109 time = xbt_dict_get_or_null (last_platform_variables, timekey);
111 value = xbt_dict_get (last_platform_variables, valuekey);
112 variable = xbt_dict_get (last_platform_variables, variablekey);
113 pajeSubVariable (atof(time), variable, resource, value);
115 xbt_dict_remove (last_platform_variables, timekey);
116 xbt_dict_remove (last_platform_variables, valuekey);
117 xbt_dict_remove (last_platform_variables, variablekey);
122 void __TRACE_surf_check_variable_set_to_zero (double now, const char *variable, const char *resource)
124 /* check if we have to set it to 0 */
125 if (!xbt_dict_get_or_null (platform_variables, resource)){
126 xbt_dynar_t array = xbt_dynar_new(sizeof(char*), xbt_free);
127 char *var_cpy = xbt_strdup(variable);
128 xbt_dynar_push (array, &var_cpy);
129 if (IS_TRACING_PLATFORM) pajeSetVariable (now, variable, resource, "0");
130 xbt_dict_set (platform_variables, resource, array, xbt_dynar_free_voidp);
132 xbt_dynar_t array = xbt_dict_get (platform_variables, resource);
136 xbt_dynar_foreach (array, i, cat) {
137 if (strcmp(variable, cat)==0){
142 char *var_cpy = xbt_strdup(variable);
143 xbt_dynar_push (array, &var_cpy);
144 if (IS_TRACING_PLATFORM) pajeSetVariable (now, variable, resource, "0");
150 void __TRACE_surf_update_action_state_resource (double now, double delta, const char *variable, const char *resource, double value)
153 char nowstr[100], nowdeltastr[100];
154 char timekey[100], valuekey[100], variablekey[100];
155 char *lastvariable = NULL;
156 char *lasttime = NULL;
157 char *lastvalue = NULL;
158 char *nowdeltastr_cpy = NULL;
159 char *valuestr_cpy = NULL;
160 char *variable_cpy = NULL;
162 if (!IS_TRACING_PLATFORM) return;
164 snprintf (valuestr, 100, "%f", value);
167 //fprintf (stderr, "resource = %s variable = %s (%f -> %f) value = %s\n", resource, variable, now, now+delta, valuestr);
169 __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
170 if (IS_TRACING_PLATFORM) pajeAddVariable (now, variable, resource, valuestr);
171 if (IS_TRACING_PLATFORM) pajeSubVariable (now+delta, variable, resource, valuestr);
177 * The following code replaces the code above with the objective
178 * to decrease the size of file because of unnecessary add/sub on
179 * variables. It should be re-checked before put in production.
182 snprintf (nowstr, 100, "%.15f", now);
183 snprintf (nowdeltastr, 100, "%.15f", now+delta);
185 snprintf (timekey, 100, "%s%cTime", resource, VARIABLE_SEPARATOR);
186 snprintf (valuekey, 100, "%s%cValue", resource, VARIABLE_SEPARATOR);
187 snprintf (variablekey, 100, "%s%cVariable", resource, VARIABLE_SEPARATOR);
189 lastvariable = xbt_dict_get_or_null (last_platform_variables, variablekey);
190 if (lastvariable == NULL){
191 __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
192 pajeAddVariable (now, variable, resource, valuestr);
193 nowdeltastr_cpy = xbt_strdup (nowdeltastr);
194 valuestr_cpy = xbt_strdup (valuestr);
195 variable_cpy = xbt_strdup (variable);
196 xbt_dict_set (last_platform_variables, timekey, nowdeltastr_cpy, xbt_free);
197 xbt_dict_set (last_platform_variables, valuekey, valuestr_cpy, xbt_free);
198 xbt_dict_set (last_platform_variables, variablekey, variable_cpy, xbt_free);
200 lasttime = xbt_dict_get_or_null (last_platform_variables, timekey);
201 lastvalue = xbt_dict_get_or_null (last_platform_variables, valuekey);
203 /* check if it is the same variable */
204 if (strcmp(lastvariable, variable) == 0){ /* same variable */
205 /* check if lasttime equals now */
206 if (atof(lasttime) == now){ /* lastime == now */
207 /* check if lastvalue equals valuestr */
208 if (atof(lastvalue) == value){ /* lastvalue == value (good, just advance time) */
209 char *nowdeltastr_cpy = xbt_strdup (nowdeltastr);
210 xbt_dict_set (last_platform_variables, timekey, nowdeltastr_cpy, xbt_free);
211 }else{ /* value has changed */
212 /* value has changed, subtract previous value, add new one */
213 pajeSubVariable (atof(lasttime), variable, resource, lastvalue);
214 pajeAddVariable (atof(nowstr), variable, resource, valuestr);
215 nowdeltastr_cpy = xbt_strdup (nowdeltastr);
216 valuestr_cpy = xbt_strdup (valuestr);
217 xbt_dict_set (last_platform_variables, timekey, nowdeltastr_cpy, xbt_free);
218 xbt_dict_set (last_platform_variables, valuekey, valuestr_cpy, xbt_free);
220 }else{ /* lasttime != now */
221 /* the last time is different from new starting time, subtract to lasttime and add from nowstr */
222 pajeSubVariable (atof(lasttime), variable, resource, lastvalue);
223 pajeAddVariable (atof(nowstr), variable, resource, valuestr);
224 nowdeltastr_cpy = xbt_strdup (nowdeltastr);
225 valuestr_cpy = xbt_strdup (valuestr);
226 xbt_dict_set (last_platform_variables, timekey, nowdeltastr_cpy, xbt_free);
227 xbt_dict_set (last_platform_variables, valuekey, valuestr_cpy, xbt_free);
229 }else{ /* variable has changed */
230 pajeSubVariable (atof(lasttime), lastvariable, resource, lastvalue);
231 __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
232 pajeAddVariable (now, variable, resource, valuestr);
233 nowdeltastr_cpy = xbt_strdup (nowdeltastr);
234 valuestr_cpy = xbt_strdup (valuestr);
235 variable_cpy = xbt_strdup (variable);
236 xbt_dict_set (last_platform_variables, timekey, nowdeltastr_cpy, xbt_free);
237 xbt_dict_set (last_platform_variables, valuekey, valuestr_cpy, xbt_free);
238 xbt_dict_set (last_platform_variables, variablekey, variable_cpy, xbt_free);
244 void __TRACE_surf_set_resource_variable (double date, const char *variable, const char *resource, double value)
246 char aux[100], key[100];
247 char *last_value = NULL;
248 if (!IS_TRACING) return;
249 snprintf (aux, 100, "%f", value);
250 snprintf (key, 100, "%s%c%s", resource, VARIABLE_SEPARATOR, variable);
252 last_value = xbt_dict_get_or_null(resource_variables, key);
254 if (atof(last_value) == value){
258 if (IS_TRACING_PLATFORM) pajeSetVariable (date, variable, resource, aux);
259 xbt_dict_set (resource_variables, xbt_strdup(key), xbt_strdup(aux), xbt_free);
262 void TRACE_surf_link_set_utilization (const char *name, smx_action_t smx_action, double value, double now, double delta)
265 if (!IS_TRACING || !IS_TRACED(smx_action)) return;
267 if (strcmp (name, "__loopback__")==0 ||
268 strcmp (name, "loopback")==0){ //ignore loopback updates
272 if (value == 0) return;
274 if (!xbt_dict_get_or_null (created_links, name)){
275 TRACE_surf_link_missing ();
279 snprintf (type, 100, "b%s", smx_action->category);
280 __TRACE_surf_update_action_state_resource (now, delta, type, name, value);
284 void TRACE_surf_host_set_utilization (const char *name, smx_action_t smx_action, double value, double now, double delta)
287 if (!IS_TRACING || !IS_TRACED(smx_action)) return;
293 snprintf (type, 100, "p%s", smx_action->category);
294 __TRACE_surf_update_action_state_resource (now, delta, type, name, value);
299 * TRACE_surf_link_declaration (name, bandwidth, latency): this function
300 * saves the bandwidth and latency of a link identified by name. This
301 * information is used in the future to create the link container in the trace.
303 * caller: net_link_new (from each network model)
304 * main: save bandwidth and latency information
307 void TRACE_surf_link_declaration (char *name, double bw, double lat)
309 if (!IS_TRACING) return;
311 double *bw_ptr = xbt_malloc(sizeof(double));
312 double *lat_ptr = xbt_malloc(sizeof(double));
315 xbt_dict_set (link_bandwidth, name, bw_ptr, xbt_free);
316 xbt_dict_set (link_latency, name, lat_ptr, xbt_free);
320 * TRACE_surf_host_declaration (name, power): this function
321 * saves the power of a host identified by name. This information
322 * is used to create the host container in the trace.
324 * caller: cpu_new (from each cpu model) + router parser
325 * main: create HOST containers, set initial power value
328 void TRACE_surf_host_declaration (char *name, double power)
330 if (!IS_TRACING) return;
331 pajeCreateContainer (SIMIX_get_clock(), name, "HOST", "platform", name);
332 xbt_dict_set (host_containers, name, xbt_strdup("1"), xbt_free);
333 if (IS_TRACING_PLATFORM){
334 __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "power", name, power);
339 * TRACE_surf_link_save_endpoints (link_name, src, dst): this function
340 * creates the container of a link identified by link_name. It gets
341 * bandwidth and latency information from the dictionaries previously
342 * filled. The end points are obtained from the host_router_name dictionary.
344 * caller: end of parsing
345 * main: create LINK containers, set initial bandwidth and latency values
348 void TRACE_surf_link_save_endpoints (char *link_name, int src, int dst)
350 char srcidstr[100], dstidstr[100];
353 if (!IS_TRACING) return;
354 snprintf (srcidstr, 100, "%d", src);
355 snprintf (dstidstr, 100, "%d", dst);
356 char *srcname = xbt_dict_get (host_router_name, srcidstr);
357 char *dstname = xbt_dict_get (host_router_name, dstidstr);
358 snprintf (key, 100, "l%d-%d", src, dst);
360 if (strcmp (link_name, "__loopback__")==0 ||
361 strcmp (link_name, "loopback")==0){ //ignore loopback updates
365 if (!xbt_dict_get_or_null (created_links, link_name)){
366 double *bw = xbt_dict_get (link_bandwidth, link_name);
367 double *lat = xbt_dict_get (link_latency, link_name);
368 pajeCreateContainerWithBandwidthLatencySrcDst (SIMIX_get_clock(), link_name, "LINK", "platform", link_name, *bw, *lat, srcname, dstname);
369 if (IS_TRACING_PLATFORM) __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "bandwidth", link_name, *bw);
370 if (IS_TRACING_PLATFORM) __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "latency", link_name, *lat);
371 xbt_dict_set (created_links, link_name, xbt_strdup ("1"), xbt_free);
376 * TRACE_surf_host_define_id (name, host_id): This function relates
377 * the name of the host with its id, as generated by the called and
378 * passed as second parameter.
380 * caller: host parser, router parser
384 void TRACE_surf_host_define_id (const char *name, int host_id)
386 if (!IS_TRACING) return;
388 snprintf (strid, 100, "%d", host_id);
389 xbt_dict_set (host_router_id, name, xbt_strdup(strid), xbt_free);
390 xbt_dict_set (host_router_name, strid, xbt_strdup(name), xbt_free);
393 void TRACE_surf_host_set_power (double date, char *resource, double power)
395 __TRACE_surf_set_resource_variable (date, "power", resource, power);
398 void TRACE_surf_link_set_bandwidth (double date, char *resource, double bandwidth)
400 __TRACE_surf_set_resource_variable (date, "bandwidth", resource, bandwidth);
403 void TRACE_surf_link_set_latency (double date, char *resource, double latency)
405 __TRACE_surf_set_resource_variable (date, "latency", resource, latency);
408 /* to trace gtnets */
409 void TRACE_surf_gtnets_communicate (void *action, int src, int dst)
411 char key[100], aux[100];
412 if (!IS_TRACING) return;
413 snprintf (key, 100, "%p", action);
415 snprintf (aux, 100, "%d", src);
416 xbt_dict_set (gtnets_src, key, xbt_strdup(aux), xbt_free);
417 snprintf (aux, 100, "%d", dst);
418 xbt_dict_set (gtnets_dst, key, xbt_strdup(aux), xbt_free);
421 int TRACE_surf_gtnets_get_src (void *action)
425 if (!IS_TRACING) return -1;
426 snprintf (key, 100, "%p", action);
428 aux = xbt_dict_get_or_null (gtnets_src, key);
436 int TRACE_surf_gtnets_get_dst (void *action)
440 if (!IS_TRACING) return -1;
441 snprintf (key, 100, "%p", action);
443 aux = xbt_dict_get_or_null (gtnets_dst, key);
451 void TRACE_surf_gtnets_destroy (void *action)
454 if (!IS_TRACING) return;
455 snprintf (key, 100, "%p", action);
456 xbt_dict_remove (gtnets_src, key);
457 xbt_dict_remove (gtnets_dst, key);
460 void TRACE_surf_link_missing (void)
462 CRITICAL0("The trace cannot be done because "
463 "the platform you are using contains "
464 "routes with more than one link.");
465 THROW0(tracing_error, TRACE_ERROR_COMPLEX_ROUTES, "Tracing failed");
468 void TRACE_msg_clean (void)
471 xbt_dict_cursor_t cursor = NULL;
472 __TRACE_surf_finalize();
474 /* get all host from host_containers */
475 xbt_dict_foreach(host_containers, cursor, key, value) {
476 pajeDestroyContainer (MSG_get_clock(), "HOST", key);
480 void TRACE_surf_host_vivaldi_parse (char *host, double x, double y, double h)
483 if (!IS_TRACING || !IS_TRACING_PLATFORM) return;
485 snprintf (valuestr, 100, "%g", x);
486 pajeSetVariable (0, "vivaldi_x", host, valuestr);
487 snprintf (valuestr, 100, "%g", y);
488 pajeSetVariable (0, "vivaldi_y", host, valuestr);
489 snprintf (valuestr, 100, "%g", h);
490 pajeSetVariable (0, "vivaldi_h", host, valuestr);