Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
renaming some tracing functions to make easy to remember what they do
[simgrid.git] / src / instr / surf_instr.c
1 /*
2  * surf.c
3  *
4  *  Created on: Nov 27, 2009
5  *      Author: Lucas Schnorr
6  *     License: This program is free software; you can redistribute
7  *              it and/or modify it under the terms of the license
8  *              (GNU LGPL) which comes with this package.
9  *
10  *     Copyright (c) 2009 The SimGrid team.
11  */
12
13 #include "instr/private.h"
14
15 #ifdef HAVE_TRACING
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(tracing_surf,tracing,"Tracing Surf");
18
19 #define VARIABLE_SEPARATOR '#'
20
21 static xbt_dict_t hosts_id;
22 static xbt_dict_t created_links;
23 static xbt_dict_t link_bandwidth;
24 static xbt_dict_t link_latency;
25 static xbt_dict_t host_containers;
26
27 static xbt_dict_t last_platform_variables; /* to control the amount of add/sub variables events:
28    dict with key {RESOURCE_NAME}#Time or {RESOURCE_NAME}#Value of dict with variables types == string */
29
30 static xbt_dict_t platform_variables; /* host or link name -> array of categories */
31
32 static xbt_dict_t resource_variables; /* (host|link)#variable -> value */
33
34 /* to trace gtnets */
35 static xbt_dict_t gtnets_src; /* %p (action) -> %s */
36 static xbt_dict_t gtnets_dst; /* %p (action) -> %s */
37
38 void __TRACE_surf_init (void)
39 {
40   hosts_id = xbt_dict_new();
41   created_links = xbt_dict_new();
42   link_bandwidth = xbt_dict_new();
43   link_latency = xbt_dict_new();
44   platform_variables = xbt_dict_new();
45   host_containers = xbt_dict_new();
46   last_platform_variables =  xbt_dict_new();
47   resource_variables = xbt_dict_new ();
48   gtnets_src = xbt_dict_new ();
49   gtnets_dst = xbt_dict_new ();
50 }
51
52 static char *strsplit (char *input, int field, char del) //caller should free the returned string
53 {
54   int length = strlen(input), i;
55   int s = 0, e = length+1;
56   int current_field = 0;
57   for (i = 0; i < length; i++){
58      if (input[i] == del){
59        if (current_field == field){
60          e = i-1;
61          break;
62        }else{
63          s = i+1;
64          current_field++;
65        }
66      }
67   }
68   //copy string from s to e (with length equal to e-s) and return
69   char *ret = malloc ((e-s+2)*sizeof(char));
70   strncpy (ret, input+s, e-s+1);
71   ret[e-s+1] = '\0';
72   return ret;
73 }
74
75 void __TRACE_surf_finalize (void)
76 {
77   if (!IS_TRACING_PLATFORM) return;
78   if (!xbt_dict_length(last_platform_variables)){
79     return;
80   }else{
81     xbt_dict_cursor_t cursor = NULL;
82     unsigned int cursor_ar = 0;
83     char *key, *value, *res;
84     char resource[200];
85
86     /* get all resources from last_platform_variables */
87     xbt_dynar_t resources = xbt_dynar_new(sizeof(char)*200, xbt_free);
88     xbt_dict_foreach(last_platform_variables, cursor, key, value) {
89       res = strsplit (key, 0, VARIABLE_SEPARATOR);
90       char *aux = strsplit (key, 1, VARIABLE_SEPARATOR);
91       if (strcmp (aux, "Time") == 0){ //only need to add one of three
92         xbt_dynar_push (resources, xbt_strdup(res));
93       }
94       free (aux);
95       free (res);
96     }
97
98     /* iterate through resources array */
99     xbt_dynar_foreach (resources, cursor_ar, resource) {
100       char timekey[100], valuekey[100], variablekey[100];
101       snprintf (timekey, 100, "%s%cTime", resource, VARIABLE_SEPARATOR);
102       snprintf (valuekey, 100, "%s%cValue", resource, VARIABLE_SEPARATOR);
103       snprintf (variablekey, 100, "%s%cVariable", resource, VARIABLE_SEPARATOR);
104
105       char *time = xbt_dict_get_or_null (last_platform_variables, timekey);
106       if (!time) continue;
107       char *value = xbt_dict_get (last_platform_variables, valuekey);
108       char *variable = xbt_dict_get (last_platform_variables, variablekey);
109       pajeSubVariable (atof(time), variable, resource, value);
110
111       xbt_dict_remove (last_platform_variables, timekey);
112       xbt_dict_remove (last_platform_variables, valuekey);
113       xbt_dict_remove (last_platform_variables, variablekey);
114     }
115   }
116 }
117
118 void __TRACE_surf_check_variable_set_to_zero (double now, const char *variable, const char *resource)
119 {
120   /* check if we have to set it to 0 */
121   if (!xbt_dict_get_or_null (platform_variables, resource)){
122     xbt_dynar_t array = xbt_dynar_new(100*sizeof(char), xbt_free);
123     xbt_dynar_push (array, xbt_strdup(variable));
124     if (IS_TRACING_PLATFORM) pajeSetVariable (now, variable, resource, "0");
125     xbt_dict_set (platform_variables, resource, array, xbt_dynar_free_voidp);
126   }else{
127     xbt_dynar_t array = xbt_dict_get (platform_variables, resource);
128     unsigned int i;
129     char cat[100];
130     int flag = 0;
131     xbt_dynar_foreach (array, i, cat) {
132       if (strcmp(variable, cat)==0){
133         flag = 1;
134       }
135     }
136     if (flag==0){
137       xbt_dynar_push (array, strdup(variable));
138       if (IS_TRACING_PLATFORM) pajeSetVariable (now, variable, resource, "0");
139     }
140   }
141   /* end of check */
142 }
143
144 void __TRACE_surf_update_action_state_resource (double now, double delta, const char *variable, const char *resource, double value)
145 {
146   if (!IS_TRACING_PLATFORM) return;
147
148   char valuestr[100];
149   snprintf (valuestr, 100, "%f", value);
150
151   /*
152   //fprintf (stderr, "resource = %s variable = %s (%f -> %f) value = %s\n", resource, variable, now, now+delta, valuestr);
153   if (1){
154     __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
155     if (IS_TRACING_PLATFORM) pajeAddVariable (now, variable, resource, valuestr);
156     if (IS_TRACING_PLATFORM) pajeSubVariable (now+delta, variable, resource, valuestr);
157     return;
158   }
159   */
160
161   /*
162    * The following code replaces the code above with the objective
163    * to decrease the size of file because of unnecessary add/sub on
164    * variables. It should be re-checked before put in production.
165    */
166
167   char nowstr[100], nowdeltastr[100];
168   snprintf (nowstr, 100, "%.15f", now);
169   snprintf (nowdeltastr, 100, "%.15f", now+delta);
170
171   char timekey[100], valuekey[100], variablekey[100];
172   snprintf (timekey, 100, "%s%cTime", resource, VARIABLE_SEPARATOR);
173   snprintf (valuekey, 100, "%s%cValue", resource, VARIABLE_SEPARATOR);
174   snprintf (variablekey, 100, "%s%cVariable", resource, VARIABLE_SEPARATOR);
175
176   char *lastvariable = xbt_dict_get_or_null (last_platform_variables, variablekey);
177   if (lastvariable == NULL){
178     __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
179     pajeAddVariable (now, variable, resource, valuestr);
180     xbt_dict_set (last_platform_variables, xbt_strdup (timekey), xbt_strdup (nowdeltastr), xbt_free);
181     xbt_dict_set (last_platform_variables, xbt_strdup (valuekey), xbt_strdup (valuestr), xbt_free);
182     xbt_dict_set (last_platform_variables, xbt_strdup (variablekey), xbt_strdup (variable), xbt_free);
183   }else{
184     char *lasttime = xbt_dict_get_or_null (last_platform_variables, timekey);
185     char *lastvalue = xbt_dict_get_or_null (last_platform_variables, valuekey);
186
187     /* check if it is the same variable */
188     if (strcmp(lastvariable, variable) == 0){ /* same variable */
189       /* check if lasttime equals now */
190       if (atof(lasttime) == now){ /* lastime == now */
191         /* check if lastvalue equals valuestr */
192         if (atof(lastvalue) == value){ /* lastvalue == value (good, just advance time) */
193           xbt_dict_set (last_platform_variables, xbt_strdup(timekey), xbt_strdup(nowdeltastr), xbt_free);
194         }else{ /* value has changed */
195           /* value has changed, subtract previous value, add new one */
196           pajeSubVariable (atof(lasttime), variable, resource, lastvalue);
197           pajeAddVariable (atof(nowstr), variable, resource, valuestr);
198           xbt_dict_set (last_platform_variables, xbt_strdup(timekey), xbt_strdup(nowdeltastr), xbt_free);
199           xbt_dict_set (last_platform_variables, xbt_strdup(valuekey), xbt_strdup(valuestr), xbt_free);
200         }
201       }else{ /* lasttime != now */
202         /* the last time is different from new starting time, subtract to lasttime and add from nowstr */
203         pajeSubVariable (atof(lasttime), variable, resource, lastvalue);
204         pajeAddVariable (atof(nowstr), variable, resource, valuestr);
205         xbt_dict_set (last_platform_variables, xbt_strdup(timekey), xbt_strdup(nowdeltastr), xbt_free);
206         xbt_dict_set (last_platform_variables, xbt_strdup(valuekey), xbt_strdup(valuestr), xbt_free);
207       }
208     }else{ /* variable has changed */
209       pajeSubVariable (atof(lasttime), lastvariable, resource, lastvalue);
210       __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
211       pajeAddVariable (now, variable, resource, valuestr);
212       xbt_dict_set (last_platform_variables, xbt_strdup (timekey), xbt_strdup (nowdeltastr), xbt_free);
213       xbt_dict_set (last_platform_variables, xbt_strdup (valuekey), xbt_strdup (valuestr), xbt_free);
214       xbt_dict_set (last_platform_variables, xbt_strdup (variablekey), xbt_strdup (variable), xbt_free);
215     }
216   }
217   return;
218 }
219
220 void __TRACE_surf_set_resource_variable (double date, const char *variable, const char *resource, double value)
221 {
222   if (!IS_TRACING) return;
223   char aux[100], key[100];
224   snprintf (aux, 100, "%f", value);
225   snprintf (key, 100, "%s%c%s", resource, VARIABLE_SEPARATOR, variable);
226
227   char *last_value = xbt_dict_get_or_null(resource_variables, key);
228   if (last_value){
229     if (atof(last_value) == value){
230       return;
231     }
232   }
233   if (IS_TRACING_PLATFORM) pajeSetVariable (date, variable, resource, aux);
234   xbt_dict_set (resource_variables, xbt_strdup(key), xbt_strdup(aux), xbt_free);
235 }
236
237 void TRACE_surf_update_action_state (void *surf_action, smx_action_t smx_action,
238     double value, const char *stateValue, double now, double delta)
239 {
240 }
241
242 void TRACE_surf_update_action_state_net_resource (const char *name, smx_action_t smx_action, double value, double now, double delta)
243 {
244   if (!IS_TRACING || !IS_TRACED(smx_action)) return;
245
246   if (strcmp (name, "__loopback__")==0 ||
247       strcmp (name, "loopback")==0){ //ignore loopback updates
248     return;
249   }
250
251   if (value == 0) return;
252
253   if (!xbt_dict_get_or_null (created_links, name)){
254     TRACE_surf_missing_link ();
255     return;
256   }
257
258   char type[100];
259   snprintf (type, 100, "b%s", smx_action->category);
260   __TRACE_surf_update_action_state_resource (now, delta, type, name, value);
261   return;
262 }
263
264 void TRACE_surf_update_action_state_cpu_resource (const char *name, smx_action_t smx_action, double value, double now, double delta)
265 {
266   if (!IS_TRACING || !IS_TRACED(smx_action)) return;
267
268   if (value==0){
269     return;
270   }
271
272   char type[100];
273   snprintf (type, 100, "p%s", smx_action->category);
274   __TRACE_surf_update_action_state_resource (now, delta, type, name, value);
275   return;
276 }
277
278 void TRACE_surf_link_declaration (char *name, double bw, double lat)
279 {
280   if (!IS_TRACING) return;
281   //if (IS_TRACING_PLATFORM) pajeCreateContainerWithBandwidthLatency (SIMIX_get_clock(), name, "LINK", "platform", name, bw, lat);
282   //save bw and lat information for this link
283   double *bw_ptr, *lat_ptr;
284   bw_ptr = xbt_new (double, 1);
285   lat_ptr = xbt_new (double, 1);
286   *bw_ptr = bw;
287   *lat_ptr = lat;
288   xbt_dict_set (link_bandwidth, xbt_strdup(name), bw_ptr, xbt_free);
289   xbt_dict_set (link_latency, xbt_strdup(name), lat_ptr, xbt_free);
290 }
291
292 void TRACE_surf_host_declaration (char *name, double power)
293 {
294   if (!IS_TRACING) return;
295   if (IS_TRACING_PLATFORM){
296         pajeCreateContainer (SIMIX_get_clock(), name, "HOST", "platform", name);
297         xbt_dict_set (host_containers, xbt_strdup(name), xbt_strdup("1"), xbt_free);
298   }
299   __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "power", name, power);
300 }
301
302 void TRACE_surf_routing_full_parse_end (char *link_name, int src, int dst)
303 {
304   if (!IS_TRACING) return;
305   char srcidstr[100], dstidstr[100];
306   snprintf (srcidstr, 100, "%d", src);
307   snprintf (dstidstr, 100, "%d", dst);
308   char *srcname = xbt_dict_get (hosts_id, srcidstr);
309   char *dstname = xbt_dict_get (hosts_id, dstidstr);
310
311   char key[100];
312   snprintf (key, 100, "l%d-%d", src, dst);
313
314   if (strcmp (link_name, "__loopback__")==0 ||
315       strcmp (link_name, "loopback")==0){ //ignore loopback updates
316     return;
317   }
318
319   if (!xbt_dict_get_or_null (created_links, link_name)){
320     //if (IS_TRACING_PLATFORM) pajeStartLink (SIMIX_get_clock(), "edge", "platform", "route", srcname, key);
321     //if (IS_TRACING_PLATFORM) pajeEndLink (SIMIX_get_clock()+0.1, "edge", "platform", "route", dstname, key);
322     double *bw = xbt_dict_get (link_bandwidth, link_name);
323     double *lat = xbt_dict_get (link_latency, link_name);
324     if (IS_TRACING_PLATFORM) pajeCreateContainerWithBandwidthLatencySrcDst (SIMIX_get_clock(), link_name, "LINK", "platform", link_name, *bw, *lat, srcname, dstname);
325     __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "bandwidth", link_name, *bw);
326     __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "latency", link_name, *lat);
327     xbt_dict_set (created_links, xbt_strdup(link_name), xbt_strdup ("1"), xbt_free);
328   }
329 }
330
331 void TRACE_surf_host_set_power (double date, char *resource, double power)
332 {
333   __TRACE_surf_set_resource_variable (date, "power", resource, power);
334 }
335
336 void TRACE_surf_link_set_bandwidth (double date, char *resource, double bandwidth)
337 {
338   __TRACE_surf_set_resource_variable (date, "bandwidth", resource, bandwidth);
339 }
340
341 void TRACE_surf_link_set_latency (double date, char *resource, double latency)
342 {
343   __TRACE_surf_set_resource_variable (date, "latency", resource, latency);
344 }
345
346 void TRACE_surf_define_host_id (const char *name, int host_id)
347 {
348   if (!IS_TRACING) return;
349   char strid[100];
350   snprintf (strid, 100, "%d", host_id);
351   xbt_dict_set (hosts_id, strdup(name), strdup(strid), free);
352   xbt_dict_set (hosts_id, strdup(strid), strdup(name), free);
353 }
354
355 /* to trace gtnets */
356 void TRACE_surf_gtnets_communicate (void *action, int src, int dst)
357 {
358   if (!IS_TRACING) return;
359   char key[100], aux[100];
360   snprintf (key, 100, "%p", action);
361
362   snprintf (aux, 100, "%d", src);
363   xbt_dict_set (gtnets_src, xbt_strdup(key), xbt_strdup(aux), xbt_free);
364   snprintf (aux, 100, "%d", dst);
365   xbt_dict_set (gtnets_dst, xbt_strdup(key), xbt_strdup(aux), xbt_free);
366 }
367
368 int TRACE_surf_gtnets_get_src (void *action)
369 {
370   if (!IS_TRACING) return -1;
371   char key[100];
372   snprintf (key, 100, "%p", action);
373
374   char *aux = xbt_dict_get_or_null (gtnets_src, key);
375   if (aux){
376         return atoi(aux);
377   }else{
378     return -1;
379   }
380 }
381
382 int TRACE_surf_gtnets_get_dst (void *action)
383 {
384   if (!IS_TRACING) return -1;
385   char key[100];
386   snprintf (key, 100, "%p", action);
387
388   char *aux = xbt_dict_get_or_null (gtnets_dst, key);
389   if (aux){
390         return atoi(aux);
391   }else{
392     return -1;
393   }
394 }
395
396 void TRACE_surf_gtnets_destroy (void *action)
397 {
398   if (!IS_TRACING) return;
399   char key[100];
400   snprintf (key, 100, "%p", action);
401   xbt_dict_remove (gtnets_src, key);
402   xbt_dict_remove (gtnets_dst, key);
403 }
404
405 void TRACE_surf_missing_link (void)
406 {
407   CRITICAL0("The trace cannot be done because "
408                  "the platform you are using contains "
409                  "routes with more than one link.");
410   THROW0(tracing_error, TRACE_ERROR_COMPLEX_ROUTES, "Tracing failed");
411 }
412
413 void TRACE_msg_clean (void)
414 {
415   __TRACE_surf_finalize();
416
417   xbt_dict_cursor_t cursor = NULL;
418   char *key, *value;
419
420   /* get all host from host_containers */
421   xbt_dict_foreach(host_containers, cursor, key, value) {
422     if (IS_TRACING_PLATFORM) pajeDestroyContainer (MSG_get_clock(), "HOST", key);
423   }
424 }
425
426 void TRACE_surf_vivaldi_parse_host (char *host, double x, double y, double h)
427 {
428   if (!IS_TRACING || !IS_TRACING_PLATFORM) return;
429
430   char valuestr[100];
431   snprintf (valuestr, 100, "%g", x);
432   pajeSetVariable (0, "vivaldi_x", host, valuestr);
433   snprintf (valuestr, 100, "%g", y);
434   pajeSetVariable (0, "vivaldi_y", host, valuestr);
435   snprintf (valuestr, 100, "%g", h);
436   pajeSetVariable (0, "vivaldi_h", host, valuestr);
437 }
438
439 #endif