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 (A_surfxml_AS_id, 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);
187 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
188 container_t new = newContainer (A_surfxml_AS_id, INSTR_AS, father);
191 xbt_dynar_push (currentContainer, &new);
194 static void instr_routing_parse_end_AS ()
196 xbt_dynar_pop_ptr (currentContainer);
199 static void instr_routing_parse_start_link ()
201 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
202 const char *link_id = A_surfxml_link_id;
204 double bandwidth_value = atof(A_surfxml_link_bandwidth);
205 double latency_value = atof(A_surfxml_link_latency);
206 xbt_dynar_t links_to_create = xbt_dynar_new (sizeof(char*), &xbt_free_ref);
208 if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX){
209 char *up = bprintf("%s_UP", link_id);
210 char *down = bprintf("%s_DOWN", link_id);
211 xbt_dynar_push_as (links_to_create, char*, xbt_strdup(up));
212 xbt_dynar_push_as (links_to_create, char*, xbt_strdup(down));
216 xbt_dynar_push_as (links_to_create, char*, strdup(link_id));
219 char *link_name = NULL;
221 xbt_dynar_foreach (links_to_create, i, link_name){
223 container_t new = newContainer (link_name, INSTR_LINK, father);
225 type_t bandwidth = getVariableType ("bandwidth", NULL, new->type);
226 type_t latency = getVariableType ("latency", NULL, new->type);
227 new_pajeSetVariable (0, new, bandwidth, bandwidth_value);
228 new_pajeSetVariable (0, new, latency, latency_value);
229 if (TRACE_uncategorized()){
230 getVariableType ("bandwidth_used", "0.5 0.5 0.5", new->type);
234 xbt_dynar_free (&links_to_create);
237 static void instr_routing_parse_end_link ()
241 static void instr_routing_parse_start_host ()
243 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
244 container_t new = newContainer (A_surfxml_host_id, INSTR_HOST, father);
246 type_t power = getVariableType ("power", NULL, new->type);
247 new_pajeSetVariable (0, new, power, atof(A_surfxml_host_power));
248 if (TRACE_uncategorized()){
249 getVariableType ("power_used", "0.5 0.5 0.5", new->type);
252 if (TRACE_smpi_is_enabled()) {
253 if (TRACE_smpi_is_grouped()){
254 type_t mpi = getContainerType("MPI", new->type);
255 getStateType ("MPI_STATE", mpi);
256 getLinkType ("MPI_LINK", getRootType(), mpi, mpi);
260 if (TRACE_msg_process_is_enabled()) {
261 type_t msg_process = getContainerType("MSG_PROCESS", new->type);
262 type_t state = getStateType ("MSG_PROCESS_STATE", msg_process);
263 getValue ("executing", "0 1 0", state);
264 getValue ("suspend", "1 0 1", state);
265 getValue ("sleep", "1 1 0", state);
266 getValue ("receive", "1 0 0", state);
267 getValue ("send", "0 0 1", state);
268 getValue ("task_execute", "0 1 1", state);
269 getLinkType ("MSG_PROCESS_LINK", getRootType(), msg_process, msg_process);
270 getLinkType ("MSG_PROCESS_TASK_LINK", getRootType(), msg_process, msg_process);
273 if (TRACE_msg_task_is_enabled()) {
274 type_t msg_task = getContainerType ("MSG_TASK", new->type);
275 type_t state = getStateType ("MSG_TASK_STATE", msg_task);
276 getValue ("MSG_task_execute", "0 1 0", state);
277 getValue ("created", "1 1 0", state);
278 getLinkType ("MSG_TASK_LINK", getRootType(), msg_task, msg_task);
282 static void instr_routing_parse_end_host ()
286 static void instr_routing_parse_start_router ()
288 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
289 newContainer (A_surfxml_router_id, INSTR_ROUTER, father);
292 static void instr_routing_parse_end_router ()
296 static void instr_routing_parse_end_platform ()
298 xbt_dynar_free(¤tContainer);
299 currentContainer = NULL;
300 xbt_dict_t filter = xbt_dict_new ();
301 recursiveGraphExtraction (getRootContainer(), filter);
302 xbt_dict_free(&filter);
303 platform_created = 1;
304 TRACE_paje_dump_buffer(1);
307 void instr_routing_define_callbacks ()
309 if (!TRACE_is_active())
311 surfxml_add_callback(STag_surfxml_AS_cb_list, &instr_routing_parse_start_AS);
312 surfxml_add_callback(ETag_surfxml_AS_cb_list, &instr_routing_parse_end_AS);
313 surfxml_add_callback(STag_surfxml_link_cb_list, &instr_routing_parse_start_link);
314 surfxml_add_callback(ETag_surfxml_link_cb_list, &instr_routing_parse_end_link);
315 surfxml_add_callback(STag_surfxml_host_cb_list, &instr_routing_parse_start_host);
316 surfxml_add_callback(ETag_surfxml_host_cb_list, &instr_routing_parse_end_host);
317 surfxml_add_callback(STag_surfxml_router_cb_list, &instr_routing_parse_start_router);
318 surfxml_add_callback(ETag_surfxml_router_cb_list, &instr_routing_parse_end_router);
319 surfxml_add_callback(ETag_surfxml_platform_cb_list, &instr_routing_parse_end_platform);
323 * user categories support
325 static void recursiveNewUserVariableType (const char *new_typename, const char *color, type_t root)
327 if (!strcmp (root->name, "HOST")){
328 char tnstr[INSTR_DEFAULT_STR_SIZE];
329 snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "p%s", new_typename);
330 getVariableType(tnstr, color, root);
332 if (!strcmp (root->name, "LINK")){
333 char tnstr[INSTR_DEFAULT_STR_SIZE];
334 snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "b%s", new_typename);
335 getVariableType(tnstr, color, root);
337 xbt_dict_cursor_t cursor = NULL;
340 xbt_dict_foreach(root->children, cursor, name, child_type) {
341 recursiveNewUserVariableType (new_typename, color, child_type);
345 void instr_new_user_variable_type (const char *new_typename, const char *color)
347 recursiveNewUserVariableType (new_typename, color, getRootType());
350 static void recursiveNewUserLinkVariableType (const char *new_typename, const char *color, type_t root)
352 if (!strcmp (root->name, "LINK")){
353 getVariableType(new_typename, color, root);
355 xbt_dict_cursor_t cursor = NULL;
358 xbt_dict_foreach(root->children, cursor, name, child_type) {
359 recursiveNewUserLinkVariableType (new_typename, color, child_type);
363 void instr_new_user_link_variable_type (const char *new_typename, const char *color)
365 recursiveNewUserLinkVariableType (new_typename, color, getRootType());
369 static void recursiveNewUserHostVariableType (const char *new_typename, const char *color, type_t root)
371 if (!strcmp (root->name, "HOST")){
372 getVariableType(new_typename, color, root);
374 xbt_dict_cursor_t cursor = NULL;
377 xbt_dict_foreach(root->children, cursor, name, child_type) {
378 recursiveNewUserHostVariableType (new_typename, color, child_type);
382 void instr_new_user_host_variable_type (const char *new_typename, const char *color)
384 recursiveNewUserHostVariableType (new_typename, color, getRootType());
387 int instr_platform_traced ()
389 return platform_created;
392 #endif /* HAVE_TRACING */