Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
detecting if TRACE_start was called too soon (before TRACE_global_init)
[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; //name(char*) -> bandwidth(double)
18 static xbt_dict_t link_latency;   //name(char*) -> latency(double)
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 /*
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.
299  *
300  * caller: net_link_new (from each network model)
301  * main: save bandwidth and latency information
302  * return: void
303  */
304 void TRACE_surf_link_declaration (char *name, double bw, double lat)
305 {
306   if (!IS_TRACING) return;
307
308   double *bw_ptr = xbt_malloc(sizeof(double));
309   double *lat_ptr = xbt_malloc(sizeof(double));
310   *bw_ptr = bw;
311   *lat_ptr = lat;
312   xbt_dict_set (link_bandwidth, name, bw_ptr, xbt_free);
313   xbt_dict_set (link_latency, name, lat_ptr, xbt_free);
314 }
315
316 /*
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.
320  *
321  * caller: cpu_new (from each cpu model) + router parser
322  * main: create HOST containers, set initial power value
323  * return: void
324  */
325 void TRACE_surf_host_declaration (char *name, double power)
326 {
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);
332   }
333 }
334
335 /*
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.
340  *
341  * caller: end of parsing
342  * main: create LINK containers, set initial bandwidth and latency values
343  * return: void
344  */
345 void TRACE_surf_link_save_endpoints (char *link_name, int src, int dst)
346 {
347         char srcidstr[100], dstidstr[100];
348         char key[100];
349
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);
356
357   if (strcmp (link_name, "__loopback__")==0 ||
358       strcmp (link_name, "loopback")==0){ //ignore loopback updates
359     return;
360   }
361
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);
369   }
370 }
371
372 /*
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.
376  *
377  * caller: host parser, router parser
378  * main: save host_id
379  * return: void
380  */
381 void TRACE_surf_host_define_id (const char *name, int host_id)
382 {
383   char strid[100];
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);
388 }
389
390 void TRACE_surf_host_set_power (double date, char *resource, double power)
391 {
392   __TRACE_surf_set_resource_variable (date, "power", resource, power);
393 }
394
395 void TRACE_surf_link_set_bandwidth (double date, char *resource, double bandwidth)
396 {
397   __TRACE_surf_set_resource_variable (date, "bandwidth", resource, bandwidth);
398 }
399
400 void TRACE_surf_link_set_latency (double date, char *resource, double latency)
401 {
402   __TRACE_surf_set_resource_variable (date, "latency", resource, latency);
403 }
404
405 /* to trace gtnets */
406 void TRACE_surf_gtnets_communicate (void *action, int src, int dst)
407 {
408         char key[100], aux[100];
409   if (!IS_TRACING) return;
410   snprintf (key, 100, "%p", action);
411
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);
416 }
417
418 int TRACE_surf_gtnets_get_src (void *action)
419 {
420         char key[100];
421         char *aux = NULL;
422   if (!IS_TRACING) return -1;
423   snprintf (key, 100, "%p", action);
424
425   aux = xbt_dict_get_or_null (gtnets_src, key);
426   if (aux){
427         return atoi(aux);
428   }else{
429     return -1;
430   }
431 }
432
433 int TRACE_surf_gtnets_get_dst (void *action)
434 {
435         char key[100];
436         char *aux = NULL;
437   if (!IS_TRACING) return -1;
438   snprintf (key, 100, "%p", action);
439
440   aux = xbt_dict_get_or_null (gtnets_dst, key);
441   if (aux){
442         return atoi(aux);
443   }else{
444     return -1;
445   }
446 }
447
448 void TRACE_surf_gtnets_destroy (void *action)
449 {
450   char key[100];
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);
455 }
456
457 void TRACE_surf_link_missing (void)
458 {
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");
463 }
464
465 void TRACE_msg_clean (void)
466 {
467   char *key, *value;
468   xbt_dict_cursor_t cursor = NULL;
469   __TRACE_surf_finalize();
470
471   /* get all host from host_containers */
472   xbt_dict_foreach(host_containers, cursor, key, value) {
473     pajeDestroyContainer (MSG_get_clock(), "HOST", key);
474   }
475 }
476
477 void TRACE_surf_host_vivaldi_parse (char *host, double x, double y, double h)
478 {
479         char valuestr[100];
480   if (!IS_TRACING || !IS_TRACING_PLATFORM) return;
481
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);
488 }
489
490 #endif