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"
14 #include "instr/config.h"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(tracing_surf,tracing,"Tracing Surf");
20 #define VARIABLE_SEPARATOR '#'
22 static xbt_dict_t hosts_id;
23 static xbt_dict_t created_links;
24 static xbt_dict_t link_bandwidth;
25 static xbt_dict_t link_latency;
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();
48 //platform_variable_last_value = xbt_dict_new();
49 //platform_variable_last_time = xbt_dict_new();
51 last_platform_variables = xbt_dict_new();
53 resource_variables = xbt_dict_new ();
55 gtnets_src = xbt_dict_new ();
56 gtnets_dst = xbt_dict_new ();
59 static char *strsplit (char *input, int field, char del) //caller should free the returned string
61 int length = strlen(input), i;
62 int s = 0, e = length+1;
63 int current_field = 0;
64 for (i = 0; i < length; i++){
66 if (current_field == field){
75 //copy string from s to e (with length equal to e-s) and return
76 char *ret = malloc ((e-s+2)*sizeof(char));
77 strncpy (ret, input+s, e-s+1);
82 void __TRACE_surf_finalize (void)
84 if (!IS_TRACING_PLATFORM) return;
85 if (!xbt_dict_length(last_platform_variables)){
88 xbt_dict_cursor_t cursor = NULL;
89 unsigned int cursor_ar = 0;
90 char *key, *value, *res;
93 /* get all resources from last_platform_variables */
94 xbt_dynar_t resources = xbt_dynar_new(sizeof(char)*200, xbt_free);
95 xbt_dict_foreach(last_platform_variables, cursor, key, value) {
96 res = strsplit (key, 0, VARIABLE_SEPARATOR);
97 char *aux = strsplit (key, 1, VARIABLE_SEPARATOR);
98 if (strcmp (aux, "Time") == 0){ //only need to add one of three
99 xbt_dynar_push (resources, xbt_strdup(res));
105 /* iterate through resources array */
106 xbt_dynar_foreach (resources, cursor_ar, resource) {
107 char timekey[100], valuekey[100], variablekey[100];
108 snprintf (timekey, 100, "%s%cTime", resource, VARIABLE_SEPARATOR);
109 snprintf (valuekey, 100, "%s%cValue", resource, VARIABLE_SEPARATOR);
110 snprintf (variablekey, 100, "%s%cVariable", resource, VARIABLE_SEPARATOR);
112 char *time = xbt_dict_get_or_null (last_platform_variables, timekey);
114 char *value = xbt_dict_get (last_platform_variables, valuekey);
115 char *variable = xbt_dict_get (last_platform_variables, variablekey);
116 pajeSubVariable (atof(time), variable, resource, value);
118 //TODO: should remove, but it is causing sigabort
119 //xbt_dict_remove (last_platform_variables, timekey);
120 //xbt_dict_remove (last_platform_variables, valuekey);
121 //xbt_dict_remove (last_platform_variables, variablekey);
126 void __TRACE_surf_check_variable_set_to_zero (double now, const char *variable, const char *resource)
128 /* check if we have to set it to 0 */
129 if (!xbt_dict_get_or_null (platform_variables, resource)){
130 xbt_dynar_t array = xbt_dynar_new(100*sizeof(char), xbt_free);
131 xbt_dynar_push (array, xbt_strdup(variable));
132 if (IS_TRACING_PLATFORM) pajeSetVariable (now, variable, resource, "0");
133 xbt_dict_set (platform_variables, resource, array, xbt_dynar_free_voidp);
135 xbt_dynar_t array = xbt_dict_get (platform_variables, resource);
139 xbt_dynar_foreach (array, i, cat) {
140 if (strcmp(variable, cat)==0){
145 xbt_dynar_push (array, strdup(variable));
146 if (IS_TRACING_PLATFORM) pajeSetVariable (now, variable, resource, "0");
152 void __TRACE_surf_update_action_state_resource (double now, double delta, const char *variable, const char *resource, double value)
154 if (!IS_TRACING_PLATFORM) return;
157 snprintf (valuestr, 100, "%f", value);
160 //fprintf (stderr, "resource = %s variable = %s (%f -> %f) value = %s\n", resource, variable, now, now+delta, valuestr);
162 __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
163 if (IS_TRACING_PLATFORM) pajeAddVariable (now, variable, resource, valuestr);
164 if (IS_TRACING_PLATFORM) pajeSubVariable (now+delta, variable, resource, valuestr);
170 * The following code replaces the code above with the objective
171 * to decrease the size of file because of unnecessary add/sub on
172 * variables. It should be re-checked before put in production.
175 char nowstr[100], nowdeltastr[100];
176 snprintf (nowstr, 100, "%.15f", now);
177 snprintf (nowdeltastr, 100, "%.15f", now+delta);
179 char timekey[100], valuekey[100], variablekey[100];
180 snprintf (timekey, 100, "%s%cTime", resource, VARIABLE_SEPARATOR);
181 snprintf (valuekey, 100, "%s%cValue", resource, VARIABLE_SEPARATOR);
182 snprintf (variablekey, 100, "%s%cVariable", resource, VARIABLE_SEPARATOR);
184 char *lastvariable = xbt_dict_get_or_null (last_platform_variables, variablekey);
185 if (lastvariable == NULL){
186 __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
187 pajeAddVariable (now, variable, resource, valuestr);
188 xbt_dict_set (last_platform_variables, xbt_strdup (timekey), xbt_strdup (nowdeltastr), xbt_free);
189 xbt_dict_set (last_platform_variables, xbt_strdup (valuekey), xbt_strdup (valuestr), xbt_free);
190 xbt_dict_set (last_platform_variables, xbt_strdup (variablekey), xbt_strdup (variable), xbt_free);
192 char *lasttime = xbt_dict_get_or_null (last_platform_variables, timekey);
193 char *lastvalue = xbt_dict_get_or_null (last_platform_variables, valuekey);
195 /* check if it is the same variable */
196 if (strcmp(lastvariable, variable) == 0){ /* same variable */
197 /* check if lasttime equals now */
198 if (atof(lasttime) == now){ /* lastime == now */
199 /* check if lastvalue equals valuestr */
200 if (atof(lastvalue) == value){ /* lastvalue == value (good, just advance time) */
201 xbt_dict_set (last_platform_variables, xbt_strdup(timekey), xbt_strdup(nowdeltastr), xbt_free);
202 }else{ /* value has changed */
203 /* value has changed, subtract previous value, add new one */
204 pajeSubVariable (atof(lasttime), variable, resource, lastvalue);
205 pajeAddVariable (atof(nowstr), 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);
209 }else{ /* lasttime != now */
210 /* the last time is different from new starting time, subtract to lasttime and add from nowstr */
211 pajeSubVariable (atof(lasttime), variable, resource, lastvalue);
212 pajeAddVariable (atof(nowstr), variable, resource, valuestr);
213 xbt_dict_set (last_platform_variables, xbt_strdup(timekey), xbt_strdup(nowdeltastr), xbt_free);
214 xbt_dict_set (last_platform_variables, xbt_strdup(valuekey), xbt_strdup(valuestr), xbt_free);
216 }else{ /* variable has changed */
217 pajeSubVariable (atof(lasttime), lastvariable, resource, lastvalue);
218 __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
219 pajeAddVariable (now, variable, resource, valuestr);
220 xbt_dict_set (last_platform_variables, xbt_strdup (timekey), xbt_strdup (nowdeltastr), xbt_free);
221 xbt_dict_set (last_platform_variables, xbt_strdup (valuekey), xbt_strdup (valuestr), xbt_free);
222 xbt_dict_set (last_platform_variables, xbt_strdup (variablekey), xbt_strdup (variable), xbt_free);
228 void __TRACE_surf_set_resource_variable (double date, const char *variable, const char *resource, double value)
230 if (!IS_TRACING) return;
231 char aux[100], key[100];
232 snprintf (aux, 100, "%f", value);
233 snprintf (key, 100, "%s%c%s", resource, VARIABLE_SEPARATOR, variable);
235 char *last_value = xbt_dict_get_or_null(resource_variables, key);
237 if (atof(last_value) == value){
241 if (IS_TRACING_PLATFORM) pajeSetVariable (date, variable, resource, aux);
242 xbt_dict_set (resource_variables, xbt_strdup(key), xbt_strdup(aux), xbt_free);
245 void TRACE_surf_update_action_state (void *surf_action, smx_action_t smx_action,
246 double value, const char *stateValue, double now, double delta)
250 void TRACE_surf_update_action_state_net_resource (const char *name, smx_action_t smx_action, double value, double now, double delta)
252 if (!IS_TRACING || !IS_TRACED(smx_action)) return;
254 if (strcmp (name, "__loopback__")==0 ||
255 strcmp (name, "loopback")==0){ //ignore loopback updates
259 if (value == 0) return;
261 if (!xbt_dict_get_or_null (created_links, name)){
262 TRACE_surf_missing_link ();
267 snprintf (type, 100, "b%s", smx_action->category);
268 __TRACE_surf_update_action_state_resource (now, delta, type, name, value);
272 void TRACE_surf_update_action_state_cpu_resource (const char *name, smx_action_t smx_action, double value, double now, double delta)
274 if (!IS_TRACING || !IS_TRACED(smx_action)) return;
281 snprintf (type, 100, "p%s", smx_action->category);
282 __TRACE_surf_update_action_state_resource (now, delta, type, name, value);
286 void TRACE_surf_net_link_new (char *name, double bw, double lat)
288 if (!IS_TRACING) return;
289 //if (IS_TRACING_PLATFORM) pajeCreateContainerWithBandwidthLatency (SIMIX_get_clock(), name, "LINK", "platform", name, bw, lat);
290 //save bw and lat information for this link
291 double *bw_ptr, *lat_ptr;
292 bw_ptr = xbt_new (double, 1);
293 lat_ptr = xbt_new (double, 1);
296 xbt_dict_set (link_bandwidth, xbt_strdup(name), bw_ptr, xbt_free);
297 xbt_dict_set (link_latency, xbt_strdup(name), lat_ptr, xbt_free);
300 void TRACE_surf_cpu_new (char *name, double power)
302 if (!IS_TRACING) return;
303 if (IS_TRACING_PLATFORM) pajeCreateContainer (SIMIX_get_clock(), name, "HOST", "platform", name);
304 __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "power", name, power);
307 void TRACE_surf_routing_full_parse_end (char *link_name, int src, int dst)
309 if (!IS_TRACING) return;
310 char srcidstr[100], dstidstr[100];
311 snprintf (srcidstr, 100, "%d", src);
312 snprintf (dstidstr, 100, "%d", dst);
313 char *srcname = xbt_dict_get (hosts_id, srcidstr);
314 char *dstname = xbt_dict_get (hosts_id, dstidstr);
317 snprintf (key, 100, "l%d-%d", src, dst);
319 if (strcmp (link_name, "__loopback__")==0 ||
320 strcmp (link_name, "loopback")==0){ //ignore loopback updates
324 if (!xbt_dict_get_or_null (created_links, link_name)){
325 //if (IS_TRACING_PLATFORM) pajeStartLink (SIMIX_get_clock(), "edge", "platform", "route", srcname, key);
326 //if (IS_TRACING_PLATFORM) pajeEndLink (SIMIX_get_clock()+0.1, "edge", "platform", "route", dstname, key);
327 double *bw = xbt_dict_get (link_bandwidth, link_name);
328 double *lat = xbt_dict_get (link_latency, link_name);
329 if (IS_TRACING_PLATFORM) pajeCreateContainerWithBandwidthLatencySrcDst (SIMIX_get_clock(), link_name, "LINK", "platform", link_name, *bw, *lat, srcname, dstname);
330 __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "bandwidth", link_name, *bw);
331 __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "latency", link_name, *lat);
332 xbt_dict_set (created_links, xbt_strdup(link_name), xbt_strdup ("1"), xbt_free);
336 void TRACE_surf_cpu_set_power (double date, char *resource, double power)
338 __TRACE_surf_set_resource_variable (date, "power", resource, power);
341 void TRACE_surf_link_set_bandwidth (double date, char *resource, double bandwidth)
343 __TRACE_surf_set_resource_variable (date, "bandwidth", resource, bandwidth);
346 void TRACE_surf_link_set_latency (double date, char *resource, double latency)
348 __TRACE_surf_set_resource_variable (date, "latency", resource, latency);
351 void TRACE_surf_define_host_id (const char *name, int host_id)
353 if (!IS_TRACING) return;
355 snprintf (strid, 100, "%d", host_id);
356 xbt_dict_set (hosts_id, strdup(name), strdup(strid), free);
357 xbt_dict_set (hosts_id, strdup(strid), strdup(name), free);
360 /* to trace gtnets */
361 void TRACE_surf_gtnets_communicate (void *action, int src, int dst)
363 if (!IS_TRACING) return;
364 char key[100], aux[100];
365 snprintf (key, 100, "%p", action);
367 snprintf (aux, 100, "%d", src);
368 xbt_dict_set (gtnets_src, xbt_strdup(key), xbt_strdup(aux), xbt_free);
369 snprintf (aux, 100, "%d", dst);
370 xbt_dict_set (gtnets_dst, xbt_strdup(key), xbt_strdup(aux), xbt_free);
373 int TRACE_surf_gtnets_get_src (void *action)
375 if (!IS_TRACING) return -1;
377 snprintf (key, 100, "%p", action);
379 char *aux = xbt_dict_get_or_null (gtnets_src, key);
387 int TRACE_surf_gtnets_get_dst (void *action)
389 if (!IS_TRACING) return -1;
391 snprintf (key, 100, "%p", action);
393 char *aux = xbt_dict_get_or_null (gtnets_dst, key);
401 void TRACE_surf_gtnets_destroy (void *action)
403 if (!IS_TRACING) return;
405 snprintf (key, 100, "%p", action);
406 xbt_dict_remove (gtnets_src, key);
407 xbt_dict_remove (gtnets_dst, key);
410 void TRACE_surf_missing_link (void)
412 CRITICAL0("The trace cannot be done because "
413 "the platform you are using contains "
414 "routes with more than one link.");
415 THROW0(tracing_error, TRACE_ERROR_COMPLEX_ROUTES, "Tracing failed");