Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix: association between router name and its id
[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 hosts_id;
16 static xbt_dict_t created_links;
17 static xbt_dict_t link_bandwidth;
18 static xbt_dict_t link_latency;
19 static xbt_dict_t host_containers;
20
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 */
23
24 static xbt_dict_t platform_variables; /* host or link name -> array of categories */
25
26 static xbt_dict_t resource_variables; /* (host|link)#variable -> value */
27
28 /* to trace gtnets */
29 static xbt_dict_t gtnets_src; /* %p (action) -> %s */
30 static xbt_dict_t gtnets_dst; /* %p (action) -> %s */
31
32 void __TRACE_surf_init (void)
33 {
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 ();
44 }
45
46 static char *strsplit (char *input, int field, char del) //caller should free the returned string
47 {
48         char *ret = NULL;
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++){
53      if (input[i] == del){
54        if (current_field == field){
55          e = i-1;
56          break;
57        }else{
58          s = i+1;
59          current_field++;
60        }
61      }
62   }
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);
66   ret[e-s+1] = '\0';
67   return ret;
68 }
69
70 void __TRACE_surf_finalize (void)
71 {
72         xbt_dict_cursor_t cursor = NULL;
73         unsigned int cursor_ar = 0;
74     char *key, *value, *res;
75     char *resource;
76     char *aux = NULL;
77     char *var_cpy = NULL;
78     xbt_dynar_t resources = NULL;
79   if (!IS_TRACING_PLATFORM) return;
80   if (!xbt_dict_length(last_platform_variables)){
81     return;
82   }else{
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);
91       }
92       free (aux);
93       free (res);
94     }
95
96     /* iterate through resources array */
97     xbt_dynar_foreach (resources, cursor_ar, resource) {
98       char timekey[100], valuekey[100], variablekey[100];
99       char *time = NULL;
100       char *value = NULL;
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);
105
106       time = xbt_dict_get_or_null (last_platform_variables, timekey);
107       if (!time) continue;
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);
111
112       xbt_dict_remove (last_platform_variables, timekey);
113       xbt_dict_remove (last_platform_variables, valuekey);
114       xbt_dict_remove (last_platform_variables, variablekey);
115     }
116   }
117 }
118
119 void __TRACE_surf_check_variable_set_to_zero (double now, const char *variable, const char *resource)
120 {
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);
128   }else{
129     xbt_dynar_t array = xbt_dict_get (platform_variables, resource);
130     unsigned int i;
131     char* cat;
132     int flag = 0;
133     xbt_dynar_foreach (array, i, cat) {
134       if (strcmp(variable, cat)==0){
135         flag = 1;
136       }
137     }
138     if (flag==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");
142     }
143   }
144   /* end of check */
145 }
146
147 void __TRACE_surf_update_action_state_resource (double now, double delta, const char *variable, const char *resource, double value)
148 {
149         char valuestr[100];
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;
158
159   if (!IS_TRACING_PLATFORM) return;
160
161   snprintf (valuestr, 100, "%f", value);
162
163   /*
164   //fprintf (stderr, "resource = %s variable = %s (%f -> %f) value = %s\n", resource, variable, now, now+delta, valuestr);
165   if (1){
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);
169     return;
170   }
171   */
172
173   /*
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.
177    */
178
179   snprintf (nowstr, 100, "%.15f", now);
180   snprintf (nowdeltastr, 100, "%.15f", now+delta);
181
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);
185
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);
196   }else{
197     lasttime = xbt_dict_get_or_null (last_platform_variables, timekey);
198     lastvalue = xbt_dict_get_or_null (last_platform_variables, valuekey);
199
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);
216         }
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);
225       }
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);
236     }
237   }
238   return;
239 }
240
241 void __TRACE_surf_set_resource_variable (double date, const char *variable, const char *resource, double value)
242 {
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);
248
249   last_value = xbt_dict_get_or_null(resource_variables, key);
250   if (last_value){
251     if (atof(last_value) == value){
252       return;
253     }
254   }
255   if (IS_TRACING_PLATFORM) pajeSetVariable (date, variable, resource, aux);
256   xbt_dict_set (resource_variables, xbt_strdup(key), xbt_strdup(aux), xbt_free);
257 }
258
259 void TRACE_surf_link_set_utilization (const char *name, smx_action_t smx_action, double value, double now, double delta)
260 {
261         char type[100];
262   if (!IS_TRACING || !IS_TRACED(smx_action)) return;
263
264   if (strcmp (name, "__loopback__")==0 ||
265       strcmp (name, "loopback")==0){ //ignore loopback updates
266     return;
267   }
268
269   if (value == 0) return;
270
271   if (!xbt_dict_get_or_null (created_links, name)){
272     TRACE_surf_link_missing ();
273     return;
274   }
275
276   snprintf (type, 100, "b%s", smx_action->category);
277   __TRACE_surf_update_action_state_resource (now, delta, type, name, value);
278   return;
279 }
280
281 void TRACE_surf_host_set_utilization (const char *name, smx_action_t smx_action, double value, double now, double delta)
282 {
283         char type[100];
284   if (!IS_TRACING || !IS_TRACED(smx_action)) return;
285
286   if (value==0){
287     return;
288   }
289
290   snprintf (type, 100, "p%s", smx_action->category);
291   __TRACE_surf_update_action_state_resource (now, delta, type, name, value);
292   return;
293 }
294
295 void TRACE_surf_link_declaration (char *name, double bw, double lat)
296 {
297         double *bw_ptr, *lat_ptr;
298   if (!IS_TRACING) return;
299   //if (IS_TRACING_PLATFORM) pajeCreateContainerWithBandwidthLatency (SIMIX_get_clock(), name, "LINK", "platform", name, bw, lat);
300   //save bw and lat information for this link
301   bw_ptr = xbt_new (double, 1);
302   lat_ptr = xbt_new (double, 1);
303   *bw_ptr = bw;
304   *lat_ptr = lat;
305   xbt_dict_set (link_bandwidth, name, bw_ptr, xbt_free);
306   xbt_dict_set (link_latency, name, lat_ptr, xbt_free);
307 }
308
309 void TRACE_surf_host_declaration (char *name, double power)
310 {
311   if (!IS_TRACING) return;
312   pajeCreateContainer (SIMIX_get_clock(), name, "HOST", "platform", name);
313   xbt_dict_set (host_containers, name, xbt_strdup("1"), xbt_free);
314   if (IS_TRACING_PLATFORM){
315     __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "power", name, power);
316   }
317 }
318
319 void TRACE_surf_link_save_endpoints (char *link_name, int src, int dst)
320 {
321         char srcidstr[100], dstidstr[100];
322         char key[100];
323         char *srcname = NULL;
324         char *dstname = NULL;
325     double *bw = 0;
326     double *lat = 0;
327   if (!IS_TRACING) return;
328   snprintf (srcidstr, 100, "%d", src);
329   snprintf (dstidstr, 100, "%d", dst);
330   srcname = xbt_dict_get (hosts_id, srcidstr);
331   dstname = xbt_dict_get (hosts_id, dstidstr);
332   snprintf (key, 100, "l%d-%d", src, dst);
333
334   if (strcmp (link_name, "__loopback__")==0 ||
335       strcmp (link_name, "loopback")==0){ //ignore loopback updates
336     return;
337   }
338
339   if (!xbt_dict_get_or_null (created_links, link_name)){
340     //if (IS_TRACING_PLATFORM) pajeStartLink (SIMIX_get_clock(), "edge", "platform", "route", srcname, key);
341     //if (IS_TRACING_PLATFORM) pajeEndLink (SIMIX_get_clock()+0.1, "edge", "platform", "route", dstname, key);
342     bw = xbt_dict_get (link_bandwidth, link_name);
343     lat = xbt_dict_get (link_latency, link_name);
344     pajeCreateContainerWithBandwidthLatencySrcDst (SIMIX_get_clock(), link_name, "LINK", "platform", link_name, *bw, *lat, srcname, dstname);
345     if (IS_TRACING_PLATFORM) __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "bandwidth", link_name, *bw);
346     if (IS_TRACING_PLATFORM) __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "latency", link_name, *lat);
347     xbt_dict_set (created_links, link_name, xbt_strdup ("1"), xbt_free);
348   }
349 }
350
351 void TRACE_surf_host_set_power (double date, char *resource, double power)
352 {
353   __TRACE_surf_set_resource_variable (date, "power", resource, power);
354 }
355
356 void TRACE_surf_link_set_bandwidth (double date, char *resource, double bandwidth)
357 {
358   __TRACE_surf_set_resource_variable (date, "bandwidth", resource, bandwidth);
359 }
360
361 void TRACE_surf_link_set_latency (double date, char *resource, double latency)
362 {
363   __TRACE_surf_set_resource_variable (date, "latency", resource, latency);
364 }
365
366 void TRACE_surf_host_define_id (const char *name, int host_id)
367 {
368         char strid[100];
369   if (!IS_TRACING) return;
370   snprintf (strid, 100, "%d", host_id);
371   xbt_dict_set (hosts_id, name, strdup(strid), free);
372   xbt_dict_set (hosts_id, strid, strdup(name), free);
373 }
374
375 /* to trace gtnets */
376 void TRACE_surf_gtnets_communicate (void *action, int src, int dst)
377 {
378         char key[100], aux[100];
379   if (!IS_TRACING) return;
380   snprintf (key, 100, "%p", action);
381
382   snprintf (aux, 100, "%d", src);
383   xbt_dict_set (gtnets_src, key, xbt_strdup(aux), xbt_free);
384   snprintf (aux, 100, "%d", dst);
385   xbt_dict_set (gtnets_dst, key, xbt_strdup(aux), xbt_free);
386 }
387
388 int TRACE_surf_gtnets_get_src (void *action)
389 {
390         char key[100];
391         char *aux = NULL;
392   if (!IS_TRACING) return -1;
393   snprintf (key, 100, "%p", action);
394
395   aux = xbt_dict_get_or_null (gtnets_src, key);
396   if (aux){
397         return atoi(aux);
398   }else{
399     return -1;
400   }
401 }
402
403 int TRACE_surf_gtnets_get_dst (void *action)
404 {
405         char key[100];
406         char *aux = NULL;
407   if (!IS_TRACING) return -1;
408   snprintf (key, 100, "%p", action);
409
410   aux = xbt_dict_get_or_null (gtnets_dst, key);
411   if (aux){
412         return atoi(aux);
413   }else{
414     return -1;
415   }
416 }
417
418 void TRACE_surf_gtnets_destroy (void *action)
419 {
420   char key[100];
421   if (!IS_TRACING) return;
422   snprintf (key, 100, "%p", action);
423   xbt_dict_remove (gtnets_src, key);
424   xbt_dict_remove (gtnets_dst, key);
425 }
426
427 void TRACE_surf_link_missing (void)
428 {
429   CRITICAL0("The trace cannot be done because "
430                  "the platform you are using contains "
431                  "routes with more than one link.");
432   THROW0(tracing_error, TRACE_ERROR_COMPLEX_ROUTES, "Tracing failed");
433 }
434
435 void TRACE_msg_clean (void)
436 {
437   char *key, *value;
438   xbt_dict_cursor_t cursor = NULL;
439   __TRACE_surf_finalize();
440
441   /* get all host from host_containers */
442   xbt_dict_foreach(host_containers, cursor, key, value) {
443     pajeDestroyContainer (MSG_get_clock(), "HOST", key);
444   }
445 }
446
447 void TRACE_surf_host_vivaldi_parse (char *host, double x, double y, double h)
448 {
449         char valuestr[100];
450   if (!IS_TRACING || !IS_TRACING_PLATFORM) return;
451
452   snprintf (valuestr, 100, "%g", x);
453   pajeSetVariable (0, "vivaldi_x", host, valuestr);
454   snprintf (valuestr, 100, "%g", y);
455   pajeSetVariable (0, "vivaldi_y", host, valuestr);
456   snprintf (valuestr, 100, "%g", h);
457   pajeSetVariable (0, "vivaldi_h", host, valuestr);
458 }
459
460 #endif