Logo AND Algorithmique Numérique Distribuée

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