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 host_router_id; //name(char*) -> id(char*)
16 static xbt_dict_t host_router_name; //id(char*) -> name (char*)
18 static xbt_dict_t created_links;
19 static xbt_dict_t link_bandwidth; //name(char*) -> bandwidth(double)
20 static xbt_dict_t link_latency; //name(char*) -> latency(double)
21 static xbt_dict_t host_containers;
23 static xbt_dict_t platform_variables; /* host or link name -> array of categories */
25 static xbt_dict_t resource_variables; /* (host|link)#variable -> value */
28 static xbt_dict_t gtnets_src; /* %p (action) -> %s */
29 static xbt_dict_t gtnets_dst; /* %p (action) -> %s */
31 void __TRACE_surf_init (void)
33 host_router_id = xbt_dict_new();
34 host_router_name = 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 resource_variables = xbt_dict_new ();
41 gtnets_src = xbt_dict_new ();
42 gtnets_dst = xbt_dict_new ();
43 __TRACE_surf_resource_utilization_initialize();
46 void __TRACE_surf_finalize (void)
48 __TRACE_surf_resource_utilization_finalize();
51 void __TRACE_surf_check_variable_set_to_zero (double now, const char *variable, const char *resource)
53 /* check if we have to set it to 0 */
54 if (!xbt_dict_get_or_null (platform_variables, resource)){
55 xbt_dynar_t array = xbt_dynar_new(sizeof(char*), xbt_free);
56 char *var_cpy = xbt_strdup(variable);
57 xbt_dynar_push (array, &var_cpy);
58 if (IS_TRACING_PLATFORM) pajeSetVariable (now, variable, resource, "0");
59 xbt_dict_set (platform_variables, resource, array, xbt_dynar_free_voidp);
61 xbt_dynar_t array = xbt_dict_get (platform_variables, resource);
65 xbt_dynar_foreach (array, i, cat) {
66 if (strcmp(variable, cat)==0){
71 char *var_cpy = xbt_strdup(variable);
72 xbt_dynar_push (array, &var_cpy);
73 if (IS_TRACING_PLATFORM) pajeSetVariable (now, variable, resource, "0");
79 void __TRACE_surf_set_resource_variable (double date, const char *variable, const char *resource, double value)
81 char aux[100], key[100];
82 char *last_value = NULL;
83 if (!IS_TRACING) return;
84 snprintf (aux, 100, "%f", value);
85 snprintf (key, 100, "%s%c%s", resource, VARIABLE_SEPARATOR, variable);
87 last_value = xbt_dict_get_or_null(resource_variables, key);
89 if (atof(last_value) == value){
93 if (IS_TRACING_PLATFORM) pajeSetVariable (date, variable, resource, aux);
94 xbt_dict_set (resource_variables, xbt_strdup(key), xbt_strdup(aux), xbt_free);
98 * TRACE_surf_link_declaration (name, bandwidth, latency): this function
99 * saves the bandwidth and latency of a link identified by name. This
100 * information is used in the future to create the link container in the trace.
102 * caller: net_link_new (from each network model)
103 * main: save bandwidth and latency information
106 void TRACE_surf_link_declaration (char *name, double bw, double lat)
108 if (!IS_TRACING) return;
110 double *bw_ptr = xbt_malloc(sizeof(double));
111 double *lat_ptr = xbt_malloc(sizeof(double));
114 xbt_dict_set (link_bandwidth, name, bw_ptr, xbt_free);
115 xbt_dict_set (link_latency, name, lat_ptr, xbt_free);
119 * TRACE_surf_host_declaration (name, power): this function
120 * saves the power of a host identified by name. This information
121 * is used to create the host container in the trace.
123 * caller: cpu_new (from each cpu model) + router parser
124 * main: create HOST containers, set initial power value
127 void TRACE_surf_host_declaration (char *name, double power)
129 if (!IS_TRACING) return;
130 pajeCreateContainer (SIMIX_get_clock(), name, "HOST", "platform", name);
131 xbt_dict_set (host_containers, name, xbt_strdup("1"), xbt_free);
132 if (IS_TRACING_PLATFORM){
133 __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "power", name, power);
138 * TRACE_surf_link_save_endpoints (link_name, src, dst): this function
139 * creates the container of a link identified by link_name. It gets
140 * bandwidth and latency information from the dictionaries previously
141 * filled. The end points are obtained from the host_router_name dictionary.
143 * caller: end of parsing
144 * main: create LINK containers, set initial bandwidth and latency values
147 void TRACE_surf_link_save_endpoints (char *link_name, int src, int dst)
149 char srcidstr[100], dstidstr[100];
152 if (!IS_TRACING) return;
153 snprintf (srcidstr, 100, "%d", src);
154 snprintf (dstidstr, 100, "%d", dst);
155 char *srcname = xbt_dict_get (host_router_name, srcidstr);
156 char *dstname = xbt_dict_get (host_router_name, dstidstr);
157 snprintf (key, 100, "l%d-%d", src, dst);
159 if (strcmp (link_name, "__loopback__")==0 ||
160 strcmp (link_name, "loopback")==0){ //ignore loopback updates
164 if (!xbt_dict_get_or_null (created_links, link_name)){
165 double *bw = xbt_dict_get (link_bandwidth, link_name);
166 double *lat = xbt_dict_get (link_latency, link_name);
167 pajeCreateContainerWithBandwidthLatencySrcDst (SIMIX_get_clock(), link_name, "LINK", "platform", link_name, *bw, *lat, srcname, dstname);
168 if (IS_TRACING_PLATFORM) __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "bandwidth", link_name, *bw);
169 if (IS_TRACING_PLATFORM) __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "latency", link_name, *lat);
170 xbt_dict_set (created_links, link_name, xbt_strdup ("1"), xbt_free);
175 * TRACE_surf_host_define_id (name, host_id): This function relates
176 * the name of the host with its id, as generated by the called and
177 * passed as second parameter.
179 * caller: host parser, router parser
183 void TRACE_surf_host_define_id (const char *name, int host_id)
185 if (!IS_TRACING) return;
187 snprintf (strid, 100, "%d", host_id);
188 xbt_dict_set (host_router_id, name, xbt_strdup(strid), xbt_free);
189 xbt_dict_set (host_router_name, strid, xbt_strdup(name), xbt_free);
192 void TRACE_surf_host_set_power (double date, char *resource, double power)
194 __TRACE_surf_set_resource_variable (date, "power", resource, power);
197 void TRACE_surf_link_set_bandwidth (double date, char *resource, double bandwidth)
199 __TRACE_surf_set_resource_variable (date, "bandwidth", resource, bandwidth);
202 void TRACE_surf_link_set_latency (double date, char *resource, double latency)
204 __TRACE_surf_set_resource_variable (date, "latency", resource, latency);
207 /* to trace gtnets */
208 void TRACE_surf_gtnets_communicate (void *action, int src, int dst)
210 char key[100], aux[100];
211 if (!IS_TRACING) return;
212 snprintf (key, 100, "%p", action);
214 snprintf (aux, 100, "%d", src);
215 xbt_dict_set (gtnets_src, key, xbt_strdup(aux), xbt_free);
216 snprintf (aux, 100, "%d", dst);
217 xbt_dict_set (gtnets_dst, key, xbt_strdup(aux), xbt_free);
220 int TRACE_surf_gtnets_get_src (void *action)
224 if (!IS_TRACING) return -1;
225 snprintf (key, 100, "%p", action);
227 aux = xbt_dict_get_or_null (gtnets_src, key);
235 int TRACE_surf_gtnets_get_dst (void *action)
239 if (!IS_TRACING) return -1;
240 snprintf (key, 100, "%p", action);
242 aux = xbt_dict_get_or_null (gtnets_dst, key);
250 void TRACE_surf_gtnets_destroy (void *action)
253 if (!IS_TRACING) return;
254 snprintf (key, 100, "%p", action);
255 xbt_dict_remove (gtnets_src, key);
256 xbt_dict_remove (gtnets_dst, key);
259 void TRACE_surf_link_missing (void)
261 CRITICAL0("The trace cannot be done because "
262 "the platform you are using contains "
263 "routes with more than one link.");
264 THROW0(tracing_error, TRACE_ERROR_COMPLEX_ROUTES, "Tracing failed");
267 void TRACE_msg_clean (void)
270 xbt_dict_cursor_t cursor = NULL;
271 __TRACE_surf_finalize();
273 /* get all host from host_containers */
274 xbt_dict_foreach(host_containers, cursor, key, value) {
275 pajeDestroyContainer (MSG_get_clock(), "HOST", key);
279 void TRACE_surf_host_vivaldi_parse (char *host, double x, double y, double h)
282 if (!IS_TRACING || !IS_TRACING_PLATFORM) return;
284 snprintf (valuestr, 100, "%g", x);
285 pajeSetVariable (0, "vivaldi_x", host, valuestr);
286 snprintf (valuestr, 100, "%g", y);
287 pajeSetVariable (0, "vivaldi_y", host, valuestr);
288 snprintf (valuestr, 100, "%g", h);
289 pajeSetVariable (0, "vivaldi_h", host, valuestr);