4 * Created on: Nov 27, 2009
5 * Author: Lucas Schnorr
6 * License: This program is free software; you can redistribute
7 * it and/or modify it under the terms of the license
8 * (GNU LGPL) which comes with this package.
10 * Copyright (c) 2009 The SimGrid team.
13 #include "instr/private.h"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(tracing_surf,tracing,"Tracing Surf");
19 #define VARIABLE_SEPARATOR '#'
21 static xbt_dict_t hosts_id;
22 static xbt_dict_t created_links;
23 static xbt_dict_t link_bandwidth;
24 static xbt_dict_t link_latency;
25 static xbt_dict_t host_containers;
26 //static xbt_dict_t platform_variable_last_value; /* to control the amount of add/sub variables events*/
27 //static xbt_dict_t platform_variable_last_time; /* to control the amount of add/sub variables events*/
29 static xbt_dict_t last_platform_variables; /* to control the amount of add/sub variables events:
30 dict with key {RESOURCE_NAME}#Time or {RESOURCE_NAME}#Value of dict with variables types == string */
32 static xbt_dict_t platform_variables; /* host or link name -> array of categories */
34 static xbt_dict_t resource_variables; /* (host|link)#variable -> value */
37 static xbt_dict_t gtnets_src; /* %p (action) -> %s */
38 static xbt_dict_t gtnets_dst; /* %p (action) -> %s */
40 void __TRACE_surf_init (void)
42 hosts_id = xbt_dict_new();
43 created_links = xbt_dict_new();
44 link_bandwidth = xbt_dict_new();
45 link_latency = xbt_dict_new();
46 platform_variables = xbt_dict_new();
47 host_containers = xbt_dict_new();
49 //platform_variable_last_value = xbt_dict_new();
50 //platform_variable_last_time = xbt_dict_new();
52 last_platform_variables = xbt_dict_new();
54 resource_variables = xbt_dict_new ();
56 gtnets_src = xbt_dict_new ();
57 gtnets_dst = xbt_dict_new ();
60 static char *strsplit (char *input, int field, char del) //caller should free the returned string
62 int length = strlen(input), i;
63 int s = 0, e = length+1;
64 int current_field = 0;
65 for (i = 0; i < length; i++){
67 if (current_field == field){
76 //copy string from s to e (with length equal to e-s) and return
77 char *ret = malloc ((e-s+2)*sizeof(char));
78 strncpy (ret, input+s, e-s+1);
83 void __TRACE_surf_finalize (void)
85 if (!IS_TRACING_PLATFORM) return;
86 if (!xbt_dict_length(last_platform_variables)){
89 xbt_dict_cursor_t cursor = NULL;
90 unsigned int cursor_ar = 0;
91 char *key, *value, *res;
94 /* get all resources from last_platform_variables */
95 xbt_dynar_t resources = xbt_dynar_new(sizeof(char)*200, xbt_free);
96 xbt_dict_foreach(last_platform_variables, cursor, key, value) {
97 res = strsplit (key, 0, VARIABLE_SEPARATOR);
98 char *aux = strsplit (key, 1, VARIABLE_SEPARATOR);
99 if (strcmp (aux, "Time") == 0){ //only need to add one of three
100 xbt_dynar_push (resources, xbt_strdup(res));
106 /* iterate through resources array */
107 xbt_dynar_foreach (resources, cursor_ar, resource) {
108 char timekey[100], valuekey[100], variablekey[100];
109 snprintf (timekey, 100, "%s%cTime", resource, VARIABLE_SEPARATOR);
110 snprintf (valuekey, 100, "%s%cValue", resource, VARIABLE_SEPARATOR);
111 snprintf (variablekey, 100, "%s%cVariable", resource, VARIABLE_SEPARATOR);
113 char *time = xbt_dict_get_or_null (last_platform_variables, timekey);
115 char *value = xbt_dict_get (last_platform_variables, valuekey);
116 char *variable = xbt_dict_get (last_platform_variables, variablekey);
117 pajeSubVariable (atof(time), variable, resource, value);
119 //TODO: should remove, but it is causing sigabort
120 //xbt_dict_remove (last_platform_variables, timekey);
121 //xbt_dict_remove (last_platform_variables, valuekey);
122 //xbt_dict_remove (last_platform_variables, variablekey);
127 void __TRACE_surf_check_variable_set_to_zero (double now, const char *variable, const char *resource)
129 /* check if we have to set it to 0 */
130 if (!xbt_dict_get_or_null (platform_variables, resource)){
131 xbt_dynar_t array = xbt_dynar_new(100*sizeof(char), xbt_free);
132 xbt_dynar_push (array, xbt_strdup(variable));
133 if (IS_TRACING_PLATFORM) pajeSetVariable (now, variable, resource, "0");
134 xbt_dict_set (platform_variables, resource, array, xbt_dynar_free_voidp);
136 xbt_dynar_t array = xbt_dict_get (platform_variables, resource);
140 xbt_dynar_foreach (array, i, cat) {
141 if (strcmp(variable, cat)==0){
146 xbt_dynar_push (array, strdup(variable));
147 if (IS_TRACING_PLATFORM) pajeSetVariable (now, variable, resource, "0");
153 void __TRACE_surf_update_action_state_resource (double now, double delta, const char *variable, const char *resource, double value)
155 if (!IS_TRACING_PLATFORM) return;
158 snprintf (valuestr, 100, "%f", value);
161 //fprintf (stderr, "resource = %s variable = %s (%f -> %f) value = %s\n", resource, variable, now, now+delta, valuestr);
163 __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
164 if (IS_TRACING_PLATFORM) pajeAddVariable (now, variable, resource, valuestr);
165 if (IS_TRACING_PLATFORM) pajeSubVariable (now+delta, variable, resource, valuestr);
171 * The following code replaces the code above with the objective
172 * to decrease the size of file because of unnecessary add/sub on
173 * variables. It should be re-checked before put in production.
176 char nowstr[100], nowdeltastr[100];
177 snprintf (nowstr, 100, "%.15f", now);
178 snprintf (nowdeltastr, 100, "%.15f", now+delta);
180 char timekey[100], valuekey[100], variablekey[100];
181 snprintf (timekey, 100, "%s%cTime", resource, VARIABLE_SEPARATOR);
182 snprintf (valuekey, 100, "%s%cValue", resource, VARIABLE_SEPARATOR);
183 snprintf (variablekey, 100, "%s%cVariable", resource, VARIABLE_SEPARATOR);
185 char *lastvariable = xbt_dict_get_or_null (last_platform_variables, variablekey);
186 if (lastvariable == NULL){
187 __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
188 pajeAddVariable (now, variable, resource, valuestr);
189 xbt_dict_set (last_platform_variables, xbt_strdup (timekey), xbt_strdup (nowdeltastr), xbt_free);
190 xbt_dict_set (last_platform_variables, xbt_strdup (valuekey), xbt_strdup (valuestr), xbt_free);
191 xbt_dict_set (last_platform_variables, xbt_strdup (variablekey), xbt_strdup (variable), xbt_free);
193 char *lasttime = xbt_dict_get_or_null (last_platform_variables, timekey);
194 char *lastvalue = xbt_dict_get_or_null (last_platform_variables, valuekey);
196 /* check if it is the same variable */
197 if (strcmp(lastvariable, variable) == 0){ /* same variable */
198 /* check if lasttime equals now */
199 if (atof(lasttime) == now){ /* lastime == now */
200 /* check if lastvalue equals valuestr */
201 if (atof(lastvalue) == value){ /* lastvalue == value (good, just advance time) */
202 xbt_dict_set (last_platform_variables, xbt_strdup(timekey), xbt_strdup(nowdeltastr), xbt_free);
203 }else{ /* value has changed */
204 /* value has changed, subtract previous value, add new one */
205 pajeSubVariable (atof(lasttime), variable, resource, lastvalue);
206 pajeAddVariable (atof(nowstr), variable, resource, valuestr);
207 xbt_dict_set (last_platform_variables, xbt_strdup(timekey), xbt_strdup(nowdeltastr), xbt_free);
208 xbt_dict_set (last_platform_variables, xbt_strdup(valuekey), xbt_strdup(valuestr), xbt_free);
210 }else{ /* lasttime != now */
211 /* the last time is different from new starting time, subtract to lasttime and add from nowstr */
212 pajeSubVariable (atof(lasttime), variable, resource, lastvalue);
213 pajeAddVariable (atof(nowstr), variable, resource, valuestr);
214 xbt_dict_set (last_platform_variables, xbt_strdup(timekey), xbt_strdup(nowdeltastr), xbt_free);
215 xbt_dict_set (last_platform_variables, xbt_strdup(valuekey), xbt_strdup(valuestr), xbt_free);
217 }else{ /* variable has changed */
218 pajeSubVariable (atof(lasttime), lastvariable, resource, lastvalue);
219 __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
220 pajeAddVariable (now, variable, resource, valuestr);
221 xbt_dict_set (last_platform_variables, xbt_strdup (timekey), xbt_strdup (nowdeltastr), xbt_free);
222 xbt_dict_set (last_platform_variables, xbt_strdup (valuekey), xbt_strdup (valuestr), xbt_free);
223 xbt_dict_set (last_platform_variables, xbt_strdup (variablekey), xbt_strdup (variable), xbt_free);
229 void __TRACE_surf_set_resource_variable (double date, const char *variable, const char *resource, double value)
231 if (!IS_TRACING) return;
232 char aux[100], key[100];
233 snprintf (aux, 100, "%f", value);
234 snprintf (key, 100, "%s%c%s", resource, VARIABLE_SEPARATOR, variable);
236 char *last_value = xbt_dict_get_or_null(resource_variables, key);
238 if (atof(last_value) == value){
242 if (IS_TRACING_PLATFORM) pajeSetVariable (date, variable, resource, aux);
243 xbt_dict_set (resource_variables, xbt_strdup(key), xbt_strdup(aux), xbt_free);
246 void TRACE_surf_update_action_state (void *surf_action, smx_action_t smx_action,
247 double value, const char *stateValue, double now, double delta)
251 void TRACE_surf_update_action_state_net_resource (const char *name, smx_action_t smx_action, double value, double now, double delta)
253 if (!IS_TRACING || !IS_TRACED(smx_action)) return;
255 if (strcmp (name, "__loopback__")==0 ||
256 strcmp (name, "loopback")==0){ //ignore loopback updates
260 if (value == 0) return;
262 if (!xbt_dict_get_or_null (created_links, name)){
263 TRACE_surf_missing_link ();
268 snprintf (type, 100, "b%s", smx_action->category);
269 __TRACE_surf_update_action_state_resource (now, delta, type, name, value);
273 void TRACE_surf_update_action_state_cpu_resource (const char *name, smx_action_t smx_action, double value, double now, double delta)
275 if (!IS_TRACING || !IS_TRACED(smx_action)) return;
282 snprintf (type, 100, "p%s", smx_action->category);
283 __TRACE_surf_update_action_state_resource (now, delta, type, name, value);
287 void TRACE_surf_net_link_new (char *name, double bw, double lat)
289 if (!IS_TRACING) return;
290 //if (IS_TRACING_PLATFORM) pajeCreateContainerWithBandwidthLatency (SIMIX_get_clock(), name, "LINK", "platform", name, bw, lat);
291 //save bw and lat information for this link
292 double *bw_ptr, *lat_ptr;
293 bw_ptr = xbt_new (double, 1);
294 lat_ptr = xbt_new (double, 1);
297 xbt_dict_set (link_bandwidth, xbt_strdup(name), bw_ptr, xbt_free);
298 xbt_dict_set (link_latency, xbt_strdup(name), lat_ptr, xbt_free);
301 void TRACE_surf_cpu_new (char *name, double power)
303 if (!IS_TRACING) return;
304 if (IS_TRACING_PLATFORM){
305 pajeCreateContainer (SIMIX_get_clock(), name, "HOST", "platform", name);
306 xbt_dict_set (host_containers, xbt_strdup(name), xbt_strdup("1"), xbt_free);
308 __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "power", name, power);
311 void TRACE_surf_routing_full_parse_end (char *link_name, int src, int dst)
313 if (!IS_TRACING) return;
314 char srcidstr[100], dstidstr[100];
315 snprintf (srcidstr, 100, "%d", src);
316 snprintf (dstidstr, 100, "%d", dst);
317 char *srcname = xbt_dict_get (hosts_id, srcidstr);
318 char *dstname = xbt_dict_get (hosts_id, dstidstr);
321 snprintf (key, 100, "l%d-%d", src, dst);
323 if (strcmp (link_name, "__loopback__")==0 ||
324 strcmp (link_name, "loopback")==0){ //ignore loopback updates
328 if (!xbt_dict_get_or_null (created_links, link_name)){
329 //if (IS_TRACING_PLATFORM) pajeStartLink (SIMIX_get_clock(), "edge", "platform", "route", srcname, key);
330 //if (IS_TRACING_PLATFORM) pajeEndLink (SIMIX_get_clock()+0.1, "edge", "platform", "route", dstname, key);
331 double *bw = xbt_dict_get (link_bandwidth, link_name);
332 double *lat = xbt_dict_get (link_latency, link_name);
333 if (IS_TRACING_PLATFORM) pajeCreateContainerWithBandwidthLatencySrcDst (SIMIX_get_clock(), link_name, "LINK", "platform", link_name, *bw, *lat, srcname, dstname);
334 __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "bandwidth", link_name, *bw);
335 __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "latency", link_name, *lat);
336 xbt_dict_set (created_links, xbt_strdup(link_name), xbt_strdup ("1"), xbt_free);
340 void TRACE_surf_cpu_set_power (double date, char *resource, double power)
342 __TRACE_surf_set_resource_variable (date, "power", resource, power);
345 void TRACE_surf_link_set_bandwidth (double date, char *resource, double bandwidth)
347 __TRACE_surf_set_resource_variable (date, "bandwidth", resource, bandwidth);
350 void TRACE_surf_link_set_latency (double date, char *resource, double latency)
352 __TRACE_surf_set_resource_variable (date, "latency", resource, latency);
355 void TRACE_surf_define_host_id (const char *name, int host_id)
357 if (!IS_TRACING) return;
359 snprintf (strid, 100, "%d", host_id);
360 xbt_dict_set (hosts_id, strdup(name), strdup(strid), free);
361 xbt_dict_set (hosts_id, strdup(strid), strdup(name), free);
364 /* to trace gtnets */
365 void TRACE_surf_gtnets_communicate (void *action, int src, int dst)
367 if (!IS_TRACING) return;
368 char key[100], aux[100];
369 snprintf (key, 100, "%p", action);
371 snprintf (aux, 100, "%d", src);
372 xbt_dict_set (gtnets_src, xbt_strdup(key), xbt_strdup(aux), xbt_free);
373 snprintf (aux, 100, "%d", dst);
374 xbt_dict_set (gtnets_dst, xbt_strdup(key), xbt_strdup(aux), xbt_free);
377 int TRACE_surf_gtnets_get_src (void *action)
379 if (!IS_TRACING) return -1;
381 snprintf (key, 100, "%p", action);
383 char *aux = xbt_dict_get_or_null (gtnets_src, key);
391 int TRACE_surf_gtnets_get_dst (void *action)
393 if (!IS_TRACING) return -1;
395 snprintf (key, 100, "%p", action);
397 char *aux = xbt_dict_get_or_null (gtnets_dst, key);
405 void TRACE_surf_gtnets_destroy (void *action)
407 if (!IS_TRACING) return;
409 snprintf (key, 100, "%p", action);
410 xbt_dict_remove (gtnets_src, key);
411 xbt_dict_remove (gtnets_dst, key);
414 void TRACE_surf_missing_link (void)
416 CRITICAL0("The trace cannot be done because "
417 "the platform you are using contains "
418 "routes with more than one link.");
419 THROW0(tracing_error, TRACE_ERROR_COMPLEX_ROUTES, "Tracing failed");
422 void TRACE_msg_clean (void)
424 __TRACE_surf_finalize();
426 xbt_dict_cursor_t cursor = NULL;
427 unsigned int cursor_ar = 0;
430 /* get all host from host_containers */
431 xbt_dict_foreach(host_containers, cursor, key, value) {
432 if (IS_TRACING_PLATFORM) pajeDestroyContainer (MSG_get_clock(), "HOST", key);
436 void TRACE_surf_vivaldi_parse_host (char *host, double x, double y, double h)
438 if (!IS_TRACING || !IS_TRACING_PLATFORM) return;
441 snprintf (valuestr, 100, "%g", x);
442 pajeSetVariable (0, "vivaldi_x", host, valuestr);
443 snprintf (valuestr, 100, "%g", y);
444 pajeSetVariable (0, "vivaldi_y", host, valuestr);
445 snprintf (valuestr, 100, "%g", h);
446 pajeSetVariable (0, "vivaldi_h", host, valuestr);