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 */
23 typedef struct s_type *type_t;
24 typedef struct s_type {
28 struct s_type *father;
39 typedef struct s_container *container_t;
40 typedef struct s_container {
41 char *name; /* Unique name of this container */
42 char *id; /* Unique id of this container */
43 type_t type; /* Type of this container */
44 int level; /* Level in the hierarchy, root level is 0 */
45 e_container_types kind; /* This container is of what kind */
46 struct s_container *father;
50 static type_t rootType = NULL; /* the root type */
51 static container_t rootContainer = NULL; /* the root container */
52 static xbt_dynar_t currentContainer = NULL; /* push and pop, used only in creation */
53 static xbt_dict_t allContainers = NULL; /* all created containers indexed by name */
54 xbt_dynar_t allLinkTypes = NULL; /* all link types defined */
55 xbt_dynar_t allHostTypes = NULL; /* all host types defined */
57 static void instr_routing_parse_start_AS (void);
58 static void instr_routing_parse_end_AS (void);
59 static void instr_routing_parse_start_link (void);
60 static void instr_routing_parse_end_link (void);
61 static void instr_routing_parse_start_host (void);
62 static void instr_routing_parse_end_host (void);
63 static void instr_routing_parse_start_router (void);
64 static void instr_routing_parse_end_router (void);
65 static void instr_routing_parse_end_platform (void);
67 static long long int newTypeId ()
69 static long long int counter = 0;
73 static type_t newType (const char *typename, e_entity_types kind, type_t father)
75 type_t ret = xbt_new0(s_type_t, 1);
76 ret->name = xbt_strdup (typename);
79 ret->children = xbt_dict_new ();
81 long long int id = newTypeId();
82 char str_id[INSTR_DEFAULT_STR_SIZE];
83 snprintf (str_id, INSTR_DEFAULT_STR_SIZE, "%lld", id);
84 ret->id = xbt_strdup (str_id);
87 xbt_dict_set (father->children, typename, ret, NULL);
92 static type_t newContainerType (const char *typename, e_entity_types kind, type_t father)
94 type_t ret = newType (typename, kind, father);
95 // if (father) INFO4("ContainerType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
96 if (father) pajeDefineContainerType(ret->id, ret->father->id, ret->name);
100 static type_t newVariableType (const char *typename, e_entity_types kind, const char *color, type_t father)
102 type_t ret = newType (typename, kind, father);
103 // INFO4("VariableType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
105 pajeDefineVariableTypeWithColor(ret->id, ret->father->id, ret->name, color);
107 pajeDefineVariableType(ret->id, ret->father->id, ret->name);
112 static type_t newLinkType (const char *typename, e_entity_types kind, type_t father, type_t source, type_t dest)
114 type_t ret = newType (typename, kind, father);
115 // INFO8("LinkType %s(%s), child of %s(%s) %s(%s)->%s(%s)", ret->name, ret->id, father->name, father->id, source->name, source->id, dest->name, dest->id);
116 pajeDefineLinkType(ret->id, ret->father->id, source->id, dest->id, ret->name);
120 static type_t getContainerType (const char *typename, type_t father)
124 ret = newContainerType (typename, TYPE_CONTAINER, father);
127 //check if my father type already has my typename
128 ret = (type_t)xbt_dict_get_or_null (father->children, typename);
130 ret = newContainerType (typename, TYPE_CONTAINER, father);
136 static type_t getVariableType (const char *typename, const char *color, type_t father)
138 type_t ret = xbt_dict_get_or_null (father->children, typename);
140 ret = newVariableType (typename, TYPE_VARIABLE, color, father);
145 static type_t getLinkType (const char *typename, type_t father, type_t source, type_t dest)
147 type_t ret = xbt_dict_get_or_null (father->children, typename);
149 ret = newLinkType (typename, TYPE_LINK, father, source, dest);
154 void instr_routing_define_callbacks ()
156 if (!TRACE_is_active())
158 surfxml_add_callback(STag_surfxml_AS_cb_list, &instr_routing_parse_start_AS);
159 surfxml_add_callback(ETag_surfxml_AS_cb_list, &instr_routing_parse_end_AS);
160 surfxml_add_callback(STag_surfxml_link_cb_list, &instr_routing_parse_start_link);
161 surfxml_add_callback(ETag_surfxml_link_cb_list, &instr_routing_parse_end_link);
162 surfxml_add_callback(STag_surfxml_host_cb_list, &instr_routing_parse_start_host);
163 surfxml_add_callback(ETag_surfxml_host_cb_list, &instr_routing_parse_end_host);
164 surfxml_add_callback(STag_surfxml_router_cb_list, &instr_routing_parse_start_router);
165 surfxml_add_callback(ETag_surfxml_router_cb_list, &instr_routing_parse_end_router);
166 surfxml_add_callback(ETag_surfxml_platform_cb_list, &instr_routing_parse_end_platform);
169 static long long int newContainedId ()
171 static long long counter = 0;
175 static container_t newContainer (const char *name, e_container_types kind, container_t father)
177 long long int counter = newContainedId();
178 char id_str[INSTR_DEFAULT_STR_SIZE];
179 snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", counter);
181 container_t new = xbt_new0(s_container_t, 1);
182 new->name = xbt_strdup (name); // name of the container
183 new->id = xbt_strdup (id_str); // id (or alias) of the container
184 new->father = father;
185 // father of this container
187 new->father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
191 // level depends on level of father
193 new->level = new->father->level+1;
197 // type definition (method depends on kind of this new container)
199 if (new->kind == INSTR_AS){
200 //if this container is of an AS, its type name depends on its level
201 char as_typename[INSTR_DEFAULT_STR_SIZE];
202 snprintf (as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", new->level);
204 new->type = getContainerType (as_typename, new->father->type);
206 new->type = getContainerType ("0", NULL);
209 //otherwise, the name is its kind
211 case INSTR_HOST: new->type = getContainerType ("HOST", new->father->type); break;
212 case INSTR_LINK: new->type = getContainerType ("LINK", new->father->type); break;
213 case INSTR_ROUTER: new->type = getContainerType ("ROUTER", new->father->type); break;
214 default: xbt_die ("Congratulations, you have found a bug on newContainer function of instr_routing.c"); break;
217 new->children = xbt_dict_new();
219 xbt_dict_set(new->father->children, new->name, new, NULL);
220 pajeCreateContainer (0, new->id, new->type->id, new->father->id, new->name);
223 //register hosts, routers, links containers
224 if (new->kind == INSTR_HOST || new->kind == INSTR_LINK || new->kind == INSTR_ROUTER) {
225 xbt_dict_set (allContainers, new->name, new, NULL);
228 //register the host container types
229 if (new->kind == INSTR_HOST){
230 xbt_dynar_push_as (allHostTypes, type_t, new->type);
233 //register the link container types
234 if (new->kind == INSTR_LINK){
235 xbt_dynar_push_as(allLinkTypes, type_t, new->type);
240 static container_t findChild (container_t root, container_t a1)
242 if (root == a1) return root;
244 xbt_dict_cursor_t cursor = NULL;
247 xbt_dict_foreach(root->children, cursor, child_name, child) {
248 if (findChild (child, a1)) return child;
253 static container_t findCommonFather (container_t root, container_t a1, container_t a2)
255 if (a1->father == a2->father) return a1->father;
257 xbt_dict_cursor_t cursor = NULL;
260 container_t a1_try = NULL;
261 container_t a2_try = NULL;
262 xbt_dict_foreach(root->children, cursor, child_name, child) {
263 a1_try = findChild (child, a1);
264 a2_try = findChild (child, a2);
265 if (a1_try && a2_try) return child;
270 static void linkContainers (const char *a1, const char *a2)
273 if (strcmp (a1, "__loopback__") == 0 || strcmp (a2, "__loopback__") == 0)
276 container_t a1_container = ((container_t)xbt_dict_get (allContainers, a1));
277 type_t a1_type = a1_container->type;
279 container_t a2_container = ((container_t)xbt_dict_get (allContainers, a2));
280 type_t a2_type = a2_container->type;
282 container_t container = findCommonFather (rootContainer, a1_container, a2_container);
283 xbt_assert0 (container != NULL, "common father not found");
286 char link_typename[INSTR_DEFAULT_STR_SIZE];
287 snprintf (link_typename, INSTR_DEFAULT_STR_SIZE, "%s-%s", a1_type->name, a2_type->name);
288 type_t link_type = getLinkType (link_typename, container->type, a1_type, a2_type);
291 static long long counter = 0;
292 char key[INSTR_DEFAULT_STR_SIZE];
293 snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter++);
294 pajeStartLink(SIMIX_get_clock(), link_type->id, container->id, "G", a1_container->id, key);
295 pajeEndLink(SIMIX_get_clock(), link_type->id, container->id, "G", a2_container->id, key);
298 static void recursiveGraphExtraction (container_t container)
300 if (xbt_dict_length(container->children)){
301 xbt_dict_cursor_t cursor = NULL;
304 //bottom-up recursion
305 xbt_dict_foreach(container->children, cursor, child_name, child) {
306 recursiveGraphExtraction (child);
310 xbt_dict_cursor_t cursor1 = NULL, cursor2 = NULL;
311 container_t child1, child2;
312 const char *child_name1, *child_name2;
314 xbt_dict_t filter = xbt_dict_new ();
316 xbt_dict_foreach(container->children, cursor1, child_name1, child1) {
317 xbt_dict_foreach(container->children, cursor2, child_name2, child2) {
318 //check if we already register this pair (we only need one direction)
319 char aux1[INSTR_DEFAULT_STR_SIZE], aux2[INSTR_DEFAULT_STR_SIZE];
320 snprintf (aux1, INSTR_DEFAULT_STR_SIZE, "%s%s", child_name1, child_name2);
321 snprintf (aux2, INSTR_DEFAULT_STR_SIZE, "%s%s", child_name2, child_name1);
322 if (xbt_dict_get_or_null (filter, aux1)) continue;
323 if (xbt_dict_get_or_null (filter, aux2)) continue;
325 //ok, not found, register it
326 xbt_dict_set (filter, aux1, xbt_strdup ("1"), xbt_free);
327 xbt_dict_set (filter, aux2, xbt_strdup ("1"), xbt_free);
329 if ((child1->kind == INSTR_HOST || child1->kind == INSTR_ROUTER) &&
330 (child2->kind == INSTR_HOST || child2->kind == INSTR_ROUTER)){
336 route = global_routing->get_route (child_name1, child_name2);
338 //no route between them, that's possible
342 //link the route members
345 char *previous_entity_name = (char*)child_name1;
346 xbt_dynar_foreach (route, cpt, link) {
347 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
348 linkContainers (previous_entity_name, link_name);
349 previous_entity_name = link_name;
351 linkContainers (previous_entity_name, child_name2);
352 }else if (child1->kind == INSTR_AS &&
353 child2->kind == INSTR_AS &&
354 strcmp(child_name1, child_name2) != 0){
357 routing_component_t root = global_routing->root;
358 route_extended_t route;
361 route = root->get_route (root, child_name1, child_name2);
363 //no route between them, that's possible
366 xbt_assert2(route!=NULL,
367 "there is no ASroute between %s and %s", child_name1, child_name2);
370 char *previous_entity_name = route->src_gateway;
371 xbt_dynar_foreach (route->generic_route.link_list, cpt, link) {
372 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
373 linkContainers (previous_entity_name, link_name);
374 previous_entity_name = link_name;
376 linkContainers (previous_entity_name, route->dst_gateway);
380 xbt_dict_free(&filter);
387 static void instr_routing_parse_start_AS ()
389 if (rootContainer == NULL){
390 currentContainer = xbt_dynar_new (sizeof(s_container_t), NULL);
391 allContainers = xbt_dict_new ();
392 allLinkTypes = xbt_dynar_new (sizeof(s_type_t), NULL);
393 allHostTypes = xbt_dynar_new (sizeof(s_type_t), NULL);
395 rootContainer = newContainer ("0", INSTR_AS, NULL);
396 xbt_dynar_push (currentContainer, rootContainer);
399 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
400 container_t new = newContainer (A_surfxml_AS_id, INSTR_AS, father);
403 xbt_dynar_push (currentContainer, new);
406 static void instr_routing_parse_end_AS ()
408 xbt_dynar_pop_ptr (currentContainer);
411 static void instr_routing_parse_start_link ()
413 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
414 container_t new = newContainer (A_surfxml_link_id, INSTR_LINK, father);
416 type_t bandwidth = getVariableType ("bandwidth", NULL, new->type);
417 type_t latency = getVariableType ("latency", NULL, new->type);
418 pajeSetVariable (0, bandwidth->id, new->id, A_surfxml_link_bandwidth);
419 pajeSetVariable (0, latency->id, new->id, A_surfxml_link_latency);
420 if (TRACE_uncategorized()){
421 getVariableType ("bandwidth_used", "0.5 0.5 0.5", new->type);
425 static void instr_routing_parse_end_link ()
429 static void instr_routing_parse_start_host ()
431 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
432 container_t new = newContainer (A_surfxml_host_id, INSTR_HOST, father);
434 type_t power = getVariableType ("power", NULL, new->type);
435 pajeSetVariable (0, power->id, new->id, A_surfxml_host_power);
436 if (TRACE_uncategorized()){
437 getVariableType ("power_used", "0.5 0.5 0.5", new->type);
441 static void instr_routing_parse_end_host ()
445 static void instr_routing_parse_start_router ()
447 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
448 newContainer (A_surfxml_router_id, INSTR_ROUTER, father);
451 static void instr_routing_parse_end_router ()
455 static void instr_routing_parse_end_platform ()
457 currentContainer = NULL;
458 recursiveGraphExtraction (rootContainer);
464 int instr_link_is_traced (const char *name)
466 if (((container_t)xbt_dict_get_or_null (allContainers, name))){
473 char *instr_variable_type (const char *name, const char *resource)
475 container_t container = (container_t)xbt_dict_get (allContainers, resource);
476 xbt_dict_cursor_t cursor = NULL;
479 xbt_dict_foreach(container->type->children, cursor, type_name, type) {
480 if (strcmp (name, type->name) == 0) return type->id;
485 char *instr_resource_type (const char *resource_name)
487 return ((container_t)xbt_dict_get_or_null (allContainers, resource_name))->id;
490 static void recursiveDestroyContainer (container_t container)
492 xbt_dict_cursor_t cursor = NULL;
495 xbt_dict_foreach(container->children, cursor, child_name, child) {
496 recursiveDestroyContainer (child);
499 pajeDestroyContainer(SIMIX_get_clock(), container->type->id, container->id);
501 xbt_free (container->name);
502 xbt_free (container->id);
503 xbt_free (container->children);
504 xbt_free (container);
508 static void recursiveDestroyType (type_t type)
510 xbt_dict_cursor_t cursor = NULL;
513 xbt_dict_foreach(type->children, cursor, child_name, child) {
514 recursiveDestroyType (child);
516 xbt_free (type->name);
518 xbt_free (type->children);
523 void instr_destroy_platform ()
525 if (rootContainer) recursiveDestroyContainer (rootContainer);
526 if (rootType) recursiveDestroyType (rootType);
530 * user categories support
532 static void recursiveNewUserVariableType (const char *new_typename, const char *color, type_t root)
534 if (!strcmp (root->name, "HOST") || !strcmp (root->name, "LINK")){
535 newVariableType(new_typename, TYPE_VARIABLE, color, root);
537 xbt_dict_cursor_t cursor = NULL;
540 xbt_dict_foreach(root->children, cursor, name, child_type) {
541 recursiveNewUserVariableType (new_typename, color, child_type);
545 void instr_new_user_variable_type (const char *new_typename, const char *color)
547 recursiveNewUserVariableType (new_typename, color, rootType);
550 static void recursiveNewUserLinkVariableType (const char *new_typename, const char *color, type_t root)
552 if (!strcmp (root->name, "LINK")){
553 newVariableType(new_typename, TYPE_VARIABLE, color, root);
555 xbt_dict_cursor_t cursor = NULL;
558 xbt_dict_foreach(root->children, cursor, name, child_type) {
559 recursiveNewUserLinkVariableType (new_typename, color, child_type);
563 void instr_new_user_link_variable_type (const char *new_typename, const char *color)
565 recursiveNewUserLinkVariableType (new_typename, color, rootType);
569 static void recursiveNewUserHostVariableType (const char *new_typename, const char *color, type_t root)
571 if (!strcmp (root->name, "HOST")){
572 newVariableType(new_typename, TYPE_VARIABLE, color, root);
574 xbt_dict_cursor_t cursor = NULL;
577 xbt_dict_foreach(root->children, cursor, name, child_type) {
578 recursiveNewUserHostVariableType (new_typename, color, child_type);
582 void instr_new_user_host_variable_type (const char *new_typename, const char *color)
584 recursiveNewUserHostVariableType (new_typename, color, rootType);
587 #endif /* HAVE_TRACING */