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, const char *key, 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, key, ret, NULL);
60 type_t getContainerType (const char *typename, type_t father)
64 ret = newType (typename, typename, TYPE_CONTAINER, father);
65 if (father) pajeDefineContainerType(ret->id, ret->father->id, ret->name);
68 //check if my father type already has my typename
69 ret = (type_t)xbt_dict_get_or_null (father->children, typename);
71 ret = newType (typename, typename, TYPE_CONTAINER, father);
72 pajeDefineContainerType(ret->id, ret->father->id, ret->name);
78 type_t getEventType (const char *typename, const char *color, type_t father)
80 type_t ret = xbt_dict_get_or_null (father->children, typename);
82 ret = newType (typename, typename, TYPE_EVENT, father);
83 //INFO4("EventType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
85 pajeDefineEventTypeWithColor (ret->id, ret->father->id, ret->name, color);
87 pajeDefineEventType(ret->id, ret->father->id, ret->name);
93 type_t getVariableType (const char *typename, const char *color, type_t father)
95 type_t ret = xbt_dict_get_or_null (father->children, typename);
97 ret = newType (typename, typename, TYPE_VARIABLE, father);
98 //INFO4("VariableType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
100 pajeDefineVariableTypeWithColor(ret->id, ret->father->id, ret->name, color);
102 pajeDefineVariableType(ret->id, ret->father->id, ret->name);
108 type_t getLinkType (const char *typename, type_t father, type_t source, type_t dest)
110 //FIXME should check using source and dest here and not by the typename (g5k example)
111 char key[INSTR_DEFAULT_STR_SIZE];
112 snprintf (key, INSTR_DEFAULT_STR_SIZE, "%s-%s-%s", typename, source->id, dest->id);
113 type_t ret = xbt_dict_get_or_null (father->children, key);
115 ret = newType (typename, key, TYPE_LINK, 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);
122 type_t getStateType (const char *typename, type_t father)
124 type_t ret = xbt_dict_get_or_null (father->children, typename);
126 ret = newType (typename, typename, TYPE_STATE, father);
127 //INFO4("StateType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
128 pajeDefineStateType(ret->id, ret->father->id, ret->name);
133 void instr_routing_define_callbacks ()
135 if (!TRACE_is_active())
137 surfxml_add_callback(STag_surfxml_AS_cb_list, &instr_routing_parse_start_AS);
138 surfxml_add_callback(ETag_surfxml_AS_cb_list, &instr_routing_parse_end_AS);
139 surfxml_add_callback(STag_surfxml_link_cb_list, &instr_routing_parse_start_link);
140 surfxml_add_callback(ETag_surfxml_link_cb_list, &instr_routing_parse_end_link);
141 surfxml_add_callback(STag_surfxml_host_cb_list, &instr_routing_parse_start_host);
142 surfxml_add_callback(ETag_surfxml_host_cb_list, &instr_routing_parse_end_host);
143 surfxml_add_callback(STag_surfxml_router_cb_list, &instr_routing_parse_start_router);
144 surfxml_add_callback(ETag_surfxml_router_cb_list, &instr_routing_parse_end_router);
145 surfxml_add_callback(ETag_surfxml_platform_cb_list, &instr_routing_parse_end_platform);
148 static long long int newContainedId ()
150 static long long counter = 0;
154 container_t newContainer (const char *name, e_container_types kind, container_t father)
156 long long int counter = newContainedId();
157 char id_str[INSTR_DEFAULT_STR_SIZE];
158 snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", counter);
160 container_t new = xbt_new0(s_container_t, 1);
161 new->name = xbt_strdup (name); // name of the container
162 new->id = xbt_strdup (id_str); // id (or alias) of the container
163 new->father = father;
164 // level depends on level of father
166 new->level = new->father->level+1;
170 // type definition (method depends on kind of this new container)
172 if (new->kind == INSTR_AS){
173 //if this container is of an AS, its type name depends on its level
174 char as_typename[INSTR_DEFAULT_STR_SIZE];
175 snprintf (as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", new->level);
177 new->type = getContainerType (as_typename, new->father->type);
179 new->type = getContainerType ("0", NULL);
182 //otherwise, the name is its kind
184 case INSTR_HOST: new->type = getContainerType ("HOST", new->father->type); break;
185 case INSTR_LINK: new->type = getContainerType ("LINK", new->father->type); break;
186 case INSTR_ROUTER: new->type = getContainerType ("ROUTER", new->father->type); break;
187 case INSTR_SMPI: new->type = getContainerType ("MPI", new->father->type); break;
188 case INSTR_MSG_PROCESS: new->type = getContainerType ("MSG_PROCESS", new->father->type); break;
189 case INSTR_MSG_TASK: new->type = getContainerType ("MSG_TASK", new->father->type); break;
190 default: xbt_die ("Congratulations, you have found a bug on newContainer function of instr_routing.c"); break;
193 new->children = xbt_dict_new();
195 xbt_dict_set(new->father->children, new->name, new, NULL);
196 pajeCreateContainer (SIMIX_get_clock(), new->id, new->type->id, new->father->id, new->name);
199 //register hosts, routers, links containers
200 if (new->kind == INSTR_HOST || new->kind == INSTR_LINK || new->kind == INSTR_ROUTER) {
201 xbt_dict_set (allContainers, new->name, new, NULL);
204 //register the host container types
205 if (new->kind == INSTR_HOST){
206 xbt_dynar_push_as (allHostTypes, type_t, new->type);
209 //register the link container types
210 if (new->kind == INSTR_LINK){
211 xbt_dynar_push_as(allLinkTypes, type_t, new->type);
216 static container_t recursiveGetContainer (const char *name, container_t root)
218 if (strcmp (root->name, name) == 0) return root;
220 xbt_dict_cursor_t cursor = NULL;
223 xbt_dict_foreach(root->children, cursor, child_name, child) {
224 container_t ret = recursiveGetContainer(name, child);
230 container_t getContainer (const char *name)
232 return recursiveGetContainer(name, rootContainer);
235 static type_t recursiveGetType (const char *name, type_t root)
237 if (strcmp (root->name, name) == 0) return root;
239 xbt_dict_cursor_t cursor = NULL;
242 xbt_dict_foreach(root->children, cursor, child_name, child) {
243 type_t ret = recursiveGetType(name, child);
249 type_t getType (const char *name)
251 return recursiveGetType (name, rootType);
254 void destroyContainer (container_t container)
256 //remove me from my father
257 if (container->father){
258 xbt_dict_remove(container->father->children, container->name);
261 //trace my destruction
262 pajeDestroyContainer(SIMIX_get_clock(), container->type->id, container->id);
265 xbt_free (container->name);
266 xbt_free (container->id);
267 xbt_free (container->children);
268 xbt_free (container);
272 static container_t findChild (container_t root, container_t a1)
274 if (root == a1) return root;
276 xbt_dict_cursor_t cursor = NULL;
279 xbt_dict_foreach(root->children, cursor, child_name, child) {
280 if (findChild (child, a1)) return child;
285 static container_t findCommonFather (container_t root, container_t a1, container_t a2)
287 if (a1->father == a2->father) return a1->father;
289 xbt_dict_cursor_t cursor = NULL;
292 container_t a1_try = NULL;
293 container_t a2_try = NULL;
294 xbt_dict_foreach(root->children, cursor, child_name, child) {
295 a1_try = findChild (child, a1);
296 a2_try = findChild (child, a2);
297 if (a1_try && a2_try) return child;
302 static void linkContainers (const char *a1, const char *a2)
305 if (strcmp (a1, "__loopback__") == 0 || strcmp (a2, "__loopback__") == 0)
308 container_t a1_container = ((container_t)xbt_dict_get (allContainers, a1));
309 type_t a1_type = a1_container->type;
311 container_t a2_container = ((container_t)xbt_dict_get (allContainers, a2));
312 type_t a2_type = a2_container->type;
314 container_t container = findCommonFather (rootContainer, a1_container, a2_container);
315 xbt_assert0 (container != NULL, "common father not found");
318 char link_typename[INSTR_DEFAULT_STR_SIZE];
319 snprintf (link_typename, INSTR_DEFAULT_STR_SIZE, "%s-%s", a1_type->name, a2_type->name);
320 type_t link_type = getLinkType (link_typename, container->type, a1_type, a2_type);
323 static long long counter = 0;
324 char key[INSTR_DEFAULT_STR_SIZE];
325 snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter++);
326 pajeStartLink(SIMIX_get_clock(), link_type->id, container->id, "G", a1_container->id, key);
327 pajeEndLink(SIMIX_get_clock(), link_type->id, container->id, "G", a2_container->id, key);
330 static void recursiveGraphExtraction (container_t container)
332 if (xbt_dict_length(container->children)){
333 xbt_dict_cursor_t cursor = NULL;
336 //bottom-up recursion
337 xbt_dict_foreach(container->children, cursor, child_name, child) {
338 recursiveGraphExtraction (child);
342 xbt_dict_cursor_t cursor1 = NULL, cursor2 = NULL;
343 container_t child1, child2;
344 const char *child_name1, *child_name2;
346 xbt_dict_t filter = xbt_dict_new ();
348 xbt_dict_foreach(container->children, cursor1, child_name1, child1) {
349 xbt_dict_foreach(container->children, cursor2, child_name2, child2) {
350 //check if we already register this pair (we only need one direction)
351 char aux1[INSTR_DEFAULT_STR_SIZE], aux2[INSTR_DEFAULT_STR_SIZE];
352 snprintf (aux1, INSTR_DEFAULT_STR_SIZE, "%s%s", child_name1, child_name2);
353 snprintf (aux2, INSTR_DEFAULT_STR_SIZE, "%s%s", child_name2, child_name1);
354 if (xbt_dict_get_or_null (filter, aux1)) continue;
355 if (xbt_dict_get_or_null (filter, aux2)) continue;
357 //ok, not found, register it
358 xbt_dict_set (filter, aux1, xbt_strdup ("1"), xbt_free);
359 xbt_dict_set (filter, aux2, xbt_strdup ("1"), xbt_free);
361 if ((child1->kind == INSTR_HOST || child1->kind == INSTR_ROUTER) &&
362 (child2->kind == INSTR_HOST || child2->kind == INSTR_ROUTER)){
365 xbt_dynar_t route = NULL;
368 route = global_routing->get_route (child_name1, child_name2);
370 //no route between them, that's possible
374 //link the route members
377 char *previous_entity_name = (char*)child_name1;
378 xbt_dynar_foreach (route, cpt, link) {
379 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
380 linkContainers (previous_entity_name, link_name);
381 previous_entity_name = link_name;
383 linkContainers (previous_entity_name, child_name2);
384 }else if (child1->kind == INSTR_AS &&
385 child2->kind == INSTR_AS &&
386 strcmp(child_name1, child_name2) != 0){
389 routing_component_t root = global_routing->root;
390 route_extended_t route = NULL;
393 route = root->get_route (root, child_name1, child_name2);
395 //no route between them, that's possible
398 xbt_assert2(route!=NULL,
399 "there is no ASroute between %s and %s", child_name1, child_name2);
402 char *previous_entity_name = route->src_gateway;
403 xbt_dynar_foreach (route->generic_route.link_list, cpt, link) {
404 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
405 linkContainers (previous_entity_name, link_name);
406 previous_entity_name = link_name;
408 linkContainers (previous_entity_name, route->dst_gateway);
412 xbt_dict_free(&filter);
419 static void instr_routing_parse_start_AS ()
421 if (rootContainer == NULL){
422 currentContainer = xbt_dynar_new (sizeof(container_t), NULL);
423 allContainers = xbt_dict_new ();
424 allLinkTypes = xbt_dynar_new (sizeof(s_type_t), NULL);
425 allHostTypes = xbt_dynar_new (sizeof(s_type_t), NULL);
427 rootContainer = newContainer ("0", INSTR_AS, NULL);
428 xbt_dynar_push (currentContainer, &rootContainer);
430 if (TRACE_smpi_is_enabled()) {
431 if (!TRACE_smpi_is_grouped()){
432 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
433 type_t mpi = getContainerType("MPI", father->type);
434 getStateType ("MPI_STATE", mpi);
435 getLinkType ("MPI_LINK", rootType, mpi, mpi);
439 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
440 container_t new = newContainer (A_surfxml_AS_id, INSTR_AS, father);
443 xbt_dynar_push (currentContainer, &new);
446 static void instr_routing_parse_end_AS ()
448 xbt_dynar_pop_ptr (currentContainer);
451 static void instr_routing_parse_start_link ()
453 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
454 container_t new = newContainer (A_surfxml_link_id, INSTR_LINK, father);
456 type_t bandwidth = getVariableType ("bandwidth", NULL, new->type);
457 type_t latency = getVariableType ("latency", NULL, new->type);
458 pajeSetVariable (0, bandwidth->id, new->id, A_surfxml_link_bandwidth);
459 pajeSetVariable (0, latency->id, new->id, A_surfxml_link_latency);
460 if (TRACE_uncategorized()){
461 getVariableType ("bandwidth_used", "0.5 0.5 0.5", new->type);
465 static void instr_routing_parse_end_link ()
469 static void instr_routing_parse_start_host ()
471 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
472 container_t new = newContainer (A_surfxml_host_id, INSTR_HOST, father);
474 type_t power = getVariableType ("power", NULL, new->type);
475 pajeSetVariable (0, power->id, new->id, A_surfxml_host_power);
476 if (TRACE_uncategorized()){
477 getVariableType ("power_used", "0.5 0.5 0.5", new->type);
480 if (TRACE_smpi_is_enabled()) {
481 if (TRACE_smpi_is_grouped()){
482 type_t mpi = getContainerType("MPI", new->type);
483 getStateType ("MPI_STATE", mpi);
484 getLinkType ("MPI_LINK", rootType, mpi, mpi);
488 if (TRACE_msg_process_is_enabled()) {
489 type_t msg_process = getContainerType("MSG_PROCESS", new->type);
490 getStateType ("MSG_PROCESS_STATE", msg_process);
491 getLinkType ("MSG_PROCESS_LINK", rootType, msg_process, msg_process);
494 if (TRACE_msg_task_is_enabled()) {
495 type_t msg_task = getContainerType ("MSG_TASK", new->type);
496 getStateType ("MSG_TASK_STATE", msg_task);
497 getLinkType ("MSG_TASK_LINK", rootType, msg_task, msg_task);
501 static void instr_routing_parse_end_host ()
505 static void instr_routing_parse_start_router ()
507 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
508 newContainer (A_surfxml_router_id, INSTR_ROUTER, father);
511 static void instr_routing_parse_end_router ()
515 static void instr_routing_parse_end_platform ()
517 xbt_dynar_free(¤tContainer);
518 currentContainer = NULL;
519 recursiveGraphExtraction (rootContainer);
520 platform_created = 1;
526 int instr_link_is_traced (const char *name)
528 if (((container_t)xbt_dict_get_or_null (allContainers, name))){
535 char *instr_variable_type (const char *name, const char *resource)
537 container_t container = (container_t)xbt_dict_get (allContainers, resource);
538 xbt_dict_cursor_t cursor = NULL;
541 xbt_dict_foreach(container->type->children, cursor, type_name, type) {
542 if (strcmp (name, type->name) == 0) return type->id;
547 char *instr_resource_type (const char *resource_name)
549 return ((container_t)xbt_dict_get_or_null (allContainers, resource_name))->id;
552 static void recursiveDestroyContainer (container_t container)
554 xbt_dict_cursor_t cursor = NULL;
557 xbt_dict_foreach(container->children, cursor, child_name, child) {
558 recursiveDestroyContainer (child);
560 destroyContainer (container);
563 static void recursiveDestroyType (type_t type)
565 xbt_dict_cursor_t cursor = NULL;
568 xbt_dict_foreach(type->children, cursor, child_name, child) {
569 recursiveDestroyType (child);
571 xbt_free (type->name);
573 xbt_free (type->children);
578 void instr_destroy_platform ()
580 if (rootContainer) recursiveDestroyContainer (rootContainer);
581 if (rootType) recursiveDestroyType (rootType);
585 * user categories support
587 static void recursiveNewUserVariableType (const char *new_typename, const char *color, type_t root)
589 if (!strcmp (root->name, "HOST") || !strcmp (root->name, "LINK")){
590 getVariableType(new_typename, color, root);
592 xbt_dict_cursor_t cursor = NULL;
595 xbt_dict_foreach(root->children, cursor, name, child_type) {
596 recursiveNewUserVariableType (new_typename, color, child_type);
600 void instr_new_user_variable_type (const char *new_typename, const char *color)
602 recursiveNewUserVariableType (new_typename, color, rootType);
605 static void recursiveNewUserLinkVariableType (const char *new_typename, const char *color, type_t root)
607 if (!strcmp (root->name, "LINK")){
608 getVariableType(new_typename, color, root);
610 xbt_dict_cursor_t cursor = NULL;
613 xbt_dict_foreach(root->children, cursor, name, child_type) {
614 recursiveNewUserLinkVariableType (new_typename, color, child_type);
618 void instr_new_user_link_variable_type (const char *new_typename, const char *color)
620 recursiveNewUserLinkVariableType (new_typename, color, rootType);
624 static void recursiveNewUserHostVariableType (const char *new_typename, const char *color, type_t root)
626 if (!strcmp (root->name, "HOST")){
627 getVariableType(new_typename, color, root);
629 xbt_dict_cursor_t cursor = NULL;
632 xbt_dict_foreach(root->children, cursor, name, child_type) {
633 recursiveNewUserHostVariableType (new_typename, color, child_type);
637 void instr_new_user_host_variable_type (const char *new_typename, const char *color)
639 recursiveNewUserHostVariableType (new_typename, color, rootType);
642 int instr_platform_traced ()
644 return platform_created;
647 #endif /* HAVE_TRACING */