Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
450dca98ddbd37d0fe89648ce271710dd197eb0c
[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 char *instr_AS_type (int level);
40
41 static char *instr_AS_type (int level)
42 {
43   char *ret = xbt_new (char, INSTR_DEFAULT_STR_SIZE);
44   if (level == 0){
45     snprintf (ret, INSTR_DEFAULT_STR_SIZE, "0");
46   }else{
47     snprintf (ret, INSTR_DEFAULT_STR_SIZE, "L%d", level);
48   }
49   return ret;
50 }
51
52 static void newContainerType (const char *type, const char *parentType, const char *name)
53 {
54   char *defined = xbt_dict_get_or_null (defined_types, type);
55   if (!defined){
56     pajeDefineContainerType(type, parentType, name);
57     xbt_dict_set(defined_types, type, xbt_strdup("1"), xbt_free);
58   }
59 }
60
61
62 static void newVariableType (const char *type, const char *parentType, const char *name, const char *color)
63 {
64   char *defined = xbt_dict_get_or_null (defined_types, type);
65   if (!defined){
66     if (color){
67       pajeDefineVariableTypeWithColor(type, parentType, name, color);
68     }else{
69       pajeDefineVariableType(type, parentType, name);
70     }
71     xbt_dict_set(defined_types, type, xbt_strdup("1"), xbt_free);
72   }
73 }
74
75 void instr_routing_define_callbacks ()
76 {
77   if (!TRACE_is_active())
78     return;
79   surfxml_add_callback(STag_surfxml_AS_cb_list, &instr_routing_parse_start_AS);
80   surfxml_add_callback(ETag_surfxml_AS_cb_list, &instr_routing_parse_end_AS);
81   surfxml_add_callback(STag_surfxml_link_cb_list, &instr_routing_parse_start_link);
82   surfxml_add_callback(ETag_surfxml_link_cb_list, &instr_routing_parse_end_link);
83   surfxml_add_callback(STag_surfxml_host_cb_list, &instr_routing_parse_start_host);
84   surfxml_add_callback(ETag_surfxml_host_cb_list, &instr_routing_parse_end_host);
85   surfxml_add_callback(STag_surfxml_router_cb_list, &instr_routing_parse_start_router);
86   surfxml_add_callback(ETag_surfxml_router_cb_list, &instr_routing_parse_end_router);
87 }
88
89
90 static container_t newContainer (const char *name, const char *type, const char *typename)
91 {
92   container_t newContainer = xbt_new0(s_container_t, 1);
93   newContainer->name = xbt_strdup (name);
94   newContainer->father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
95   newContainer->level = newContainer->father->level+1;
96   newContainer->type = xbt_strdup (type);
97   newContainer->children = xbt_dict_new();
98   xbt_dict_set(newContainer->father->children, newContainer->name, newContainer, NULL);
99
100   newContainerType (newContainer->type, newContainer->father->type, typename);
101   pajeCreateContainer (0, newContainer->name, newContainer->type, newContainer->father->name, newContainer->name);
102
103   return newContainer;
104 }
105
106 static void recursiveDestroyContainer (container_t container)
107 {
108   xbt_dict_cursor_t cursor = NULL;
109   container_t child;
110   char *child_name;
111   xbt_dict_foreach(container->children, cursor, child_name, child) {
112     recursiveDestroyContainer (child);
113   }
114
115   pajeDestroyContainer(SIMIX_get_clock(), container->type, container->name);
116
117   xbt_free (container->name);
118   xbt_free (container->type);
119   xbt_free (container->children);
120   xbt_free (container);
121   container = NULL;
122 }
123
124 /*
125  * Callbacks
126  */
127 static void instr_routing_parse_start_AS ()
128 {
129   if (rootContainer == NULL){
130     rootContainer = xbt_new0(s_container_t, 1);
131     rootContainer->name = xbt_strdup ("0");
132     rootContainer->type = xbt_strdup ("0");
133     rootContainer->level = 0;
134     rootContainer->father = NULL;
135     rootContainer->children = xbt_dict_new();
136
137     currentContainer = xbt_dynar_new (sizeof(s_container_t), NULL);
138     xbt_dynar_push (currentContainer, rootContainer);
139
140     allContainers = xbt_dict_new ();
141     hosts_types = xbt_dict_new ();
142     links_types = xbt_dict_new ();
143   }
144
145   container_t newContainer = xbt_new0(s_container_t, 1);
146   newContainer->name = xbt_strdup (A_surfxml_AS_id);
147   newContainer->father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
148   newContainer->level = newContainer->father->level+1;
149   newContainer->type = instr_AS_type (newContainer->level);
150   newContainer->children = xbt_dict_new();
151   xbt_dict_set(newContainer->father->children, newContainer->name, newContainer, NULL);
152
153   //trace
154   newContainerType (newContainer->type, newContainer->father->type, newContainer->type);
155   pajeCreateContainer (0, newContainer->name, newContainer->type, newContainer->father->name, newContainer->name);
156
157   //push
158   xbt_dynar_push (currentContainer, newContainer);
159 }
160
161 static void instr_routing_parse_end_AS ()
162 {
163   xbt_dynar_pop_ptr (currentContainer);
164 }
165
166 static void instr_routing_parse_start_link ()
167 {
168   container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
169   char type[INSTR_DEFAULT_STR_SIZE];
170   snprintf (type, INSTR_DEFAULT_STR_SIZE, "LINK-%s", father->type);
171   container_t new = newContainer (A_surfxml_link_id, type, "LINK");
172
173   //bandwidth and latency
174   char bandwidth_type[INSTR_DEFAULT_STR_SIZE], latency_type[INSTR_DEFAULT_STR_SIZE];
175   snprintf (bandwidth_type, INSTR_DEFAULT_STR_SIZE, "bandwidth-%s", type);
176   snprintf (latency_type, INSTR_DEFAULT_STR_SIZE, "latency-%s", type);
177   newVariableType (bandwidth_type, type, "bandwidth", NULL);
178   newVariableType (latency_type, type, "latency", NULL);
179   pajeSetVariable(0, bandwidth_type, new->name, A_surfxml_link_bandwidth);
180   pajeSetVariable(0, latency_type, new->name, A_surfxml_link_latency);
181
182   if (TRACE_uncategorized()){
183     //bandwidth_used
184     char bandwidth_used_type[INSTR_DEFAULT_STR_SIZE];
185     snprintf (bandwidth_used_type, INSTR_DEFAULT_STR_SIZE, "bandwidth_used-%s", type);
186     newVariableType (bandwidth_used_type, type, "bandwidth_used", "0.5 0.5 0.5");
187   }
188
189   //register created link on the dictionary
190   xbt_dict_set (allContainers, A_surfxml_link_id, new, NULL);
191
192   //register this link type
193   xbt_dict_set (links_types, type, xbt_strdup("1"), xbt_free);
194 }
195
196 static void instr_routing_parse_end_link ()
197 {
198 }
199
200 static void instr_routing_parse_start_host ()
201 {
202   container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
203   char type[INSTR_DEFAULT_STR_SIZE];
204   snprintf (type, INSTR_DEFAULT_STR_SIZE, "HOST-%s", father->type);
205   container_t new = newContainer (A_surfxml_host_id, type, "HOST");
206
207   //power
208   char power_type[INSTR_DEFAULT_STR_SIZE];
209   snprintf (power_type, INSTR_DEFAULT_STR_SIZE, "power-%s", type);
210   newVariableType (power_type, type, "power", NULL);
211   pajeSetVariable(0, power_type, new->name, A_surfxml_host_power);
212
213   if (TRACE_uncategorized()){
214     //power_used
215     char power_used_type[INSTR_DEFAULT_STR_SIZE];
216     snprintf (power_used_type, INSTR_DEFAULT_STR_SIZE, "power_used-%s", type);
217     newVariableType (power_used_type, type, "power_used", "0.5 0.5 0.5");
218   }
219
220   //register created host on the dictionary
221   xbt_dict_set (allContainers, A_surfxml_host_id, new, NULL);
222
223   //register this link type
224   xbt_dict_set (hosts_types, type, xbt_strdup("1"), xbt_free);
225 }
226
227 static void instr_routing_parse_end_host ()
228 {
229 }
230
231 static void instr_routing_parse_start_router ()
232 {
233   container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
234   char type[INSTR_DEFAULT_STR_SIZE];
235   snprintf (type, INSTR_DEFAULT_STR_SIZE, "ROUTER-%s", father->type);
236   container_t new = newContainer (A_surfxml_router_id, type, "ROUTER");
237
238   //register created host on the dictionary
239   xbt_dict_set (allContainers, A_surfxml_router_id, new, NULL);
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 (((container_t)xbt_dict_get_or_null (allContainers, name))){
252     return 1;
253   } else {
254     return 0;
255   }
256 }
257
258 char *instr_link_type (const char *name)
259 {
260   return ((container_t)xbt_dict_get (allContainers, name))->type;
261 }
262
263 char *instr_host_type (const char *name)
264 {
265   return ((container_t)xbt_dict_get (allContainers, name))->type;
266 }
267
268 void instr_destroy_platform ()
269 {
270   if (rootContainer) recursiveDestroyContainer (rootContainer);
271 }
272
273 #endif /* HAVE_TRACING */
274