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 int platform_created = 0; /* indicate whether the platform file has been traced */
51 static type_t rootType = NULL; /* the root type */
52 static container_t rootContainer = NULL; /* the root container */
53 static xbt_dynar_t currentContainer = NULL; /* push and pop, used only in creation */
54 static xbt_dict_t allContainers = NULL; /* all created containers indexed by name */
55 xbt_dynar_t allLinkTypes = NULL; /* all link types defined */
56 xbt_dynar_t allHostTypes = NULL; /* all host types defined */
58 static void instr_routing_parse_start_AS (void);
59 static void instr_routing_parse_end_AS (void);
60 static void instr_routing_parse_start_link (void);
61 static void instr_routing_parse_end_link (void);
62 static void instr_routing_parse_start_host (void);
63 static void instr_routing_parse_end_host (void);
64 static void instr_routing_parse_start_router (void);
65 static void instr_routing_parse_end_router (void);
66 static void instr_routing_parse_end_platform (void);
68 static long long int newTypeId ()
70 static long long int counter = 0;
74 static type_t newType (const char *typename, e_entity_types kind, type_t father)
76 type_t ret = xbt_new0(s_type_t, 1);
77 ret->name = xbt_strdup (typename);
80 ret->children = xbt_dict_new ();
82 long long int id = newTypeId();
83 char str_id[INSTR_DEFAULT_STR_SIZE];
84 snprintf (str_id, INSTR_DEFAULT_STR_SIZE, "%lld", id);
85 ret->id = xbt_strdup (str_id);
88 xbt_dict_set (father->children, typename, ret, NULL);
93 static type_t newContainerType (const char *typename, e_entity_types kind, type_t father)
95 type_t ret = newType (typename, kind, father);
96 // if (father) INFO4("ContainerType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
97 if (father) pajeDefineContainerType(ret->id, ret->father->id, ret->name);
101 static type_t newVariableType (const char *typename, e_entity_types kind, const char *color, type_t father)
103 type_t ret = newType (typename, kind, father);
104 // INFO4("VariableType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
106 pajeDefineVariableTypeWithColor(ret->id, ret->father->id, ret->name, color);
108 pajeDefineVariableType(ret->id, ret->father->id, ret->name);
113 static type_t newLinkType (const char *typename, e_entity_types kind, type_t father, type_t source, type_t dest)
115 type_t ret = newType (typename, kind, father);
116 // 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);
117 pajeDefineLinkType(ret->id, ret->father->id, source->id, dest->id, ret->name);
121 static type_t getContainerType (const char *typename, type_t father)
125 ret = newContainerType (typename, TYPE_CONTAINER, father);
128 //check if my father type already has my typename
129 ret = (type_t)xbt_dict_get_or_null (father->children, typename);
131 ret = newContainerType (typename, TYPE_CONTAINER, father);
137 static type_t getVariableType (const char *typename, const char *color, type_t father)
139 type_t ret = xbt_dict_get_or_null (father->children, typename);
141 ret = newVariableType (typename, TYPE_VARIABLE, color, father);
146 static type_t getLinkType (const char *typename, type_t father, type_t source, type_t dest)
148 type_t ret = xbt_dict_get_or_null (father->children, typename);
150 ret = newLinkType (typename, TYPE_LINK, father, source, dest);
155 void instr_routing_define_callbacks ()
157 if (!TRACE_is_active())
159 surfxml_add_callback(STag_surfxml_AS_cb_list, &instr_routing_parse_start_AS);
160 surfxml_add_callback(ETag_surfxml_AS_cb_list, &instr_routing_parse_end_AS);
161 surfxml_add_callback(STag_surfxml_link_cb_list, &instr_routing_parse_start_link);
162 surfxml_add_callback(ETag_surfxml_link_cb_list, &instr_routing_parse_end_link);
163 surfxml_add_callback(STag_surfxml_host_cb_list, &instr_routing_parse_start_host);
164 surfxml_add_callback(ETag_surfxml_host_cb_list, &instr_routing_parse_end_host);
165 surfxml_add_callback(STag_surfxml_router_cb_list, &instr_routing_parse_start_router);
166 surfxml_add_callback(ETag_surfxml_router_cb_list, &instr_routing_parse_end_router);
167 surfxml_add_callback(ETag_surfxml_platform_cb_list, &instr_routing_parse_end_platform);
170 static long long int newContainedId ()
172 static long long counter = 0;
176 static container_t newContainer (const char *name, e_container_types kind, container_t father)
178 long long int counter = newContainedId();
179 char id_str[INSTR_DEFAULT_STR_SIZE];
180 snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", counter);
182 container_t new = xbt_new0(s_container_t, 1);
183 new->name = xbt_strdup (name); // name of the container
184 new->id = xbt_strdup (id_str); // id (or alias) of the container
185 new->father = father;
186 // father of this container
188 new->father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
192 // level depends on level of father
194 new->level = new->father->level+1;
198 // type definition (method depends on kind of this new container)
200 if (new->kind == INSTR_AS){
201 //if this container is of an AS, its type name depends on its level
202 char as_typename[INSTR_DEFAULT_STR_SIZE];
203 snprintf (as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", new->level);
205 new->type = getContainerType (as_typename, new->father->type);
207 new->type = getContainerType ("0", NULL);
210 //otherwise, the name is its kind
212 case INSTR_HOST: new->type = getContainerType ("HOST", new->father->type); break;
213 case INSTR_LINK: new->type = getContainerType ("LINK", new->father->type); break;
214 case INSTR_ROUTER: new->type = getContainerType ("ROUTER", new->father->type); break;
215 default: xbt_die ("Congratulations, you have found a bug on newContainer function of instr_routing.c"); break;
218 new->children = xbt_dict_new();
220 xbt_dict_set(new->father->children, new->name, new, NULL);
221 pajeCreateContainer (0, new->id, new->type->id, new->father->id, new->name);
224 //register hosts, routers, links containers
225 if (new->kind == INSTR_HOST || new->kind == INSTR_LINK || new->kind == INSTR_ROUTER) {
226 xbt_dict_set (allContainers, new->name, new, NULL);
229 //register the host container types
230 if (new->kind == INSTR_HOST){
231 xbt_dynar_push_as (allHostTypes, type_t, new->type);
234 //register the link container types
235 if (new->kind == INSTR_LINK){
236 xbt_dynar_push_as(allLinkTypes, type_t, new->type);
241 static container_t findChild (container_t root, container_t a1)
243 if (root == a1) return root;
245 xbt_dict_cursor_t cursor = NULL;
248 xbt_dict_foreach(root->children, cursor, child_name, child) {
249 if (findChild (child, a1)) return child;
254 static container_t findCommonFather (container_t root, container_t a1, container_t a2)
256 if (a1->father == a2->father) return a1->father;
258 xbt_dict_cursor_t cursor = NULL;
261 container_t a1_try = NULL;
262 container_t a2_try = NULL;
263 xbt_dict_foreach(root->children, cursor, child_name, child) {
264 a1_try = findChild (child, a1);
265 a2_try = findChild (child, a2);
266 if (a1_try && a2_try) return child;
271 static void linkContainers (const char *a1, const char *a2)
274 if (strcmp (a1, "__loopback__") == 0 || strcmp (a2, "__loopback__") == 0)
277 container_t a1_container = ((container_t)xbt_dict_get (allContainers, a1));
278 type_t a1_type = a1_container->type;
280 container_t a2_container = ((container_t)xbt_dict_get (allContainers, a2));
281 type_t a2_type = a2_container->type;
283 container_t container = findCommonFather (rootContainer, a1_container, a2_container);
284 xbt_assert0 (container != NULL, "common father not found");
287 char link_typename[INSTR_DEFAULT_STR_SIZE];
288 snprintf (link_typename, INSTR_DEFAULT_STR_SIZE, "%s-%s", a1_type->name, a2_type->name);
289 type_t link_type = getLinkType (link_typename, container->type, a1_type, a2_type);
292 static long long counter = 0;
293 char key[INSTR_DEFAULT_STR_SIZE];
294 snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter++);
295 pajeStartLink(SIMIX_get_clock(), link_type->id, container->id, "G", a1_container->id, key);
296 pajeEndLink(SIMIX_get_clock(), link_type->id, container->id, "G", a2_container->id, key);
299 static void recursiveGraphExtraction (container_t container)
301 if (xbt_dict_length(container->children)){
302 xbt_dict_cursor_t cursor = NULL;
305 //bottom-up recursion
306 xbt_dict_foreach(container->children, cursor, child_name, child) {
307 recursiveGraphExtraction (child);
311 xbt_dict_cursor_t cursor1 = NULL, cursor2 = NULL;
312 container_t child1, child2;
313 const char *child_name1, *child_name2;
315 xbt_dict_t filter = xbt_dict_new ();
317 xbt_dict_foreach(container->children, cursor1, child_name1, child1) {
318 xbt_dict_foreach(container->children, cursor2, child_name2, child2) {
319 //check if we already register this pair (we only need one direction)
320 char aux1[INSTR_DEFAULT_STR_SIZE], aux2[INSTR_DEFAULT_STR_SIZE];
321 snprintf (aux1, INSTR_DEFAULT_STR_SIZE, "%s%s", child_name1, child_name2);
322 snprintf (aux2, INSTR_DEFAULT_STR_SIZE, "%s%s", child_name2, child_name1);
323 if (xbt_dict_get_or_null (filter, aux1)) continue;
324 if (xbt_dict_get_or_null (filter, aux2)) continue;
326 //ok, not found, register it
327 xbt_dict_set (filter, aux1, xbt_strdup ("1"), xbt_free);
328 xbt_dict_set (filter, aux2, xbt_strdup ("1"), xbt_free);
330 if ((child1->kind == INSTR_HOST || child1->kind == INSTR_ROUTER) &&
331 (child2->kind == INSTR_HOST || child2->kind == INSTR_ROUTER)){
337 route = global_routing->get_route (child_name1, child_name2);
339 //no route between them, that's possible
343 //link the route members
346 char *previous_entity_name = (char*)child_name1;
347 xbt_dynar_foreach (route, cpt, link) {
348 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
349 linkContainers (previous_entity_name, link_name);
350 previous_entity_name = link_name;
352 linkContainers (previous_entity_name, child_name2);
353 }else if (child1->kind == INSTR_AS &&
354 child2->kind == INSTR_AS &&
355 strcmp(child_name1, child_name2) != 0){
358 routing_component_t root = global_routing->root;
359 route_extended_t route;
362 route = root->get_route (root, child_name1, child_name2);
364 //no route between them, that's possible
367 xbt_assert2(route!=NULL,
368 "there is no ASroute between %s and %s", child_name1, child_name2);
371 char *previous_entity_name = route->src_gateway;
372 xbt_dynar_foreach (route->generic_route.link_list, cpt, link) {
373 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
374 linkContainers (previous_entity_name, link_name);
375 previous_entity_name = link_name;
377 linkContainers (previous_entity_name, route->dst_gateway);
381 xbt_dict_free(&filter);
388 static void instr_routing_parse_start_AS ()
390 if (rootContainer == NULL){
391 currentContainer = xbt_dynar_new (sizeof(s_container_t), NULL);
392 allContainers = xbt_dict_new ();
393 allLinkTypes = xbt_dynar_new (sizeof(s_type_t), NULL);
394 allHostTypes = xbt_dynar_new (sizeof(s_type_t), NULL);
396 rootContainer = newContainer ("0", INSTR_AS, NULL);
397 xbt_dynar_push (currentContainer, rootContainer);
400 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
401 container_t new = newContainer (A_surfxml_AS_id, INSTR_AS, father);
404 xbt_dynar_push (currentContainer, new);
407 static void instr_routing_parse_end_AS ()
409 xbt_dynar_pop_ptr (currentContainer);
412 static void instr_routing_parse_start_link ()
414 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
415 container_t new = newContainer (A_surfxml_link_id, INSTR_LINK, father);
417 type_t bandwidth = getVariableType ("bandwidth", NULL, new->type);
418 type_t latency = getVariableType ("latency", NULL, new->type);
419 pajeSetVariable (0, bandwidth->id, new->id, A_surfxml_link_bandwidth);
420 pajeSetVariable (0, latency->id, new->id, A_surfxml_link_latency);
421 if (TRACE_uncategorized()){
422 getVariableType ("bandwidth_used", "0.5 0.5 0.5", new->type);
426 static void instr_routing_parse_end_link ()
430 static void instr_routing_parse_start_host ()
432 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
433 container_t new = newContainer (A_surfxml_host_id, INSTR_HOST, father);
435 type_t power = getVariableType ("power", NULL, new->type);
436 pajeSetVariable (0, power->id, new->id, A_surfxml_host_power);
437 if (TRACE_uncategorized()){
438 getVariableType ("power_used", "0.5 0.5 0.5", new->type);
442 static void instr_routing_parse_end_host ()
446 static void instr_routing_parse_start_router ()
448 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
449 newContainer (A_surfxml_router_id, INSTR_ROUTER, father);
452 static void instr_routing_parse_end_router ()
456 static void instr_routing_parse_end_platform ()
458 currentContainer = NULL;
459 recursiveGraphExtraction (rootContainer);
460 platform_created = 1;
466 int instr_link_is_traced (const char *name)
468 if (((container_t)xbt_dict_get_or_null (allContainers, name))){
475 char *instr_variable_type (const char *name, const char *resource)
477 container_t container = (container_t)xbt_dict_get (allContainers, resource);
478 xbt_dict_cursor_t cursor = NULL;
481 xbt_dict_foreach(container->type->children, cursor, type_name, type) {
482 if (strcmp (name, type->name) == 0) return type->id;
487 char *instr_resource_type (const char *resource_name)
489 return ((container_t)xbt_dict_get_or_null (allContainers, resource_name))->id;
492 static void recursiveDestroyContainer (container_t container)
494 xbt_dict_cursor_t cursor = NULL;
497 xbt_dict_foreach(container->children, cursor, child_name, child) {
498 recursiveDestroyContainer (child);
501 pajeDestroyContainer(SIMIX_get_clock(), container->type->id, container->id);
503 xbt_free (container->name);
504 xbt_free (container->id);
505 xbt_free (container->children);
506 xbt_free (container);
510 static void recursiveDestroyType (type_t type)
512 xbt_dict_cursor_t cursor = NULL;
515 xbt_dict_foreach(type->children, cursor, child_name, child) {
516 recursiveDestroyType (child);
518 xbt_free (type->name);
520 xbt_free (type->children);
525 void instr_destroy_platform ()
527 if (rootContainer) recursiveDestroyContainer (rootContainer);
528 if (rootType) recursiveDestroyType (rootType);
532 * user categories support
534 static void recursiveNewUserVariableType (const char *new_typename, const char *color, type_t root)
536 if (!strcmp (root->name, "HOST") || !strcmp (root->name, "LINK")){
537 getVariableType(new_typename, color, root);
539 xbt_dict_cursor_t cursor = NULL;
542 xbt_dict_foreach(root->children, cursor, name, child_type) {
543 recursiveNewUserVariableType (new_typename, color, child_type);
547 void instr_new_user_variable_type (const char *new_typename, const char *color)
549 recursiveNewUserVariableType (new_typename, color, rootType);
552 static void recursiveNewUserLinkVariableType (const char *new_typename, const char *color, type_t root)
554 if (!strcmp (root->name, "LINK")){
555 getVariableType(new_typename, color, root);
557 xbt_dict_cursor_t cursor = NULL;
560 xbt_dict_foreach(root->children, cursor, name, child_type) {
561 recursiveNewUserLinkVariableType (new_typename, color, child_type);
565 void instr_new_user_link_variable_type (const char *new_typename, const char *color)
567 recursiveNewUserLinkVariableType (new_typename, color, rootType);
571 static void recursiveNewUserHostVariableType (const char *new_typename, const char *color, type_t root)
573 if (!strcmp (root->name, "HOST")){
574 getVariableType(new_typename, color, root);
576 xbt_dict_cursor_t cursor = NULL;
579 xbt_dict_foreach(root->children, cursor, name, child_type) {
580 recursiveNewUserHostVariableType (new_typename, color, child_type);
584 void instr_new_user_host_variable_type (const char *new_typename, const char *color)
586 recursiveNewUserHostVariableType (new_typename, color, rootType);
589 int instr_platform_traced ()
591 return platform_created;
594 #endif /* HAVE_TRACING */