Logo AND Algorithmique Numérique Distribuée

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