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 id of this container */
27 char *type; /* Type of this container */
28 char *typename; /* Type name of this container */
29 int level; /* Level in the hierarchy, root level is 0 */
30 e_container_types kind; /* This container is of what kind */
31 struct s_container *father;
35 static container_t rootContainer = NULL; /* the root container */
36 static xbt_dynar_t currentContainer = NULL; /* push and pop, used only in creation */
37 static xbt_dict_t allContainers = NULL; /* all created containers indexed by name */
38 xbt_dict_t hosts_types = NULL;
39 xbt_dict_t links_types = NULL;
41 static void instr_routing_parse_start_AS (void);
42 static void instr_routing_parse_end_AS (void);
43 static void instr_routing_parse_start_link (void);
44 static void instr_routing_parse_end_link (void);
45 static void instr_routing_parse_start_host (void);
46 static void instr_routing_parse_end_host (void);
47 static void instr_routing_parse_start_router (void);
48 static void instr_routing_parse_end_router (void);
49 static void instr_routing_parse_end_platform (void);
50 static char *instr_AS_type (int level);
52 static char *instr_AS_type (int level)
54 char *ret = xbt_new (char, INSTR_DEFAULT_STR_SIZE);
56 snprintf (ret, INSTR_DEFAULT_STR_SIZE, "0");
58 snprintf (ret, INSTR_DEFAULT_STR_SIZE, "L%d", level);
63 static void newContainerType (const char *type, const char *parentType, const char *name)
65 char *defined = xbt_dict_get_or_null (defined_types, type);
67 pajeDefineContainerType(type, parentType, name);
68 xbt_dict_set(defined_types, type, xbt_strdup("1"), xbt_free);
73 static void newVariableType (const char *type, const char *parentType, const char *name, const char *color)
75 char *defined = xbt_dict_get_or_null (defined_types, type);
78 pajeDefineVariableTypeWithColor(type, parentType, name, color);
80 pajeDefineVariableType(type, parentType, name);
82 xbt_dict_set(defined_types, type, xbt_strdup("1"), xbt_free);
86 static void newLinkType (const char *type, const char *parentType, const char *sourceType, const char *destType, const char *name)
88 char *defined = xbt_dict_get_or_null (defined_types, type);
90 pajeDefineLinkType(type, parentType, sourceType, destType, name);
91 xbt_dict_set(defined_types, type, xbt_strdup("1"), xbt_free);
95 void instr_routing_define_callbacks ()
97 if (!TRACE_is_active())
99 surfxml_add_callback(STag_surfxml_AS_cb_list, &instr_routing_parse_start_AS);
100 surfxml_add_callback(ETag_surfxml_AS_cb_list, &instr_routing_parse_end_AS);
101 surfxml_add_callback(STag_surfxml_link_cb_list, &instr_routing_parse_start_link);
102 surfxml_add_callback(ETag_surfxml_link_cb_list, &instr_routing_parse_end_link);
103 surfxml_add_callback(STag_surfxml_host_cb_list, &instr_routing_parse_start_host);
104 surfxml_add_callback(ETag_surfxml_host_cb_list, &instr_routing_parse_end_host);
105 surfxml_add_callback(STag_surfxml_router_cb_list, &instr_routing_parse_start_router);
106 surfxml_add_callback(ETag_surfxml_router_cb_list, &instr_routing_parse_end_router);
107 surfxml_add_callback(ETag_surfxml_platform_cb_list, &instr_routing_parse_end_platform);
111 static container_t newContainer (const char *name, const char *type, e_container_types kind)
113 container_t newContainer = xbt_new0(s_container_t, 1);
114 newContainer->name = xbt_strdup (name);
115 newContainer->father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
116 newContainer->level = newContainer->father->level+1;
117 newContainer->type = xbt_strdup (type);
119 case INSTR_HOST: newContainer->typename = xbt_strdup ("HOST"); break;
120 case INSTR_LINK: newContainer->typename = xbt_strdup ("LINK"); break;
121 case INSTR_ROUTER: newContainer->typename = xbt_strdup ("ROUTER"); break;
122 case INSTR_AS: newContainer->typename = xbt_strdup ("AS"); break;
123 default: xbt_die ("Congratulations, you have found a bug on newContainer function of instr_routing.c"); break;
125 newContainer->kind = kind;
126 newContainer->children = xbt_dict_new();
127 xbt_dict_set(newContainer->father->children, newContainer->name, newContainer, NULL);
129 newContainerType (newContainer->type, newContainer->father->type, newContainer->typename);
130 pajeCreateContainer (0, newContainer->name, newContainer->type, newContainer->father->name, newContainer->name);
135 static void recursiveDestroyContainer (container_t container)
137 xbt_dict_cursor_t cursor = NULL;
140 xbt_dict_foreach(container->children, cursor, child_name, child) {
141 recursiveDestroyContainer (child);
144 pajeDestroyContainer(SIMIX_get_clock(), container->type, container->name);
146 xbt_free (container->name);
147 xbt_free (container->type);
148 xbt_free (container->children);
149 xbt_free (container);
153 static void linkContainers (container_t container, const char *a1, const char *a2)
156 if (strcmp (a1, "__loopback__") == 0 || strcmp (a2, "__loopback__") == 0)
159 char *a1_type = ((container_t)xbt_dict_get (allContainers, a1))->type;
160 char *a1_typename = ((container_t)xbt_dict_get (allContainers, a1))->typename;
162 char *a2_type = ((container_t)xbt_dict_get (allContainers, a2))->type;
163 char *a2_typename = ((container_t)xbt_dict_get (allContainers, a2))->typename;
166 char new_link_type[INSTR_DEFAULT_STR_SIZE], new_link_typename[INSTR_DEFAULT_STR_SIZE];
167 snprintf (new_link_type, INSTR_DEFAULT_STR_SIZE, "%s-%s", a1_type, a2_type);
168 snprintf (new_link_typename, INSTR_DEFAULT_STR_SIZE, "%s-%s", a1_typename, a2_typename);
169 newLinkType (new_link_type, container->type, a1_type, a2_type, new_link_typename);
172 static long long counter = 0;
173 char key[INSTR_DEFAULT_STR_SIZE];
174 snprintf (key, INSTR_DEFAULT_STR_SIZE, "G%lld", counter++);
175 pajeStartLink(SIMIX_get_clock(), new_link_type, container->name, "graph", a1, key);
176 pajeEndLink(SIMIX_get_clock(), new_link_type, container->name, "graph", a2, key);
179 static void recursiveGraphExtraction (container_t container)
181 if (xbt_dict_length(container->children)){
182 xbt_dict_cursor_t cursor = NULL;
185 //bottom-up recursion
186 xbt_dict_foreach(container->children, cursor, child_name, child) {
187 recursiveGraphExtraction (child);
191 xbt_dict_cursor_t cursor1 = NULL, cursor2 = NULL;
192 container_t child1, child2;
193 const char *child_name1, *child_name2;
195 xbt_dict_foreach(container->children, cursor1, child_name1, child1) {
196 xbt_dict_foreach(container->children, cursor2, child_name2, child2) {
197 if ((child1->kind == INSTR_HOST || child1->kind == INSTR_ROUTER) &&
198 (child2->kind == INSTR_HOST || child2->kind == INSTR_ROUTER)){
204 route = global_routing->get_route (child_name1, child_name2);
206 //no route between them, that's possible
210 //link the route members
213 char *previous_entity_name = (char*)child_name1;
214 xbt_dynar_foreach (route, cpt, link) {
215 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
216 linkContainers (container, previous_entity_name, link_name);
217 previous_entity_name = link_name;
219 linkContainers (container, previous_entity_name, child_name2);
220 }else if (child1->kind == INSTR_AS &&
221 child2->kind == INSTR_AS &&
222 strcmp(child_name1, child_name2) != 0){
225 routing_component_t root = global_routing->root;
226 route_extended_t route;
229 route = root->get_route (root, child_name1, child_name2);
231 //no route between them, that's possible
236 char *previous_entity_name = route->src_gateway;
237 xbt_dynar_foreach (route->generic_route.link_list, cpt, link) {
238 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
239 linkContainers (container, previous_entity_name, link_name);
240 previous_entity_name = link_name;
242 linkContainers (container, previous_entity_name, route->dst_gateway);
252 static void instr_routing_parse_start_AS ()
254 if (rootContainer == NULL){
255 rootContainer = xbt_new0(s_container_t, 1);
256 rootContainer->name = xbt_strdup ("0");
257 rootContainer->type = xbt_strdup ("0");
258 rootContainer->typename = xbt_strdup ("0");
259 rootContainer->level = 0;
260 rootContainer->father = NULL;
261 rootContainer->children = xbt_dict_new();
262 rootContainer->kind = INSTR_AS;
264 currentContainer = xbt_dynar_new (sizeof(s_container_t), NULL);
265 xbt_dynar_push (currentContainer, rootContainer);
267 allContainers = xbt_dict_new ();
268 hosts_types = xbt_dict_new ();
269 links_types = xbt_dict_new ();
272 container_t newContainer = xbt_new0(s_container_t, 1);
273 newContainer->name = xbt_strdup (A_surfxml_AS_id);
274 newContainer->father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
275 newContainer->level = newContainer->father->level+1;
276 newContainer->type = instr_AS_type (newContainer->level);
277 newContainer->typename = instr_AS_type (newContainer->level);
278 newContainer->children = xbt_dict_new();
279 newContainer->kind = INSTR_AS;
280 xbt_dict_set(newContainer->father->children, newContainer->name, newContainer, NULL);
283 newContainerType (newContainer->type, newContainer->father->type, newContainer->type);
284 pajeCreateContainer (0, newContainer->name, newContainer->type, newContainer->father->name, newContainer->name);
287 xbt_dynar_push (currentContainer, newContainer);
290 static void instr_routing_parse_end_AS ()
292 xbt_dynar_pop_ptr (currentContainer);
295 static void instr_routing_parse_start_link ()
297 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
298 char type[INSTR_DEFAULT_STR_SIZE];
299 snprintf (type, INSTR_DEFAULT_STR_SIZE, "LINK-%s", father->type);
300 container_t new = newContainer (A_surfxml_link_id, type, INSTR_LINK);
302 //bandwidth and latency
303 char bandwidth_type[INSTR_DEFAULT_STR_SIZE], latency_type[INSTR_DEFAULT_STR_SIZE];
304 snprintf (bandwidth_type, INSTR_DEFAULT_STR_SIZE, "bandwidth-%s", type);
305 snprintf (latency_type, INSTR_DEFAULT_STR_SIZE, "latency-%s", type);
306 newVariableType (bandwidth_type, type, "bandwidth", NULL);
307 newVariableType (latency_type, type, "latency", NULL);
308 pajeSetVariable(0, bandwidth_type, new->name, A_surfxml_link_bandwidth);
309 pajeSetVariable(0, latency_type, new->name, A_surfxml_link_latency);
311 if (TRACE_uncategorized()){
313 char bandwidth_used_type[INSTR_DEFAULT_STR_SIZE];
314 snprintf (bandwidth_used_type, INSTR_DEFAULT_STR_SIZE, "bandwidth_used-%s", type);
315 newVariableType (bandwidth_used_type, type, "bandwidth_used", "0.5 0.5 0.5");
318 //register created link on the dictionary
319 xbt_dict_set (allContainers, A_surfxml_link_id, new, NULL);
321 //register this link type
322 xbt_dict_set (links_types, type, xbt_strdup("1"), xbt_free);
325 static void instr_routing_parse_end_link ()
329 static void instr_routing_parse_start_host ()
331 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
332 char type[INSTR_DEFAULT_STR_SIZE];
333 snprintf (type, INSTR_DEFAULT_STR_SIZE, "HOST-%s", father->type);
334 container_t new = newContainer (A_surfxml_host_id, type, INSTR_HOST);
337 char power_type[INSTR_DEFAULT_STR_SIZE];
338 snprintf (power_type, INSTR_DEFAULT_STR_SIZE, "power-%s", type);
339 newVariableType (power_type, type, "power", NULL);
340 pajeSetVariable(0, power_type, new->name, A_surfxml_host_power);
342 if (TRACE_uncategorized()){
344 char power_used_type[INSTR_DEFAULT_STR_SIZE];
345 snprintf (power_used_type, INSTR_DEFAULT_STR_SIZE, "power_used-%s", type);
346 newVariableType (power_used_type, type, "power_used", "0.5 0.5 0.5");
349 //register created host on the dictionary
350 xbt_dict_set (allContainers, A_surfxml_host_id, new, NULL);
352 //register this link type
353 xbt_dict_set (hosts_types, type, xbt_strdup("1"), xbt_free);
356 static void instr_routing_parse_end_host ()
360 static void instr_routing_parse_start_router ()
362 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
363 char type[INSTR_DEFAULT_STR_SIZE];
364 snprintf (type, INSTR_DEFAULT_STR_SIZE, "ROUTER-%s", father->type);
365 container_t new = newContainer (A_surfxml_router_id, type, INSTR_ROUTER);
367 //register created host on the dictionary
368 xbt_dict_set (allContainers, A_surfxml_router_id, new, NULL);
371 static void instr_routing_parse_end_router ()
375 static void instr_routing_parse_end_platform ()
377 currentContainer = NULL;
378 recursiveGraphExtraction (rootContainer);
384 int instr_link_is_traced (const char *name)
386 if (((container_t)xbt_dict_get_or_null (allContainers, name))){
393 char *instr_link_type (const char *name)
395 return ((container_t)xbt_dict_get (allContainers, name))->type;
398 char *instr_host_type (const char *name)
400 return ((container_t)xbt_dict_get (allContainers, name))->type;
403 void instr_destroy_platform ()
405 if (rootContainer) recursiveDestroyContainer (rootContainer);
408 #endif /* HAVE_TRACING */