Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
trace: new interface function for tracing resource categorized utilization with SMPI
[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 last_platform_variables; /* to control the amount of add/sub variables events:
24    dict with key {RESOURCE_NAME}#Time or {RESOURCE_NAME}#Value of dict with variables types == string */
25
26 static xbt_dict_t platform_variables; /* host or link name -> array of categories */
27
28 static xbt_dict_t resource_variables; /* (host|link)#variable -> value */
29
30 /* to trace gtnets */
31 static xbt_dict_t gtnets_src; /* %p (action) -> %s */
32 static xbt_dict_t gtnets_dst; /* %p (action) -> %s */
33
34 void __TRACE_surf_init (void)
35 {
36   host_router_id = xbt_dict_new();
37   host_router_name = xbt_dict_new();
38   created_links = xbt_dict_new();
39   link_bandwidth = xbt_dict_new();
40   link_latency = xbt_dict_new();
41   platform_variables = xbt_dict_new();
42   host_containers = xbt_dict_new();
43   last_platform_variables =  xbt_dict_new();
44   resource_variables = xbt_dict_new ();
45   gtnets_src = xbt_dict_new ();
46   gtnets_dst = xbt_dict_new ();
47 }
48
49 static char *strsplit (char *input, int field, char del) //caller should free the returned string
50 {
51         char *ret = NULL;
52   int length = strlen(input), i;
53   int s = 0, e = length+1;
54   int current_field = 0;
55   for (i = 0; i < length; i++){
56      if (input[i] == del){
57        if (current_field == field){
58          e = i-1;
59          break;
60        }else{
61          s = i+1;
62          current_field++;
63        }
64      }
65   }
66   //copy string from s to e (with length equal to e-s) and return
67   ret = malloc ((e-s+2)*sizeof(char));
68   strncpy (ret, input+s, e-s+1);
69   ret[e-s+1] = '\0';
70   return ret;
71 }
72
73 void __TRACE_surf_finalize (void)
74 {
75         xbt_dict_cursor_t cursor = NULL;
76         unsigned int cursor_ar = 0;
77     char *key, *value, *res;
78     char *resource;
79     char *aux = NULL;
80     char *var_cpy = NULL;
81     xbt_dynar_t resources = NULL;
82   if (!IS_TRACING_PLATFORM) return;
83   if (!xbt_dict_length(last_platform_variables)){
84     return;
85   }else{
86     /* get all resources from last_platform_variables */
87     resources = xbt_dynar_new(sizeof(char*), xbt_free);
88     xbt_dict_foreach(last_platform_variables, cursor, key, value) {
89       res = strsplit (key, 0, VARIABLE_SEPARATOR);
90       aux = strsplit (key, 1, VARIABLE_SEPARATOR);
91       if (strcmp (aux, "Time") == 0){ //only need to add one of three
92         var_cpy = xbt_strdup (res);
93         xbt_dynar_push (resources, &var_cpy);
94       }
95       free (aux);
96       free (res);
97     }
98
99     /* iterate through resources array */
100     xbt_dynar_foreach (resources, cursor_ar, resource) {
101       char timekey[100], valuekey[100], variablekey[100];
102       char *time = NULL;
103       char *value = NULL;
104       char *variable = NULL;
105       snprintf (timekey, 100, "%s%cTime", resource, VARIABLE_SEPARATOR);
106       snprintf (valuekey, 100, "%s%cValue", resource, VARIABLE_SEPARATOR);
107       snprintf (variablekey, 100, "%s%cVariable", resource, VARIABLE_SEPARATOR);
108
109       time = xbt_dict_get_or_null (last_platform_variables, timekey);
110       if (!time) continue;
111       value = xbt_dict_get (last_platform_variables, valuekey);
112       variable = xbt_dict_get (last_platform_variables, variablekey);
113       pajeSubVariable (atof(time), variable, resource, value);
114
115       xbt_dict_remove (last_platform_variables, timekey);
116       xbt_dict_remove (last_platform_variables, valuekey);
117       xbt_dict_remove (last_platform_variables, variablekey);
118     }
119   }
120 }
121
122 void __TRACE_surf_check_variable_set_to_zero (double now, const char *variable, const char *resource)
123 {
124   /* check if we have to set it to 0 */
125   if (!xbt_dict_get_or_null (platform_variables, resource)){
126     xbt_dynar_t array = xbt_dynar_new(sizeof(char*), xbt_free);
127     char *var_cpy = xbt_strdup(variable);
128     xbt_dynar_push (array, &var_cpy);
129     if (IS_TRACING_PLATFORM) pajeSetVariable (now, variable, resource, "0");
130     xbt_dict_set (platform_variables, resource, array, xbt_dynar_free_voidp);
131   }else{
132     xbt_dynar_t array = xbt_dict_get (platform_variables, resource);
133     unsigned int i;
134     char* cat;
135     int flag = 0;
136     xbt_dynar_foreach (array, i, cat) {
137       if (strcmp(variable, cat)==0){
138         flag = 1;
139       }
140     }
141     if (flag==0){
142       char *var_cpy = xbt_strdup(variable);
143       xbt_dynar_push (array, &var_cpy);
144       if (IS_TRACING_PLATFORM) pajeSetVariable (now, variable, resource, "0");
145     }
146   }
147   /* end of check */
148 }
149
150 void __TRACE_surf_update_action_state_resource (double now, double delta, const char *variable, const char *resource, double value)
151 {
152         char valuestr[100];
153         char nowstr[100], nowdeltastr[100];
154         char timekey[100], valuekey[100], variablekey[100];
155         char *lastvariable = NULL;
156         char *lasttime = NULL;
157     char *lastvalue = NULL;
158     char *nowdeltastr_cpy = NULL;
159     char *valuestr_cpy = NULL;
160     char *variable_cpy = NULL;
161
162   if (!IS_TRACING_PLATFORM) return;
163
164   snprintf (valuestr, 100, "%f", value);
165
166   /*
167   //fprintf (stderr, "resource = %s variable = %s (%f -> %f) value = %s\n", resource, variable, now, now+delta, valuestr);
168   if (1){
169     __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
170     if (IS_TRACING_PLATFORM) pajeAddVariable (now, variable, resource, valuestr);
171     if (IS_TRACING_PLATFORM) pajeSubVariable (now+delta, variable, resource, valuestr);
172     return;
173   }
174   */
175
176   /*
177    * The following code replaces the code above with the objective
178    * to decrease the size of file because of unnecessary add/sub on
179    * variables. It should be re-checked before put in production.
180    */
181
182   snprintf (nowstr, 100, "%.15f", now);
183   snprintf (nowdeltastr, 100, "%.15f", now+delta);
184
185   snprintf (timekey, 100, "%s%cTime", resource, VARIABLE_SEPARATOR);
186   snprintf (valuekey, 100, "%s%cValue", resource, VARIABLE_SEPARATOR);
187   snprintf (variablekey, 100, "%s%cVariable", resource, VARIABLE_SEPARATOR);
188
189   lastvariable = xbt_dict_get_or_null (last_platform_variables, variablekey);
190   if (lastvariable == NULL){
191     __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
192     pajeAddVariable (now, variable, resource, valuestr);
193     nowdeltastr_cpy = xbt_strdup (nowdeltastr);
194     valuestr_cpy = xbt_strdup (valuestr);
195     variable_cpy = xbt_strdup (variable);
196     xbt_dict_set (last_platform_variables, timekey, nowdeltastr_cpy, xbt_free);
197     xbt_dict_set (last_platform_variables, valuekey, valuestr_cpy, xbt_free);
198     xbt_dict_set (last_platform_variables, variablekey, variable_cpy, xbt_free);
199   }else{
200     lasttime = xbt_dict_get_or_null (last_platform_variables, timekey);
201     lastvalue = xbt_dict_get_or_null (last_platform_variables, valuekey);
202
203     /* check if it is the same variable */
204     if (strcmp(lastvariable, variable) == 0){ /* same variable */
205       /* check if lasttime equals now */
206       if (atof(lasttime) == now){ /* lastime == now */
207         /* check if lastvalue equals valuestr */
208         if (atof(lastvalue) == value){ /* lastvalue == value (good, just advance time) */
209           char *nowdeltastr_cpy = xbt_strdup (nowdeltastr);
210           xbt_dict_set (last_platform_variables, timekey, nowdeltastr_cpy, xbt_free);
211         }else{ /* value has changed */
212           /* value has changed, subtract previous value, add new one */
213           pajeSubVariable (atof(lasttime), variable, resource, lastvalue);
214           pajeAddVariable (atof(nowstr), variable, resource, valuestr);
215           nowdeltastr_cpy = xbt_strdup (nowdeltastr);
216           valuestr_cpy = xbt_strdup (valuestr);
217           xbt_dict_set (last_platform_variables, timekey, nowdeltastr_cpy, xbt_free);
218           xbt_dict_set (last_platform_variables, valuekey, valuestr_cpy, xbt_free);
219         }
220       }else{ /* lasttime != now */
221         /* the last time is different from new starting time, subtract to lasttime and add from nowstr */
222         pajeSubVariable (atof(lasttime), variable, resource, lastvalue);
223         pajeAddVariable (atof(nowstr), variable, resource, valuestr);
224         nowdeltastr_cpy = xbt_strdup (nowdeltastr);
225         valuestr_cpy = xbt_strdup (valuestr);
226         xbt_dict_set (last_platform_variables, timekey, nowdeltastr_cpy, xbt_free);
227         xbt_dict_set (last_platform_variables, valuekey, valuestr_cpy, xbt_free);
228       }
229     }else{ /* variable has changed */
230       pajeSubVariable (atof(lasttime), lastvariable, resource, lastvalue);
231       __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
232       pajeAddVariable (now, variable, resource, valuestr);
233       nowdeltastr_cpy = xbt_strdup (nowdeltastr);
234       valuestr_cpy = xbt_strdup (valuestr);
235       variable_cpy = xbt_strdup (variable);
236       xbt_dict_set (last_platform_variables, timekey, nowdeltastr_cpy, xbt_free);
237       xbt_dict_set (last_platform_variables, valuekey, valuestr_cpy, xbt_free);
238       xbt_dict_set (last_platform_variables, variablekey, variable_cpy, xbt_free);
239     }
240   }
241   return;
242 }
243
244 void __TRACE_surf_set_resource_variable (double date, const char *variable, const char *resource, double value)
245 {
246         char aux[100], key[100];
247         char *last_value = NULL;
248   if (!IS_TRACING) return;
249   snprintf (aux, 100, "%f", value);
250   snprintf (key, 100, "%s%c%s", resource, VARIABLE_SEPARATOR, variable);
251
252   last_value = xbt_dict_get_or_null(resource_variables, key);
253   if (last_value){
254     if (atof(last_value) == value){
255       return;
256     }
257   }
258   if (IS_TRACING_PLATFORM) pajeSetVariable (date, variable, resource, aux);
259   xbt_dict_set (resource_variables, xbt_strdup(key), xbt_strdup(aux), xbt_free);
260 }
261
262 void TRACE_surf_link_set_utilization (const char *name, smx_action_t smx_action, double value, double now, double delta)
263 {
264         char type[100];
265   if (!IS_TRACING || !IS_TRACED(smx_action)) return;
266
267   if (strcmp (name, "__loopback__")==0 ||
268       strcmp (name, "loopback")==0){ //ignore loopback updates
269     return;
270   }
271
272   if (value == 0) return;
273
274   if (!xbt_dict_get_or_null (created_links, name)){
275     TRACE_surf_link_missing ();
276     return;
277   }
278
279   snprintf (type, 100, "b%s", smx_action->category);
280   __TRACE_surf_update_action_state_resource (now, delta, type, name, value);
281   return;
282 }
283
284 void TRACE_surf_host_set_utilization (const char *name, smx_action_t smx_action, double value, double now, double delta)
285 {
286         char type[100];
287   if (!IS_TRACING || !IS_TRACED(smx_action)) return;
288
289   if (value==0){
290     return;
291   }
292
293   snprintf (type, 100, "p%s", smx_action->category);
294   __TRACE_surf_update_action_state_resource (now, delta, type, name, value);
295   return;
296 }
297
298 /*
299  * TRACE_surf_link_declaration (name, bandwidth, latency): this function
300  * saves the bandwidth and latency of a link identified by name. This
301  * information is used in the future to create the link container in the trace.
302  *
303  * caller: net_link_new (from each network model)
304  * main: save bandwidth and latency information
305  * return: void
306  */
307 void TRACE_surf_link_declaration (char *name, double bw, double lat)
308 {
309   if (!IS_TRACING) return;
310
311   double *bw_ptr = xbt_malloc(sizeof(double));
312   double *lat_ptr = xbt_malloc(sizeof(double));
313   *bw_ptr = bw;
314   *lat_ptr = lat;
315   xbt_dict_set (link_bandwidth, name, bw_ptr, xbt_free);
316   xbt_dict_set (link_latency, name, lat_ptr, xbt_free);
317 }
318
319 /*
320  * TRACE_surf_host_declaration (name, power): this function
321  * saves the power of a host identified by name. This information
322  * is used to create the host container in the trace.
323  *
324  * caller: cpu_new (from each cpu model) + router parser
325  * main: create HOST containers, set initial power value
326  * return: void
327  */
328 void TRACE_surf_host_declaration (char *name, double power)
329 {
330   if (!IS_TRACING) return;
331   pajeCreateContainer (SIMIX_get_clock(), name, "HOST", "platform", name);
332   xbt_dict_set (host_containers, name, xbt_strdup("1"), xbt_free);
333   if (IS_TRACING_PLATFORM){
334     __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "power", name, power);
335   }
336 }
337
338 /*
339  * TRACE_surf_link_save_endpoints (link_name, src, dst): this function
340  * creates the container of a link identified by link_name. It gets
341  * bandwidth and latency information from the dictionaries previously
342  * filled. The end points are obtained from the host_router_name dictionary.
343  *
344  * caller: end of parsing
345  * main: create LINK containers, set initial bandwidth and latency values
346  * return: void
347  */
348 void TRACE_surf_link_save_endpoints (char *link_name, int src, int dst)
349 {
350         char srcidstr[100], dstidstr[100];
351         char key[100];
352
353   if (!IS_TRACING) return;
354   snprintf (srcidstr, 100, "%d", src);
355   snprintf (dstidstr, 100, "%d", dst);
356   char *srcname = xbt_dict_get (host_router_name, srcidstr);
357   char *dstname = xbt_dict_get (host_router_name, dstidstr);
358   snprintf (key, 100, "l%d-%d", src, dst);
359
360   if (strcmp (link_name, "__loopback__")==0 ||
361       strcmp (link_name, "loopback")==0){ //ignore loopback updates
362     return;
363   }
364
365   if (!xbt_dict_get_or_null (created_links, link_name)){
366     double *bw = xbt_dict_get (link_bandwidth, link_name);
367     double *lat = xbt_dict_get (link_latency, link_name);
368     pajeCreateContainerWithBandwidthLatencySrcDst (SIMIX_get_clock(), link_name, "LINK", "platform", link_name, *bw, *lat, srcname, dstname);
369     if (IS_TRACING_PLATFORM) __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "bandwidth", link_name, *bw);
370     if (IS_TRACING_PLATFORM) __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "latency", link_name, *lat);
371     xbt_dict_set (created_links, link_name, xbt_strdup ("1"), xbt_free);
372   }
373 }
374
375 /*
376  * TRACE_surf_host_define_id (name, host_id): This function relates
377  * the name of the host with its id, as generated by the called and
378  * passed as second parameter.
379  *
380  * caller: host parser, router parser
381  * main: save host_id
382  * return: void
383  */
384 void TRACE_surf_host_define_id (const char *name, int host_id)
385 {
386   if (!IS_TRACING) return;
387   char strid[100];
388   snprintf (strid, 100, "%d", host_id);
389   xbt_dict_set (host_router_id, name, xbt_strdup(strid), xbt_free);
390   xbt_dict_set (host_router_name, strid, xbt_strdup(name), xbt_free);
391 }
392
393 void TRACE_surf_host_set_power (double date, char *resource, double power)
394 {
395   __TRACE_surf_set_resource_variable (date, "power", resource, power);
396 }
397
398 void TRACE_surf_link_set_bandwidth (double date, char *resource, double bandwidth)
399 {
400   __TRACE_surf_set_resource_variable (date, "bandwidth", resource, bandwidth);
401 }
402
403 void TRACE_surf_link_set_latency (double date, char *resource, double latency)
404 {
405   __TRACE_surf_set_resource_variable (date, "latency", resource, latency);
406 }
407
408 /* to trace gtnets */
409 void TRACE_surf_gtnets_communicate (void *action, int src, int dst)
410 {
411         char key[100], aux[100];
412   if (!IS_TRACING) return;
413   snprintf (key, 100, "%p", action);
414
415   snprintf (aux, 100, "%d", src);
416   xbt_dict_set (gtnets_src, key, xbt_strdup(aux), xbt_free);
417   snprintf (aux, 100, "%d", dst);
418   xbt_dict_set (gtnets_dst, key, xbt_strdup(aux), xbt_free);
419 }
420
421 int TRACE_surf_gtnets_get_src (void *action)
422 {
423         char key[100];
424         char *aux = NULL;
425   if (!IS_TRACING) return -1;
426   snprintf (key, 100, "%p", action);
427
428   aux = xbt_dict_get_or_null (gtnets_src, key);
429   if (aux){
430         return atoi(aux);
431   }else{
432     return -1;
433   }
434 }
435
436 int TRACE_surf_gtnets_get_dst (void *action)
437 {
438         char key[100];
439         char *aux = NULL;
440   if (!IS_TRACING) return -1;
441   snprintf (key, 100, "%p", action);
442
443   aux = xbt_dict_get_or_null (gtnets_dst, key);
444   if (aux){
445         return atoi(aux);
446   }else{
447     return -1;
448   }
449 }
450
451 void TRACE_surf_gtnets_destroy (void *action)
452 {
453   char key[100];
454   if (!IS_TRACING) return;
455   snprintf (key, 100, "%p", action);
456   xbt_dict_remove (gtnets_src, key);
457   xbt_dict_remove (gtnets_dst, key);
458 }
459
460 void TRACE_surf_link_missing (void)
461 {
462   CRITICAL0("The trace cannot be done because "
463                  "the platform you are using contains "
464                  "routes with more than one link.");
465   THROW0(tracing_error, TRACE_ERROR_COMPLEX_ROUTES, "Tracing failed");
466 }
467
468 void TRACE_msg_clean (void)
469 {
470   char *key, *value;
471   xbt_dict_cursor_t cursor = NULL;
472   __TRACE_surf_finalize();
473
474   /* get all host from host_containers */
475   xbt_dict_foreach(host_containers, cursor, key, value) {
476     pajeDestroyContainer (MSG_get_clock(), "HOST", key);
477   }
478 }
479
480 void TRACE_surf_host_vivaldi_parse (char *host, double x, double y, double h)
481 {
482         char valuestr[100];
483   if (!IS_TRACING || !IS_TRACING_PLATFORM) return;
484
485   snprintf (valuestr, 100, "%g", x);
486   pajeSetVariable (0, "vivaldi_x", host, valuestr);
487   snprintf (valuestr, 100, "%g", y);
488   pajeSetVariable (0, "vivaldi_y", host, valuestr);
489   snprintf (valuestr, 100, "%g", h);
490   pajeSetVariable (0, "vivaldi_h", host, valuestr);
491 }
492
493 #endif