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
48 int length = strlen(input), i;
49 int s = 0, e = length+1;
50 int current_field = 0;
51 for (i = 0; i < length; i++){
53 if (current_field == field){
62 //copy string from s to e (with length equal to e-s) and return
63 char *ret = malloc ((e-s+2)*sizeof(char));
64 strncpy (ret, input+s, e-s+1);
69 void __TRACE_surf_finalize (void)
71 if (!IS_TRACING_PLATFORM) return;
72 if (!xbt_dict_length(last_platform_variables)){
75 xbt_dict_cursor_t cursor = NULL;
76 unsigned int cursor_ar = 0;
77 char *key, *value, *res;
80 /* get all resources from last_platform_variables */
81 xbt_dynar_t resources = xbt_dynar_new(sizeof(char)*200, xbt_free);
82 xbt_dict_foreach(last_platform_variables, cursor, key, value) {
83 res = strsplit (key, 0, VARIABLE_SEPARATOR);
84 char *aux = strsplit (key, 1, VARIABLE_SEPARATOR);
85 if (strcmp (aux, "Time") == 0){ //only need to add one of three
86 xbt_dynar_push (resources, xbt_strdup(res));
92 /* iterate through resources array */
93 xbt_dynar_foreach (resources, cursor_ar, resource) {
94 char timekey[100], valuekey[100], variablekey[100];
95 snprintf (timekey, 100, "%s%cTime", resource, VARIABLE_SEPARATOR);
96 snprintf (valuekey, 100, "%s%cValue", resource, VARIABLE_SEPARATOR);
97 snprintf (variablekey, 100, "%s%cVariable", resource, VARIABLE_SEPARATOR);
99 char *time = xbt_dict_get_or_null (last_platform_variables, timekey);
101 char *value = xbt_dict_get (last_platform_variables, valuekey);
102 char *variable = xbt_dict_get (last_platform_variables, variablekey);
103 pajeSubVariable (atof(time), variable, resource, value);
105 xbt_dict_remove (last_platform_variables, timekey);
106 xbt_dict_remove (last_platform_variables, valuekey);
107 xbt_dict_remove (last_platform_variables, variablekey);
112 void __TRACE_surf_check_variable_set_to_zero (double now, const char *variable, const char *resource)
114 /* check if we have to set it to 0 */
115 if (!xbt_dict_get_or_null (platform_variables, resource)){
116 xbt_dynar_t array = xbt_dynar_new(100*sizeof(char), xbt_free);
117 xbt_dynar_push (array, xbt_strdup(variable));
118 if (IS_TRACING_PLATFORM) pajeSetVariable (now, variable, resource, "0");
119 xbt_dict_set (platform_variables, resource, array, xbt_dynar_free_voidp);
121 xbt_dynar_t array = xbt_dict_get (platform_variables, resource);
125 xbt_dynar_foreach (array, i, cat) {
126 if (strcmp(variable, cat)==0){
131 xbt_dynar_push (array, strdup(variable));
132 if (IS_TRACING_PLATFORM) pajeSetVariable (now, variable, resource, "0");
138 void __TRACE_surf_update_action_state_resource (double now, double delta, const char *variable, const char *resource, double value)
140 if (!IS_TRACING_PLATFORM) return;
143 snprintf (valuestr, 100, "%f", value);
146 //fprintf (stderr, "resource = %s variable = %s (%f -> %f) value = %s\n", resource, variable, now, now+delta, valuestr);
148 __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
149 if (IS_TRACING_PLATFORM) pajeAddVariable (now, variable, resource, valuestr);
150 if (IS_TRACING_PLATFORM) pajeSubVariable (now+delta, variable, resource, valuestr);
156 * The following code replaces the code above with the objective
157 * to decrease the size of file because of unnecessary add/sub on
158 * variables. It should be re-checked before put in production.
161 char nowstr[100], nowdeltastr[100];
162 snprintf (nowstr, 100, "%.15f", now);
163 snprintf (nowdeltastr, 100, "%.15f", now+delta);
165 char timekey[100], valuekey[100], variablekey[100];
166 snprintf (timekey, 100, "%s%cTime", resource, VARIABLE_SEPARATOR);
167 snprintf (valuekey, 100, "%s%cValue", resource, VARIABLE_SEPARATOR);
168 snprintf (variablekey, 100, "%s%cVariable", resource, VARIABLE_SEPARATOR);
170 char *lastvariable = xbt_dict_get_or_null (last_platform_variables, variablekey);
171 if (lastvariable == NULL){
172 __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
173 pajeAddVariable (now, variable, resource, valuestr);
174 xbt_dict_set (last_platform_variables, xbt_strdup (timekey), xbt_strdup (nowdeltastr), xbt_free);
175 xbt_dict_set (last_platform_variables, xbt_strdup (valuekey), xbt_strdup (valuestr), xbt_free);
176 xbt_dict_set (last_platform_variables, xbt_strdup (variablekey), xbt_strdup (variable), xbt_free);
178 char *lasttime = xbt_dict_get_or_null (last_platform_variables, timekey);
179 char *lastvalue = xbt_dict_get_or_null (last_platform_variables, valuekey);
181 /* check if it is the same variable */
182 if (strcmp(lastvariable, variable) == 0){ /* same variable */
183 /* check if lasttime equals now */
184 if (atof(lasttime) == now){ /* lastime == now */
185 /* check if lastvalue equals valuestr */
186 if (atof(lastvalue) == value){ /* lastvalue == value (good, just advance time) */
187 xbt_dict_set (last_platform_variables, xbt_strdup(timekey), xbt_strdup(nowdeltastr), xbt_free);
188 }else{ /* value has changed */
189 /* value has changed, subtract previous value, add new one */
190 pajeSubVariable (atof(lasttime), variable, resource, lastvalue);
191 pajeAddVariable (atof(nowstr), variable, resource, valuestr);
192 xbt_dict_set (last_platform_variables, xbt_strdup(timekey), xbt_strdup(nowdeltastr), xbt_free);
193 xbt_dict_set (last_platform_variables, xbt_strdup(valuekey), xbt_strdup(valuestr), xbt_free);
195 }else{ /* lasttime != now */
196 /* the last time is different from new starting time, subtract to lasttime and add from nowstr */
197 pajeSubVariable (atof(lasttime), variable, resource, lastvalue);
198 pajeAddVariable (atof(nowstr), variable, resource, valuestr);
199 xbt_dict_set (last_platform_variables, xbt_strdup(timekey), xbt_strdup(nowdeltastr), xbt_free);
200 xbt_dict_set (last_platform_variables, xbt_strdup(valuekey), xbt_strdup(valuestr), xbt_free);
202 }else{ /* variable has changed */
203 pajeSubVariable (atof(lasttime), lastvariable, resource, lastvalue);
204 __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
205 pajeAddVariable (now, variable, resource, valuestr);
206 xbt_dict_set (last_platform_variables, xbt_strdup (timekey), xbt_strdup (nowdeltastr), xbt_free);
207 xbt_dict_set (last_platform_variables, xbt_strdup (valuekey), xbt_strdup (valuestr), xbt_free);
208 xbt_dict_set (last_platform_variables, xbt_strdup (variablekey), xbt_strdup (variable), xbt_free);
214 void __TRACE_surf_set_resource_variable (double date, const char *variable, const char *resource, double value)
216 if (!IS_TRACING) return;
217 char aux[100], key[100];
218 snprintf (aux, 100, "%f", value);
219 snprintf (key, 100, "%s%c%s", resource, VARIABLE_SEPARATOR, variable);
221 char *last_value = xbt_dict_get_or_null(resource_variables, key);
223 if (atof(last_value) == value){
227 if (IS_TRACING_PLATFORM) pajeSetVariable (date, variable, resource, aux);
228 xbt_dict_set (resource_variables, xbt_strdup(key), xbt_strdup(aux), xbt_free);
231 void TRACE_surf_link_set_utilization (const char *name, smx_action_t smx_action, double value, double now, double delta)
233 if (!IS_TRACING || !IS_TRACED(smx_action)) return;
235 if (strcmp (name, "__loopback__")==0 ||
236 strcmp (name, "loopback")==0){ //ignore loopback updates
240 if (value == 0) return;
242 if (!xbt_dict_get_or_null (created_links, name)){
243 TRACE_surf_link_missing ();
248 snprintf (type, 100, "b%s", smx_action->category);
249 __TRACE_surf_update_action_state_resource (now, delta, type, name, value);
253 void TRACE_surf_host_set_utilization (const char *name, smx_action_t smx_action, double value, double now, double delta)
255 if (!IS_TRACING || !IS_TRACED(smx_action)) return;
262 snprintf (type, 100, "p%s", smx_action->category);
263 __TRACE_surf_update_action_state_resource (now, delta, type, name, value);
267 void TRACE_surf_link_declaration (char *name, double bw, double lat)
269 if (!IS_TRACING) return;
270 //if (IS_TRACING_PLATFORM) pajeCreateContainerWithBandwidthLatency (SIMIX_get_clock(), name, "LINK", "platform", name, bw, lat);
271 //save bw and lat information for this link
272 double *bw_ptr, *lat_ptr;
273 bw_ptr = xbt_new (double, 1);
274 lat_ptr = xbt_new (double, 1);
277 xbt_dict_set (link_bandwidth, xbt_strdup(name), bw_ptr, xbt_free);
278 xbt_dict_set (link_latency, xbt_strdup(name), lat_ptr, xbt_free);
281 void TRACE_surf_host_declaration (char *name, double power)
283 if (!IS_TRACING) return;
284 pajeCreateContainer (SIMIX_get_clock(), name, "HOST", "platform", name);
285 xbt_dict_set (host_containers, xbt_strdup(name), xbt_strdup("1"), xbt_free);
286 if (IS_TRACING_PLATFORM){
287 __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "power", name, power);
291 void TRACE_surf_link_save_endpoints (char *link_name, int src, int dst)
293 if (!IS_TRACING) return;
294 char srcidstr[100], dstidstr[100];
295 snprintf (srcidstr, 100, "%d", src);
296 snprintf (dstidstr, 100, "%d", dst);
297 char *srcname = xbt_dict_get (hosts_id, srcidstr);
298 char *dstname = xbt_dict_get (hosts_id, dstidstr);
301 snprintf (key, 100, "l%d-%d", src, dst);
303 if (strcmp (link_name, "__loopback__")==0 ||
304 strcmp (link_name, "loopback")==0){ //ignore loopback updates
308 if (!xbt_dict_get_or_null (created_links, link_name)){
309 //if (IS_TRACING_PLATFORM) pajeStartLink (SIMIX_get_clock(), "edge", "platform", "route", srcname, key);
310 //if (IS_TRACING_PLATFORM) pajeEndLink (SIMIX_get_clock()+0.1, "edge", "platform", "route", dstname, key);
311 double *bw = xbt_dict_get (link_bandwidth, link_name);
312 double *lat = xbt_dict_get (link_latency, link_name);
313 pajeCreateContainerWithBandwidthLatencySrcDst (SIMIX_get_clock(), link_name, "LINK", "platform", link_name, *bw, *lat, srcname, dstname);
314 if (IS_TRACING_PLATFORM) __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "bandwidth", link_name, *bw);
315 if (IS_TRACING_PLATFORM) __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "latency", link_name, *lat);
316 xbt_dict_set (created_links, xbt_strdup(link_name), xbt_strdup ("1"), xbt_free);
320 void TRACE_surf_host_set_power (double date, char *resource, double power)
322 __TRACE_surf_set_resource_variable (date, "power", resource, power);
325 void TRACE_surf_link_set_bandwidth (double date, char *resource, double bandwidth)
327 __TRACE_surf_set_resource_variable (date, "bandwidth", resource, bandwidth);
330 void TRACE_surf_link_set_latency (double date, char *resource, double latency)
332 __TRACE_surf_set_resource_variable (date, "latency", resource, latency);
335 void TRACE_surf_host_define_id (const char *name, int host_id)
337 if (!IS_TRACING) return;
339 snprintf (strid, 100, "%d", host_id);
340 xbt_dict_set (hosts_id, strdup(name), strdup(strid), free);
341 xbt_dict_set (hosts_id, strdup(strid), strdup(name), free);
344 /* to trace gtnets */
345 void TRACE_surf_gtnets_communicate (void *action, int src, int dst)
347 if (!IS_TRACING) return;
348 char key[100], aux[100];
349 snprintf (key, 100, "%p", action);
351 snprintf (aux, 100, "%d", src);
352 xbt_dict_set (gtnets_src, xbt_strdup(key), xbt_strdup(aux), xbt_free);
353 snprintf (aux, 100, "%d", dst);
354 xbt_dict_set (gtnets_dst, xbt_strdup(key), xbt_strdup(aux), xbt_free);
357 int TRACE_surf_gtnets_get_src (void *action)
359 if (!IS_TRACING) return -1;
361 snprintf (key, 100, "%p", action);
363 char *aux = xbt_dict_get_or_null (gtnets_src, key);
371 int TRACE_surf_gtnets_get_dst (void *action)
373 if (!IS_TRACING) return -1;
375 snprintf (key, 100, "%p", action);
377 char *aux = xbt_dict_get_or_null (gtnets_dst, key);
385 void TRACE_surf_gtnets_destroy (void *action)
387 if (!IS_TRACING) return;
389 snprintf (key, 100, "%p", action);
390 xbt_dict_remove (gtnets_src, key);
391 xbt_dict_remove (gtnets_dst, key);
394 void TRACE_surf_link_missing (void)
396 CRITICAL0("The trace cannot be done because "
397 "the platform you are using contains "
398 "routes with more than one link.");
399 THROW0(tracing_error, TRACE_ERROR_COMPLEX_ROUTES, "Tracing failed");
402 void TRACE_msg_clean (void)
404 __TRACE_surf_finalize();
406 xbt_dict_cursor_t cursor = NULL;
409 /* get all host from host_containers */
410 xbt_dict_foreach(host_containers, cursor, key, value) {
411 pajeDestroyContainer (MSG_get_clock(), "HOST", key);
415 void TRACE_surf_host_vivaldi_parse (char *host, double x, double y, double h)
417 if (!IS_TRACING || !IS_TRACING_PLATFORM) return;
420 snprintf (valuestr, 100, "%g", x);
421 pajeSetVariable (0, "vivaldi_x", host, valuestr);
422 snprintf (valuestr, 100, "%g", y);
423 pajeSetVariable (0, "vivaldi_y", host, valuestr);
424 snprintf (valuestr, 100, "%g", h);
425 pajeSetVariable (0, "vivaldi_h", host, valuestr);