Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cca1d54fbb860b65200e565a30531b0447b603a3
[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       //TODO: should remove, but it is causing sigabort
112       //xbt_dict_remove (last_platform_variables, timekey);
113       //xbt_dict_remove (last_platform_variables, valuekey);
114       //xbt_dict_remove (last_platform_variables, variablekey);
115     }
116   }
117 }
118
119 void __TRACE_surf_check_variable_set_to_zero (double now, const char *variable, const char *resource)
120 {
121   /* check if we have to set it to 0 */
122   if (!xbt_dict_get_or_null (platform_variables, resource)){
123     xbt_dynar_t array = xbt_dynar_new(100*sizeof(char), xbt_free);
124     xbt_dynar_push (array, xbt_strdup(variable));
125     if (IS_TRACING_PLATFORM) pajeSetVariable (now, variable, resource, "0");
126     xbt_dict_set (platform_variables, resource, array, xbt_dynar_free_voidp);
127   }else{
128     xbt_dynar_t array = xbt_dict_get (platform_variables, resource);
129     unsigned int i;
130     char cat[100];
131     int flag = 0;
132     xbt_dynar_foreach (array, i, cat) {
133       if (strcmp(variable, cat)==0){
134         flag = 1;
135       }
136     }
137     if (flag==0){
138       xbt_dynar_push (array, strdup(variable));
139       if (IS_TRACING_PLATFORM) pajeSetVariable (now, variable, resource, "0");
140     }
141   }
142   /* end of check */
143 }
144
145 void __TRACE_surf_update_action_state_resource (double now, double delta, const char *variable, const char *resource, double value)
146 {
147   if (!IS_TRACING_PLATFORM) return;
148
149   char valuestr[100];
150   snprintf (valuestr, 100, "%f", value);
151
152   /*
153   //fprintf (stderr, "resource = %s variable = %s (%f -> %f) value = %s\n", resource, variable, now, now+delta, valuestr);
154   if (1){
155     __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
156     if (IS_TRACING_PLATFORM) pajeAddVariable (now, variable, resource, valuestr);
157     if (IS_TRACING_PLATFORM) pajeSubVariable (now+delta, variable, resource, valuestr);
158     return;
159   }
160   */
161
162   /*
163    * The following code replaces the code above with the objective
164    * to decrease the size of file because of unnecessary add/sub on
165    * variables. It should be re-checked before put in production.
166    */
167
168   char nowstr[100], nowdeltastr[100];
169   snprintf (nowstr, 100, "%.15f", now);
170   snprintf (nowdeltastr, 100, "%.15f", now+delta);
171
172   char timekey[100], valuekey[100], variablekey[100];
173   snprintf (timekey, 100, "%s%cTime", resource, VARIABLE_SEPARATOR);
174   snprintf (valuekey, 100, "%s%cValue", resource, VARIABLE_SEPARATOR);
175   snprintf (variablekey, 100, "%s%cVariable", resource, VARIABLE_SEPARATOR);
176
177   char *lastvariable = xbt_dict_get_or_null (last_platform_variables, variablekey);
178   if (lastvariable == NULL){
179     __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
180     pajeAddVariable (now, variable, resource, valuestr);
181     xbt_dict_set (last_platform_variables, xbt_strdup (timekey), xbt_strdup (nowdeltastr), xbt_free);
182     xbt_dict_set (last_platform_variables, xbt_strdup (valuekey), xbt_strdup (valuestr), xbt_free);
183     xbt_dict_set (last_platform_variables, xbt_strdup (variablekey), xbt_strdup (variable), xbt_free);
184   }else{
185     char *lasttime = xbt_dict_get_or_null (last_platform_variables, timekey);
186     char *lastvalue = xbt_dict_get_or_null (last_platform_variables, valuekey);
187
188     /* check if it is the same variable */
189     if (strcmp(lastvariable, variable) == 0){ /* same variable */
190       /* check if lasttime equals now */
191       if (atof(lasttime) == now){ /* lastime == now */
192         /* check if lastvalue equals valuestr */
193         if (atof(lastvalue) == value){ /* lastvalue == value (good, just advance time) */
194           xbt_dict_set (last_platform_variables, xbt_strdup(timekey), xbt_strdup(nowdeltastr), xbt_free);
195         }else{ /* value has changed */
196           /* value has changed, subtract previous value, add new one */
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{ /* lasttime != now */
203         /* the last time is different from new starting time, subtract to lasttime and add from nowstr */
204         pajeSubVariable (atof(lasttime), variable, resource, lastvalue);
205         pajeAddVariable (atof(nowstr), 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       }
209     }else{ /* variable has changed */
210       pajeSubVariable (atof(lasttime), lastvariable, resource, lastvalue);
211       __TRACE_surf_check_variable_set_to_zero (now, variable, resource);
212       pajeAddVariable (now, variable, resource, valuestr);
213       xbt_dict_set (last_platform_variables, xbt_strdup (timekey), xbt_strdup (nowdeltastr), xbt_free);
214       xbt_dict_set (last_platform_variables, xbt_strdup (valuekey), xbt_strdup (valuestr), xbt_free);
215       xbt_dict_set (last_platform_variables, xbt_strdup (variablekey), xbt_strdup (variable), xbt_free);
216     }
217   }
218   return;
219 }
220
221 void __TRACE_surf_set_resource_variable (double date, const char *variable, const char *resource, double value)
222 {
223   if (!IS_TRACING) return;
224   char aux[100], key[100];
225   snprintf (aux, 100, "%f", value);
226   snprintf (key, 100, "%s%c%s", resource, VARIABLE_SEPARATOR, variable);
227
228   char *last_value = xbt_dict_get_or_null(resource_variables, key);
229   if (last_value){
230     if (atof(last_value) == value){
231       return;
232     }
233   }
234   if (IS_TRACING_PLATFORM) pajeSetVariable (date, variable, resource, aux);
235   xbt_dict_set (resource_variables, xbt_strdup(key), xbt_strdup(aux), xbt_free);
236 }
237
238 void TRACE_surf_update_action_state (void *surf_action, smx_action_t smx_action,
239     double value, const char *stateValue, double now, double delta)
240 {
241 }
242
243 void TRACE_surf_update_action_state_net_resource (const char *name, smx_action_t smx_action, double value, double now, double delta)
244 {
245   if (!IS_TRACING || !IS_TRACED(smx_action)) return;
246
247   if (strcmp (name, "__loopback__")==0 ||
248       strcmp (name, "loopback")==0){ //ignore loopback updates
249     return;
250   }
251
252   if (value == 0) return;
253
254   if (!xbt_dict_get_or_null (created_links, name)){
255     TRACE_surf_missing_link ();
256     return;
257   }
258
259   char type[100];
260   snprintf (type, 100, "b%s", smx_action->category);
261   __TRACE_surf_update_action_state_resource (now, delta, type, name, value);
262   return;
263 }
264
265 void TRACE_surf_update_action_state_cpu_resource (const char *name, smx_action_t smx_action, double value, double now, double delta)
266 {
267   if (!IS_TRACING || !IS_TRACED(smx_action)) return;
268
269   if (value==0){
270     return;
271   }
272
273   char type[100];
274   snprintf (type, 100, "p%s", smx_action->category);
275   __TRACE_surf_update_action_state_resource (now, delta, type, name, value);
276   return;
277 }
278
279 void TRACE_surf_net_link_new (char *name, double bw, double lat)
280 {
281   if (!IS_TRACING) return;
282   //if (IS_TRACING_PLATFORM) pajeCreateContainerWithBandwidthLatency (SIMIX_get_clock(), name, "LINK", "platform", name, bw, lat);
283   //save bw and lat information for this link
284   double *bw_ptr, *lat_ptr;
285   bw_ptr = xbt_new (double, 1);
286   lat_ptr = xbt_new (double, 1);
287   *bw_ptr = bw;
288   *lat_ptr = lat;
289   xbt_dict_set (link_bandwidth, xbt_strdup(name), bw_ptr, xbt_free);
290   xbt_dict_set (link_latency, xbt_strdup(name), lat_ptr, xbt_free);
291 }
292
293 void TRACE_surf_cpu_new (char *name, double power)
294 {
295   if (!IS_TRACING) return;
296   if (IS_TRACING_PLATFORM){
297         pajeCreateContainer (SIMIX_get_clock(), name, "HOST", "platform", name);
298         xbt_dict_set (host_containers, xbt_strdup(name), xbt_strdup("1"), xbt_free);
299   }
300   __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "power", name, power);
301 }
302
303 void TRACE_surf_routing_full_parse_end (char *link_name, int src, int dst)
304 {
305   if (!IS_TRACING) return;
306   char srcidstr[100], dstidstr[100];
307   snprintf (srcidstr, 100, "%d", src);
308   snprintf (dstidstr, 100, "%d", dst);
309   char *srcname = xbt_dict_get (hosts_id, srcidstr);
310   char *dstname = xbt_dict_get (hosts_id, dstidstr);
311
312   char key[100];
313   snprintf (key, 100, "l%d-%d", src, dst);
314
315   if (strcmp (link_name, "__loopback__")==0 ||
316       strcmp (link_name, "loopback")==0){ //ignore loopback updates
317     return;
318   }
319
320   if (!xbt_dict_get_or_null (created_links, link_name)){
321     //if (IS_TRACING_PLATFORM) pajeStartLink (SIMIX_get_clock(), "edge", "platform", "route", srcname, key);
322     //if (IS_TRACING_PLATFORM) pajeEndLink (SIMIX_get_clock()+0.1, "edge", "platform", "route", dstname, key);
323     double *bw = xbt_dict_get (link_bandwidth, link_name);
324     double *lat = xbt_dict_get (link_latency, link_name);
325     if (IS_TRACING_PLATFORM) pajeCreateContainerWithBandwidthLatencySrcDst (SIMIX_get_clock(), link_name, "LINK", "platform", link_name, *bw, *lat, srcname, dstname);
326     __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "bandwidth", link_name, *bw);
327     __TRACE_surf_set_resource_variable (SIMIX_get_clock(), "latency", link_name, *lat);
328     xbt_dict_set (created_links, xbt_strdup(link_name), xbt_strdup ("1"), xbt_free);
329   }
330 }
331
332 void TRACE_surf_cpu_set_power (double date, char *resource, double power)
333 {
334   __TRACE_surf_set_resource_variable (date, "power", resource, power);
335 }
336
337 void TRACE_surf_link_set_bandwidth (double date, char *resource, double bandwidth)
338 {
339   __TRACE_surf_set_resource_variable (date, "bandwidth", resource, bandwidth);
340 }
341
342 void TRACE_surf_link_set_latency (double date, char *resource, double latency)
343 {
344   __TRACE_surf_set_resource_variable (date, "latency", resource, latency);
345 }
346
347 void TRACE_surf_define_host_id (const char *name, int host_id)
348 {
349   if (!IS_TRACING) return;
350   char strid[100];
351   snprintf (strid, 100, "%d", host_id);
352   xbt_dict_set (hosts_id, strdup(name), strdup(strid), free);
353   xbt_dict_set (hosts_id, strdup(strid), strdup(name), free);
354 }
355
356 /* to trace gtnets */
357 void TRACE_surf_gtnets_communicate (void *action, int src, int dst)
358 {
359   if (!IS_TRACING) return;
360   char key[100], aux[100];
361   snprintf (key, 100, "%p", action);
362
363   snprintf (aux, 100, "%d", src);
364   xbt_dict_set (gtnets_src, xbt_strdup(key), xbt_strdup(aux), xbt_free);
365   snprintf (aux, 100, "%d", dst);
366   xbt_dict_set (gtnets_dst, xbt_strdup(key), xbt_strdup(aux), xbt_free);
367 }
368
369 int TRACE_surf_gtnets_get_src (void *action)
370 {
371   if (!IS_TRACING) return -1;
372   char key[100];
373   snprintf (key, 100, "%p", action);
374
375   char *aux = xbt_dict_get_or_null (gtnets_src, key);
376   if (aux){
377         return atoi(aux);
378   }else{
379     return -1;
380   }
381 }
382
383 int TRACE_surf_gtnets_get_dst (void *action)
384 {
385   if (!IS_TRACING) return -1;
386   char key[100];
387   snprintf (key, 100, "%p", action);
388
389   char *aux = xbt_dict_get_or_null (gtnets_dst, key);
390   if (aux){
391         return atoi(aux);
392   }else{
393     return -1;
394   }
395 }
396
397 void TRACE_surf_gtnets_destroy (void *action)
398 {
399   if (!IS_TRACING) return;
400   char key[100];
401   snprintf (key, 100, "%p", action);
402   xbt_dict_remove (gtnets_src, key);
403   xbt_dict_remove (gtnets_dst, key);
404 }
405
406 void TRACE_surf_missing_link (void)
407 {
408   CRITICAL0("The trace cannot be done because "
409                  "the platform you are using contains "
410                  "routes with more than one link.");
411   THROW0(tracing_error, TRACE_ERROR_COMPLEX_ROUTES, "Tracing failed");
412 }
413
414 void TRACE_msg_clean (void)
415 {
416   __TRACE_surf_finalize();
417
418   xbt_dict_cursor_t cursor = NULL;
419   unsigned int cursor_ar = 0;
420   char *key, *value;
421
422   /* get all host from host_containers */
423   xbt_dict_foreach(host_containers, cursor, key, value) {
424     if (IS_TRACING_PLATFORM) pajeDestroyContainer (MSG_get_clock(), "HOST", key);
425   }
426 }
427
428 void TRACE_surf_vivaldi_parse_host (char *host, double x, double y, double h)
429 {
430   if (!IS_TRACING || !IS_TRACING_PLATFORM) return;
431
432   char valuestr[100];
433   snprintf (valuestr, 100, "%g", x);
434   pajeSetVariable (0, "vivaldi_x", host, valuestr);
435   snprintf (valuestr, 100, "%g", y);
436   pajeSetVariable (0, "vivaldi_y", host, valuestr);
437   snprintf (valuestr, 100, "%g", h);
438   pajeSetVariable (0, "vivaldi_h", host, valuestr);
439 }
440
441 #endif