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 platform_variable_last_value; /* to control the amount of add/sub variables events*/
26 //static xbt_dict_t platform_variable_last_time; /* to control the amount of add/sub variables events*/
28 static xbt_dict_t last_platform_variables; /* to control the amount of add/sub variables events:
29 dict with key {RESOURCE_NAME}#Time or {RESOURCE_NAME}#Value of dict with variables types == string */
31 static xbt_dict_t platform_variables; /* host or link name -> array of categories */
33 static xbt_dict_t resource_variables; /* (host|link)#variable -> value */
36 static xbt_dict_t gtnets_src; /* %p (action) -> %s */
37 static xbt_dict_t gtnets_dst; /* %p (action) -> %s */
39 void __TRACE_surf_init (void)
41 hosts_id = xbt_dict_new();
42 created_links = xbt_dict_new();
43 link_bandwidth = xbt_dict_new();
44 link_latency = xbt_dict_new();
45 platform_variables = xbt_dict_new();
47 //platform_variable_last_value = xbt_dict_new();
48 //platform_variable_last_time = xbt_dict_new();
50 last_platform_variables = xbt_dict_new();
52 resource_variables = xbt_dict_new ();
54 gtnets_src = xbt_dict_new ();
55 gtnets_dst = xbt_dict_new ();
58 static char *strsplit (char *input, int field, char del) //caller should free the returned string
60 int length = strlen(input), i;
61 int s = 0, e = length+1;
62 int current_field = 0;
63 for (i = 0; i < length; i++){
65 if (current_field == field){
74 //copy string from s to e (with length equal to e-s) and return
75 char *ret = malloc ((e-s+2)*sizeof(char));
76 strncpy (ret, input+s, e-s+1);
81 void __TRACE_surf_finalize (void)
83 if (!IS_TRACING_PLATFORM) return;
84 if (!xbt_dict_length(last_platform_variables)){
87 xbt_dict_cursor_t cursor = NULL;
88 unsigned int cursor_ar = 0;
89 char *key, *value, *res;
92 /* get all resources from last_platform_variables */
93 xbt_dynar_t resources = xbt_dynar_new(sizeof(char)*200, xbt_free);
94 xbt_dict_foreach(last_platform_variables, cursor, key, value) {
95 res = strsplit (key, 0, VARIABLE_SEPARATOR);
96 char *aux = strsplit (key, 1, VARIABLE_SEPARATOR);
97 if (strcmp (aux, "Time") == 0){ //only need to add one of three
98 xbt_dynar_push (resources, xbt_strdup(res));
104 /* iterate through resources array */
105 xbt_dynar_foreach (resources, cursor_ar, resource) {
106 char timekey[100], valuekey[100], variablekey[100];
107 snprintf (timekey, 100, "%s%cTime", resource, VARIABLE_SEPARATOR);
108 snprintf (valuekey, 100, "%s%cValue", resource, VARIABLE_SEPARATOR);
109 snprintf (variablekey, 100, "%s%cVariable", resource, VARIABLE_SEPARATOR);
111 char *time = xbt_dict_get_or_null (last_platform_variables, timekey);
113 char *value = xbt_dict_get (last_platform_variables, valuekey);
114 char *variable = xbt_dict_get (last_platform_variables, variablekey);
115 pajeSubVariable (atof(time), variable, resource, value);
117 //TODO: should remove, but it is causing sigabort
118 //xbt_dict_remove (last_platform_variables, timekey);
119 //xbt_dict_remove (last_platform_variables, valuekey);
120 //xbt_dict_remove (last_platform_variables, variablekey);
125 void __TRACE_surf_check_variable_set_to_zero (double now, const char *variable, const char *resource)
127 /* check if we have to set it to 0 */
128 if (!xbt_dict_get_or_null (platform_variables, resource)){
129 xbt_dynar_t array = xbt_dynar_new(100*sizeof(char), xbt_free);
130 xbt_dynar_push (array, xbt_strdup(variable));
131 if (IS_TRACING_PLATFORM) pajeSetVariable (now, variable, resource, "0");
132 xbt_dict_set (platform_variables, resource, array, xbt_dynar_free_voidp);
134 xbt_dynar_t array = xbt_dict_get (platform_variables, resource);
138 xbt_dynar_foreach (array, i, cat) {
139 if (strcmp(variable, cat)==0){
144 xbt_dynar_push (array, strdup(variable));
145 if (IS_TRACING_PLATFORM) pajeSetVariable (now, variable, resource, "0");
151 void __TRACE_surf_update_action_state_resource (double now, double delta, const char *variable, const char *resource, double value)
153 if (!IS_TRACING_PLATFORM) return;
156 snprintf (valuestr, 100, "%f", value);
159 //fprintf (stderr, "resource = %s variable = %s (%f -> %f) value = %s\n", resource, variable, now, now+delta, valuestr);
161 __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
162 if (IS_TRACING_PLATFORM) pajeAddVariable (now, variable, resource, valuestr);
163 if (IS_TRACING_PLATFORM) pajeSubVariable (now+delta, variable, resource, valuestr);
169 * The following code replaces the code above with the objective
170 * to decrease the size of file because of unnecessary add/sub on
171 * variables. It should be re-checked before put in production.
174 char nowstr[100], nowdeltastr[100];
175 snprintf (nowstr, 100, "%.15f", now);
176 snprintf (nowdeltastr, 100, "%.15f", now+delta);
178 char timekey[100], valuekey[100], variablekey[100];
179 snprintf (timekey, 100, "%s%cTime", resource, VARIABLE_SEPARATOR);
180 snprintf (valuekey, 100, "%s%cValue", resource, VARIABLE_SEPARATOR);
181 snprintf (variablekey, 100, "%s%cVariable", resource, VARIABLE_SEPARATOR);
183 char *lastvariable = xbt_dict_get_or_null (last_platform_variables, variablekey);
184 if (lastvariable == NULL){
185 __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
186 pajeAddVariable (now, variable, resource, valuestr);
187 xbt_dict_set (last_platform_variables, xbt_strdup (timekey), xbt_strdup (nowdeltastr), xbt_free);
188 xbt_dict_set (last_platform_variables, xbt_strdup (valuekey), xbt_strdup (valuestr), xbt_free);
189 xbt_dict_set (last_platform_variables, xbt_strdup (variablekey), xbt_strdup (variable), xbt_free);
191 char *lasttime = xbt_dict_get_or_null (last_platform_variables, timekey);
192 char *lastvalue = xbt_dict_get_or_null (last_platform_variables, valuekey);
194 /* check if it is the same variable */
195 if (strcmp(lastvariable, variable) == 0){ /* same variable */
196 /* check if lasttime equals now */
197 if (atof(lasttime) == now){ /* lastime == now */
198 /* check if lastvalue equals valuestr */
199 if (atof(lastvalue) == value){ /* lastvalue == value (good, just advance time) */
200 xbt_dict_set (last_platform_variables, xbt_strdup(timekey), xbt_strdup(nowdeltastr), xbt_free);
201 }else{ /* value has changed */
202 /* value has changed, subtract previous value, add new one */
203 pajeSubVariable (atof(lasttime), variable, resource, lastvalue);
204 pajeAddVariable (atof(nowstr), variable, resource, valuestr);
205 xbt_dict_set (last_platform_variables, xbt_strdup(timekey), xbt_strdup(nowdeltastr), xbt_free);
206 xbt_dict_set (last_platform_variables, xbt_strdup(valuekey), xbt_strdup(valuestr), xbt_free);
208 }else{ /* lasttime != now */
209 /* the last time is different from new starting time, subtract to lasttime and add from nowstr */
210 pajeSubVariable (atof(lasttime), variable, resource, lastvalue);
211 pajeAddVariable (atof(nowstr), variable, resource, valuestr);
212 xbt_dict_set (last_platform_variables, xbt_strdup(timekey), xbt_strdup(nowdeltastr), xbt_free);
213 xbt_dict_set (last_platform_variables, xbt_strdup(valuekey), xbt_strdup(valuestr), xbt_free);
215 }else{ /* variable has changed */
216 pajeSubVariable (atof(lasttime), lastvariable, resource, lastvalue);
217 __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
218 pajeAddVariable (now, variable, resource, valuestr);
219 xbt_dict_set (last_platform_variables, xbt_strdup (timekey), xbt_strdup (nowdeltastr), xbt_free);
220 xbt_dict_set (last_platform_variables, xbt_strdup (valuekey), xbt_strdup (valuestr), xbt_free);
221 xbt_dict_set (last_platform_variables, xbt_strdup (variablekey), xbt_strdup (variable), xbt_free);
227 void __TRACE_surf_set_resource_variable (double date, const char *variable, const char *resource, double value)
229 if (!IS_TRACING) return;
230 char aux[100], key[100];
231 snprintf (aux, 100, "%f", value);
232 snprintf (key, 100, "%s%c%s", resource, VARIABLE_SEPARATOR, variable);
234 char *last_value = xbt_dict_get_or_null(resource_variables, key);
236 if (atof(last_value) == value){
240 if (IS_TRACING_PLATFORM) pajeSetVariable (date, variable, resource, aux);
241 xbt_dict_set (resource_variables, xbt_strdup(key), xbt_strdup(aux), xbt_free);
244 void TRACE_surf_update_action_state (void *surf_action, smx_action_t smx_action,
245 double value, const char *stateValue, double now, double delta)
249 void TRACE_surf_update_action_state_net_resource (const char *name, smx_action_t smx_action, double value, double now, double delta)
251 if (!IS_TRACING || !IS_TRACED(smx_action)) return;
253 if (strcmp (name, "__loopback__")==0 ||
254 strcmp (name, "loopback")==0){ //ignore loopback updates
258 if (value == 0) return;
260 if (!xbt_dict_get_or_null (created_links, name)){
261 TRACE_surf_missing_link ();
266 snprintf (type, 100, "b%s", smx_action->category);
267 __TRACE_surf_update_action_state_resource (now, delta, type, name, value);
271 void TRACE_surf_update_action_state_cpu_resource (const char *name, smx_action_t smx_action, double value, double now, double delta)
273 if (!IS_TRACING || !IS_TRACED(smx_action)) return;
280 snprintf (type, 100, "p%s", smx_action->category);
281 __TRACE_surf_update_action_state_resource (now, delta, type, name, value);
285 void TRACE_surf_net_link_new (char *name, double bw, double lat)
287 if (!IS_TRACING) return;
288 //if (IS_TRACING_PLATFORM) pajeCreateContainerWithBandwidthLatency (SIMIX_get_clock(), name, "LINK", "platform", name, bw, lat);
289 //save bw and lat information for this link
290 double *bw_ptr, *lat_ptr;
291 bw_ptr = xbt_new (double, 1);
292 lat_ptr = xbt_new (double, 1);
295 xbt_dict_set (link_bandwidth, xbt_strdup(name), bw_ptr, xbt_free);
296 xbt_dict_set (link_latency, xbt_strdup(name), lat_ptr, xbt_free);
299 void TRACE_surf_cpu_new (char *name, double power)
301 if (!IS_TRACING) return;
302 if (IS_TRACING_PLATFORM) pajeCreateContainer (SIMIX_get_clock(), name, "HOST", "platform", name);
303 __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "power", name, power);
306 void TRACE_surf_routing_full_parse_end (char *link_name, int src, int dst)
308 if (!IS_TRACING) return;
309 char srcidstr[100], dstidstr[100];
310 snprintf (srcidstr, 100, "%d", src);
311 snprintf (dstidstr, 100, "%d", dst);
312 char *srcname = xbt_dict_get (hosts_id, srcidstr);
313 char *dstname = xbt_dict_get (hosts_id, dstidstr);
316 snprintf (key, 100, "l%d-%d", src, dst);
318 if (strcmp (link_name, "__loopback__")==0 ||
319 strcmp (link_name, "loopback")==0){ //ignore loopback updates
323 if (!xbt_dict_get_or_null (created_links, link_name)){
324 //if (IS_TRACING_PLATFORM) pajeStartLink (SIMIX_get_clock(), "edge", "platform", "route", srcname, key);
325 //if (IS_TRACING_PLATFORM) pajeEndLink (SIMIX_get_clock()+0.1, "edge", "platform", "route", dstname, key);
326 double *bw = xbt_dict_get (link_bandwidth, link_name);
327 double *lat = xbt_dict_get (link_latency, link_name);
328 if (IS_TRACING_PLATFORM) pajeCreateContainerWithBandwidthLatencySrcDst (SIMIX_get_clock(), link_name, "LINK", "platform", link_name, *bw, *lat, srcname, dstname);
329 __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "bandwidth", link_name, *bw);
330 __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "latency", link_name, *lat);
331 xbt_dict_set (created_links, xbt_strdup(link_name), xbt_strdup ("1"), xbt_free);
335 void TRACE_surf_cpu_set_power (double date, char *resource, double power)
337 __TRACE_surf_set_resource_variable (date, "power", resource, power);
340 void TRACE_surf_link_set_bandwidth (double date, char *resource, double bandwidth)
342 __TRACE_surf_set_resource_variable (date, "bandwidth", resource, bandwidth);
345 void TRACE_surf_link_set_latency (double date, char *resource, double latency)
347 __TRACE_surf_set_resource_variable (date, "latency", resource, latency);
350 void TRACE_surf_define_host_id (const char *name, int host_id)
352 if (!IS_TRACING) return;
354 snprintf (strid, 100, "%d", host_id);
355 xbt_dict_set (hosts_id, strdup(name), strdup(strid), free);
356 xbt_dict_set (hosts_id, strdup(strid), strdup(name), free);
359 /* to trace gtnets */
360 void TRACE_surf_gtnets_communicate (void *action, int src, int dst)
362 if (!IS_TRACING) return;
363 char key[100], aux[100];
364 snprintf (key, 100, "%p", action);
366 snprintf (aux, 100, "%d", src);
367 xbt_dict_set (gtnets_src, xbt_strdup(key), xbt_strdup(aux), xbt_free);
368 snprintf (aux, 100, "%d", dst);
369 xbt_dict_set (gtnets_dst, xbt_strdup(key), xbt_strdup(aux), xbt_free);
372 int TRACE_surf_gtnets_get_src (void *action)
374 if (!IS_TRACING) return -1;
376 snprintf (key, 100, "%p", action);
378 char *aux = xbt_dict_get_or_null (gtnets_src, key);
386 int TRACE_surf_gtnets_get_dst (void *action)
388 if (!IS_TRACING) return -1;
390 snprintf (key, 100, "%p", action);
392 char *aux = xbt_dict_get_or_null (gtnets_dst, key);
400 void TRACE_surf_gtnets_destroy (void *action)
402 if (!IS_TRACING) return;
404 snprintf (key, 100, "%p", action);
405 xbt_dict_remove (gtnets_src, key);
406 xbt_dict_remove (gtnets_dst, key);
409 void TRACE_surf_missing_link (void)
411 CRITICAL0("The trace cannot be done because "
412 "the platform you are using contains "
413 "routes with more than one link.");
414 THROW0(tracing_error, TRACE_ERROR_COMPLEX_ROUTES, "Tracing failed");