Logo AND Algorithmique Numérique Distribuée

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