Logo AND Algorithmique Numérique Distribuée

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