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;
27 static xbt_dict_t last_platform_variables; /* to control the amount of add/sub variables events:
28 dict with key {RESOURCE_NAME}#Time or {RESOURCE_NAME}#Value of dict with variables types == string */
30 static xbt_dict_t platform_variables; /* host or link name -> array of categories */
32 static xbt_dict_t resource_variables; /* (host|link)#variable -> value */
35 static xbt_dict_t gtnets_src; /* %p (action) -> %s */
36 static xbt_dict_t gtnets_dst; /* %p (action) -> %s */
38 void __TRACE_surf_init (void)
40 hosts_id = xbt_dict_new();
41 created_links = xbt_dict_new();
42 link_bandwidth = xbt_dict_new();
43 link_latency = xbt_dict_new();
44 platform_variables = xbt_dict_new();
45 host_containers = xbt_dict_new();
46 last_platform_variables = xbt_dict_new();
47 resource_variables = xbt_dict_new ();
48 gtnets_src = xbt_dict_new ();
49 gtnets_dst = xbt_dict_new ();
52 static char *strsplit (char *input, int field, char del) //caller should free the returned string
54 int length = strlen(input), i;
55 int s = 0, e = length+1;
56 int current_field = 0;
57 for (i = 0; i < length; i++){
59 if (current_field == field){
68 //copy string from s to e (with length equal to e-s) and return
69 char *ret = malloc ((e-s+2)*sizeof(char));
70 strncpy (ret, input+s, e-s+1);
75 void __TRACE_surf_finalize (void)
77 if (!IS_TRACING_PLATFORM) return;
78 if (!xbt_dict_length(last_platform_variables)){
81 xbt_dict_cursor_t cursor = NULL;
82 unsigned int cursor_ar = 0;
83 char *key, *value, *res;
86 /* get all resources from last_platform_variables */
87 xbt_dynar_t resources = xbt_dynar_new(sizeof(char)*200, xbt_free);
88 xbt_dict_foreach(last_platform_variables, cursor, key, value) {
89 res = strsplit (key, 0, VARIABLE_SEPARATOR);
90 char *aux = strsplit (key, 1, VARIABLE_SEPARATOR);
91 if (strcmp (aux, "Time") == 0){ //only need to add one of three
92 xbt_dynar_push (resources, xbt_strdup(res));
98 /* iterate through resources array */
99 xbt_dynar_foreach (resources, cursor_ar, resource) {
100 char timekey[100], valuekey[100], variablekey[100];
101 snprintf (timekey, 100, "%s%cTime", resource, VARIABLE_SEPARATOR);
102 snprintf (valuekey, 100, "%s%cValue", resource, VARIABLE_SEPARATOR);
103 snprintf (variablekey, 100, "%s%cVariable", resource, VARIABLE_SEPARATOR);
105 char *time = xbt_dict_get_or_null (last_platform_variables, timekey);
107 char *value = xbt_dict_get (last_platform_variables, valuekey);
108 char *variable = xbt_dict_get (last_platform_variables, variablekey);
109 pajeSubVariable (atof(time), variable, resource, value);
111 //TODO: should remove, but it is causing sigabort
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(100*sizeof(char), xbt_free);
124 xbt_dynar_push (array, xbt_strdup(variable));
125 if (IS_TRACING_PLATFORM) pajeSetVariable (now, variable, resource, "0");
126 xbt_dict_set (platform_variables, resource, array, xbt_dynar_free_voidp);
128 xbt_dynar_t array = xbt_dict_get (platform_variables, resource);
132 xbt_dynar_foreach (array, i, cat) {
133 if (strcmp(variable, cat)==0){
138 xbt_dynar_push (array, strdup(variable));
139 if (IS_TRACING_PLATFORM) pajeSetVariable (now, variable, resource, "0");
145 void __TRACE_surf_update_action_state_resource (double now, double delta, const char *variable, const char *resource, double value)
147 if (!IS_TRACING_PLATFORM) return;
150 snprintf (valuestr, 100, "%f", value);
153 //fprintf (stderr, "resource = %s variable = %s (%f -> %f) value = %s\n", resource, variable, now, now+delta, valuestr);
155 __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
156 if (IS_TRACING_PLATFORM) pajeAddVariable (now, variable, resource, valuestr);
157 if (IS_TRACING_PLATFORM) pajeSubVariable (now+delta, variable, resource, valuestr);
163 * The following code replaces the code above with the objective
164 * to decrease the size of file because of unnecessary add/sub on
165 * variables. It should be re-checked before put in production.
168 char nowstr[100], nowdeltastr[100];
169 snprintf (nowstr, 100, "%.15f", now);
170 snprintf (nowdeltastr, 100, "%.15f", now+delta);
172 char timekey[100], valuekey[100], variablekey[100];
173 snprintf (timekey, 100, "%s%cTime", resource, VARIABLE_SEPARATOR);
174 snprintf (valuekey, 100, "%s%cValue", resource, VARIABLE_SEPARATOR);
175 snprintf (variablekey, 100, "%s%cVariable", resource, VARIABLE_SEPARATOR);
177 char *lastvariable = xbt_dict_get_or_null (last_platform_variables, variablekey);
178 if (lastvariable == NULL){
179 __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
180 pajeAddVariable (now, variable, resource, valuestr);
181 xbt_dict_set (last_platform_variables, xbt_strdup (timekey), xbt_strdup (nowdeltastr), xbt_free);
182 xbt_dict_set (last_platform_variables, xbt_strdup (valuekey), xbt_strdup (valuestr), xbt_free);
183 xbt_dict_set (last_platform_variables, xbt_strdup (variablekey), xbt_strdup (variable), xbt_free);
185 char *lasttime = xbt_dict_get_or_null (last_platform_variables, timekey);
186 char *lastvalue = xbt_dict_get_or_null (last_platform_variables, valuekey);
188 /* check if it is the same variable */
189 if (strcmp(lastvariable, variable) == 0){ /* same variable */
190 /* check if lasttime equals now */
191 if (atof(lasttime) == now){ /* lastime == now */
192 /* check if lastvalue equals valuestr */
193 if (atof(lastvalue) == value){ /* lastvalue == value (good, just advance time) */
194 xbt_dict_set (last_platform_variables, xbt_strdup(timekey), xbt_strdup(nowdeltastr), xbt_free);
195 }else{ /* value has changed */
196 /* value has changed, subtract previous value, add new one */
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{ /* lasttime != now */
203 /* the last time is different from new starting time, subtract to lasttime and add from nowstr */
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{ /* variable has changed */
210 pajeSubVariable (atof(lasttime), lastvariable, resource, lastvalue);
211 __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
212 pajeAddVariable (now, 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);
215 xbt_dict_set (last_platform_variables, xbt_strdup (variablekey), xbt_strdup (variable), xbt_free);
221 void __TRACE_surf_set_resource_variable (double date, const char *variable, const char *resource, double value)
223 if (!IS_TRACING) return;
224 char aux[100], key[100];
225 snprintf (aux, 100, "%f", value);
226 snprintf (key, 100, "%s%c%s", resource, VARIABLE_SEPARATOR, variable);
228 char *last_value = xbt_dict_get_or_null(resource_variables, key);
230 if (atof(last_value) == value){
234 if (IS_TRACING_PLATFORM) pajeSetVariable (date, variable, resource, aux);
235 xbt_dict_set (resource_variables, xbt_strdup(key), xbt_strdup(aux), xbt_free);
238 void TRACE_surf_update_action_state (void *surf_action, smx_action_t smx_action,
239 double value, const char *stateValue, double now, double delta)
243 void TRACE_surf_update_action_state_net_resource (const char *name, smx_action_t smx_action, double value, double now, double delta)
245 if (!IS_TRACING || !IS_TRACED(smx_action)) return;
247 if (strcmp (name, "__loopback__")==0 ||
248 strcmp (name, "loopback")==0){ //ignore loopback updates
252 if (value == 0) return;
254 if (!xbt_dict_get_or_null (created_links, name)){
255 TRACE_surf_missing_link ();
260 snprintf (type, 100, "b%s", smx_action->category);
261 __TRACE_surf_update_action_state_resource (now, delta, type, name, value);
265 void TRACE_surf_update_action_state_cpu_resource (const char *name, smx_action_t smx_action, double value, double now, double delta)
267 if (!IS_TRACING || !IS_TRACED(smx_action)) return;
274 snprintf (type, 100, "p%s", smx_action->category);
275 __TRACE_surf_update_action_state_resource (now, delta, type, name, value);
279 void TRACE_surf_net_link_new (char *name, double bw, double lat)
281 if (!IS_TRACING) return;
282 //if (IS_TRACING_PLATFORM) pajeCreateContainerWithBandwidthLatency (SIMIX_get_clock(), name, "LINK", "platform", name, bw, lat);
283 //save bw and lat information for this link
284 double *bw_ptr, *lat_ptr;
285 bw_ptr = xbt_new (double, 1);
286 lat_ptr = xbt_new (double, 1);
289 xbt_dict_set (link_bandwidth, xbt_strdup(name), bw_ptr, xbt_free);
290 xbt_dict_set (link_latency, xbt_strdup(name), lat_ptr, xbt_free);
293 void TRACE_surf_cpu_new (char *name, double power)
295 if (!IS_TRACING) return;
296 if (IS_TRACING_PLATFORM){
297 pajeCreateContainer (SIMIX_get_clock(), name, "HOST", "platform", name);
298 xbt_dict_set (host_containers, xbt_strdup(name), xbt_strdup("1"), xbt_free);
300 __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "power", name, power);
303 void TRACE_surf_routing_full_parse_end (char *link_name, int src, int dst)
305 if (!IS_TRACING) return;
306 char srcidstr[100], dstidstr[100];
307 snprintf (srcidstr, 100, "%d", src);
308 snprintf (dstidstr, 100, "%d", dst);
309 char *srcname = xbt_dict_get (hosts_id, srcidstr);
310 char *dstname = xbt_dict_get (hosts_id, dstidstr);
313 snprintf (key, 100, "l%d-%d", src, dst);
315 if (strcmp (link_name, "__loopback__")==0 ||
316 strcmp (link_name, "loopback")==0){ //ignore loopback updates
320 if (!xbt_dict_get_or_null (created_links, link_name)){
321 //if (IS_TRACING_PLATFORM) pajeStartLink (SIMIX_get_clock(), "edge", "platform", "route", srcname, key);
322 //if (IS_TRACING_PLATFORM) pajeEndLink (SIMIX_get_clock()+0.1, "edge", "platform", "route", dstname, key);
323 double *bw = xbt_dict_get (link_bandwidth, link_name);
324 double *lat = xbt_dict_get (link_latency, link_name);
325 if (IS_TRACING_PLATFORM) pajeCreateContainerWithBandwidthLatencySrcDst (SIMIX_get_clock(), link_name, "LINK", "platform", link_name, *bw, *lat, srcname, dstname);
326 __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "bandwidth", link_name, *bw);
327 __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "latency", link_name, *lat);
328 xbt_dict_set (created_links, xbt_strdup(link_name), xbt_strdup ("1"), xbt_free);
332 void TRACE_surf_cpu_set_power (double date, char *resource, double power)
334 __TRACE_surf_set_resource_variable (date, "power", resource, power);
337 void TRACE_surf_link_set_bandwidth (double date, char *resource, double bandwidth)
339 __TRACE_surf_set_resource_variable (date, "bandwidth", resource, bandwidth);
342 void TRACE_surf_link_set_latency (double date, char *resource, double latency)
344 __TRACE_surf_set_resource_variable (date, "latency", resource, latency);
347 void TRACE_surf_define_host_id (const char *name, int host_id)
349 if (!IS_TRACING) return;
351 snprintf (strid, 100, "%d", host_id);
352 xbt_dict_set (hosts_id, strdup(name), strdup(strid), free);
353 xbt_dict_set (hosts_id, strdup(strid), strdup(name), free);
356 /* to trace gtnets */
357 void TRACE_surf_gtnets_communicate (void *action, int src, int dst)
359 if (!IS_TRACING) return;
360 char key[100], aux[100];
361 snprintf (key, 100, "%p", action);
363 snprintf (aux, 100, "%d", src);
364 xbt_dict_set (gtnets_src, xbt_strdup(key), xbt_strdup(aux), xbt_free);
365 snprintf (aux, 100, "%d", dst);
366 xbt_dict_set (gtnets_dst, xbt_strdup(key), xbt_strdup(aux), xbt_free);
369 int TRACE_surf_gtnets_get_src (void *action)
371 if (!IS_TRACING) return -1;
373 snprintf (key, 100, "%p", action);
375 char *aux = xbt_dict_get_or_null (gtnets_src, key);
383 int TRACE_surf_gtnets_get_dst (void *action)
385 if (!IS_TRACING) return -1;
387 snprintf (key, 100, "%p", action);
389 char *aux = xbt_dict_get_or_null (gtnets_dst, key);
397 void TRACE_surf_gtnets_destroy (void *action)
399 if (!IS_TRACING) return;
401 snprintf (key, 100, "%p", action);
402 xbt_dict_remove (gtnets_src, key);
403 xbt_dict_remove (gtnets_dst, key);
406 void TRACE_surf_missing_link (void)
408 CRITICAL0("The trace cannot be done because "
409 "the platform you are using contains "
410 "routes with more than one link.");
411 THROW0(tracing_error, TRACE_ERROR_COMPLEX_ROUTES, "Tracing failed");
414 void TRACE_msg_clean (void)
416 __TRACE_surf_finalize();
418 xbt_dict_cursor_t cursor = NULL;
419 unsigned int cursor_ar = 0;
422 /* get all host from host_containers */
423 xbt_dict_foreach(host_containers, cursor, key, value) {
424 if (IS_TRACING_PLATFORM) pajeDestroyContainer (MSG_get_clock(), "HOST", key);
428 void TRACE_surf_vivaldi_parse_host (char *host, double x, double y, double h)
430 if (!IS_TRACING || !IS_TRACING_PLATFORM) return;
433 snprintf (valuestr, 100, "%g", x);
434 pajeSetVariable (0, "vivaldi_x", host, valuestr);
435 snprintf (valuestr, 100, "%g", y);
436 pajeSetVariable (0, "vivaldi_y", host, valuestr);
437 snprintf (valuestr, 100, "%g", h);
438 pajeSetVariable (0, "vivaldi_h", host, valuestr);