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 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 type_t newEventType (const char *typename, e_entity_types kind, const char *color, type_t father)
70 type_t ret = newType (typename, kind, father);
71 // INFO4("EventType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
73 pajeDefineEventTypeWithColor (ret->id, ret->father->id, ret->name, color);
75 pajeDefineEventType(ret->id, ret->father->id, ret->name);
80 type_t newVariableType (const char *typename, e_entity_types kind, const char *color, type_t father)
82 type_t ret = newType (typename, kind, father);
83 // INFO4("VariableType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
85 pajeDefineVariableTypeWithColor(ret->id, ret->father->id, ret->name, color);
87 pajeDefineVariableType(ret->id, ret->father->id, ret->name);
92 type_t newLinkType (const char *typename, e_entity_types kind, type_t father, type_t source, type_t dest)
94 type_t ret = newType (typename, kind, father);
95 // 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);
96 pajeDefineLinkType(ret->id, ret->father->id, source->id, dest->id, ret->name);
100 type_t newStateType (const char *typename, e_entity_types kind, type_t father)
102 type_t ret = newType (typename, kind, father);
103 // INFO4("StateType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
104 pajeDefineStateType(ret->id, ret->father->id, ret->name);
108 static type_t getContainerType (const char *typename, type_t father)
112 ret = newContainerType (typename, TYPE_CONTAINER, father);
115 //check if my father type already has my typename
116 ret = (type_t)xbt_dict_get_or_null (father->children, typename);
118 ret = newContainerType (typename, TYPE_CONTAINER, father);
124 static type_t getEventType (const char *typename, const char *color, type_t father)
126 type_t ret = xbt_dict_get_or_null (father->children, typename);
128 ret = newEventType (typename, TYPE_EVENT, color, father);
133 static type_t getVariableType (const char *typename, const char *color, type_t father)
135 type_t ret = xbt_dict_get_or_null (father->children, typename);
137 ret = newVariableType (typename, TYPE_VARIABLE, color, father);
142 static type_t getLinkType (const char *typename, type_t father, type_t source, type_t dest)
144 type_t ret = xbt_dict_get_or_null (father->children, typename);
146 ret = newLinkType (typename, TYPE_LINK, father, source, dest);
151 static type_t getStateType (const char *typename, type_t father)
153 type_t ret = xbt_dict_get_or_null (father->children, typename);
155 ret = newStateType (typename, TYPE_STATE, father);
160 void instr_routing_define_callbacks ()
162 if (!TRACE_is_active())
164 surfxml_add_callback(STag_surfxml_AS_cb_list, &instr_routing_parse_start_AS);
165 surfxml_add_callback(ETag_surfxml_AS_cb_list, &instr_routing_parse_end_AS);
166 surfxml_add_callback(STag_surfxml_link_cb_list, &instr_routing_parse_start_link);
167 surfxml_add_callback(ETag_surfxml_link_cb_list, &instr_routing_parse_end_link);
168 surfxml_add_callback(STag_surfxml_host_cb_list, &instr_routing_parse_start_host);
169 surfxml_add_callback(ETag_surfxml_host_cb_list, &instr_routing_parse_end_host);
170 surfxml_add_callback(STag_surfxml_router_cb_list, &instr_routing_parse_start_router);
171 surfxml_add_callback(ETag_surfxml_router_cb_list, &instr_routing_parse_end_router);
172 surfxml_add_callback(ETag_surfxml_platform_cb_list, &instr_routing_parse_end_platform);
175 static long long int newContainedId ()
177 static long long counter = 0;
181 container_t newContainer (const char *name, e_container_types kind, container_t father)
183 long long int counter = newContainedId();
184 char id_str[INSTR_DEFAULT_STR_SIZE];
185 snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", counter);
187 container_t new = xbt_new0(s_container_t, 1);
188 new->name = xbt_strdup (name); // name of the container
189 new->id = xbt_strdup (id_str); // id (or alias) of the container
190 new->father = father;
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 case INSTR_SMPI: new->type = getContainerType ("MPI", new->father->type); break;
215 case INSTR_MSG_PROCESS: new->type = getContainerType ("MSG_PROCESS", new->father->type); break;
216 default: xbt_die ("Congratulations, you have found a bug on newContainer function of instr_routing.c"); break;
219 new->children = xbt_dict_new();
221 xbt_dict_set(new->father->children, new->name, new, NULL);
222 pajeCreateContainer (SIMIX_get_clock(), new->id, new->type->id, new->father->id, new->name);
225 //register hosts, routers, links containers
226 if (new->kind == INSTR_HOST || new->kind == INSTR_LINK || new->kind == INSTR_ROUTER) {
227 xbt_dict_set (allContainers, new->name, new, NULL);
230 //register the host container types
231 if (new->kind == INSTR_HOST){
232 xbt_dynar_push_as (allHostTypes, type_t, new->type);
235 //register the link container types
236 if (new->kind == INSTR_LINK){
237 xbt_dynar_push_as(allLinkTypes, type_t, new->type);
242 static container_t recursiveGetContainer (const char *name, container_t root)
244 if (strcmp (root->name, name) == 0) return root;
246 xbt_dict_cursor_t cursor = NULL;
249 xbt_dict_foreach(root->children, cursor, child_name, child) {
250 container_t ret = recursiveGetContainer(name, child);
256 container_t getContainer (const char *name)
258 return recursiveGetContainer(name, rootContainer);
261 static type_t recursiveGetType (const char *name, type_t root)
263 if (strcmp (root->name, name) == 0) return root;
265 xbt_dict_cursor_t cursor = NULL;
268 xbt_dict_foreach(root->children, cursor, child_name, child) {
269 type_t ret = recursiveGetType(name, child);
275 type_t getType (const char *name)
277 return recursiveGetType (name, rootType);
280 void destroyContainer (container_t container)
282 //remove me from my father
283 if (container->father){
284 xbt_dict_remove(container->father->children, container->name);
287 //trace my destruction
288 pajeDestroyContainer(SIMIX_get_clock(), container->type->id, container->id);
291 xbt_free (container->name);
292 xbt_free (container->id);
293 xbt_free (container->children);
294 xbt_free (container);
298 static container_t findChild (container_t root, container_t a1)
300 if (root == a1) return root;
302 xbt_dict_cursor_t cursor = NULL;
305 xbt_dict_foreach(root->children, cursor, child_name, child) {
306 if (findChild (child, a1)) return child;
311 static container_t findCommonFather (container_t root, container_t a1, container_t a2)
313 if (a1->father == a2->father) return a1->father;
315 xbt_dict_cursor_t cursor = NULL;
318 container_t a1_try = NULL;
319 container_t a2_try = NULL;
320 xbt_dict_foreach(root->children, cursor, child_name, child) {
321 a1_try = findChild (child, a1);
322 a2_try = findChild (child, a2);
323 if (a1_try && a2_try) return child;
328 static void linkContainers (const char *a1, const char *a2)
331 if (strcmp (a1, "__loopback__") == 0 || strcmp (a2, "__loopback__") == 0)
334 container_t a1_container = ((container_t)xbt_dict_get (allContainers, a1));
335 type_t a1_type = a1_container->type;
337 container_t a2_container = ((container_t)xbt_dict_get (allContainers, a2));
338 type_t a2_type = a2_container->type;
340 container_t container = findCommonFather (rootContainer, a1_container, a2_container);
341 xbt_assert0 (container != NULL, "common father not found");
344 char link_typename[INSTR_DEFAULT_STR_SIZE];
345 snprintf (link_typename, INSTR_DEFAULT_STR_SIZE, "%s-%s", a1_type->name, a2_type->name);
346 type_t link_type = getLinkType (link_typename, container->type, a1_type, a2_type);
349 static long long counter = 0;
350 char key[INSTR_DEFAULT_STR_SIZE];
351 snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter++);
352 pajeStartLink(SIMIX_get_clock(), link_type->id, container->id, "G", a1_container->id, key);
353 pajeEndLink(SIMIX_get_clock(), link_type->id, container->id, "G", a2_container->id, key);
356 static void recursiveGraphExtraction (container_t container)
358 if (xbt_dict_length(container->children)){
359 xbt_dict_cursor_t cursor = NULL;
362 //bottom-up recursion
363 xbt_dict_foreach(container->children, cursor, child_name, child) {
364 recursiveGraphExtraction (child);
368 xbt_dict_cursor_t cursor1 = NULL, cursor2 = NULL;
369 container_t child1, child2;
370 const char *child_name1, *child_name2;
372 xbt_dict_t filter = xbt_dict_new ();
374 xbt_dict_foreach(container->children, cursor1, child_name1, child1) {
375 xbt_dict_foreach(container->children, cursor2, child_name2, child2) {
376 //check if we already register this pair (we only need one direction)
377 char aux1[INSTR_DEFAULT_STR_SIZE], aux2[INSTR_DEFAULT_STR_SIZE];
378 snprintf (aux1, INSTR_DEFAULT_STR_SIZE, "%s%s", child_name1, child_name2);
379 snprintf (aux2, INSTR_DEFAULT_STR_SIZE, "%s%s", child_name2, child_name1);
380 if (xbt_dict_get_or_null (filter, aux1)) continue;
381 if (xbt_dict_get_or_null (filter, aux2)) continue;
383 //ok, not found, register it
384 xbt_dict_set (filter, aux1, xbt_strdup ("1"), xbt_free);
385 xbt_dict_set (filter, aux2, xbt_strdup ("1"), xbt_free);
387 if ((child1->kind == INSTR_HOST || child1->kind == INSTR_ROUTER) &&
388 (child2->kind == INSTR_HOST || child2->kind == INSTR_ROUTER)){
394 route = global_routing->get_route (child_name1, child_name2);
396 //no route between them, that's possible
400 //link the route members
403 char *previous_entity_name = (char*)child_name1;
404 xbt_dynar_foreach (route, cpt, link) {
405 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
406 linkContainers (previous_entity_name, link_name);
407 previous_entity_name = link_name;
409 linkContainers (previous_entity_name, child_name2);
410 }else if (child1->kind == INSTR_AS &&
411 child2->kind == INSTR_AS &&
412 strcmp(child_name1, child_name2) != 0){
415 routing_component_t root = global_routing->root;
416 route_extended_t route;
419 route = root->get_route (root, child_name1, child_name2);
421 //no route between them, that's possible
424 xbt_assert2(route!=NULL,
425 "there is no ASroute between %s and %s", child_name1, child_name2);
428 char *previous_entity_name = route->src_gateway;
429 xbt_dynar_foreach (route->generic_route.link_list, cpt, link) {
430 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
431 linkContainers (previous_entity_name, link_name);
432 previous_entity_name = link_name;
434 linkContainers (previous_entity_name, route->dst_gateway);
438 xbt_dict_free(&filter);
445 static void instr_routing_parse_start_AS ()
447 if (rootContainer == NULL){
448 currentContainer = xbt_dynar_new (sizeof(s_container_t), NULL);
449 allContainers = xbt_dict_new ();
450 allLinkTypes = xbt_dynar_new (sizeof(s_type_t), NULL);
451 allHostTypes = xbt_dynar_new (sizeof(s_type_t), NULL);
453 rootContainer = newContainer ("0", INSTR_AS, NULL);
454 xbt_dynar_push (currentContainer, rootContainer);
456 if (TRACE_smpi_is_enabled()) {
457 if (!TRACE_smpi_is_grouped()){
458 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
459 type_t mpi = getContainerType("MPI", father->type);
460 getStateType ("MPI_STATE", mpi);
461 getLinkType ("MPI_LINK", rootType, mpi, mpi);
465 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
466 container_t new = newContainer (A_surfxml_AS_id, INSTR_AS, father);
469 xbt_dynar_push (currentContainer, new);
472 static void instr_routing_parse_end_AS ()
474 xbt_dynar_pop_ptr (currentContainer);
477 static void instr_routing_parse_start_link ()
479 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
480 container_t new = newContainer (A_surfxml_link_id, INSTR_LINK, father);
482 type_t bandwidth = getVariableType ("bandwidth", NULL, new->type);
483 type_t latency = getVariableType ("latency", NULL, new->type);
484 pajeSetVariable (0, bandwidth->id, new->id, A_surfxml_link_bandwidth);
485 pajeSetVariable (0, latency->id, new->id, A_surfxml_link_latency);
486 if (TRACE_uncategorized()){
487 getVariableType ("bandwidth_used", "0.5 0.5 0.5", new->type);
491 static void instr_routing_parse_end_link ()
495 static void instr_routing_parse_start_host ()
497 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
498 container_t new = newContainer (A_surfxml_host_id, INSTR_HOST, father);
500 type_t power = getVariableType ("power", NULL, new->type);
501 pajeSetVariable (0, power->id, new->id, A_surfxml_host_power);
502 if (TRACE_uncategorized()){
503 getVariableType ("power_used", "0.5 0.5 0.5", new->type);
506 if (TRACE_smpi_is_enabled()) {
507 if (TRACE_smpi_is_grouped()){
508 type_t mpi = getContainerType("MPI", new->type);
509 getStateType ("MPI_STATE", mpi);
510 getLinkType ("MPI_LINK", rootType, mpi, mpi);
514 if (TRACE_msg_process_is_enabled()) {
515 getContainerType("MSG_PROCESS", new->type);
519 static void instr_routing_parse_end_host ()
523 static void instr_routing_parse_start_router ()
525 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
526 newContainer (A_surfxml_router_id, INSTR_ROUTER, father);
529 static void instr_routing_parse_end_router ()
533 static void instr_routing_parse_end_platform ()
535 currentContainer = NULL;
536 recursiveGraphExtraction (rootContainer);
537 platform_created = 1;
543 int instr_link_is_traced (const char *name)
545 if (((container_t)xbt_dict_get_or_null (allContainers, name))){
552 char *instr_variable_type (const char *name, const char *resource)
554 container_t container = (container_t)xbt_dict_get (allContainers, resource);
555 xbt_dict_cursor_t cursor = NULL;
558 xbt_dict_foreach(container->type->children, cursor, type_name, type) {
559 if (strcmp (name, type->name) == 0) return type->id;
564 char *instr_resource_type (const char *resource_name)
566 return ((container_t)xbt_dict_get_or_null (allContainers, resource_name))->id;
569 static void recursiveDestroyContainer (container_t container)
571 xbt_dict_cursor_t cursor = NULL;
574 xbt_dict_foreach(container->children, cursor, child_name, child) {
575 recursiveDestroyContainer (child);
577 destroyContainer (container);
580 static void recursiveDestroyType (type_t type)
582 xbt_dict_cursor_t cursor = NULL;
585 xbt_dict_foreach(type->children, cursor, child_name, child) {
586 recursiveDestroyType (child);
588 xbt_free (type->name);
590 xbt_free (type->children);
595 void instr_destroy_platform ()
597 if (rootContainer) recursiveDestroyContainer (rootContainer);
598 if (rootType) recursiveDestroyType (rootType);
602 * user categories support
604 static void recursiveNewUserVariableType (const char *new_typename, const char *color, type_t root)
606 if (!strcmp (root->name, "HOST") || !strcmp (root->name, "LINK")){
607 getVariableType(new_typename, color, root);
609 xbt_dict_cursor_t cursor = NULL;
612 xbt_dict_foreach(root->children, cursor, name, child_type) {
613 recursiveNewUserVariableType (new_typename, color, child_type);
617 void instr_new_user_variable_type (const char *new_typename, const char *color)
619 recursiveNewUserVariableType (new_typename, color, rootType);
622 static void recursiveNewUserLinkVariableType (const char *new_typename, const char *color, type_t root)
624 if (!strcmp (root->name, "LINK")){
625 getVariableType(new_typename, color, root);
627 xbt_dict_cursor_t cursor = NULL;
630 xbt_dict_foreach(root->children, cursor, name, child_type) {
631 recursiveNewUserLinkVariableType (new_typename, color, child_type);
635 void instr_new_user_link_variable_type (const char *new_typename, const char *color)
637 recursiveNewUserLinkVariableType (new_typename, color, rootType);
641 static void recursiveNewUserHostVariableType (const char *new_typename, const char *color, type_t root)
643 if (!strcmp (root->name, "HOST")){
644 getVariableType(new_typename, color, root);
646 xbt_dict_cursor_t cursor = NULL;
649 xbt_dict_foreach(root->children, cursor, name, child_type) {
650 recursiveNewUserHostVariableType (new_typename, color, child_type);
654 void instr_new_user_host_variable_type (const char *new_typename, const char *color)
656 recursiveNewUserHostVariableType (new_typename, color, rootType);
659 int instr_platform_traced ()
661 return platform_created;
664 #endif /* HAVE_TRACING */