Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] pay attention to configuration options when tracing
[simgrid.git] / src / instr / instr_routing.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/instr_private.h"
8
9 #ifdef HAVE_TRACING
10 #include "surf/surfxml_parse_private.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_routing, instr, "Tracing platform hierarchy");
13
14 extern routing_global_t global_routing; /* from surf/surf_routing.c */
15 extern xbt_dict_t defined_types; /* from instr_interface.c */
16
17 typedef struct s_container *container_t;
18 typedef struct s_container {
19   char *name;
20   char *type;
21   int level;
22   struct s_container *father;
23   xbt_dict_t children;
24 }s_container_t;
25
26 static container_t rootContainer = NULL;
27 static xbt_dynar_t currentContainer = NULL;
28 static xbt_dict_t created_links = NULL;
29 static xbt_dict_t created_hosts = NULL;
30 xbt_dict_t hosts_types = NULL;
31 xbt_dict_t links_types = NULL;
32
33 static void instr_routing_parse_start_AS (void);
34 static void instr_routing_parse_end_AS (void);
35 static void instr_routing_parse_start_link (void);
36 static void instr_routing_parse_end_link (void);
37 static void instr_routing_parse_start_host (void);
38 static void instr_routing_parse_end_host (void);
39 static void instr_routing_parse_start_router (void);
40 static void instr_routing_parse_end_router (void);
41 static char *instr_AS_type (int level);
42
43 static char *instr_AS_type (int level)
44 {
45   char *ret = xbt_new (char, INSTR_DEFAULT_STR_SIZE);
46   if (level == 0){
47     snprintf (ret, INSTR_DEFAULT_STR_SIZE, "0");
48   }else{
49     snprintf (ret, INSTR_DEFAULT_STR_SIZE, "L%d", level);
50   }
51   return ret;
52 }
53
54 static void newContainerType (const char *type, const char *parentType, const char *name)
55 {
56   char *defined = xbt_dict_get_or_null (defined_types, type);
57   if (!defined){
58     pajeDefineContainerType(type, parentType, name);
59     xbt_dict_set(defined_types, type, xbt_strdup("1"), xbt_free);
60   }
61 }
62
63
64 static void newVariableType (const char *type, const char *parentType, const char *name, const char *color)
65 {
66   char *defined = xbt_dict_get_or_null (defined_types, type);
67   if (!defined){
68     if (color){
69       pajeDefineVariableTypeWithColor(type, parentType, name, color);
70     }else{
71       pajeDefineVariableType(type, parentType, name);
72     }
73     xbt_dict_set(defined_types, type, xbt_strdup("1"), xbt_free);
74   }
75 }
76
77 void instr_routing_define_callbacks ()
78 {
79   if (!TRACE_is_active())
80     return;
81   surfxml_add_callback(STag_surfxml_AS_cb_list, &instr_routing_parse_start_AS);
82   surfxml_add_callback(ETag_surfxml_AS_cb_list, &instr_routing_parse_end_AS);
83   surfxml_add_callback(STag_surfxml_link_cb_list, &instr_routing_parse_start_link);
84   surfxml_add_callback(ETag_surfxml_link_cb_list, &instr_routing_parse_end_link);
85   surfxml_add_callback(STag_surfxml_host_cb_list, &instr_routing_parse_start_host);
86   surfxml_add_callback(ETag_surfxml_host_cb_list, &instr_routing_parse_end_host);
87   surfxml_add_callback(STag_surfxml_router_cb_list, &instr_routing_parse_start_router);
88   surfxml_add_callback(ETag_surfxml_router_cb_list, &instr_routing_parse_end_router);
89 }
90
91
92 static container_t newContainer (const char *name, const char *type, const char *typename)
93 {
94   container_t newContainer = xbt_new0(s_container_t, 1);
95   newContainer->name = xbt_strdup (name);
96   newContainer->father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
97   newContainer->level = newContainer->father->level+1;
98   newContainer->type = xbt_strdup (type);
99   newContainer->children = xbt_dict_new();
100   xbt_dict_set(newContainer->father->children, newContainer->name, newContainer, NULL);
101
102   newContainerType (newContainer->type, newContainer->father->type, typename);
103   pajeCreateContainer (0, newContainer->name, newContainer->type, newContainer->father->name, newContainer->name);
104
105   return newContainer;
106 }
107
108 static void recursiveDestroyContainer (container_t container)
109 {
110   xbt_dict_cursor_t cursor = NULL;
111   container_t child;
112   char *child_name;
113   xbt_dict_foreach(container->children, cursor, child_name, child) {
114     recursiveDestroyContainer (child);
115   }
116
117   pajeDestroyContainer(SIMIX_get_clock(), container->type, container->name);
118
119   xbt_free (container->name);
120   xbt_free (container->type);
121   xbt_free (container->children);
122   xbt_free (container);
123   container = NULL;
124 }
125
126 /*
127  * Callbacks
128  */
129 static void instr_routing_parse_start_AS ()
130 {
131   if (rootContainer == NULL){
132     rootContainer = xbt_new0(s_container_t, 1);
133     rootContainer->name = xbt_strdup ("0");
134     rootContainer->type = xbt_strdup ("0");
135     rootContainer->level = 0;
136     rootContainer->father = NULL;
137     rootContainer->children = xbt_dict_new();
138
139     currentContainer = xbt_dynar_new (sizeof(s_container_t), NULL);
140     xbt_dynar_push (currentContainer, rootContainer);
141
142     created_links = xbt_dict_new ();
143     created_hosts = xbt_dict_new ();
144     hosts_types = xbt_dict_new ();
145     links_types = xbt_dict_new ();
146   }
147
148   container_t newContainer = xbt_new0(s_container_t, 1);
149   newContainer->name = xbt_strdup (A_surfxml_AS_id);
150   newContainer->father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
151   newContainer->level = newContainer->father->level+1;
152   newContainer->type = instr_AS_type (newContainer->level);
153   newContainer->children = xbt_dict_new();
154   xbt_dict_set(newContainer->father->children, newContainer->name, newContainer, NULL);
155
156   //trace
157   newContainerType (newContainer->type, newContainer->father->type, newContainer->type);
158   pajeCreateContainer (0, newContainer->name, newContainer->type, newContainer->father->name, newContainer->name);
159
160   //push
161   xbt_dynar_push (currentContainer, newContainer);
162 }
163
164 static void instr_routing_parse_end_AS ()
165 {
166   xbt_dynar_pop_ptr (currentContainer);
167 }
168
169 static void instr_routing_parse_start_link ()
170 {
171   container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
172   char type[INSTR_DEFAULT_STR_SIZE];
173   snprintf (type, INSTR_DEFAULT_STR_SIZE, "LINK-%s", father->type);
174   container_t new = newContainer (A_surfxml_link_id, type, "LINK");
175
176   //bandwidth and latency
177   char bandwidth_type[INSTR_DEFAULT_STR_SIZE], latency_type[INSTR_DEFAULT_STR_SIZE];
178   snprintf (bandwidth_type, INSTR_DEFAULT_STR_SIZE, "bandwidth-%s", type);
179   snprintf (latency_type, INSTR_DEFAULT_STR_SIZE, "latency-%s", type);
180   newVariableType (bandwidth_type, type, "bandwidth", NULL);
181   newVariableType (latency_type, type, "latency", NULL);
182   pajeSetVariable(0, bandwidth_type, new->name, A_surfxml_link_bandwidth);
183   pajeSetVariable(0, latency_type, new->name, A_surfxml_link_latency);
184
185   if (TRACE_uncategorized()){
186     //bandwidth_used
187     char bandwidth_used_type[INSTR_DEFAULT_STR_SIZE];
188     snprintf (bandwidth_used_type, INSTR_DEFAULT_STR_SIZE, "bandwidth_used-%s", type);
189     newVariableType (bandwidth_used_type, type, "bandwidth_used", "0.5 0.5 0.5");
190   }
191
192   //register created link on the dictionary
193   xbt_dict_set (created_links, A_surfxml_link_id, new, NULL);
194
195   //register this link type
196   xbt_dict_set (links_types, type, xbt_strdup("1"), xbt_free);
197 }
198
199 static void instr_routing_parse_end_link ()
200 {
201 }
202
203 static void instr_routing_parse_start_host ()
204 {
205   container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
206   char type[INSTR_DEFAULT_STR_SIZE];
207   snprintf (type, INSTR_DEFAULT_STR_SIZE, "HOST-%s", father->type);
208   container_t new = newContainer (A_surfxml_host_id, type, "HOST");
209
210   //power
211   char power_type[INSTR_DEFAULT_STR_SIZE];
212   snprintf (power_type, INSTR_DEFAULT_STR_SIZE, "power-%s", type);
213   newVariableType (power_type, type, "power", NULL);
214   pajeSetVariable(0, power_type, new->name, A_surfxml_host_power);
215
216   if (TRACE_uncategorized()){
217     //power_used
218     char power_used_type[INSTR_DEFAULT_STR_SIZE];
219     snprintf (power_used_type, INSTR_DEFAULT_STR_SIZE, "power_used-%s", type);
220     newVariableType (power_used_type, type, "power_used", "0.5 0.5 0.5");
221   }
222
223   //register created host on the dictionary
224   xbt_dict_set (created_hosts, A_surfxml_host_id, new, NULL);
225
226   //register this link type
227   xbt_dict_set (hosts_types, type, xbt_strdup("1"), xbt_free);
228 }
229
230 static void instr_routing_parse_end_host ()
231 {
232 }
233
234 static void instr_routing_parse_start_router ()
235 {
236   container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
237   char type[INSTR_DEFAULT_STR_SIZE];
238   snprintf (type, INSTR_DEFAULT_STR_SIZE, "ROUTER-%s", father->type);
239   newContainer (A_surfxml_router_id, type, "ROUTER");
240 }
241
242 static void instr_routing_parse_end_router ()
243 {
244 }
245
246 /*
247  * Support functions
248  */
249 int instr_link_is_traced (const char *name)
250 {
251   if (xbt_dict_get_or_null(created_links, name)) {
252     return 1;
253   } else {
254     return 0;
255   }
256 }
257
258 char *instr_link_type (const char *name)
259 {
260   container_t created_link = xbt_dict_get_or_null(created_links, name);
261   if (created_link){
262     return created_link->type;
263   }else{
264     return NULL;
265   }
266 }
267
268
269 char *instr_host_type (const char *name)
270 {
271   container_t created_host = xbt_dict_get_or_null(created_hosts, name);
272   if (created_host){
273     return created_host->type;
274   }else{
275     return NULL;
276   }
277 }
278
279 void instr_destroy_platform ()
280 {
281   if (rootContainer) recursiveDestroyContainer (rootContainer);
282 }
283
284 #endif /* HAVE_TRACING */
285