Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ed7eed46272e4f9bc3910f68f4aa892c0e598046
[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 #include "surf/surf_private.h"
9
10 #ifdef HAVE_TRACING
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(tracing_surf,tracing,"Tracing Surf");
13
14 #define VARIABLE_SEPARATOR '#'
15
16 static xbt_dict_t created_links;
17 static xbt_dict_t host_containers;
18 static xbt_dict_t resource_variables; /* (host|link)#variable -> value */
19
20 /* to trace gtnets */
21 static xbt_dict_t gtnets_src; /* %p (action) -> %s */
22 static xbt_dict_t gtnets_dst; /* %p (action) -> %s */
23
24 void TRACE_surf_init (void)
25 {
26   created_links = xbt_dict_new();
27   host_containers = xbt_dict_new();
28   resource_variables = xbt_dict_new ();
29   gtnets_src = xbt_dict_new ();
30   gtnets_dst = xbt_dict_new ();
31
32   __TRACE_surf_resource_utilization_initialize();
33 }
34
35 void TRACE_surf_finalize (void)
36 {
37   __TRACE_surf_resource_utilization_finalize();
38 }
39
40 static void __TRACE_surf_set_resource_variable (double date, const char *variable, const char *resource, double value)
41 {
42         char aux[100], key[100];
43         char *last_value = NULL;
44   if (!IS_TRACING) return;
45   snprintf (aux, 100, "%f", value);
46   snprintf (key, 100, "%s%c%s", resource, VARIABLE_SEPARATOR, variable);
47
48   last_value = xbt_dict_get_or_null(resource_variables, key);
49   if (last_value){
50     if (atof(last_value) == value){
51       return;
52     }
53   }
54   if (IS_TRACING_PLATFORM) pajeSetVariable (date, variable, resource, aux);
55   xbt_dict_set (resource_variables, xbt_strdup(key), xbt_strdup(aux), xbt_free);
56 }
57
58 /*
59  * TRACE_surf_link_declaration (name, bandwidth, latency): this function
60  * saves the bandwidth and latency of a link identified by name. This
61  * information is used in the future to create the link container in the trace.
62  *
63  * caller: net_link_new (from each network model)
64  * main: create LINK container, set initial bandwidth and latency
65  * return: void
66  */
67 void TRACE_surf_link_declaration (void *link, char *name, double bw, double lat)
68 {
69   if (!IS_TRACING) return;
70
71   //filter out loopback
72   if (!strcmp (name, "loopback") || !strcmp (name, "__loopback__")) return;
73
74   char alias[100];
75   snprintf (alias, 100, "%p", link);
76   pajeCreateContainer (SIMIX_get_clock(), alias, "LINK", "platform", name);
77   xbt_dict_set (created_links, alias, xbt_strdup ("1"), xbt_free);
78   TRACE_surf_link_set_bandwidth (SIMIX_get_clock(), link, bw);
79   TRACE_surf_link_set_latency (SIMIX_get_clock(), link, lat);
80 }
81
82 /*
83  * TRACE_surf_host_declaration (name, power): this function
84  * saves the power of a host identified by name. This information
85  * is used to create the host container in the trace.
86  *
87  * caller: cpu_new (from each cpu model) + router parser
88  * main: create HOST containers, set initial power value
89  * return: void
90  */
91 void TRACE_surf_host_declaration (char *name, double power)
92 {
93   if (!IS_TRACING) return;
94   pajeCreateContainer (SIMIX_get_clock(), name, "HOST", "platform", name);
95   xbt_dict_set (host_containers, name, xbt_strdup("1"), xbt_free);
96   TRACE_surf_host_set_power (SIMIX_get_clock(), name, power);
97 }
98
99 void TRACE_surf_host_set_power (double date, char *resource, double power)
100 {
101   __TRACE_surf_set_resource_variable (date, "power", resource, power);
102 }
103
104 void TRACE_surf_link_set_bandwidth (double date, void *link, double bandwidth)
105 {
106   if (!TRACE_surf_link_is_traced (link)) return;
107
108   char resource[100];
109   snprintf (resource, 100, "%p", link);
110   __TRACE_surf_set_resource_variable (date, "bandwidth", resource, bandwidth);
111 }
112
113 void TRACE_surf_link_set_latency (double date, void *link, double latency)
114 {
115   if (!TRACE_surf_link_is_traced (link)) return;
116
117   char resource[100];
118   snprintf (resource, 100, "%p", link);
119   __TRACE_surf_set_resource_variable (date, "latency", resource, latency);
120 }
121
122 /* to trace gtnets */
123 void TRACE_surf_gtnets_communicate (void *action, int src, int dst)
124 {
125         char key[100], aux[100];
126   if (!IS_TRACING) return;
127   snprintf (key, 100, "%p", action);
128
129   snprintf (aux, 100, "%d", src);
130   xbt_dict_set (gtnets_src, key, xbt_strdup(aux), xbt_free);
131   snprintf (aux, 100, "%d", dst);
132   xbt_dict_set (gtnets_dst, key, xbt_strdup(aux), xbt_free);
133 }
134
135 int TRACE_surf_gtnets_get_src (void *action)
136 {
137         char key[100];
138         char *aux = NULL;
139   if (!IS_TRACING) return -1;
140   snprintf (key, 100, "%p", action);
141
142   aux = xbt_dict_get_or_null (gtnets_src, key);
143   if (aux){
144         return atoi(aux);
145   }else{
146     return -1;
147   }
148 }
149
150 int TRACE_surf_gtnets_get_dst (void *action)
151 {
152         char key[100];
153         char *aux = NULL;
154   if (!IS_TRACING) return -1;
155   snprintf (key, 100, "%p", action);
156
157   aux = xbt_dict_get_or_null (gtnets_dst, key);
158   if (aux){
159         return atoi(aux);
160   }else{
161     return -1;
162   }
163 }
164
165 void TRACE_surf_gtnets_destroy (void *action)
166 {
167   char key[100];
168   if (!IS_TRACING) return;
169   snprintf (key, 100, "%p", action);
170   xbt_dict_remove (gtnets_src, key);
171   xbt_dict_remove (gtnets_dst, key);
172 }
173
174 void TRACE_surf_link_missing (void)
175 {
176   CRITICAL0("The trace cannot be done because "
177                  "the platform you are using contains "
178                  "routes with more than one link.");
179   THROW0(tracing_error, TRACE_ERROR_COMPLEX_ROUTES, "Tracing failed");
180 }
181
182 void TRACE_msg_clean (void)
183 {
184   char *key, *value;
185   xbt_dict_cursor_t cursor = NULL;
186   TRACE_surf_finalize();
187
188   /* get all host from host_containers */
189   xbt_dict_foreach(host_containers, cursor, key, value) {
190     pajeDestroyContainer (MSG_get_clock(), "HOST", key);
191   }
192   xbt_dict_foreach(created_links, cursor, key, value) {
193     pajeDestroyContainer (MSG_get_clock(), "LINK", key);
194   }
195 }
196
197 void TRACE_surf_host_vivaldi_parse (char *host, double x, double y, double h)
198 {
199         char valuestr[100];
200   if (!IS_TRACING || !IS_TRACING_PLATFORM) return;
201
202   snprintf (valuestr, 100, "%g", x);
203   pajeSetVariable (0, "vivaldi_x", host, valuestr);
204   snprintf (valuestr, 100, "%g", y);
205   pajeSetVariable (0, "vivaldi_y", host, valuestr);
206   snprintf (valuestr, 100, "%g", h);
207   pajeSetVariable (0, "vivaldi_h", host, valuestr);
208 }
209
210 extern routing_global_t global_routing;
211 void TRACE_surf_save_onelink (void)
212 {
213   if (!IS_TRACING) return;
214
215   //get the onelinks from the parsed platform
216   xbt_dynar_t onelink_routes = global_routing->get_onelink_routes();
217   if (!onelink_routes) return;
218
219   //save them in trace file
220   onelink_t onelink;
221   unsigned int iter;
222   xbt_dynar_foreach(onelink_routes, iter, onelink) {
223     char *src = onelink->src;
224     char *dst = onelink->dst;
225     void *link = onelink->link_ptr;
226
227     if (TRACE_surf_link_is_traced (link)){
228       char resource[100];
229       snprintf (resource, 100, "%p", link);
230
231       pajeNewEvent (0.1, "source", resource, src);
232       pajeNewEvent (0.1, "destination", resource, dst);
233     }
234   }
235 }
236
237 int TRACE_surf_link_is_traced (void *link)
238 {
239   char alias[100];
240   snprintf (alias, 100, "%p", link);
241   if (xbt_dict_get_or_null (created_links, alias)){
242     return 1;
243   }else{
244     return 0;
245   }
246 }
247
248 #endif