Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
dedicated file to trace categorized resource utilization
[simgrid.git] / src / instr / surf_instr.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "instr/private.h"
8
9 #ifdef HAVE_TRACING
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(tracing_surf,tracing,"Tracing Surf");
12
13 #define VARIABLE_SEPARATOR '#'
14
15 static xbt_dict_t host_router_id; //name(char*) -> id(char*)
16 static xbt_dict_t host_router_name; //id(char*) -> name (char*)
17
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;
22
23 static xbt_dict_t platform_variables; /* host or link name -> array of categories */
24
25 static xbt_dict_t resource_variables; /* (host|link)#variable -> value */
26
27 /* to trace gtnets */
28 static xbt_dict_t gtnets_src; /* %p (action) -> %s */
29 static xbt_dict_t gtnets_dst; /* %p (action) -> %s */
30
31 void __TRACE_surf_init (void)
32 {
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();
44 }
45
46 void __TRACE_surf_finalize (void)
47 {
48   __TRACE_surf_resource_utilization_finalize();
49 }
50
51 void __TRACE_surf_check_variable_set_to_zero (double now, const char *variable, const char *resource)
52 {
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);
60   }else{
61     xbt_dynar_t array = xbt_dict_get (platform_variables, resource);
62     unsigned int i;
63     char* cat;
64     int flag = 0;
65     xbt_dynar_foreach (array, i, cat) {
66       if (strcmp(variable, cat)==0){
67         flag = 1;
68       }
69     }
70     if (flag==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");
74     }
75   }
76   /* end of check */
77 }
78
79 void __TRACE_surf_set_resource_variable (double date, const char *variable, const char *resource, double value)
80 {
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);
86
87   last_value = xbt_dict_get_or_null(resource_variables, key);
88   if (last_value){
89     if (atof(last_value) == value){
90       return;
91     }
92   }
93   if (IS_TRACING_PLATFORM) pajeSetVariable (date, variable, resource, aux);
94   xbt_dict_set (resource_variables, xbt_strdup(key), xbt_strdup(aux), xbt_free);
95 }
96
97 /*
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.
101  *
102  * caller: net_link_new (from each network model)
103  * main: save bandwidth and latency information
104  * return: void
105  */
106 void TRACE_surf_link_declaration (char *name, double bw, double lat)
107 {
108   if (!IS_TRACING) return;
109
110   double *bw_ptr = xbt_malloc(sizeof(double));
111   double *lat_ptr = xbt_malloc(sizeof(double));
112   *bw_ptr = bw;
113   *lat_ptr = lat;
114   xbt_dict_set (link_bandwidth, name, bw_ptr, xbt_free);
115   xbt_dict_set (link_latency, name, lat_ptr, xbt_free);
116 }
117
118 /*
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.
122  *
123  * caller: cpu_new (from each cpu model) + router parser
124  * main: create HOST containers, set initial power value
125  * return: void
126  */
127 void TRACE_surf_host_declaration (char *name, double power)
128 {
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);
134   }
135 }
136
137 /*
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.
142  *
143  * caller: end of parsing
144  * main: create LINK containers, set initial bandwidth and latency values
145  * return: void
146  */
147 void TRACE_surf_link_save_endpoints (char *link_name, int src, int dst)
148 {
149         char srcidstr[100], dstidstr[100];
150         char key[100];
151
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);
158
159   if (strcmp (link_name, "__loopback__")==0 ||
160       strcmp (link_name, "loopback")==0){ //ignore loopback updates
161     return;
162   }
163
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);
171   }
172 }
173
174 /*
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.
178  *
179  * caller: host parser, router parser
180  * main: save host_id
181  * return: void
182  */
183 void TRACE_surf_host_define_id (const char *name, int host_id)
184 {
185   if (!IS_TRACING) return;
186   char strid[100];
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);
190 }
191
192 void TRACE_surf_host_set_power (double date, char *resource, double power)
193 {
194   __TRACE_surf_set_resource_variable (date, "power", resource, power);
195 }
196
197 void TRACE_surf_link_set_bandwidth (double date, char *resource, double bandwidth)
198 {
199   __TRACE_surf_set_resource_variable (date, "bandwidth", resource, bandwidth);
200 }
201
202 void TRACE_surf_link_set_latency (double date, char *resource, double latency)
203 {
204   __TRACE_surf_set_resource_variable (date, "latency", resource, latency);
205 }
206
207 /* to trace gtnets */
208 void TRACE_surf_gtnets_communicate (void *action, int src, int dst)
209 {
210         char key[100], aux[100];
211   if (!IS_TRACING) return;
212   snprintf (key, 100, "%p", action);
213
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);
218 }
219
220 int TRACE_surf_gtnets_get_src (void *action)
221 {
222         char key[100];
223         char *aux = NULL;
224   if (!IS_TRACING) return -1;
225   snprintf (key, 100, "%p", action);
226
227   aux = xbt_dict_get_or_null (gtnets_src, key);
228   if (aux){
229         return atoi(aux);
230   }else{
231     return -1;
232   }
233 }
234
235 int TRACE_surf_gtnets_get_dst (void *action)
236 {
237         char key[100];
238         char *aux = NULL;
239   if (!IS_TRACING) return -1;
240   snprintf (key, 100, "%p", action);
241
242   aux = xbt_dict_get_or_null (gtnets_dst, key);
243   if (aux){
244         return atoi(aux);
245   }else{
246     return -1;
247   }
248 }
249
250 void TRACE_surf_gtnets_destroy (void *action)
251 {
252   char key[100];
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);
257 }
258
259 void TRACE_surf_link_missing (void)
260 {
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");
265 }
266
267 void TRACE_msg_clean (void)
268 {
269   char *key, *value;
270   xbt_dict_cursor_t cursor = NULL;
271   __TRACE_surf_finalize();
272
273   /* get all host from host_containers */
274   xbt_dict_foreach(host_containers, cursor, key, value) {
275     pajeDestroyContainer (MSG_get_clock(), "HOST", key);
276   }
277 }
278
279 void TRACE_surf_host_vivaldi_parse (char *host, double x, double y, double h)
280 {
281         char valuestr[100];
282   if (!IS_TRACING || !IS_TRACING_PLATFORM) return;
283
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);
290 }
291
292 #endif