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_type *type_t;
25 typedef struct s_type {
29 struct s_type *father;
40 typedef struct s_container *container_t;
41 typedef struct s_container {
42 char *name; /* Unique name of this container */
43 char *id; /* Unique id of this container */
44 type_t type; /* Type of this container */
45 int level; /* Level in the hierarchy, root level is 0 */
46 e_container_types kind; /* This container is of what kind */
47 struct s_container *father;
51 static int platform_created = 0; /* indicate whether the platform file has been traced */
52 static type_t rootType = NULL; /* the root type */
53 static container_t rootContainer = NULL; /* the root container */
54 static xbt_dynar_t currentContainer = NULL; /* push and pop, used only in creation */
55 static xbt_dict_t allContainers = NULL; /* all created containers indexed by name */
56 xbt_dynar_t allLinkTypes = NULL; /* all link types defined */
57 xbt_dynar_t allHostTypes = NULL; /* all host types defined */
59 static void instr_routing_parse_start_AS (void);
60 static void instr_routing_parse_end_AS (void);
61 static void instr_routing_parse_start_link (void);
62 static void instr_routing_parse_end_link (void);
63 static void instr_routing_parse_start_host (void);
64 static void instr_routing_parse_end_host (void);
65 static void instr_routing_parse_start_router (void);
66 static void instr_routing_parse_end_router (void);
67 static void instr_routing_parse_end_platform (void);
69 static long long int newTypeId ()
71 static long long int counter = 0;
75 static type_t newType (const char *typename, e_entity_types kind, type_t father)
77 type_t ret = xbt_new0(s_type_t, 1);
78 ret->name = xbt_strdup (typename);
81 ret->children = xbt_dict_new ();
83 long long int id = newTypeId();
84 char str_id[INSTR_DEFAULT_STR_SIZE];
85 snprintf (str_id, INSTR_DEFAULT_STR_SIZE, "%lld", id);
86 ret->id = xbt_strdup (str_id);
89 xbt_dict_set (father->children, typename, ret, NULL);
94 static type_t newContainerType (const char *typename, e_entity_types kind, type_t father)
96 type_t ret = newType (typename, kind, father);
97 // if (father) INFO4("ContainerType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
98 if (father) pajeDefineContainerType(ret->id, ret->father->id, ret->name);
102 static type_t newVariableType (const char *typename, e_entity_types kind, const char *color, type_t father)
104 type_t ret = newType (typename, kind, father);
105 // INFO4("VariableType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
107 pajeDefineVariableTypeWithColor(ret->id, ret->father->id, ret->name, color);
109 pajeDefineVariableType(ret->id, ret->father->id, ret->name);
114 static type_t newLinkType (const char *typename, e_entity_types kind, type_t father, type_t source, type_t dest)
116 type_t ret = newType (typename, kind, father);
117 // 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);
118 pajeDefineLinkType(ret->id, ret->father->id, source->id, dest->id, ret->name);
122 static type_t newStateType (const char *typename, e_entity_types kind, type_t father)
124 type_t ret = newType (typename, kind, father);
125 // INFO4("StateType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
126 pajeDefineStateType(ret->id, ret->father->id, ret->name);
130 static type_t getContainerType (const char *typename, type_t father)
134 ret = newContainerType (typename, TYPE_CONTAINER, father);
137 //check if my father type already has my typename
138 ret = (type_t)xbt_dict_get_or_null (father->children, typename);
140 ret = newContainerType (typename, TYPE_CONTAINER, father);
146 static type_t getVariableType (const char *typename, const char *color, type_t father)
148 type_t ret = xbt_dict_get_or_null (father->children, typename);
150 ret = newVariableType (typename, TYPE_VARIABLE, color, father);
155 static type_t getLinkType (const char *typename, type_t father, type_t source, type_t dest)
157 type_t ret = xbt_dict_get_or_null (father->children, typename);
159 ret = newLinkType (typename, TYPE_LINK, father, source, dest);
164 static type_t getStateType (const char *typename, type_t father)
166 type_t ret = xbt_dict_get_or_null (father->children, typename);
168 ret = newStateType (typename, TYPE_STATE, father);
173 void instr_routing_define_callbacks ()
175 if (!TRACE_is_active())
177 surfxml_add_callback(STag_surfxml_AS_cb_list, &instr_routing_parse_start_AS);
178 surfxml_add_callback(ETag_surfxml_AS_cb_list, &instr_routing_parse_end_AS);
179 surfxml_add_callback(STag_surfxml_link_cb_list, &instr_routing_parse_start_link);
180 surfxml_add_callback(ETag_surfxml_link_cb_list, &instr_routing_parse_end_link);
181 surfxml_add_callback(STag_surfxml_host_cb_list, &instr_routing_parse_start_host);
182 surfxml_add_callback(ETag_surfxml_host_cb_list, &instr_routing_parse_end_host);
183 surfxml_add_callback(STag_surfxml_router_cb_list, &instr_routing_parse_start_router);
184 surfxml_add_callback(ETag_surfxml_router_cb_list, &instr_routing_parse_end_router);
185 surfxml_add_callback(ETag_surfxml_platform_cb_list, &instr_routing_parse_end_platform);
188 static long long int newContainedId ()
190 static long long counter = 0;
194 static container_t newContainer (const char *name, e_container_types kind, container_t father)
196 long long int counter = newContainedId();
197 char id_str[INSTR_DEFAULT_STR_SIZE];
198 snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", counter);
200 container_t new = xbt_new0(s_container_t, 1);
201 new->name = xbt_strdup (name); // name of the container
202 new->id = xbt_strdup (id_str); // id (or alias) of the container
203 new->father = father;
204 // father of this container
206 new->father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
210 // level depends on level of father
212 new->level = new->father->level+1;
216 // type definition (method depends on kind of this new container)
218 if (new->kind == INSTR_AS){
219 //if this container is of an AS, its type name depends on its level
220 char as_typename[INSTR_DEFAULT_STR_SIZE];
221 snprintf (as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", new->level);
223 new->type = getContainerType (as_typename, new->father->type);
225 new->type = getContainerType ("0", NULL);
228 //otherwise, the name is its kind
230 case INSTR_HOST: new->type = getContainerType ("HOST", new->father->type); break;
231 case INSTR_LINK: new->type = getContainerType ("LINK", new->father->type); break;
232 case INSTR_ROUTER: new->type = getContainerType ("ROUTER", new->father->type); break;
233 default: xbt_die ("Congratulations, you have found a bug on newContainer function of instr_routing.c"); break;
236 new->children = xbt_dict_new();
238 xbt_dict_set(new->father->children, new->name, new, NULL);
239 pajeCreateContainer (0, new->id, new->type->id, new->father->id, new->name);
242 //register hosts, routers, links containers
243 if (new->kind == INSTR_HOST || new->kind == INSTR_LINK || new->kind == INSTR_ROUTER) {
244 xbt_dict_set (allContainers, new->name, new, NULL);
247 //register the host container types
248 if (new->kind == INSTR_HOST){
249 xbt_dynar_push_as (allHostTypes, type_t, new->type);
252 //register the link container types
253 if (new->kind == INSTR_LINK){
254 xbt_dynar_push_as(allLinkTypes, type_t, new->type);
259 static container_t findChild (container_t root, container_t a1)
261 if (root == a1) return root;
263 xbt_dict_cursor_t cursor = NULL;
266 xbt_dict_foreach(root->children, cursor, child_name, child) {
267 if (findChild (child, a1)) return child;
272 static container_t findCommonFather (container_t root, container_t a1, container_t a2)
274 if (a1->father == a2->father) return a1->father;
276 xbt_dict_cursor_t cursor = NULL;
279 container_t a1_try = NULL;
280 container_t a2_try = NULL;
281 xbt_dict_foreach(root->children, cursor, child_name, child) {
282 a1_try = findChild (child, a1);
283 a2_try = findChild (child, a2);
284 if (a1_try && a2_try) return child;
289 static void linkContainers (const char *a1, const char *a2)
292 if (strcmp (a1, "__loopback__") == 0 || strcmp (a2, "__loopback__") == 0)
295 container_t a1_container = ((container_t)xbt_dict_get (allContainers, a1));
296 type_t a1_type = a1_container->type;
298 container_t a2_container = ((container_t)xbt_dict_get (allContainers, a2));
299 type_t a2_type = a2_container->type;
301 container_t container = findCommonFather (rootContainer, a1_container, a2_container);
302 xbt_assert0 (container != NULL, "common father not found");
305 char link_typename[INSTR_DEFAULT_STR_SIZE];
306 snprintf (link_typename, INSTR_DEFAULT_STR_SIZE, "%s-%s", a1_type->name, a2_type->name);
307 type_t link_type = getLinkType (link_typename, container->type, a1_type, a2_type);
310 static long long counter = 0;
311 char key[INSTR_DEFAULT_STR_SIZE];
312 snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter++);
313 pajeStartLink(SIMIX_get_clock(), link_type->id, container->id, "G", a1_container->id, key);
314 pajeEndLink(SIMIX_get_clock(), link_type->id, container->id, "G", a2_container->id, key);
317 static void recursiveGraphExtraction (container_t container)
319 if (xbt_dict_length(container->children)){
320 xbt_dict_cursor_t cursor = NULL;
323 //bottom-up recursion
324 xbt_dict_foreach(container->children, cursor, child_name, child) {
325 recursiveGraphExtraction (child);
329 xbt_dict_cursor_t cursor1 = NULL, cursor2 = NULL;
330 container_t child1, child2;
331 const char *child_name1, *child_name2;
333 xbt_dict_t filter = xbt_dict_new ();
335 xbt_dict_foreach(container->children, cursor1, child_name1, child1) {
336 xbt_dict_foreach(container->children, cursor2, child_name2, child2) {
337 //check if we already register this pair (we only need one direction)
338 char aux1[INSTR_DEFAULT_STR_SIZE], aux2[INSTR_DEFAULT_STR_SIZE];
339 snprintf (aux1, INSTR_DEFAULT_STR_SIZE, "%s%s", child_name1, child_name2);
340 snprintf (aux2, INSTR_DEFAULT_STR_SIZE, "%s%s", child_name2, child_name1);
341 if (xbt_dict_get_or_null (filter, aux1)) continue;
342 if (xbt_dict_get_or_null (filter, aux2)) continue;
344 //ok, not found, register it
345 xbt_dict_set (filter, aux1, xbt_strdup ("1"), xbt_free);
346 xbt_dict_set (filter, aux2, xbt_strdup ("1"), xbt_free);
348 if ((child1->kind == INSTR_HOST || child1->kind == INSTR_ROUTER) &&
349 (child2->kind == INSTR_HOST || child2->kind == INSTR_ROUTER)){
355 route = global_routing->get_route (child_name1, child_name2);
357 //no route between them, that's possible
361 //link the route members
364 char *previous_entity_name = (char*)child_name1;
365 xbt_dynar_foreach (route, cpt, link) {
366 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
367 linkContainers (previous_entity_name, link_name);
368 previous_entity_name = link_name;
370 linkContainers (previous_entity_name, child_name2);
371 }else if (child1->kind == INSTR_AS &&
372 child2->kind == INSTR_AS &&
373 strcmp(child_name1, child_name2) != 0){
376 routing_component_t root = global_routing->root;
377 route_extended_t route;
380 route = root->get_route (root, child_name1, child_name2);
382 //no route between them, that's possible
385 xbt_assert2(route!=NULL,
386 "there is no ASroute between %s and %s", child_name1, child_name2);
389 char *previous_entity_name = route->src_gateway;
390 xbt_dynar_foreach (route->generic_route.link_list, cpt, link) {
391 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
392 linkContainers (previous_entity_name, link_name);
393 previous_entity_name = link_name;
395 linkContainers (previous_entity_name, route->dst_gateway);
399 xbt_dict_free(&filter);
406 static void instr_routing_parse_start_AS ()
408 if (rootContainer == NULL){
409 currentContainer = xbt_dynar_new (sizeof(s_container_t), NULL);
410 allContainers = xbt_dict_new ();
411 allLinkTypes = xbt_dynar_new (sizeof(s_type_t), NULL);
412 allHostTypes = xbt_dynar_new (sizeof(s_type_t), NULL);
414 rootContainer = newContainer ("0", INSTR_AS, NULL);
415 xbt_dynar_push (currentContainer, rootContainer);
417 if (TRACE_smpi_is_enabled()) {
418 if (!TRACE_smpi_is_grouped()){
419 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
420 type_t mpi = getContainerType("MPI_PROCESS", father->type);
421 getStateType ("MPI_STATE", mpi);
422 getLinkType ("MPI_LINK", rootType, mpi, mpi);
426 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
427 container_t new = newContainer (A_surfxml_AS_id, INSTR_AS, father);
430 xbt_dynar_push (currentContainer, new);
433 static void instr_routing_parse_end_AS ()
435 xbt_dynar_pop_ptr (currentContainer);
438 static void instr_routing_parse_start_link ()
440 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
441 container_t new = newContainer (A_surfxml_link_id, INSTR_LINK, father);
443 type_t bandwidth = getVariableType ("bandwidth", NULL, new->type);
444 type_t latency = getVariableType ("latency", NULL, new->type);
445 pajeSetVariable (0, bandwidth->id, new->id, A_surfxml_link_bandwidth);
446 pajeSetVariable (0, latency->id, new->id, A_surfxml_link_latency);
447 if (TRACE_uncategorized()){
448 getVariableType ("bandwidth_used", "0.5 0.5 0.5", new->type);
452 static void instr_routing_parse_end_link ()
456 static void instr_routing_parse_start_host ()
458 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
459 container_t new = newContainer (A_surfxml_host_id, INSTR_HOST, father);
461 type_t power = getVariableType ("power", NULL, new->type);
462 pajeSetVariable (0, power->id, new->id, A_surfxml_host_power);
463 if (TRACE_uncategorized()){
464 getVariableType ("power_used", "0.5 0.5 0.5", new->type);
467 if (TRACE_smpi_is_enabled()) {
468 if (TRACE_smpi_is_grouped()){
469 type_t mpi = getContainerType("MPI_PROCESS", new->type);
470 getStateType ("MPI_STATE", mpi);
471 getLinkType ("MPI_LINK", rootType, mpi, mpi);
476 static void instr_routing_parse_end_host ()
480 static void instr_routing_parse_start_router ()
482 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
483 newContainer (A_surfxml_router_id, INSTR_ROUTER, father);
486 static void instr_routing_parse_end_router ()
490 static void instr_routing_parse_end_platform ()
492 currentContainer = NULL;
493 recursiveGraphExtraction (rootContainer);
494 platform_created = 1;
500 int instr_link_is_traced (const char *name)
502 if (((container_t)xbt_dict_get_or_null (allContainers, name))){
509 char *instr_variable_type (const char *name, const char *resource)
511 container_t container = (container_t)xbt_dict_get (allContainers, resource);
512 xbt_dict_cursor_t cursor = NULL;
515 xbt_dict_foreach(container->type->children, cursor, type_name, type) {
516 if (strcmp (name, type->name) == 0) return type->id;
521 char *instr_resource_type (const char *resource_name)
523 return ((container_t)xbt_dict_get_or_null (allContainers, resource_name))->id;
526 static void recursiveDestroyContainer (container_t container)
528 xbt_dict_cursor_t cursor = NULL;
531 xbt_dict_foreach(container->children, cursor, child_name, child) {
532 recursiveDestroyContainer (child);
535 pajeDestroyContainer(SIMIX_get_clock(), container->type->id, container->id);
537 xbt_free (container->name);
538 xbt_free (container->id);
539 xbt_free (container->children);
540 xbt_free (container);
544 static void recursiveDestroyType (type_t type)
546 xbt_dict_cursor_t cursor = NULL;
549 xbt_dict_foreach(type->children, cursor, child_name, child) {
550 recursiveDestroyType (child);
552 xbt_free (type->name);
554 xbt_free (type->children);
559 void instr_destroy_platform ()
561 if (rootContainer) recursiveDestroyContainer (rootContainer);
562 if (rootType) recursiveDestroyType (rootType);
566 * user categories support
568 static void recursiveNewUserVariableType (const char *new_typename, const char *color, type_t root)
570 if (!strcmp (root->name, "HOST") || !strcmp (root->name, "LINK")){
571 getVariableType(new_typename, color, root);
573 xbt_dict_cursor_t cursor = NULL;
576 xbt_dict_foreach(root->children, cursor, name, child_type) {
577 recursiveNewUserVariableType (new_typename, color, child_type);
581 void instr_new_user_variable_type (const char *new_typename, const char *color)
583 recursiveNewUserVariableType (new_typename, color, rootType);
586 static void recursiveNewUserLinkVariableType (const char *new_typename, const char *color, type_t root)
588 if (!strcmp (root->name, "LINK")){
589 getVariableType(new_typename, color, root);
591 xbt_dict_cursor_t cursor = NULL;
594 xbt_dict_foreach(root->children, cursor, name, child_type) {
595 recursiveNewUserLinkVariableType (new_typename, color, child_type);
599 void instr_new_user_link_variable_type (const char *new_typename, const char *color)
601 recursiveNewUserLinkVariableType (new_typename, color, rootType);
605 static void recursiveNewUserHostVariableType (const char *new_typename, const char *color, type_t root)
607 if (!strcmp (root->name, "HOST")){
608 getVariableType(new_typename, color, root);
610 xbt_dict_cursor_t cursor = NULL;
613 xbt_dict_foreach(root->children, cursor, name, child_type) {
614 recursiveNewUserHostVariableType (new_typename, color, child_type);
618 void instr_new_user_host_variable_type (const char *new_typename, const char *color)
620 recursiveNewUserHostVariableType (new_typename, color, rootType);
623 int instr_platform_traced ()
625 return platform_created;
628 #endif /* HAVE_TRACING */