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 */
17 static int platform_created = 0; /* indicate whether the platform file has been traced */
18 static type_t rootType = NULL; /* the root type */
19 static container_t rootContainer = NULL; /* the root container */
20 static xbt_dynar_t currentContainer = NULL; /* push and pop, used only in creation */
21 static xbt_dict_t allContainers = NULL; /* all created containers indexed by name */
22 xbt_dynar_t allLinkTypes = NULL; /* all link types defined */
23 xbt_dynar_t allHostTypes = NULL; /* all host types defined */
25 static void instr_routing_parse_start_AS (void);
26 static void instr_routing_parse_end_AS (void);
27 static void instr_routing_parse_start_link (void);
28 static void instr_routing_parse_end_link (void);
29 static void instr_routing_parse_start_host (void);
30 static void instr_routing_parse_end_host (void);
31 static void instr_routing_parse_start_router (void);
32 static void instr_routing_parse_end_router (void);
33 static void instr_routing_parse_end_platform (void);
35 static long long int newTypeId ()
37 static long long int counter = 0;
41 static type_t newType (const char *typename, e_entity_types kind, type_t father)
43 type_t ret = xbt_new0(s_type_t, 1);
44 ret->name = xbt_strdup (typename);
47 ret->children = xbt_dict_new ();
49 long long int id = newTypeId();
50 char str_id[INSTR_DEFAULT_STR_SIZE];
51 snprintf (str_id, INSTR_DEFAULT_STR_SIZE, "%lld", id);
52 ret->id = xbt_strdup (str_id);
55 xbt_dict_set (father->children, typename, ret, NULL);
60 static type_t newContainerType (const char *typename, e_entity_types kind, type_t father)
62 type_t ret = newType (typename, kind, father);
63 // if (father) INFO4("ContainerType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
64 if (father) pajeDefineContainerType(ret->id, ret->father->id, ret->name);
68 static type_t newVariableType (const char *typename, e_entity_types kind, const char *color, type_t father)
70 type_t ret = newType (typename, kind, father);
71 // INFO4("VariableType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
73 pajeDefineVariableTypeWithColor(ret->id, ret->father->id, ret->name, color);
75 pajeDefineVariableType(ret->id, ret->father->id, ret->name);
80 static type_t newLinkType (const char *typename, e_entity_types kind, type_t father, type_t source, type_t dest)
82 type_t ret = newType (typename, kind, father);
83 // 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);
84 pajeDefineLinkType(ret->id, ret->father->id, source->id, dest->id, ret->name);
88 static type_t newStateType (const char *typename, e_entity_types kind, type_t father)
90 type_t ret = newType (typename, kind, father);
91 // INFO4("StateType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
92 pajeDefineStateType(ret->id, ret->father->id, ret->name);
96 static type_t getContainerType (const char *typename, type_t father)
100 ret = newContainerType (typename, TYPE_CONTAINER, father);
103 //check if my father type already has my typename
104 ret = (type_t)xbt_dict_get_or_null (father->children, typename);
106 ret = newContainerType (typename, TYPE_CONTAINER, father);
112 static type_t getVariableType (const char *typename, const char *color, type_t father)
114 type_t ret = xbt_dict_get_or_null (father->children, typename);
116 ret = newVariableType (typename, TYPE_VARIABLE, color, father);
121 static type_t getLinkType (const char *typename, type_t father, type_t source, type_t dest)
123 type_t ret = xbt_dict_get_or_null (father->children, typename);
125 ret = newLinkType (typename, TYPE_LINK, father, source, dest);
130 static type_t getStateType (const char *typename, type_t father)
132 type_t ret = xbt_dict_get_or_null (father->children, typename);
134 ret = newStateType (typename, TYPE_STATE, father);
139 void instr_routing_define_callbacks ()
141 if (!TRACE_is_active())
143 surfxml_add_callback(STag_surfxml_AS_cb_list, &instr_routing_parse_start_AS);
144 surfxml_add_callback(ETag_surfxml_AS_cb_list, &instr_routing_parse_end_AS);
145 surfxml_add_callback(STag_surfxml_link_cb_list, &instr_routing_parse_start_link);
146 surfxml_add_callback(ETag_surfxml_link_cb_list, &instr_routing_parse_end_link);
147 surfxml_add_callback(STag_surfxml_host_cb_list, &instr_routing_parse_start_host);
148 surfxml_add_callback(ETag_surfxml_host_cb_list, &instr_routing_parse_end_host);
149 surfxml_add_callback(STag_surfxml_router_cb_list, &instr_routing_parse_start_router);
150 surfxml_add_callback(ETag_surfxml_router_cb_list, &instr_routing_parse_end_router);
151 surfxml_add_callback(ETag_surfxml_platform_cb_list, &instr_routing_parse_end_platform);
154 static long long int newContainedId ()
156 static long long counter = 0;
160 container_t newContainer (const char *name, e_container_types kind, container_t father)
162 long long int counter = newContainedId();
163 char id_str[INSTR_DEFAULT_STR_SIZE];
164 snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", counter);
166 container_t new = xbt_new0(s_container_t, 1);
167 new->name = xbt_strdup (name); // name of the container
168 new->id = xbt_strdup (id_str); // id (or alias) of the container
169 new->father = father;
170 // level depends on level of father
172 new->level = new->father->level+1;
176 // type definition (method depends on kind of this new container)
178 if (new->kind == INSTR_AS){
179 //if this container is of an AS, its type name depends on its level
180 char as_typename[INSTR_DEFAULT_STR_SIZE];
181 snprintf (as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", new->level);
183 new->type = getContainerType (as_typename, new->father->type);
185 new->type = getContainerType ("0", NULL);
188 //otherwise, the name is its kind
190 case INSTR_HOST: new->type = getContainerType ("HOST", new->father->type); break;
191 case INSTR_LINK: new->type = getContainerType ("LINK", new->father->type); break;
192 case INSTR_ROUTER: new->type = getContainerType ("ROUTER", new->father->type); break;
193 case INSTR_SMPI: new->type = getContainerType ("MPI", new->father->type); break;
194 default: xbt_die ("Congratulations, you have found a bug on newContainer function of instr_routing.c"); break;
197 new->children = xbt_dict_new();
199 xbt_dict_set(new->father->children, new->name, new, NULL);
200 pajeCreateContainer (0, new->id, new->type->id, new->father->id, new->name);
203 //register hosts, routers, links containers
204 if (new->kind == INSTR_HOST || new->kind == INSTR_LINK || new->kind == INSTR_ROUTER) {
205 xbt_dict_set (allContainers, new->name, new, NULL);
208 //register the host container types
209 if (new->kind == INSTR_HOST){
210 xbt_dynar_push_as (allHostTypes, type_t, new->type);
213 //register the link container types
214 if (new->kind == INSTR_LINK){
215 xbt_dynar_push_as(allLinkTypes, type_t, new->type);
220 static container_t recursiveGetContainer (const char *name, container_t root)
222 if (strcmp (root->name, name) == 0) return root;
224 xbt_dict_cursor_t cursor = NULL;
227 xbt_dict_foreach(root->children, cursor, child_name, child) {
228 container_t ret = recursiveGetContainer(name, child);
234 container_t getContainer (const char *name)
236 return recursiveGetContainer(name, rootContainer);
239 void destroyContainer (container_t container)
241 //remove me from my father
242 if (container->father){
243 xbt_dict_remove(container->father->children, container->name);
246 //trace my destruction
247 pajeDestroyContainer(SIMIX_get_clock(), container->type->id, container->id);
250 xbt_free (container->name);
251 xbt_free (container->id);
252 xbt_free (container->children);
253 xbt_free (container);
257 static container_t findChild (container_t root, container_t a1)
259 if (root == a1) return root;
261 xbt_dict_cursor_t cursor = NULL;
264 xbt_dict_foreach(root->children, cursor, child_name, child) {
265 if (findChild (child, a1)) return child;
270 static container_t findCommonFather (container_t root, container_t a1, container_t a2)
272 if (a1->father == a2->father) return a1->father;
274 xbt_dict_cursor_t cursor = NULL;
277 container_t a1_try = NULL;
278 container_t a2_try = NULL;
279 xbt_dict_foreach(root->children, cursor, child_name, child) {
280 a1_try = findChild (child, a1);
281 a2_try = findChild (child, a2);
282 if (a1_try && a2_try) return child;
287 static void linkContainers (const char *a1, const char *a2)
290 if (strcmp (a1, "__loopback__") == 0 || strcmp (a2, "__loopback__") == 0)
293 container_t a1_container = ((container_t)xbt_dict_get (allContainers, a1));
294 type_t a1_type = a1_container->type;
296 container_t a2_container = ((container_t)xbt_dict_get (allContainers, a2));
297 type_t a2_type = a2_container->type;
299 container_t container = findCommonFather (rootContainer, a1_container, a2_container);
300 xbt_assert0 (container != NULL, "common father not found");
303 char link_typename[INSTR_DEFAULT_STR_SIZE];
304 snprintf (link_typename, INSTR_DEFAULT_STR_SIZE, "%s-%s", a1_type->name, a2_type->name);
305 type_t link_type = getLinkType (link_typename, container->type, a1_type, a2_type);
308 static long long counter = 0;
309 char key[INSTR_DEFAULT_STR_SIZE];
310 snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter++);
311 pajeStartLink(SIMIX_get_clock(), link_type->id, container->id, "G", a1_container->id, key);
312 pajeEndLink(SIMIX_get_clock(), link_type->id, container->id, "G", a2_container->id, key);
315 static void recursiveGraphExtraction (container_t container)
317 if (xbt_dict_length(container->children)){
318 xbt_dict_cursor_t cursor = NULL;
321 //bottom-up recursion
322 xbt_dict_foreach(container->children, cursor, child_name, child) {
323 recursiveGraphExtraction (child);
327 xbt_dict_cursor_t cursor1 = NULL, cursor2 = NULL;
328 container_t child1, child2;
329 const char *child_name1, *child_name2;
331 xbt_dict_t filter = xbt_dict_new ();
333 xbt_dict_foreach(container->children, cursor1, child_name1, child1) {
334 xbt_dict_foreach(container->children, cursor2, child_name2, child2) {
335 //check if we already register this pair (we only need one direction)
336 char aux1[INSTR_DEFAULT_STR_SIZE], aux2[INSTR_DEFAULT_STR_SIZE];
337 snprintf (aux1, INSTR_DEFAULT_STR_SIZE, "%s%s", child_name1, child_name2);
338 snprintf (aux2, INSTR_DEFAULT_STR_SIZE, "%s%s", child_name2, child_name1);
339 if (xbt_dict_get_or_null (filter, aux1)) continue;
340 if (xbt_dict_get_or_null (filter, aux2)) continue;
342 //ok, not found, register it
343 xbt_dict_set (filter, aux1, xbt_strdup ("1"), xbt_free);
344 xbt_dict_set (filter, aux2, xbt_strdup ("1"), xbt_free);
346 if ((child1->kind == INSTR_HOST || child1->kind == INSTR_ROUTER) &&
347 (child2->kind == INSTR_HOST || child2->kind == INSTR_ROUTER)){
353 route = global_routing->get_route (child_name1, child_name2);
355 //no route between them, that's possible
359 //link the route members
362 char *previous_entity_name = (char*)child_name1;
363 xbt_dynar_foreach (route, cpt, link) {
364 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
365 linkContainers (previous_entity_name, link_name);
366 previous_entity_name = link_name;
368 linkContainers (previous_entity_name, child_name2);
369 }else if (child1->kind == INSTR_AS &&
370 child2->kind == INSTR_AS &&
371 strcmp(child_name1, child_name2) != 0){
374 routing_component_t root = global_routing->root;
375 route_extended_t route;
378 route = root->get_route (root, child_name1, child_name2);
380 //no route between them, that's possible
383 xbt_assert2(route!=NULL,
384 "there is no ASroute between %s and %s", child_name1, child_name2);
387 char *previous_entity_name = route->src_gateway;
388 xbt_dynar_foreach (route->generic_route.link_list, cpt, link) {
389 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
390 linkContainers (previous_entity_name, link_name);
391 previous_entity_name = link_name;
393 linkContainers (previous_entity_name, route->dst_gateway);
397 xbt_dict_free(&filter);
404 static void instr_routing_parse_start_AS ()
406 if (rootContainer == NULL){
407 currentContainer = xbt_dynar_new (sizeof(s_container_t), NULL);
408 allContainers = xbt_dict_new ();
409 allLinkTypes = xbt_dynar_new (sizeof(s_type_t), NULL);
410 allHostTypes = xbt_dynar_new (sizeof(s_type_t), NULL);
412 rootContainer = newContainer ("0", INSTR_AS, NULL);
413 xbt_dynar_push (currentContainer, rootContainer);
415 if (TRACE_smpi_is_enabled()) {
416 if (!TRACE_smpi_is_grouped()){
417 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
418 type_t mpi = getContainerType("MPI", father->type);
419 getStateType ("MPI_STATE", mpi);
420 getLinkType ("MPI_LINK", rootType, mpi, mpi);
424 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
425 container_t new = newContainer (A_surfxml_AS_id, INSTR_AS, father);
428 xbt_dynar_push (currentContainer, new);
431 static void instr_routing_parse_end_AS ()
433 xbt_dynar_pop_ptr (currentContainer);
436 static void instr_routing_parse_start_link ()
438 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
439 container_t new = newContainer (A_surfxml_link_id, INSTR_LINK, father);
441 type_t bandwidth = getVariableType ("bandwidth", NULL, new->type);
442 type_t latency = getVariableType ("latency", NULL, new->type);
443 pajeSetVariable (0, bandwidth->id, new->id, A_surfxml_link_bandwidth);
444 pajeSetVariable (0, latency->id, new->id, A_surfxml_link_latency);
445 if (TRACE_uncategorized()){
446 getVariableType ("bandwidth_used", "0.5 0.5 0.5", new->type);
450 static void instr_routing_parse_end_link ()
454 static void instr_routing_parse_start_host ()
456 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
457 container_t new = newContainer (A_surfxml_host_id, INSTR_HOST, father);
459 type_t power = getVariableType ("power", NULL, new->type);
460 pajeSetVariable (0, power->id, new->id, A_surfxml_host_power);
461 if (TRACE_uncategorized()){
462 getVariableType ("power_used", "0.5 0.5 0.5", new->type);
465 if (TRACE_smpi_is_enabled()) {
466 if (TRACE_smpi_is_grouped()){
467 type_t mpi = getContainerType("MPI", new->type);
468 getStateType ("MPI_STATE", mpi);
469 getLinkType ("MPI_LINK", rootType, mpi, mpi);
474 static void instr_routing_parse_end_host ()
478 static void instr_routing_parse_start_router ()
480 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
481 newContainer (A_surfxml_router_id, INSTR_ROUTER, father);
484 static void instr_routing_parse_end_router ()
488 static void instr_routing_parse_end_platform ()
490 currentContainer = NULL;
491 recursiveGraphExtraction (rootContainer);
492 platform_created = 1;
498 int instr_link_is_traced (const char *name)
500 if (((container_t)xbt_dict_get_or_null (allContainers, name))){
507 char *instr_variable_type (const char *name, const char *resource)
509 container_t container = (container_t)xbt_dict_get (allContainers, resource);
510 xbt_dict_cursor_t cursor = NULL;
513 xbt_dict_foreach(container->type->children, cursor, type_name, type) {
514 if (strcmp (name, type->name) == 0) return type->id;
519 char *instr_resource_type (const char *resource_name)
521 return ((container_t)xbt_dict_get_or_null (allContainers, resource_name))->id;
524 static void recursiveDestroyContainer (container_t container)
526 xbt_dict_cursor_t cursor = NULL;
529 xbt_dict_foreach(container->children, cursor, child_name, child) {
530 recursiveDestroyContainer (child);
532 destroyContainer (container);
535 static void recursiveDestroyType (type_t type)
537 xbt_dict_cursor_t cursor = NULL;
540 xbt_dict_foreach(type->children, cursor, child_name, child) {
541 recursiveDestroyType (child);
543 xbt_free (type->name);
545 xbt_free (type->children);
550 void instr_destroy_platform ()
552 if (rootContainer) recursiveDestroyContainer (rootContainer);
553 if (rootType) recursiveDestroyType (rootType);
557 * user categories support
559 static void recursiveNewUserVariableType (const char *new_typename, const char *color, type_t root)
561 if (!strcmp (root->name, "HOST") || !strcmp (root->name, "LINK")){
562 getVariableType(new_typename, color, root);
564 xbt_dict_cursor_t cursor = NULL;
567 xbt_dict_foreach(root->children, cursor, name, child_type) {
568 recursiveNewUserVariableType (new_typename, color, child_type);
572 void instr_new_user_variable_type (const char *new_typename, const char *color)
574 recursiveNewUserVariableType (new_typename, color, rootType);
577 static void recursiveNewUserLinkVariableType (const char *new_typename, const char *color, type_t root)
579 if (!strcmp (root->name, "LINK")){
580 getVariableType(new_typename, color, root);
582 xbt_dict_cursor_t cursor = NULL;
585 xbt_dict_foreach(root->children, cursor, name, child_type) {
586 recursiveNewUserLinkVariableType (new_typename, color, child_type);
590 void instr_new_user_link_variable_type (const char *new_typename, const char *color)
592 recursiveNewUserLinkVariableType (new_typename, color, rootType);
596 static void recursiveNewUserHostVariableType (const char *new_typename, const char *color, type_t root)
598 if (!strcmp (root->name, "HOST")){
599 getVariableType(new_typename, color, root);
601 xbt_dict_cursor_t cursor = NULL;
604 xbt_dict_foreach(root->children, cursor, name, child_type) {
605 recursiveNewUserHostVariableType (new_typename, color, child_type);
609 void instr_new_user_host_variable_type (const char *new_typename, const char *color)
611 recursiveNewUserHostVariableType (new_typename, color, rootType);
614 int instr_platform_traced ()
616 return platform_created;
619 #endif /* HAVE_TRACING */