1 /* Copyright (c) 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "instr/private.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(tracing_surf,tracing,"Tracing Surf");
13 #define VARIABLE_SEPARATOR '#'
15 static xbt_dict_t hosts_id;
16 static xbt_dict_t created_links;
17 static xbt_dict_t link_bandwidth; //name(char*) -> bandwidth(double)
18 static xbt_dict_t link_latency; //name(char*) -> latency(double)
19 static xbt_dict_t host_containers;
21 static xbt_dict_t last_platform_variables; /* to control the amount of add/sub variables events:
22 dict with key {RESOURCE_NAME}#Time or {RESOURCE_NAME}#Value of dict with variables types == string */
24 static xbt_dict_t platform_variables; /* host or link name -> array of categories */
26 static xbt_dict_t resource_variables; /* (host|link)#variable -> value */
29 static xbt_dict_t gtnets_src; /* %p (action) -> %s */
30 static xbt_dict_t gtnets_dst; /* %p (action) -> %s */
32 void __TRACE_surf_init (void)
34 hosts_id = xbt_dict_new();
35 created_links = xbt_dict_new();
36 link_bandwidth = xbt_dict_new();
37 link_latency = xbt_dict_new();
38 platform_variables = xbt_dict_new();
39 host_containers = xbt_dict_new();
40 last_platform_variables = xbt_dict_new();
41 resource_variables = xbt_dict_new ();
42 gtnets_src = xbt_dict_new ();
43 gtnets_dst = xbt_dict_new ();
46 static char *strsplit (char *input, int field, char del) //caller should free the returned string
49 int length = strlen(input), i;
50 int s = 0, e = length+1;
51 int current_field = 0;
52 for (i = 0; i < length; i++){
54 if (current_field == field){
63 //copy string from s to e (with length equal to e-s) and return
64 ret = malloc ((e-s+2)*sizeof(char));
65 strncpy (ret, input+s, e-s+1);
70 void __TRACE_surf_finalize (void)
72 xbt_dict_cursor_t cursor = NULL;
73 unsigned int cursor_ar = 0;
74 char *key, *value, *res;
78 xbt_dynar_t resources = NULL;
79 if (!IS_TRACING_PLATFORM) return;
80 if (!xbt_dict_length(last_platform_variables)){
83 /* get all resources from last_platform_variables */
84 resources = xbt_dynar_new(sizeof(char*), xbt_free);
85 xbt_dict_foreach(last_platform_variables, cursor, key, value) {
86 res = strsplit (key, 0, VARIABLE_SEPARATOR);
87 aux = strsplit (key, 1, VARIABLE_SEPARATOR);
88 if (strcmp (aux, "Time") == 0){ //only need to add one of three
89 var_cpy = xbt_strdup (res);
90 xbt_dynar_push (resources, &var_cpy);
96 /* iterate through resources array */
97 xbt_dynar_foreach (resources, cursor_ar, resource) {
98 char timekey[100], valuekey[100], variablekey[100];
101 char *variable = NULL;
102 snprintf (timekey, 100, "%s%cTime", resource, VARIABLE_SEPARATOR);
103 snprintf (valuekey, 100, "%s%cValue", resource, VARIABLE_SEPARATOR);
104 snprintf (variablekey, 100, "%s%cVariable", resource, VARIABLE_SEPARATOR);
106 time = xbt_dict_get_or_null (last_platform_variables, timekey);
108 value = xbt_dict_get (last_platform_variables, valuekey);
109 variable = xbt_dict_get (last_platform_variables, variablekey);
110 pajeSubVariable (atof(time), variable, resource, value);
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(sizeof(char*), xbt_free);
124 char *var_cpy = xbt_strdup(variable);
125 xbt_dynar_push (array, &var_cpy);
126 if (IS_TRACING_PLATFORM) pajeSetVariable (now, variable, resource, "0");
127 xbt_dict_set (platform_variables, resource, array, xbt_dynar_free_voidp);
129 xbt_dynar_t array = xbt_dict_get (platform_variables, resource);
133 xbt_dynar_foreach (array, i, cat) {
134 if (strcmp(variable, cat)==0){
139 char *var_cpy = xbt_strdup(variable);
140 xbt_dynar_push (array, &var_cpy);
141 if (IS_TRACING_PLATFORM) pajeSetVariable (now, variable, resource, "0");
147 void __TRACE_surf_update_action_state_resource (double now, double delta, const char *variable, const char *resource, double value)
150 char nowstr[100], nowdeltastr[100];
151 char timekey[100], valuekey[100], variablekey[100];
152 char *lastvariable = NULL;
153 char *lasttime = NULL;
154 char *lastvalue = NULL;
155 char *nowdeltastr_cpy = NULL;
156 char *valuestr_cpy = NULL;
157 char *variable_cpy = NULL;
159 if (!IS_TRACING_PLATFORM) return;
161 snprintf (valuestr, 100, "%f", value);
164 //fprintf (stderr, "resource = %s variable = %s (%f -> %f) value = %s\n", resource, variable, now, now+delta, valuestr);
166 __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
167 if (IS_TRACING_PLATFORM) pajeAddVariable (now, variable, resource, valuestr);
168 if (IS_TRACING_PLATFORM) pajeSubVariable (now+delta, variable, resource, valuestr);
174 * The following code replaces the code above with the objective
175 * to decrease the size of file because of unnecessary add/sub on
176 * variables. It should be re-checked before put in production.
179 snprintf (nowstr, 100, "%.15f", now);
180 snprintf (nowdeltastr, 100, "%.15f", now+delta);
182 snprintf (timekey, 100, "%s%cTime", resource, VARIABLE_SEPARATOR);
183 snprintf (valuekey, 100, "%s%cValue", resource, VARIABLE_SEPARATOR);
184 snprintf (variablekey, 100, "%s%cVariable", resource, VARIABLE_SEPARATOR);
186 lastvariable = xbt_dict_get_or_null (last_platform_variables, variablekey);
187 if (lastvariable == NULL){
188 __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
189 pajeAddVariable (now, variable, resource, valuestr);
190 nowdeltastr_cpy = xbt_strdup (nowdeltastr);
191 valuestr_cpy = xbt_strdup (valuestr);
192 variable_cpy = xbt_strdup (variable);
193 xbt_dict_set (last_platform_variables, timekey, nowdeltastr_cpy, xbt_free);
194 xbt_dict_set (last_platform_variables, valuekey, valuestr_cpy, xbt_free);
195 xbt_dict_set (last_platform_variables, variablekey, variable_cpy, xbt_free);
197 lasttime = xbt_dict_get_or_null (last_platform_variables, timekey);
198 lastvalue = xbt_dict_get_or_null (last_platform_variables, valuekey);
200 /* check if it is the same variable */
201 if (strcmp(lastvariable, variable) == 0){ /* same variable */
202 /* check if lasttime equals now */
203 if (atof(lasttime) == now){ /* lastime == now */
204 /* check if lastvalue equals valuestr */
205 if (atof(lastvalue) == value){ /* lastvalue == value (good, just advance time) */
206 char *nowdeltastr_cpy = xbt_strdup (nowdeltastr);
207 xbt_dict_set (last_platform_variables, timekey, nowdeltastr_cpy, xbt_free);
208 }else{ /* value has changed */
209 /* value has changed, subtract previous value, add new one */
210 pajeSubVariable (atof(lasttime), variable, resource, lastvalue);
211 pajeAddVariable (atof(nowstr), variable, resource, valuestr);
212 nowdeltastr_cpy = xbt_strdup (nowdeltastr);
213 valuestr_cpy = xbt_strdup (valuestr);
214 xbt_dict_set (last_platform_variables, timekey, nowdeltastr_cpy, xbt_free);
215 xbt_dict_set (last_platform_variables, valuekey, valuestr_cpy, xbt_free);
217 }else{ /* lasttime != now */
218 /* the last time is different from new starting time, subtract to lasttime and add from nowstr */
219 pajeSubVariable (atof(lasttime), variable, resource, lastvalue);
220 pajeAddVariable (atof(nowstr), variable, resource, valuestr);
221 nowdeltastr_cpy = xbt_strdup (nowdeltastr);
222 valuestr_cpy = xbt_strdup (valuestr);
223 xbt_dict_set (last_platform_variables, timekey, nowdeltastr_cpy, xbt_free);
224 xbt_dict_set (last_platform_variables, valuekey, valuestr_cpy, xbt_free);
226 }else{ /* variable has changed */
227 pajeSubVariable (atof(lasttime), lastvariable, resource, lastvalue);
228 __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
229 pajeAddVariable (now, variable, resource, valuestr);
230 nowdeltastr_cpy = xbt_strdup (nowdeltastr);
231 valuestr_cpy = xbt_strdup (valuestr);
232 variable_cpy = xbt_strdup (variable);
233 xbt_dict_set (last_platform_variables, timekey, nowdeltastr_cpy, xbt_free);
234 xbt_dict_set (last_platform_variables, valuekey, valuestr_cpy, xbt_free);
235 xbt_dict_set (last_platform_variables, variablekey, variable_cpy, xbt_free);
241 void __TRACE_surf_set_resource_variable (double date, const char *variable, const char *resource, double value)
243 char aux[100], key[100];
244 char *last_value = NULL;
245 if (!IS_TRACING) return;
246 snprintf (aux, 100, "%f", value);
247 snprintf (key, 100, "%s%c%s", resource, VARIABLE_SEPARATOR, variable);
249 last_value = xbt_dict_get_or_null(resource_variables, key);
251 if (atof(last_value) == value){
255 if (IS_TRACING_PLATFORM) pajeSetVariable (date, variable, resource, aux);
256 xbt_dict_set (resource_variables, xbt_strdup(key), xbt_strdup(aux), xbt_free);
259 void TRACE_surf_link_set_utilization (const char *name, smx_action_t smx_action, double value, double now, double delta)
262 if (!IS_TRACING || !IS_TRACED(smx_action)) return;
264 if (strcmp (name, "__loopback__")==0 ||
265 strcmp (name, "loopback")==0){ //ignore loopback updates
269 if (value == 0) return;
271 if (!xbt_dict_get_or_null (created_links, name)){
272 TRACE_surf_link_missing ();
276 snprintf (type, 100, "b%s", smx_action->category);
277 __TRACE_surf_update_action_state_resource (now, delta, type, name, value);
281 void TRACE_surf_host_set_utilization (const char *name, smx_action_t smx_action, double value, double now, double delta)
284 if (!IS_TRACING || !IS_TRACED(smx_action)) return;
290 snprintf (type, 100, "p%s", smx_action->category);
291 __TRACE_surf_update_action_state_resource (now, delta, type, name, value);
296 * TRACE_surf_link_declaration (name, bandwidth, latency): this function
297 * saves the bandwidth and latency of a link identified by name. This
298 * information is used in the future to create the link container in the trace.
300 * caller: net_link_new (from each network model)
301 * main: save bandwidth and latency information
304 void TRACE_surf_link_declaration (char *name, double bw, double lat)
306 if (!IS_TRACING) return;
308 double *bw_ptr = xbt_malloc(sizeof(double));
309 double *lat_ptr = xbt_malloc(sizeof(double));
312 xbt_dict_set (link_bandwidth, name, bw_ptr, xbt_free);
313 xbt_dict_set (link_latency, name, lat_ptr, xbt_free);
317 * TRACE_surf_host_declaration (name, power): this function
318 * saves the power of a host identified by name. This information
319 * is used to create the host container in the trace.
321 * caller: cpu_new (from each cpu model) + router parser
322 * main: create HOST containers, set initial power value
325 void TRACE_surf_host_declaration (char *name, double power)
327 if (!IS_TRACING) return;
328 pajeCreateContainer (SIMIX_get_clock(), name, "HOST", "platform", name);
329 xbt_dict_set (host_containers, name, xbt_strdup("1"), xbt_free);
330 if (IS_TRACING_PLATFORM){
331 __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "power", name, power);
336 * TRACE_surf_link_save_endpoints (link_name, src, dst): this function
337 * creates the container of a link identified by link_name. It gets
338 * bandwidth and latency information from the dictionaries previously
339 * filled. The end points are obtained from the hosts_id dictionary.
341 * caller: end of parsing
342 * main: create LINK containers, set initial bandwidth and latency values
345 void TRACE_surf_link_save_endpoints (char *link_name, int src, int dst)
347 char srcidstr[100], dstidstr[100];
350 if (!IS_TRACING) return;
351 snprintf (srcidstr, 100, "%d", src);
352 snprintf (dstidstr, 100, "%d", dst);
353 char *srcname = xbt_dict_get (hosts_id, srcidstr);
354 char *dstname = xbt_dict_get (hosts_id, dstidstr);
355 snprintf (key, 100, "l%d-%d", src, dst);
357 if (strcmp (link_name, "__loopback__")==0 ||
358 strcmp (link_name, "loopback")==0){ //ignore loopback updates
362 if (!xbt_dict_get_or_null (created_links, link_name)){
363 double *bw = xbt_dict_get (link_bandwidth, link_name);
364 double *lat = xbt_dict_get (link_latency, link_name);
365 pajeCreateContainerWithBandwidthLatencySrcDst (SIMIX_get_clock(), link_name, "LINK", "platform", link_name, *bw, *lat, srcname, dstname);
366 if (IS_TRACING_PLATFORM) __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "bandwidth", link_name, *bw);
367 if (IS_TRACING_PLATFORM) __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "latency", link_name, *lat);
368 xbt_dict_set (created_links, link_name, xbt_strdup ("1"), xbt_free);
373 * TRACE_surf_host_define_id (name, host_id): This function relates
374 * the name of the host with its id, as generated by the called and
375 * passed as second parameter.
377 * caller: host parser, router parser
381 void TRACE_surf_host_define_id (const char *name, int host_id)
384 if (!IS_TRACING) return;
385 snprintf (strid, 100, "%d", host_id);
386 xbt_dict_set (hosts_id, name, strdup(strid), free);
387 xbt_dict_set (hosts_id, strid, strdup(name), free);
390 void TRACE_surf_host_set_power (double date, char *resource, double power)
392 __TRACE_surf_set_resource_variable (date, "power", resource, power);
395 void TRACE_surf_link_set_bandwidth (double date, char *resource, double bandwidth)
397 __TRACE_surf_set_resource_variable (date, "bandwidth", resource, bandwidth);
400 void TRACE_surf_link_set_latency (double date, char *resource, double latency)
402 __TRACE_surf_set_resource_variable (date, "latency", resource, latency);
405 /* to trace gtnets */
406 void TRACE_surf_gtnets_communicate (void *action, int src, int dst)
408 char key[100], aux[100];
409 if (!IS_TRACING) return;
410 snprintf (key, 100, "%p", action);
412 snprintf (aux, 100, "%d", src);
413 xbt_dict_set (gtnets_src, key, xbt_strdup(aux), xbt_free);
414 snprintf (aux, 100, "%d", dst);
415 xbt_dict_set (gtnets_dst, key, xbt_strdup(aux), xbt_free);
418 int TRACE_surf_gtnets_get_src (void *action)
422 if (!IS_TRACING) return -1;
423 snprintf (key, 100, "%p", action);
425 aux = xbt_dict_get_or_null (gtnets_src, key);
433 int TRACE_surf_gtnets_get_dst (void *action)
437 if (!IS_TRACING) return -1;
438 snprintf (key, 100, "%p", action);
440 aux = xbt_dict_get_or_null (gtnets_dst, key);
448 void TRACE_surf_gtnets_destroy (void *action)
451 if (!IS_TRACING) return;
452 snprintf (key, 100, "%p", action);
453 xbt_dict_remove (gtnets_src, key);
454 xbt_dict_remove (gtnets_dst, key);
457 void TRACE_surf_link_missing (void)
459 CRITICAL0("The trace cannot be done because "
460 "the platform you are using contains "
461 "routes with more than one link.");
462 THROW0(tracing_error, TRACE_ERROR_COMPLEX_ROUTES, "Tracing failed");
465 void TRACE_msg_clean (void)
468 xbt_dict_cursor_t cursor = NULL;
469 __TRACE_surf_finalize();
471 /* get all host from host_containers */
472 xbt_dict_foreach(host_containers, cursor, key, value) {
473 pajeDestroyContainer (MSG_get_clock(), "HOST", key);
477 void TRACE_surf_host_vivaldi_parse (char *host, double x, double y, double h)
480 if (!IS_TRACING || !IS_TRACING_PLATFORM) return;
482 snprintf (valuestr, 100, "%g", x);
483 pajeSetVariable (0, "vivaldi_x", host, valuestr);
484 snprintf (valuestr, 100, "%g", y);
485 pajeSetVariable (0, "vivaldi_y", host, valuestr);
486 snprintf (valuestr, 100, "%g", h);
487 pajeSetVariable (0, "vivaldi_h", host, valuestr);