Logo AND Algorithmique Numérique Distribuée

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