1 /* Copyright (c) 2010. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "instr/instr_private.h"
10 #include "surf/surf_private.h"
11 #include "surf/network_private.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_routing, instr, "Tracing platform hierarchy");
15 extern xbt_dict_t defined_types; /* from instr_interface.c */
24 typedef struct s_container *container_t;
25 typedef struct s_container {
26 char *name; /* Unique name of this container */
27 char *id; /* Unique id of this container */
28 char *type; /* Type of this container */
29 char *typename; /* Type name of this container */
30 int level; /* Level in the hierarchy, root level is 0 */
31 e_container_types kind; /* This container is of what kind */
32 struct s_container *father;
36 static container_t rootContainer = NULL; /* the root container */
37 static xbt_dynar_t currentContainer = NULL; /* push and pop, used only in creation */
38 static xbt_dict_t allContainers = NULL; /* all created containers indexed by name */
39 xbt_dict_t hosts_types = NULL;
40 xbt_dict_t links_types = NULL;
42 static void instr_routing_parse_start_AS (void);
43 static void instr_routing_parse_end_AS (void);
44 static void instr_routing_parse_start_link (void);
45 static void instr_routing_parse_end_link (void);
46 static void instr_routing_parse_start_host (void);
47 static void instr_routing_parse_end_host (void);
48 static void instr_routing_parse_start_router (void);
49 static void instr_routing_parse_end_router (void);
50 static void instr_routing_parse_end_platform (void);
51 static char *instr_AS_type (int level);
53 static char *instr_AS_type (int level)
55 char *ret = xbt_new (char, INSTR_DEFAULT_STR_SIZE);
57 snprintf (ret, INSTR_DEFAULT_STR_SIZE, "0");
59 snprintf (ret, INSTR_DEFAULT_STR_SIZE, "L%d", level);
64 static void newContainerType (const char *type, const char *parentType, const char *name)
66 char *defined = xbt_dict_get_or_null (defined_types, type);
68 pajeDefineContainerType(type, parentType, name);
69 xbt_dict_set(defined_types, type, xbt_strdup("1"), xbt_free);
74 static void newVariableType (const char *type, const char *parentType, const char *name, const char *color)
76 char *defined = xbt_dict_get_or_null (defined_types, type);
79 pajeDefineVariableTypeWithColor(type, parentType, name, color);
81 pajeDefineVariableType(type, parentType, name);
83 xbt_dict_set(defined_types, type, xbt_strdup("1"), xbt_free);
87 static void newLinkType (const char *type, const char *parentType, const char *sourceType, const char *destType, const char *name)
89 char *defined = xbt_dict_get_or_null (defined_types, type);
91 pajeDefineLinkType(type, parentType, sourceType, destType, name);
92 xbt_dict_set(defined_types, type, xbt_strdup("1"), xbt_free);
96 void instr_routing_define_callbacks ()
98 if (!TRACE_is_active())
100 surfxml_add_callback(STag_surfxml_AS_cb_list, &instr_routing_parse_start_AS);
101 surfxml_add_callback(ETag_surfxml_AS_cb_list, &instr_routing_parse_end_AS);
102 surfxml_add_callback(STag_surfxml_link_cb_list, &instr_routing_parse_start_link);
103 surfxml_add_callback(ETag_surfxml_link_cb_list, &instr_routing_parse_end_link);
104 surfxml_add_callback(STag_surfxml_host_cb_list, &instr_routing_parse_start_host);
105 surfxml_add_callback(ETag_surfxml_host_cb_list, &instr_routing_parse_end_host);
106 surfxml_add_callback(STag_surfxml_router_cb_list, &instr_routing_parse_start_router);
107 surfxml_add_callback(ETag_surfxml_router_cb_list, &instr_routing_parse_end_router);
108 surfxml_add_callback(ETag_surfxml_platform_cb_list, &instr_routing_parse_end_platform);
111 static long long int newContainedId ()
113 static long long counter = 0;
117 static container_t newContainer (const char *name, const char *type, e_container_types kind)
119 long long int counter = newContainedId();
120 char id_str[INSTR_DEFAULT_STR_SIZE];
121 snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", counter);
123 container_t newContainer = xbt_new0(s_container_t, 1);
124 newContainer->name = xbt_strdup (name);
125 newContainer->id = xbt_strdup (id_str);
126 newContainer->father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
127 newContainer->level = newContainer->father->level+1;
128 newContainer->type = xbt_strdup (type);
130 case INSTR_HOST: newContainer->typename = xbt_strdup ("HOST"); break;
131 case INSTR_LINK: newContainer->typename = xbt_strdup ("LINK"); break;
132 case INSTR_ROUTER: newContainer->typename = xbt_strdup ("ROUTER"); break;
133 case INSTR_AS: newContainer->typename = xbt_strdup ("AS"); break;
134 default: xbt_die ("Congratulations, you have found a bug on newContainer function of instr_routing.c"); break;
136 newContainer->kind = kind;
137 newContainer->children = xbt_dict_new();
138 xbt_dict_set(newContainer->father->children, newContainer->name, newContainer, NULL);
140 newContainerType (newContainer->type, newContainer->father->type, newContainer->typename);
141 pajeCreateContainer (0, newContainer->id, newContainer->type, newContainer->father->id, newContainer->name);
146 static void recursiveDestroyContainer (container_t container)
148 xbt_dict_cursor_t cursor = NULL;
151 xbt_dict_foreach(container->children, cursor, child_name, child) {
152 recursiveDestroyContainer (child);
155 pajeDestroyContainer(SIMIX_get_clock(), container->type, container->id);
157 xbt_free (container->name);
158 xbt_free (container->id);
159 xbt_free (container->type);
160 xbt_free (container->children);
161 xbt_free (container);
165 static void linkContainers (container_t container, const char *a1, const char *a2)
168 if (strcmp (a1, "__loopback__") == 0 || strcmp (a2, "__loopback__") == 0)
171 char *a1_id = ((container_t)xbt_dict_get (allContainers, a1))->id;
172 char *a1_type = ((container_t)xbt_dict_get (allContainers, a1))->type;
173 char *a1_typename = ((container_t)xbt_dict_get (allContainers, a1))->typename;
175 char *a2_id = ((container_t)xbt_dict_get (allContainers, a2))->id;
176 char *a2_type = ((container_t)xbt_dict_get (allContainers, a2))->type;
177 char *a2_typename = ((container_t)xbt_dict_get (allContainers, a2))->typename;
180 char new_link_type[INSTR_DEFAULT_STR_SIZE], new_link_typename[INSTR_DEFAULT_STR_SIZE];
181 snprintf (new_link_type, INSTR_DEFAULT_STR_SIZE, "%s-%s", a1_type, a2_type);
182 snprintf (new_link_typename, INSTR_DEFAULT_STR_SIZE, "%s-%s", a1_typename, a2_typename);
183 newLinkType (new_link_type, container->type, a1_type, a2_type, new_link_typename);
186 static long long counter = 0;
187 char key[INSTR_DEFAULT_STR_SIZE];
188 snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter++);
189 pajeStartLink(SIMIX_get_clock(), new_link_type, container->id, "G", a1_id, key);
190 pajeEndLink(SIMIX_get_clock(), new_link_type, container->id, "G", a2_id, key);
193 static void recursiveGraphExtraction (container_t container)
195 if (xbt_dict_length(container->children)){
196 xbt_dict_cursor_t cursor = NULL;
199 //bottom-up recursion
200 xbt_dict_foreach(container->children, cursor, child_name, child) {
201 recursiveGraphExtraction (child);
205 xbt_dict_cursor_t cursor1 = NULL, cursor2 = NULL;
206 container_t child1, child2;
207 const char *child_name1, *child_name2;
209 xbt_dict_foreach(container->children, cursor1, child_name1, child1) {
210 xbt_dict_foreach(container->children, cursor2, child_name2, child2) {
211 if ((child1->kind == INSTR_HOST || child1->kind == INSTR_ROUTER) &&
212 (child2->kind == INSTR_HOST || child2->kind == INSTR_ROUTER)){
218 route = global_routing->get_route (child_name1, child_name2);
220 //no route between them, that's possible
224 //link the route members
227 char *previous_entity_name = (char*)child_name1;
228 xbt_dynar_foreach (route, cpt, link) {
229 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
230 linkContainers (container, previous_entity_name, link_name);
231 previous_entity_name = link_name;
233 linkContainers (container, previous_entity_name, child_name2);
234 }else if (child1->kind == INSTR_AS &&
235 child2->kind == INSTR_AS &&
236 strcmp(child_name1, child_name2) != 0){
239 routing_component_t root = global_routing->root;
240 route_extended_t route;
243 route = root->get_route (root, child_name1, child_name2);
245 //no route between them, that's possible
250 char *previous_entity_name = route->src_gateway;
251 xbt_dynar_foreach (route->generic_route.link_list, cpt, link) {
252 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
253 linkContainers (container, previous_entity_name, link_name);
254 previous_entity_name = link_name;
256 linkContainers (container, previous_entity_name, route->dst_gateway);
266 static void instr_routing_parse_start_AS ()
268 if (rootContainer == NULL){
269 rootContainer = xbt_new0(s_container_t, 1);
270 rootContainer->name = xbt_strdup ("0");
271 rootContainer->id = xbt_strdup (rootContainer->name);
272 rootContainer->type = xbt_strdup ("0");
273 rootContainer->typename = xbt_strdup ("0");
274 rootContainer->level = 0;
275 rootContainer->father = NULL;
276 rootContainer->children = xbt_dict_new();
277 rootContainer->kind = INSTR_AS;
279 currentContainer = xbt_dynar_new (sizeof(s_container_t), NULL);
280 xbt_dynar_push (currentContainer, rootContainer);
282 allContainers = xbt_dict_new ();
283 hosts_types = xbt_dict_new ();
284 links_types = xbt_dict_new ();
287 long long int counter = newContainedId();
288 char id_str[INSTR_DEFAULT_STR_SIZE];
289 snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", counter);
291 container_t newContainer = xbt_new0(s_container_t, 1);
292 newContainer->name = xbt_strdup (A_surfxml_AS_id);
293 newContainer->id = xbt_strdup (id_str);
294 newContainer->father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
295 newContainer->level = newContainer->father->level+1;
296 newContainer->type = instr_AS_type (newContainer->level);
297 newContainer->typename = instr_AS_type (newContainer->level);
298 newContainer->children = xbt_dict_new();
299 newContainer->kind = INSTR_AS;
300 xbt_dict_set(newContainer->father->children, newContainer->name, newContainer, NULL);
303 newContainerType (newContainer->type, newContainer->father->type, newContainer->type);
304 pajeCreateContainer (0, newContainer->id, newContainer->type, newContainer->father->id, newContainer->name);
307 xbt_dynar_push (currentContainer, newContainer);
310 static void instr_routing_parse_end_AS ()
312 xbt_dynar_pop_ptr (currentContainer);
315 static void instr_routing_parse_start_link ()
317 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
318 char type[INSTR_DEFAULT_STR_SIZE];
319 snprintf (type, INSTR_DEFAULT_STR_SIZE, "LINK-%s", father->type);
320 container_t new = newContainer (A_surfxml_link_id, type, INSTR_LINK);
322 //bandwidth and latency
323 char bandwidth_type[INSTR_DEFAULT_STR_SIZE], latency_type[INSTR_DEFAULT_STR_SIZE];
324 snprintf (bandwidth_type, INSTR_DEFAULT_STR_SIZE, "bandwidth-%s", type);
325 snprintf (latency_type, INSTR_DEFAULT_STR_SIZE, "latency-%s", type);
326 newVariableType (bandwidth_type, type, "bandwidth", NULL);
327 newVariableType (latency_type, type, "latency", NULL);
328 pajeSetVariable(0, bandwidth_type, new->id, A_surfxml_link_bandwidth);
329 pajeSetVariable(0, latency_type, new->id, A_surfxml_link_latency);
331 if (TRACE_uncategorized()){
333 char bandwidth_used_type[INSTR_DEFAULT_STR_SIZE];
334 snprintf (bandwidth_used_type, INSTR_DEFAULT_STR_SIZE, "bandwidth_used-%s", type);
335 newVariableType (bandwidth_used_type, type, "bandwidth_used", "0.5 0.5 0.5");
338 //register created link on the dictionary
339 xbt_dict_set (allContainers, A_surfxml_link_id, new, NULL);
341 //register this link type
342 xbt_dict_set (links_types, type, xbt_strdup("1"), xbt_free);
345 static void instr_routing_parse_end_link ()
349 static void instr_routing_parse_start_host ()
351 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
352 char type[INSTR_DEFAULT_STR_SIZE];
353 snprintf (type, INSTR_DEFAULT_STR_SIZE, "HOST-%s", father->type);
354 container_t new = newContainer (A_surfxml_host_id, type, INSTR_HOST);
357 char power_type[INSTR_DEFAULT_STR_SIZE];
358 snprintf (power_type, INSTR_DEFAULT_STR_SIZE, "power-%s", type);
359 newVariableType (power_type, type, "power", NULL);
360 pajeSetVariable(0, power_type, new->id, A_surfxml_host_power);
362 if (TRACE_uncategorized()){
364 char power_used_type[INSTR_DEFAULT_STR_SIZE];
365 snprintf (power_used_type, INSTR_DEFAULT_STR_SIZE, "power_used-%s", type);
366 newVariableType (power_used_type, type, "power_used", "0.5 0.5 0.5");
369 //register created host on the dictionary
370 xbt_dict_set (allContainers, A_surfxml_host_id, new, NULL);
372 //register this link type
373 xbt_dict_set (hosts_types, type, xbt_strdup("1"), xbt_free);
376 static void instr_routing_parse_end_host ()
380 static void instr_routing_parse_start_router ()
382 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
383 char type[INSTR_DEFAULT_STR_SIZE];
384 snprintf (type, INSTR_DEFAULT_STR_SIZE, "ROUTER-%s", father->type);
385 container_t new = newContainer (A_surfxml_router_id, type, INSTR_ROUTER);
387 //register created host on the dictionary
388 xbt_dict_set (allContainers, A_surfxml_router_id, new, NULL);
391 static void instr_routing_parse_end_router ()
395 static void instr_routing_parse_end_platform ()
397 currentContainer = NULL;
398 recursiveGraphExtraction (rootContainer);
404 int instr_link_is_traced (const char *name)
406 if (((container_t)xbt_dict_get_or_null (allContainers, name))){
413 char *instr_link_type (const char *name)
415 return ((container_t)xbt_dict_get (allContainers, name))->type;
418 char *instr_host_type (const char *name)
420 return ((container_t)xbt_dict_get (allContainers, name))->type;
423 char *instr_id (const char *name)
425 return ((container_t)xbt_dict_get (allContainers, name))->id;
428 void instr_destroy_platform ()
430 if (rootContainer) recursiveDestroyContainer (rootContainer);
433 #endif /* HAVE_TRACING */