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 hosts_id;
16 static xbt_dict_t created_links;
17 static xbt_dict_t link_bandwidth;
18 static xbt_dict_t link_latency;
19 static xbt_dict_t host_containers;
21 static xbt_dict_t last_platform_variables; /* to control the amount of add/sub variables events:
22 dict with key {RESOURCE_NAME}#Time or {RESOURCE_NAME}#Value of dict with variables types == string */
24 static xbt_dict_t platform_variables; /* host or link name -> array of categories */
26 static xbt_dict_t resource_variables; /* (host|link)#variable -> value */
29 static xbt_dict_t gtnets_src; /* %p (action) -> %s */
30 static xbt_dict_t gtnets_dst; /* %p (action) -> %s */
32 void __TRACE_surf_init (void)
34 hosts_id = xbt_dict_new();
35 created_links = xbt_dict_new();
36 link_bandwidth = xbt_dict_new();
37 link_latency = xbt_dict_new();
38 platform_variables = xbt_dict_new();
39 host_containers = xbt_dict_new();
40 last_platform_variables = xbt_dict_new();
41 resource_variables = xbt_dict_new ();
42 gtnets_src = xbt_dict_new ();
43 gtnets_dst = xbt_dict_new ();
46 static char *strsplit (char *input, int field, char del) //caller should free the returned string
49 int length = strlen(input), i;
50 int s = 0, e = length+1;
51 int current_field = 0;
52 for (i = 0; i < length; i++){
54 if (current_field == field){
63 //copy string from s to e (with length equal to e-s) and return
64 ret = malloc ((e-s+2)*sizeof(char));
65 strncpy (ret, input+s, e-s+1);
70 void __TRACE_surf_finalize (void)
72 xbt_dict_cursor_t cursor = NULL;
73 unsigned int cursor_ar = 0;
74 char *key, *value, *res;
78 xbt_dynar_t resources = NULL;
79 if (!IS_TRACING_PLATFORM) return;
80 if (!xbt_dict_length(last_platform_variables)){
83 /* get all resources from last_platform_variables */
84 resources = xbt_dynar_new(sizeof(char*), xbt_free);
85 xbt_dict_foreach(last_platform_variables, cursor, key, value) {
86 res = strsplit (key, 0, VARIABLE_SEPARATOR);
87 aux = strsplit (key, 1, VARIABLE_SEPARATOR);
88 if (strcmp (aux, "Time") == 0){ //only need to add one of three
89 var_cpy = xbt_strdup (res);
90 xbt_dynar_push (resources, &var_cpy);
96 /* iterate through resources array */
97 xbt_dynar_foreach (resources, cursor_ar, resource) {
98 char timekey[100], valuekey[100], variablekey[100];
101 char *variable = NULL;
102 snprintf (timekey, 100, "%s%cTime", resource, VARIABLE_SEPARATOR);
103 snprintf (valuekey, 100, "%s%cValue", resource, VARIABLE_SEPARATOR);
104 snprintf (variablekey, 100, "%s%cVariable", resource, VARIABLE_SEPARATOR);
106 time = xbt_dict_get_or_null (last_platform_variables, timekey);
108 value = xbt_dict_get (last_platform_variables, valuekey);
109 variable = xbt_dict_get (last_platform_variables, variablekey);
110 pajeSubVariable (atof(time), variable, resource, value);
112 xbt_dict_remove (last_platform_variables, timekey);
113 xbt_dict_remove (last_platform_variables, valuekey);
114 xbt_dict_remove (last_platform_variables, variablekey);
119 void __TRACE_surf_check_variable_set_to_zero (double now, const char *variable, const char *resource)
121 /* check if we have to set it to 0 */
122 if (!xbt_dict_get_or_null (platform_variables, resource)){
123 xbt_dynar_t array = xbt_dynar_new(sizeof(char*), xbt_free);
124 char *var_cpy = xbt_strdup(variable);
125 xbt_dynar_push (array, &var_cpy);
126 if (IS_TRACING_PLATFORM) pajeSetVariable (now, variable, resource, "0");
127 xbt_dict_set (platform_variables, resource, array, xbt_dynar_free_voidp);
129 xbt_dynar_t array = xbt_dict_get (platform_variables, resource);
133 xbt_dynar_foreach (array, i, cat) {
134 if (strcmp(variable, cat)==0){
139 char *var_cpy = xbt_strdup(variable);
140 xbt_dynar_push (array, &var_cpy);
141 if (IS_TRACING_PLATFORM) pajeSetVariable (now, variable, resource, "0");
147 void __TRACE_surf_update_action_state_resource (double now, double delta, const char *variable, const char *resource, double value)
150 char nowstr[100], nowdeltastr[100];
151 char timekey[100], valuekey[100], variablekey[100];
152 char *lastvariable = NULL;
153 char *lasttime = NULL;
154 char *lastvalue = NULL;
155 char *nowdeltastr_cpy = NULL;
156 char *valuestr_cpy = NULL;
157 char *variable_cpy = NULL;
159 if (!IS_TRACING_PLATFORM) return;
161 snprintf (valuestr, 100, "%f", value);
164 //fprintf (stderr, "resource = %s variable = %s (%f -> %f) value = %s\n", resource, variable, now, now+delta, valuestr);
166 __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
167 if (IS_TRACING_PLATFORM) pajeAddVariable (now, variable, resource, valuestr);
168 if (IS_TRACING_PLATFORM) pajeSubVariable (now+delta, variable, resource, valuestr);
174 * The following code replaces the code above with the objective
175 * to decrease the size of file because of unnecessary add/sub on
176 * variables. It should be re-checked before put in production.
179 snprintf (nowstr, 100, "%.15f", now);
180 snprintf (nowdeltastr, 100, "%.15f", now+delta);
182 snprintf (timekey, 100, "%s%cTime", resource, VARIABLE_SEPARATOR);
183 snprintf (valuekey, 100, "%s%cValue", resource, VARIABLE_SEPARATOR);
184 snprintf (variablekey, 100, "%s%cVariable", resource, VARIABLE_SEPARATOR);
186 lastvariable = xbt_dict_get_or_null (last_platform_variables, variablekey);
187 if (lastvariable == NULL){
188 __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
189 pajeAddVariable (now, variable, resource, valuestr);
190 nowdeltastr_cpy = xbt_strdup (nowdeltastr);
191 valuestr_cpy = xbt_strdup (valuestr);
192 variable_cpy = xbt_strdup (variable);
193 xbt_dict_set (last_platform_variables, timekey, nowdeltastr_cpy, xbt_free);
194 xbt_dict_set (last_platform_variables, valuekey, valuestr_cpy, xbt_free);
195 xbt_dict_set (last_platform_variables, variablekey, variable_cpy, xbt_free);
197 lasttime = xbt_dict_get_or_null (last_platform_variables, timekey);
198 lastvalue = xbt_dict_get_or_null (last_platform_variables, valuekey);
200 /* check if it is the same variable */
201 if (strcmp(lastvariable, variable) == 0){ /* same variable */
202 /* check if lasttime equals now */
203 if (atof(lasttime) == now){ /* lastime == now */
204 /* check if lastvalue equals valuestr */
205 if (atof(lastvalue) == value){ /* lastvalue == value (good, just advance time) */
206 char *nowdeltastr_cpy = xbt_strdup (nowdeltastr);
207 xbt_dict_set (last_platform_variables, timekey, nowdeltastr_cpy, xbt_free);
208 }else{ /* value has changed */
209 /* value has changed, subtract previous value, add new one */
210 pajeSubVariable (atof(lasttime), variable, resource, lastvalue);
211 pajeAddVariable (atof(nowstr), variable, resource, valuestr);
212 nowdeltastr_cpy = xbt_strdup (nowdeltastr);
213 valuestr_cpy = xbt_strdup (valuestr);
214 xbt_dict_set (last_platform_variables, timekey, nowdeltastr_cpy, xbt_free);
215 xbt_dict_set (last_platform_variables, valuekey, valuestr_cpy, xbt_free);
217 }else{ /* lasttime != now */
218 /* the last time is different from new starting time, subtract to lasttime and add from nowstr */
219 pajeSubVariable (atof(lasttime), variable, resource, lastvalue);
220 pajeAddVariable (atof(nowstr), variable, resource, valuestr);
221 nowdeltastr_cpy = xbt_strdup (nowdeltastr);
222 valuestr_cpy = xbt_strdup (valuestr);
223 xbt_dict_set (last_platform_variables, timekey, nowdeltastr_cpy, xbt_free);
224 xbt_dict_set (last_platform_variables, valuekey, valuestr_cpy, xbt_free);
226 }else{ /* variable has changed */
227 pajeSubVariable (atof(lasttime), lastvariable, resource, lastvalue);
228 __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
229 pajeAddVariable (now, variable, resource, valuestr);
230 nowdeltastr_cpy = xbt_strdup (nowdeltastr);
231 valuestr_cpy = xbt_strdup (valuestr);
232 variable_cpy = xbt_strdup (variable);
233 xbt_dict_set (last_platform_variables, timekey, nowdeltastr_cpy, xbt_free);
234 xbt_dict_set (last_platform_variables, valuekey, valuestr_cpy, xbt_free);
235 xbt_dict_set (last_platform_variables, variablekey, variable_cpy, xbt_free);
241 void __TRACE_surf_set_resource_variable (double date, const char *variable, const char *resource, double value)
243 char aux[100], key[100];
244 char *last_value = NULL;
245 if (!IS_TRACING) return;
246 snprintf (aux, 100, "%f", value);
247 snprintf (key, 100, "%s%c%s", resource, VARIABLE_SEPARATOR, variable);
249 last_value = xbt_dict_get_or_null(resource_variables, key);
251 if (atof(last_value) == value){
255 if (IS_TRACING_PLATFORM) pajeSetVariable (date, variable, resource, aux);
256 xbt_dict_set (resource_variables, xbt_strdup(key), xbt_strdup(aux), xbt_free);
259 void TRACE_surf_link_set_utilization (const char *name, smx_action_t smx_action, double value, double now, double delta)
262 if (!IS_TRACING || !IS_TRACED(smx_action)) return;
264 if (strcmp (name, "__loopback__")==0 ||
265 strcmp (name, "loopback")==0){ //ignore loopback updates
269 if (value == 0) return;
271 if (!xbt_dict_get_or_null (created_links, name)){
272 TRACE_surf_link_missing ();
276 snprintf (type, 100, "b%s", smx_action->category);
277 __TRACE_surf_update_action_state_resource (now, delta, type, name, value);
281 void TRACE_surf_host_set_utilization (const char *name, smx_action_t smx_action, double value, double now, double delta)
284 if (!IS_TRACING || !IS_TRACED(smx_action)) return;
290 snprintf (type, 100, "p%s", smx_action->category);
291 __TRACE_surf_update_action_state_resource (now, delta, type, name, value);
295 void TRACE_surf_link_declaration (char *name, double bw, double lat)
297 double *bw_ptr, *lat_ptr;
298 if (!IS_TRACING) return;
299 //if (IS_TRACING_PLATFORM) pajeCreateContainerWithBandwidthLatency (SIMIX_get_clock(), name, "LINK", "platform", name, bw, lat);
300 //save bw and lat information for this link
301 bw_ptr = xbt_new (double, 1);
302 lat_ptr = xbt_new (double, 1);
305 xbt_dict_set (link_bandwidth, name, bw_ptr, xbt_free);
306 xbt_dict_set (link_latency, name, lat_ptr, xbt_free);
309 void TRACE_surf_host_declaration (char *name, double power)
311 if (!IS_TRACING) return;
312 pajeCreateContainer (SIMIX_get_clock(), name, "HOST", "platform", name);
313 xbt_dict_set (host_containers, name, xbt_strdup("1"), xbt_free);
314 if (IS_TRACING_PLATFORM){
315 __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "power", name, power);
319 void TRACE_surf_link_save_endpoints (char *link_name, int src, int dst)
321 char srcidstr[100], dstidstr[100];
323 char *srcname = NULL;
324 char *dstname = NULL;
327 if (!IS_TRACING) return;
328 snprintf (srcidstr, 100, "%d", src);
329 snprintf (dstidstr, 100, "%d", dst);
330 srcname = xbt_dict_get (hosts_id, srcidstr);
331 dstname = xbt_dict_get (hosts_id, dstidstr);
332 snprintf (key, 100, "l%d-%d", src, dst);
334 if (strcmp (link_name, "__loopback__")==0 ||
335 strcmp (link_name, "loopback")==0){ //ignore loopback updates
339 if (!xbt_dict_get_or_null (created_links, link_name)){
340 //if (IS_TRACING_PLATFORM) pajeStartLink (SIMIX_get_clock(), "edge", "platform", "route", srcname, key);
341 //if (IS_TRACING_PLATFORM) pajeEndLink (SIMIX_get_clock()+0.1, "edge", "platform", "route", dstname, key);
342 bw = xbt_dict_get (link_bandwidth, link_name);
343 lat = xbt_dict_get (link_latency, link_name);
344 pajeCreateContainerWithBandwidthLatencySrcDst (SIMIX_get_clock(), link_name, "LINK", "platform", link_name, *bw, *lat, srcname, dstname);
345 if (IS_TRACING_PLATFORM) __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "bandwidth", link_name, *bw);
346 if (IS_TRACING_PLATFORM) __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "latency", link_name, *lat);
347 xbt_dict_set (created_links, link_name, xbt_strdup ("1"), xbt_free);
351 void TRACE_surf_host_set_power (double date, char *resource, double power)
353 __TRACE_surf_set_resource_variable (date, "power", resource, power);
356 void TRACE_surf_link_set_bandwidth (double date, char *resource, double bandwidth)
358 __TRACE_surf_set_resource_variable (date, "bandwidth", resource, bandwidth);
361 void TRACE_surf_link_set_latency (double date, char *resource, double latency)
363 __TRACE_surf_set_resource_variable (date, "latency", resource, latency);
366 void TRACE_surf_host_define_id (const char *name, int host_id)
369 if (!IS_TRACING) return;
370 snprintf (strid, 100, "%d", host_id);
371 xbt_dict_set (hosts_id, name, strdup(strid), free);
372 xbt_dict_set (hosts_id, strid, strdup(name), free);
375 /* to trace gtnets */
376 void TRACE_surf_gtnets_communicate (void *action, int src, int dst)
378 char key[100], aux[100];
379 if (!IS_TRACING) return;
380 snprintf (key, 100, "%p", action);
382 snprintf (aux, 100, "%d", src);
383 xbt_dict_set (gtnets_src, key, xbt_strdup(aux), xbt_free);
384 snprintf (aux, 100, "%d", dst);
385 xbt_dict_set (gtnets_dst, key, xbt_strdup(aux), xbt_free);
388 int TRACE_surf_gtnets_get_src (void *action)
392 if (!IS_TRACING) return -1;
393 snprintf (key, 100, "%p", action);
395 aux = xbt_dict_get_or_null (gtnets_src, key);
403 int TRACE_surf_gtnets_get_dst (void *action)
407 if (!IS_TRACING) return -1;
408 snprintf (key, 100, "%p", action);
410 aux = xbt_dict_get_or_null (gtnets_dst, key);
418 void TRACE_surf_gtnets_destroy (void *action)
421 if (!IS_TRACING) return;
422 snprintf (key, 100, "%p", action);
423 xbt_dict_remove (gtnets_src, key);
424 xbt_dict_remove (gtnets_dst, key);
427 void TRACE_surf_link_missing (void)
429 CRITICAL0("The trace cannot be done because "
430 "the platform you are using contains "
431 "routes with more than one link.");
432 THROW0(tracing_error, TRACE_ERROR_COMPLEX_ROUTES, "Tracing failed");
435 void TRACE_msg_clean (void)
438 xbt_dict_cursor_t cursor = NULL;
439 __TRACE_surf_finalize();
441 /* get all host from host_containers */
442 xbt_dict_foreach(host_containers, cursor, key, value) {
443 pajeDestroyContainer (MSG_get_clock(), "HOST", key);
447 void TRACE_surf_host_vivaldi_parse (char *host, double x, double y, double h)
450 if (!IS_TRACING || !IS_TRACING_PLATFORM) return;
452 snprintf (valuestr, 100, "%g", x);
453 pajeSetVariable (0, "vivaldi_x", host, valuestr);
454 snprintf (valuestr, 100, "%g", y);
455 pajeSetVariable (0, "vivaldi_y", host, valuestr);
456 snprintf (valuestr, 100, "%g", h);
457 pajeSetVariable (0, "vivaldi_h", host, valuestr);