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, xbt_dict_t filter)
53 if (strcmp (a1, "__loopback__") == 0 || strcmp (a2, "__loopback__") == 0)
56 //check if we already register this pair (we only need one direction)
57 char aux1[INSTR_DEFAULT_STR_SIZE], aux2[INSTR_DEFAULT_STR_SIZE];
58 snprintf (aux1, INSTR_DEFAULT_STR_SIZE, "%s%s", a1, a2);
59 snprintf (aux2, INSTR_DEFAULT_STR_SIZE, "%s%s", a2, a1);
60 if (xbt_dict_get_or_null (filter, aux1)) return;
61 if (xbt_dict_get_or_null (filter, aux2)) return;
63 //ok, not found, register it
64 xbt_dict_set (filter, aux1, xbt_strdup ("1"), xbt_free);
65 xbt_dict_set (filter, aux2, xbt_strdup ("1"), xbt_free);
67 container_t a1_container = getContainerByName (a1);
68 type_t a1_type = a1_container->type;
70 container_t a2_container = getContainerByName (a2);
71 type_t a2_type = a2_container->type;
73 container_t container = findCommonFather (getRootContainer(), a1_container, a2_container);
74 xbt_assert (container != NULL, "common father not found");
77 char link_typename[INSTR_DEFAULT_STR_SIZE];
78 snprintf (link_typename, INSTR_DEFAULT_STR_SIZE, "%s-%s", a1_type->name, a2_type->name);
79 type_t link_type = getLinkType (link_typename, container->type, a1_type, a2_type);
81 //register EDGE types for triva configuration
82 xbt_dict_set (trivaEdgeTypes, link_type->name, xbt_strdup("1"), xbt_free);
85 static long long counter = 0;
86 char key[INSTR_DEFAULT_STR_SIZE];
87 snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter++);
88 new_pajeStartLink(SIMIX_get_clock(), container, link_type, a1_container, "G", key);
89 new_pajeEndLink(SIMIX_get_clock(), container, link_type, a2_container, "G", key);
92 static void recursiveGraphExtraction (container_t container, xbt_dict_t filter)
94 if (xbt_dict_length(container->children)){
95 xbt_dict_cursor_t cursor = NULL;
99 xbt_dict_foreach(container->children, cursor, child_name, child) {
100 recursiveGraphExtraction (child, filter);
104 xbt_dict_cursor_t cursor1 = NULL, cursor2 = NULL;
105 container_t child1, child2;
106 const char *child_name1, *child_name2;
108 xbt_dict_foreach(container->children, cursor1, child_name1, child1) {
109 xbt_dict_foreach(container->children, cursor2, child_name2, child2) {
110 if ((child1->kind == INSTR_HOST || child1->kind == INSTR_ROUTER) &&
111 (child2->kind == INSTR_HOST || child2->kind == INSTR_ROUTER)){
114 xbt_dynar_t route = NULL;
117 route = global_routing->get_route (child_name1, child_name2);
119 //no route between them, that's possible
123 //link the route members
126 char *previous_entity_name = (char*)child_name1;
127 xbt_dynar_foreach (route, cpt, link) {
128 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
129 linkContainers (previous_entity_name, link_name, filter);
130 previous_entity_name = link_name;
132 linkContainers (previous_entity_name, child_name2, filter);
133 }else if (child1->kind == INSTR_AS &&
134 child2->kind == INSTR_AS &&
135 strcmp(child_name1, child_name2) != 0){
138 routing_component_t root = global_routing->root;
139 route_extended_t route = NULL;
142 route = root->get_route (root, child_name1, child_name2);
144 //no route between them, that's possible
147 xbt_assert(route!=NULL,
148 "there is no ASroute between %s and %s", child_name1, child_name2);
151 char *previous_entity_name = route->src_gateway;
152 xbt_dynar_foreach (route->generic_route.link_list, cpt, link) {
153 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
154 linkContainers (previous_entity_name, link_name, filter);
155 previous_entity_name = link_name;
157 linkContainers (previous_entity_name, route->dst_gateway, 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 new_pajeSetVariable (0, new, bandwidth, atof(A_surfxml_link_bandwidth));
205 new_pajeSetVariable (0, new, latency, atof(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 new_pajeSetVariable (0, new, power, atof(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 type_t state = getStateType ("MSG_PROCESS_STATE", msg_process);
237 getValue ("executing", "0 1 0", state);
238 getValue ("suspend", "1 0 1", state);
239 getValue ("sleep", "1 1 0", state);
240 getValue ("receive", "1 0 0", state);
241 getValue ("send", "0 0 1", state);
242 getValue ("task_execute", "0 1 1", state);
243 getLinkType ("MSG_PROCESS_LINK", getRootType(), msg_process, msg_process);
244 getLinkType ("MSG_PROCESS_TASK_LINK", getRootType(), msg_process, msg_process);
247 if (TRACE_msg_task_is_enabled()) {
248 type_t msg_task = getContainerType ("MSG_TASK", new->type);
249 type_t state = getStateType ("MSG_TASK_STATE", msg_task);
250 getValue ("MSG_task_execute", "0 1 0", state);
251 getValue ("created", "1 1 0", state);
252 getLinkType ("MSG_TASK_LINK", getRootType(), msg_task, msg_task);
256 static void instr_routing_parse_end_host ()
260 static void instr_routing_parse_start_router ()
262 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
263 newContainer (A_surfxml_router_id, INSTR_ROUTER, father);
266 static void instr_routing_parse_end_router ()
270 static void instr_routing_parse_end_platform ()
272 xbt_dynar_free(¤tContainer);
273 currentContainer = NULL;
274 xbt_dict_t filter = xbt_dict_new ();
275 recursiveGraphExtraction (getRootContainer(), filter);
276 xbt_dict_free(&filter);
277 platform_created = 1;
278 TRACE_paje_dump_buffer(1);
281 void instr_routing_define_callbacks ()
283 if (!TRACE_is_active())
285 surfxml_add_callback(STag_surfxml_AS_cb_list, &instr_routing_parse_start_AS);
286 surfxml_add_callback(ETag_surfxml_AS_cb_list, &instr_routing_parse_end_AS);
287 surfxml_add_callback(STag_surfxml_link_cb_list, &instr_routing_parse_start_link);
288 surfxml_add_callback(ETag_surfxml_link_cb_list, &instr_routing_parse_end_link);
289 surfxml_add_callback(STag_surfxml_host_cb_list, &instr_routing_parse_start_host);
290 surfxml_add_callback(ETag_surfxml_host_cb_list, &instr_routing_parse_end_host);
291 surfxml_add_callback(STag_surfxml_router_cb_list, &instr_routing_parse_start_router);
292 surfxml_add_callback(ETag_surfxml_router_cb_list, &instr_routing_parse_end_router);
293 surfxml_add_callback(ETag_surfxml_platform_cb_list, &instr_routing_parse_end_platform);
297 * user categories support
299 static void recursiveNewUserVariableType (const char *new_typename, const char *color, type_t root)
301 if (!strcmp (root->name, "HOST") || !strcmp (root->name, "LINK")){
302 getVariableType(new_typename, color, root);
304 xbt_dict_cursor_t cursor = NULL;
307 xbt_dict_foreach(root->children, cursor, name, child_type) {
308 recursiveNewUserVariableType (new_typename, color, child_type);
312 void instr_new_user_variable_type (const char *new_typename, const char *color)
314 recursiveNewUserVariableType (new_typename, color, getRootType());
317 static void recursiveNewUserLinkVariableType (const char *new_typename, const char *color, type_t root)
319 if (!strcmp (root->name, "LINK")){
320 getVariableType(new_typename, color, root);
322 xbt_dict_cursor_t cursor = NULL;
325 xbt_dict_foreach(root->children, cursor, name, child_type) {
326 recursiveNewUserLinkVariableType (new_typename, color, child_type);
330 void instr_new_user_link_variable_type (const char *new_typename, const char *color)
332 recursiveNewUserLinkVariableType (new_typename, color, getRootType());
336 static void recursiveNewUserHostVariableType (const char *new_typename, const char *color, type_t root)
338 if (!strcmp (root->name, "HOST")){
339 getVariableType(new_typename, color, root);
341 xbt_dict_cursor_t cursor = NULL;
344 xbt_dict_foreach(root->children, cursor, name, child_type) {
345 recursiveNewUserHostVariableType (new_typename, color, child_type);
349 void instr_new_user_host_variable_type (const char *new_typename, const char *color)
351 recursiveNewUserHostVariableType (new_typename, color, getRootType());
354 int instr_platform_traced ()
356 return platform_created;
359 #endif /* HAVE_TRACING */