Logo AND Algorithmique Numérique Distribuée

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