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 xbt_dynar_t currentContainer = NULL; /* push and pop, used only in creation */
20 static container_t findChild (container_t root, container_t a1)
22 if (root == a1) return root;
24 xbt_dict_cursor_t cursor = NULL;
27 xbt_dict_foreach(root->children, cursor, child_name, child) {
28 if (findChild (child, a1)) return child;
33 static container_t findCommonFather (container_t root, container_t a1, container_t a2)
35 if (a1->father == a2->father) return a1->father;
37 xbt_dict_cursor_t cursor = NULL;
40 container_t a1_try = NULL;
41 container_t a2_try = NULL;
42 xbt_dict_foreach(root->children, cursor, child_name, child) {
43 a1_try = findChild (child, a1);
44 a2_try = findChild (child, a2);
45 if (a1_try && a2_try) return child;
50 static void linkContainers (const char *a1, const char *a2)
53 if (strcmp (a1, "__loopback__") == 0 || strcmp (a2, "__loopback__") == 0)
56 container_t a1_container = getContainerByName (a1);
57 type_t a1_type = a1_container->type;
59 container_t a2_container = getContainerByName (a2);
60 type_t a2_type = a2_container->type;
62 container_t container = findCommonFather (getRootContainer(), a1_container, a2_container);
63 xbt_assert0 (container != NULL, "common father not found");
66 char link_typename[INSTR_DEFAULT_STR_SIZE];
67 snprintf (link_typename, INSTR_DEFAULT_STR_SIZE, "%s-%s", a1_type->name, a2_type->name);
68 type_t link_type = getLinkType (link_typename, container->type, a1_type, a2_type);
71 static long long counter = 0;
72 char key[INSTR_DEFAULT_STR_SIZE];
73 snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter++);
74 pajeStartLink(SIMIX_get_clock(), link_type->id, container->id, "G", a1_container->id, key);
75 pajeEndLink(SIMIX_get_clock(), link_type->id, container->id, "G", a2_container->id, key);
78 static void recursiveGraphExtraction (container_t container)
80 if (xbt_dict_length(container->children)){
81 xbt_dict_cursor_t cursor = NULL;
85 xbt_dict_foreach(container->children, cursor, child_name, child) {
86 recursiveGraphExtraction (child);
90 xbt_dict_cursor_t cursor1 = NULL, cursor2 = NULL;
91 container_t child1, child2;
92 const char *child_name1, *child_name2;
94 xbt_dict_t filter = xbt_dict_new ();
96 xbt_dict_foreach(container->children, cursor1, child_name1, child1) {
97 xbt_dict_foreach(container->children, cursor2, child_name2, child2) {
98 //check if we already register this pair (we only need one direction)
99 char aux1[INSTR_DEFAULT_STR_SIZE], aux2[INSTR_DEFAULT_STR_SIZE];
100 snprintf (aux1, INSTR_DEFAULT_STR_SIZE, "%s%s", child_name1, child_name2);
101 snprintf (aux2, INSTR_DEFAULT_STR_SIZE, "%s%s", child_name2, child_name1);
102 if (xbt_dict_get_or_null (filter, aux1)) continue;
103 if (xbt_dict_get_or_null (filter, aux2)) continue;
105 //ok, not found, register it
106 xbt_dict_set (filter, aux1, xbt_strdup ("1"), xbt_free);
107 xbt_dict_set (filter, aux2, xbt_strdup ("1"), xbt_free);
109 if ((child1->kind == INSTR_HOST || child1->kind == INSTR_ROUTER) &&
110 (child2->kind == INSTR_HOST || child2->kind == INSTR_ROUTER)){
113 xbt_dynar_t route = NULL;
116 route = global_routing->get_route (child_name1, child_name2);
118 //no route between them, that's possible
122 //link the route members
125 char *previous_entity_name = (char*)child_name1;
126 xbt_dynar_foreach (route, cpt, link) {
127 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
128 linkContainers (previous_entity_name, link_name);
129 previous_entity_name = link_name;
131 linkContainers (previous_entity_name, child_name2);
132 }else if (child1->kind == INSTR_AS &&
133 child2->kind == INSTR_AS &&
134 strcmp(child_name1, child_name2) != 0){
137 routing_component_t root = global_routing->root;
138 route_extended_t route = NULL;
141 route = root->get_route (root, child_name1, child_name2);
143 //no route between them, that's possible
146 xbt_assert2(route!=NULL,
147 "there is no ASroute between %s and %s", child_name1, child_name2);
150 char *previous_entity_name = route->src_gateway;
151 xbt_dynar_foreach (route->generic_route.link_list, cpt, link) {
152 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
153 linkContainers (previous_entity_name, link_name);
154 previous_entity_name = link_name;
156 linkContainers (previous_entity_name, route->dst_gateway);
160 xbt_dict_free(&filter);
167 static void instr_routing_parse_start_AS ()
169 if (getRootContainer() == NULL){
170 container_t root = newContainer ("0", INSTR_AS, NULL);
171 instr_paje_init (root);
173 currentContainer = xbt_dynar_new (sizeof(container_t), NULL);
174 xbt_dynar_push (currentContainer, &root);
176 if (TRACE_smpi_is_enabled()) {
177 if (!TRACE_smpi_is_grouped()){
178 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
179 type_t mpi = getContainerType("MPI", father->type);
180 getStateType ("MPI_STATE", mpi);
181 getLinkType ("MPI_LINK", getRootType(), mpi, mpi);
185 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
186 container_t new = newContainer (A_surfxml_AS_id, INSTR_AS, father);
189 xbt_dynar_push (currentContainer, &new);
192 static void instr_routing_parse_end_AS ()
194 xbt_dynar_pop_ptr (currentContainer);
197 static void instr_routing_parse_start_link ()
199 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
200 container_t new = newContainer (A_surfxml_link_id, INSTR_LINK, father);
202 type_t bandwidth = getVariableType ("bandwidth", NULL, new->type);
203 type_t latency = getVariableType ("latency", NULL, new->type);
204 pajeSetVariable (0, bandwidth->id, new->id, A_surfxml_link_bandwidth);
205 pajeSetVariable (0, latency->id, new->id, A_surfxml_link_latency);
206 if (TRACE_uncategorized()){
207 getVariableType ("bandwidth_used", "0.5 0.5 0.5", new->type);
211 static void instr_routing_parse_end_link ()
215 static void instr_routing_parse_start_host ()
217 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
218 container_t new = newContainer (A_surfxml_host_id, INSTR_HOST, father);
220 type_t power = getVariableType ("power", NULL, new->type);
221 pajeSetVariable (0, power->id, new->id, A_surfxml_host_power);
222 if (TRACE_uncategorized()){
223 getVariableType ("power_used", "0.5 0.5 0.5", new->type);
226 if (TRACE_smpi_is_enabled()) {
227 if (TRACE_smpi_is_grouped()){
228 type_t mpi = getContainerType("MPI", new->type);
229 getStateType ("MPI_STATE", mpi);
230 getLinkType ("MPI_LINK", getRootType(), mpi, mpi);
234 if (TRACE_msg_process_is_enabled()) {
235 type_t msg_process = getContainerType("MSG_PROCESS", new->type);
236 getStateType ("MSG_PROCESS_STATE", msg_process);
237 getLinkType ("MSG_PROCESS_LINK", getRootType(), msg_process, msg_process);
240 if (TRACE_msg_task_is_enabled()) {
241 type_t msg_task = getContainerType ("MSG_TASK", new->type);
242 getStateType ("MSG_TASK_STATE", msg_task);
243 getLinkType ("MSG_TASK_LINK", getRootType(), msg_task, msg_task);
247 static void instr_routing_parse_end_host ()
251 static void instr_routing_parse_start_router ()
253 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
254 newContainer (A_surfxml_router_id, INSTR_ROUTER, father);
257 static void instr_routing_parse_end_router ()
261 static void instr_routing_parse_end_platform ()
263 xbt_dynar_free(¤tContainer);
264 currentContainer = NULL;
265 recursiveGraphExtraction (getRootContainer());
266 platform_created = 1;
269 void instr_routing_define_callbacks ()
271 if (!TRACE_is_active())
273 surfxml_add_callback(STag_surfxml_AS_cb_list, &instr_routing_parse_start_AS);
274 surfxml_add_callback(ETag_surfxml_AS_cb_list, &instr_routing_parse_end_AS);
275 surfxml_add_callback(STag_surfxml_link_cb_list, &instr_routing_parse_start_link);
276 surfxml_add_callback(ETag_surfxml_link_cb_list, &instr_routing_parse_end_link);
277 surfxml_add_callback(STag_surfxml_host_cb_list, &instr_routing_parse_start_host);
278 surfxml_add_callback(ETag_surfxml_host_cb_list, &instr_routing_parse_end_host);
279 surfxml_add_callback(STag_surfxml_router_cb_list, &instr_routing_parse_start_router);
280 surfxml_add_callback(ETag_surfxml_router_cb_list, &instr_routing_parse_end_router);
281 surfxml_add_callback(ETag_surfxml_platform_cb_list, &instr_routing_parse_end_platform);
285 * user categories support
287 static void recursiveNewUserVariableType (const char *new_typename, const char *color, type_t root)
289 if (!strcmp (root->name, "HOST") || !strcmp (root->name, "LINK")){
290 getVariableType(new_typename, color, root);
292 xbt_dict_cursor_t cursor = NULL;
295 xbt_dict_foreach(root->children, cursor, name, child_type) {
296 recursiveNewUserVariableType (new_typename, color, child_type);
300 void instr_new_user_variable_type (const char *new_typename, const char *color)
302 recursiveNewUserVariableType (new_typename, color, getRootType());
305 static void recursiveNewUserLinkVariableType (const char *new_typename, const char *color, type_t root)
307 if (!strcmp (root->name, "LINK")){
308 getVariableType(new_typename, color, root);
310 xbt_dict_cursor_t cursor = NULL;
313 xbt_dict_foreach(root->children, cursor, name, child_type) {
314 recursiveNewUserLinkVariableType (new_typename, color, child_type);
318 void instr_new_user_link_variable_type (const char *new_typename, const char *color)
320 recursiveNewUserLinkVariableType (new_typename, color, getRootType());
324 static void recursiveNewUserHostVariableType (const char *new_typename, const char *color, type_t root)
326 if (!strcmp (root->name, "HOST")){
327 getVariableType(new_typename, color, root);
329 xbt_dict_cursor_t cursor = NULL;
332 xbt_dict_foreach(root->children, cursor, name, child_type) {
333 recursiveNewUserHostVariableType (new_typename, color, child_type);
337 void instr_new_user_host_variable_type (const char *new_typename, const char *color)
339 recursiveNewUserHostVariableType (new_typename, color, getRootType());
342 int instr_platform_traced ()
344 return platform_created;
347 #endif /* HAVE_TRACING */