Logo AND Algorithmique Numérique Distribuée

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