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 xbt_dict_remove (last_platform_variables, timekey);
112 xbt_dict_remove (last_platform_variables, valuekey);
113 xbt_dict_remove (last_platform_variables, variablekey);
118 void __TRACE_surf_check_variable_set_to_zero (double now, const char *variable, const char *resource)
120 /* check if we have to set it to 0 */
121 if (!xbt_dict_get_or_null (platform_variables, resource)){
122 xbt_dynar_t array = xbt_dynar_new(100*sizeof(char), xbt_free);
123 xbt_dynar_push (array, xbt_strdup(variable));
124 if (IS_TRACING_PLATFORM) pajeSetVariable (now, variable, resource, "0");
125 xbt_dict_set (platform_variables, resource, array, xbt_dynar_free_voidp);
127 xbt_dynar_t array = xbt_dict_get (platform_variables, resource);
131 xbt_dynar_foreach (array, i, cat) {
132 if (strcmp(variable, cat)==0){
137 xbt_dynar_push (array, strdup(variable));
138 if (IS_TRACING_PLATFORM) pajeSetVariable (now, variable, resource, "0");
144 void __TRACE_surf_update_action_state_resource (double now, double delta, const char *variable, const char *resource, double value)
146 if (!IS_TRACING_PLATFORM) return;
149 snprintf (valuestr, 100, "%f", value);
152 //fprintf (stderr, "resource = %s variable = %s (%f -> %f) value = %s\n", resource, variable, now, now+delta, valuestr);
154 __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
155 if (IS_TRACING_PLATFORM) pajeAddVariable (now, variable, resource, valuestr);
156 if (IS_TRACING_PLATFORM) pajeSubVariable (now+delta, variable, resource, valuestr);
162 * The following code replaces the code above with the objective
163 * to decrease the size of file because of unnecessary add/sub on
164 * variables. It should be re-checked before put in production.
167 char nowstr[100], nowdeltastr[100];
168 snprintf (nowstr, 100, "%.15f", now);
169 snprintf (nowdeltastr, 100, "%.15f", now+delta);
171 char timekey[100], valuekey[100], variablekey[100];
172 snprintf (timekey, 100, "%s%cTime", resource, VARIABLE_SEPARATOR);
173 snprintf (valuekey, 100, "%s%cValue", resource, VARIABLE_SEPARATOR);
174 snprintf (variablekey, 100, "%s%cVariable", resource, VARIABLE_SEPARATOR);
176 char *lastvariable = xbt_dict_get_or_null (last_platform_variables, variablekey);
177 if (lastvariable == NULL){
178 __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
179 pajeAddVariable (now, variable, resource, valuestr);
180 xbt_dict_set (last_platform_variables, xbt_strdup (timekey), xbt_strdup (nowdeltastr), xbt_free);
181 xbt_dict_set (last_platform_variables, xbt_strdup (valuekey), xbt_strdup (valuestr), xbt_free);
182 xbt_dict_set (last_platform_variables, xbt_strdup (variablekey), xbt_strdup (variable), xbt_free);
184 char *lasttime = xbt_dict_get_or_null (last_platform_variables, timekey);
185 char *lastvalue = xbt_dict_get_or_null (last_platform_variables, valuekey);
187 /* check if it is the same variable */
188 if (strcmp(lastvariable, variable) == 0){ /* same variable */
189 /* check if lasttime equals now */
190 if (atof(lasttime) == now){ /* lastime == now */
191 /* check if lastvalue equals valuestr */
192 if (atof(lastvalue) == value){ /* lastvalue == value (good, just advance time) */
193 xbt_dict_set (last_platform_variables, xbt_strdup(timekey), xbt_strdup(nowdeltastr), xbt_free);
194 }else{ /* value has changed */
195 /* value has changed, subtract previous value, add new one */
196 pajeSubVariable (atof(lasttime), variable, resource, lastvalue);
197 pajeAddVariable (atof(nowstr), variable, resource, valuestr);
198 xbt_dict_set (last_platform_variables, xbt_strdup(timekey), xbt_strdup(nowdeltastr), xbt_free);
199 xbt_dict_set (last_platform_variables, xbt_strdup(valuekey), xbt_strdup(valuestr), xbt_free);
201 }else{ /* lasttime != now */
202 /* the last time is different from new starting time, subtract to lasttime and add from nowstr */
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{ /* variable has changed */
209 pajeSubVariable (atof(lasttime), lastvariable, resource, lastvalue);
210 __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
211 pajeAddVariable (now, 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);
214 xbt_dict_set (last_platform_variables, xbt_strdup (variablekey), xbt_strdup (variable), xbt_free);
220 void __TRACE_surf_set_resource_variable (double date, const char *variable, const char *resource, double value)
222 if (!IS_TRACING) return;
223 char aux[100], key[100];
224 snprintf (aux, 100, "%f", value);
225 snprintf (key, 100, "%s%c%s", resource, VARIABLE_SEPARATOR, variable);
227 char *last_value = xbt_dict_get_or_null(resource_variables, key);
229 if (atof(last_value) == value){
233 if (IS_TRACING_PLATFORM) pajeSetVariable (date, variable, resource, aux);
234 xbt_dict_set (resource_variables, xbt_strdup(key), xbt_strdup(aux), xbt_free);
237 void TRACE_surf_update_action_state_net_resource (const char *name, smx_action_t smx_action, double value, double now, double delta)
239 if (!IS_TRACING || !IS_TRACED(smx_action)) return;
241 if (strcmp (name, "__loopback__")==0 ||
242 strcmp (name, "loopback")==0){ //ignore loopback updates
246 if (value == 0) return;
248 if (!xbt_dict_get_or_null (created_links, name)){
249 TRACE_surf_missing_link ();
254 snprintf (type, 100, "b%s", smx_action->category);
255 __TRACE_surf_update_action_state_resource (now, delta, type, name, value);
259 void TRACE_surf_update_action_state_cpu_resource (const char *name, smx_action_t smx_action, double value, double now, double delta)
261 if (!IS_TRACING || !IS_TRACED(smx_action)) return;
268 snprintf (type, 100, "p%s", smx_action->category);
269 __TRACE_surf_update_action_state_resource (now, delta, type, name, value);
273 void TRACE_surf_link_declaration (char *name, double bw, double lat)
275 if (!IS_TRACING) return;
276 //if (IS_TRACING_PLATFORM) pajeCreateContainerWithBandwidthLatency (SIMIX_get_clock(), name, "LINK", "platform", name, bw, lat);
277 //save bw and lat information for this link
278 double *bw_ptr, *lat_ptr;
279 bw_ptr = xbt_new (double, 1);
280 lat_ptr = xbt_new (double, 1);
283 xbt_dict_set (link_bandwidth, xbt_strdup(name), bw_ptr, xbt_free);
284 xbt_dict_set (link_latency, xbt_strdup(name), lat_ptr, xbt_free);
287 void TRACE_surf_host_declaration (char *name, double power)
289 if (!IS_TRACING) return;
290 if (IS_TRACING_PLATFORM){
291 pajeCreateContainer (SIMIX_get_clock(), name, "HOST", "platform", name);
292 xbt_dict_set (host_containers, xbt_strdup(name), xbt_strdup("1"), xbt_free);
294 __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "power", name, power);
297 void TRACE_surf_routing_full_parse_end (char *link_name, int src, int dst)
299 if (!IS_TRACING) return;
300 char srcidstr[100], dstidstr[100];
301 snprintf (srcidstr, 100, "%d", src);
302 snprintf (dstidstr, 100, "%d", dst);
303 char *srcname = xbt_dict_get (hosts_id, srcidstr);
304 char *dstname = xbt_dict_get (hosts_id, dstidstr);
307 snprintf (key, 100, "l%d-%d", src, dst);
309 if (strcmp (link_name, "__loopback__")==0 ||
310 strcmp (link_name, "loopback")==0){ //ignore loopback updates
314 if (!xbt_dict_get_or_null (created_links, link_name)){
315 //if (IS_TRACING_PLATFORM) pajeStartLink (SIMIX_get_clock(), "edge", "platform", "route", srcname, key);
316 //if (IS_TRACING_PLATFORM) pajeEndLink (SIMIX_get_clock()+0.1, "edge", "platform", "route", dstname, key);
317 double *bw = xbt_dict_get (link_bandwidth, link_name);
318 double *lat = xbt_dict_get (link_latency, link_name);
319 if (IS_TRACING_PLATFORM) pajeCreateContainerWithBandwidthLatencySrcDst (SIMIX_get_clock(), link_name, "LINK", "platform", link_name, *bw, *lat, srcname, dstname);
320 __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "bandwidth", link_name, *bw);
321 __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "latency", link_name, *lat);
322 xbt_dict_set (created_links, xbt_strdup(link_name), xbt_strdup ("1"), xbt_free);
326 void TRACE_surf_host_set_power (double date, char *resource, double power)
328 __TRACE_surf_set_resource_variable (date, "power", resource, power);
331 void TRACE_surf_link_set_bandwidth (double date, char *resource, double bandwidth)
333 __TRACE_surf_set_resource_variable (date, "bandwidth", resource, bandwidth);
336 void TRACE_surf_link_set_latency (double date, char *resource, double latency)
338 __TRACE_surf_set_resource_variable (date, "latency", resource, latency);
341 void TRACE_surf_define_host_id (const char *name, int host_id)
343 if (!IS_TRACING) return;
345 snprintf (strid, 100, "%d", host_id);
346 xbt_dict_set (hosts_id, strdup(name), strdup(strid), free);
347 xbt_dict_set (hosts_id, strdup(strid), strdup(name), free);
350 /* to trace gtnets */
351 void TRACE_surf_gtnets_communicate (void *action, int src, int dst)
353 if (!IS_TRACING) return;
354 char key[100], aux[100];
355 snprintf (key, 100, "%p", action);
357 snprintf (aux, 100, "%d", src);
358 xbt_dict_set (gtnets_src, xbt_strdup(key), xbt_strdup(aux), xbt_free);
359 snprintf (aux, 100, "%d", dst);
360 xbt_dict_set (gtnets_dst, xbt_strdup(key), xbt_strdup(aux), xbt_free);
363 int TRACE_surf_gtnets_get_src (void *action)
365 if (!IS_TRACING) return -1;
367 snprintf (key, 100, "%p", action);
369 char *aux = xbt_dict_get_or_null (gtnets_src, key);
377 int TRACE_surf_gtnets_get_dst (void *action)
379 if (!IS_TRACING) return -1;
381 snprintf (key, 100, "%p", action);
383 char *aux = xbt_dict_get_or_null (gtnets_dst, key);
391 void TRACE_surf_gtnets_destroy (void *action)
393 if (!IS_TRACING) return;
395 snprintf (key, 100, "%p", action);
396 xbt_dict_remove (gtnets_src, key);
397 xbt_dict_remove (gtnets_dst, key);
400 void TRACE_surf_missing_link (void)
402 CRITICAL0("The trace cannot be done because "
403 "the platform you are using contains "
404 "routes with more than one link.");
405 THROW0(tracing_error, TRACE_ERROR_COMPLEX_ROUTES, "Tracing failed");
408 void TRACE_msg_clean (void)
410 __TRACE_surf_finalize();
412 xbt_dict_cursor_t cursor = NULL;
415 /* get all host from host_containers */
416 xbt_dict_foreach(host_containers, cursor, key, value) {
417 if (IS_TRACING_PLATFORM) pajeDestroyContainer (MSG_get_clock(), "HOST", key);
421 void TRACE_surf_vivaldi_parse_host (char *host, double x, double y, double h)
423 if (!IS_TRACING || !IS_TRACING_PLATFORM) return;
426 snprintf (valuestr, 100, "%g", x);
427 pajeSetVariable (0, "vivaldi_x", host, valuestr);
428 snprintf (valuestr, 100, "%g", y);
429 pajeSetVariable (0, "vivaldi_y", host, valuestr);
430 snprintf (valuestr, 100, "%g", h);
431 pajeSetVariable (0, "vivaldi_h", host, valuestr);