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);
70 //register EDGE types for triva configuration
71 xbt_dict_set (trivaEdgeTypes, link_type->name, xbt_strdup("1"), xbt_free);
74 static long long counter = 0;
75 char key[INSTR_DEFAULT_STR_SIZE];
76 snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter++);
77 new_pajeStartLink(SIMIX_get_clock(), container, link_type, a1_container, "G", key);
78 new_pajeEndLink(SIMIX_get_clock(), container, link_type, a2_container, "G", key);
81 static void recursiveGraphExtraction (container_t container)
83 if (xbt_dict_length(container->children)){
84 xbt_dict_cursor_t cursor = NULL;
88 xbt_dict_foreach(container->children, cursor, child_name, child) {
89 recursiveGraphExtraction (child);
93 xbt_dict_cursor_t cursor1 = NULL, cursor2 = NULL;
94 container_t child1, child2;
95 const char *child_name1, *child_name2;
97 xbt_dict_t filter = xbt_dict_new ();
99 xbt_dict_foreach(container->children, cursor1, child_name1, child1) {
100 xbt_dict_foreach(container->children, cursor2, child_name2, child2) {
101 //check if we already register this pair (we only need one direction)
102 char aux1[INSTR_DEFAULT_STR_SIZE], aux2[INSTR_DEFAULT_STR_SIZE];
103 snprintf (aux1, INSTR_DEFAULT_STR_SIZE, "%s%s", child_name1, child_name2);
104 snprintf (aux2, INSTR_DEFAULT_STR_SIZE, "%s%s", child_name2, child_name1);
105 if (xbt_dict_get_or_null (filter, aux1)) continue;
106 if (xbt_dict_get_or_null (filter, aux2)) continue;
108 //ok, not found, register it
109 xbt_dict_set (filter, aux1, xbt_strdup ("1"), xbt_free);
110 xbt_dict_set (filter, aux2, xbt_strdup ("1"), xbt_free);
112 if ((child1->kind == INSTR_HOST || child1->kind == INSTR_ROUTER) &&
113 (child2->kind == INSTR_HOST || child2->kind == INSTR_ROUTER)){
116 xbt_dynar_t route = NULL;
119 route = global_routing->get_route (child_name1, child_name2);
121 //no route between them, that's possible
125 //link the route members
128 char *previous_entity_name = (char*)child_name1;
129 xbt_dynar_foreach (route, cpt, link) {
130 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
131 linkContainers (previous_entity_name, link_name);
132 previous_entity_name = link_name;
134 linkContainers (previous_entity_name, child_name2);
135 }else if (child1->kind == INSTR_AS &&
136 child2->kind == INSTR_AS &&
137 strcmp(child_name1, child_name2) != 0){
140 routing_component_t root = global_routing->root;
141 route_extended_t route = NULL;
144 route = root->get_route (root, child_name1, child_name2);
146 //no route between them, that's possible
149 xbt_assert2(route!=NULL,
150 "there is no ASroute between %s and %s", child_name1, child_name2);
153 char *previous_entity_name = route->src_gateway;
154 xbt_dynar_foreach (route->generic_route.link_list, cpt, link) {
155 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
156 linkContainers (previous_entity_name, link_name);
157 previous_entity_name = link_name;
159 linkContainers (previous_entity_name, route->dst_gateway);
163 xbt_dict_free(&filter);
170 static void instr_routing_parse_start_AS ()
172 if (getRootContainer() == NULL){
173 container_t root = newContainer ("0", INSTR_AS, NULL);
174 instr_paje_init (root);
176 currentContainer = xbt_dynar_new (sizeof(container_t), NULL);
177 xbt_dynar_push (currentContainer, &root);
179 if (TRACE_smpi_is_enabled()) {
180 if (!TRACE_smpi_is_grouped()){
181 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
182 type_t mpi = getContainerType("MPI", father->type);
183 getStateType ("MPI_STATE", mpi);
184 getLinkType ("MPI_LINK", getRootType(), mpi, mpi);
188 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
189 container_t new = newContainer (A_surfxml_AS_id, INSTR_AS, father);
192 xbt_dynar_push (currentContainer, &new);
195 static void instr_routing_parse_end_AS ()
197 xbt_dynar_pop_ptr (currentContainer);
200 static void instr_routing_parse_start_link ()
202 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
203 container_t new = newContainer (A_surfxml_link_id, INSTR_LINK, father);
205 type_t bandwidth = getVariableType ("bandwidth", NULL, new->type);
206 type_t latency = getVariableType ("latency", NULL, new->type);
207 new_pajeSetVariable (0, new, bandwidth, atof(A_surfxml_link_bandwidth));
208 new_pajeSetVariable (0, new, latency, atof(A_surfxml_link_latency));
209 if (TRACE_uncategorized()){
210 getVariableType ("bandwidth_used", "0.5 0.5 0.5", new->type);
214 static void instr_routing_parse_end_link ()
218 static void instr_routing_parse_start_host ()
220 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
221 container_t new = newContainer (A_surfxml_host_id, INSTR_HOST, father);
223 type_t power = getVariableType ("power", NULL, new->type);
224 new_pajeSetVariable (0, new, power, atof(A_surfxml_host_power));
225 if (TRACE_uncategorized()){
226 getVariableType ("power_used", "0.5 0.5 0.5", new->type);
229 if (TRACE_smpi_is_enabled()) {
230 if (TRACE_smpi_is_grouped()){
231 type_t mpi = getContainerType("MPI", new->type);
232 getStateType ("MPI_STATE", mpi);
233 getLinkType ("MPI_LINK", getRootType(), mpi, mpi);
237 if (TRACE_msg_process_is_enabled()) {
238 type_t msg_process = getContainerType("MSG_PROCESS", new->type);
239 getStateType ("MSG_PROCESS_STATE", msg_process);
240 getLinkType ("MSG_PROCESS_LINK", getRootType(), msg_process, msg_process);
243 if (TRACE_msg_task_is_enabled()) {
244 type_t msg_task = getContainerType ("MSG_TASK", new->type);
245 getStateType ("MSG_TASK_STATE", msg_task);
246 getLinkType ("MSG_TASK_LINK", getRootType(), msg_task, msg_task);
250 static void instr_routing_parse_end_host ()
254 static void instr_routing_parse_start_router ()
256 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
257 newContainer (A_surfxml_router_id, INSTR_ROUTER, father);
260 static void instr_routing_parse_end_router ()
264 static void instr_routing_parse_end_platform ()
266 xbt_dynar_free(¤tContainer);
267 currentContainer = NULL;
268 recursiveGraphExtraction (getRootContainer());
269 platform_created = 1;
270 TRACE_paje_dump_buffer(1);
273 void instr_routing_define_callbacks ()
275 if (!TRACE_is_active())
277 surfxml_add_callback(STag_surfxml_AS_cb_list, &instr_routing_parse_start_AS);
278 surfxml_add_callback(ETag_surfxml_AS_cb_list, &instr_routing_parse_end_AS);
279 surfxml_add_callback(STag_surfxml_link_cb_list, &instr_routing_parse_start_link);
280 surfxml_add_callback(ETag_surfxml_link_cb_list, &instr_routing_parse_end_link);
281 surfxml_add_callback(STag_surfxml_host_cb_list, &instr_routing_parse_start_host);
282 surfxml_add_callback(ETag_surfxml_host_cb_list, &instr_routing_parse_end_host);
283 surfxml_add_callback(STag_surfxml_router_cb_list, &instr_routing_parse_start_router);
284 surfxml_add_callback(ETag_surfxml_router_cb_list, &instr_routing_parse_end_router);
285 surfxml_add_callback(ETag_surfxml_platform_cb_list, &instr_routing_parse_end_platform);
289 * user categories support
291 static void recursiveNewUserVariableType (const char *new_typename, const char *color, type_t root)
293 if (!strcmp (root->name, "HOST") || !strcmp (root->name, "LINK")){
294 getVariableType(new_typename, color, root);
296 xbt_dict_cursor_t cursor = NULL;
299 xbt_dict_foreach(root->children, cursor, name, child_type) {
300 recursiveNewUserVariableType (new_typename, color, child_type);
304 void instr_new_user_variable_type (const char *new_typename, const char *color)
306 recursiveNewUserVariableType (new_typename, color, getRootType());
309 static void recursiveNewUserLinkVariableType (const char *new_typename, const char *color, type_t root)
311 if (!strcmp (root->name, "LINK")){
312 getVariableType(new_typename, color, root);
314 xbt_dict_cursor_t cursor = NULL;
317 xbt_dict_foreach(root->children, cursor, name, child_type) {
318 recursiveNewUserLinkVariableType (new_typename, color, child_type);
322 void instr_new_user_link_variable_type (const char *new_typename, const char *color)
324 recursiveNewUserLinkVariableType (new_typename, color, getRootType());
328 static void recursiveNewUserHostVariableType (const char *new_typename, const char *color, type_t root)
330 if (!strcmp (root->name, "HOST")){
331 getVariableType(new_typename, color, root);
333 xbt_dict_cursor_t cursor = NULL;
336 xbt_dict_foreach(root->children, cursor, name, child_type) {
337 recursiveNewUserHostVariableType (new_typename, color, child_type);
341 void instr_new_user_host_variable_type (const char *new_typename, const char *color)
343 recursiveNewUserHostVariableType (new_typename, color, getRootType());
346 int instr_platform_traced ()
348 return platform_created;
351 #endif /* HAVE_TRACING */