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 const char *link_id = A_surfxml_link_id;
202 double bandwidth_value = atof(A_surfxml_link_bandwidth);
203 double latency_value = atof(A_surfxml_link_latency);
204 xbt_dynar_t links_to_create = xbt_dynar_new (sizeof(char*), &xbt_free_ref);
206 if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX){
207 char *up = bprintf("%s_UP", link_id);
208 char *down = bprintf("%s_DOWN", link_id);
209 xbt_dynar_push_as (links_to_create, char*, xbt_strdup(up));
210 xbt_dynar_push_as (links_to_create, char*, xbt_strdup(down));
214 xbt_dynar_push_as (links_to_create, char*, strdup(link_id));
217 char *link_name = NULL;
219 xbt_dynar_foreach (links_to_create, i, link_name){
221 container_t new = newContainer (link_name, INSTR_LINK, father);
223 type_t bandwidth = getVariableType ("bandwidth", NULL, new->type);
224 type_t latency = getVariableType ("latency", NULL, new->type);
225 new_pajeSetVariable (0, new, bandwidth, bandwidth_value);
226 new_pajeSetVariable (0, new, latency, latency_value);
227 if (TRACE_uncategorized()){
228 getVariableType ("bandwidth_used", "0.5 0.5 0.5", new->type);
232 xbt_dynar_free (&links_to_create);
235 static void instr_routing_parse_end_link ()
239 static void instr_routing_parse_start_host ()
241 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
242 container_t new = newContainer (A_surfxml_host_id, INSTR_HOST, father);
244 type_t power = getVariableType ("power", NULL, new->type);
245 new_pajeSetVariable (0, new, power, atof(A_surfxml_host_power));
246 if (TRACE_uncategorized()){
247 getVariableType ("power_used", "0.5 0.5 0.5", new->type);
250 if (TRACE_smpi_is_enabled()) {
251 if (TRACE_smpi_is_grouped()){
252 type_t mpi = getContainerType("MPI", new->type);
253 getStateType ("MPI_STATE", mpi);
254 getLinkType ("MPI_LINK", getRootType(), mpi, mpi);
258 if (TRACE_msg_process_is_enabled()) {
259 type_t msg_process = getContainerType("MSG_PROCESS", new->type);
260 type_t state = getStateType ("MSG_PROCESS_STATE", msg_process);
261 getValue ("executing", "0 1 0", state);
262 getValue ("suspend", "1 0 1", state);
263 getValue ("sleep", "1 1 0", state);
264 getValue ("receive", "1 0 0", state);
265 getValue ("send", "0 0 1", state);
266 getValue ("task_execute", "0 1 1", state);
267 getLinkType ("MSG_PROCESS_LINK", getRootType(), msg_process, msg_process);
268 getLinkType ("MSG_PROCESS_TASK_LINK", getRootType(), msg_process, msg_process);
271 if (TRACE_msg_task_is_enabled()) {
272 type_t msg_task = getContainerType ("MSG_TASK", new->type);
273 type_t state = getStateType ("MSG_TASK_STATE", msg_task);
274 getValue ("MSG_task_execute", "0 1 0", state);
275 getValue ("created", "1 1 0", state);
276 getLinkType ("MSG_TASK_LINK", getRootType(), msg_task, msg_task);
280 static void instr_routing_parse_end_host ()
284 static void instr_routing_parse_start_router ()
286 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
287 newContainer (A_surfxml_router_id, INSTR_ROUTER, father);
290 static void instr_routing_parse_end_router ()
294 static void instr_routing_parse_end_platform ()
296 xbt_dynar_free(¤tContainer);
297 currentContainer = NULL;
298 xbt_dict_t filter = xbt_dict_new ();
299 recursiveGraphExtraction (getRootContainer(), filter);
300 xbt_dict_free(&filter);
301 platform_created = 1;
302 TRACE_paje_dump_buffer(1);
305 void instr_routing_define_callbacks ()
307 if (!TRACE_is_active())
309 surfxml_add_callback(STag_surfxml_AS_cb_list, &instr_routing_parse_start_AS);
310 surfxml_add_callback(ETag_surfxml_AS_cb_list, &instr_routing_parse_end_AS);
311 surfxml_add_callback(STag_surfxml_link_cb_list, &instr_routing_parse_start_link);
312 surfxml_add_callback(ETag_surfxml_link_cb_list, &instr_routing_parse_end_link);
313 surfxml_add_callback(STag_surfxml_host_cb_list, &instr_routing_parse_start_host);
314 surfxml_add_callback(ETag_surfxml_host_cb_list, &instr_routing_parse_end_host);
315 surfxml_add_callback(STag_surfxml_router_cb_list, &instr_routing_parse_start_router);
316 surfxml_add_callback(ETag_surfxml_router_cb_list, &instr_routing_parse_end_router);
317 surfxml_add_callback(ETag_surfxml_platform_cb_list, &instr_routing_parse_end_platform);
321 * user categories support
323 static void recursiveNewUserVariableType (const char *new_typename, const char *color, type_t root)
325 if (!strcmp (root->name, "HOST")){
326 char tnstr[INSTR_DEFAULT_STR_SIZE];
327 snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "p%s", new_typename);
328 getVariableType(tnstr, color, root);
330 if (!strcmp (root->name, "LINK")){
331 char tnstr[INSTR_DEFAULT_STR_SIZE];
332 snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "b%s", new_typename);
333 getVariableType(tnstr, color, root);
335 xbt_dict_cursor_t cursor = NULL;
338 xbt_dict_foreach(root->children, cursor, name, child_type) {
339 recursiveNewUserVariableType (new_typename, color, child_type);
343 void instr_new_user_variable_type (const char *new_typename, const char *color)
345 recursiveNewUserVariableType (new_typename, color, getRootType());
348 static void recursiveNewUserLinkVariableType (const char *new_typename, const char *color, type_t root)
350 if (!strcmp (root->name, "LINK")){
351 getVariableType(new_typename, color, root);
353 xbt_dict_cursor_t cursor = NULL;
356 xbt_dict_foreach(root->children, cursor, name, child_type) {
357 recursiveNewUserLinkVariableType (new_typename, color, child_type);
361 void instr_new_user_link_variable_type (const char *new_typename, const char *color)
363 recursiveNewUserLinkVariableType (new_typename, color, getRootType());
367 static void recursiveNewUserHostVariableType (const char *new_typename, const char *color, type_t root)
369 if (!strcmp (root->name, "HOST")){
370 getVariableType(new_typename, color, root);
372 xbt_dict_cursor_t cursor = NULL;
375 xbt_dict_foreach(root->children, cursor, name, child_type) {
376 recursiveNewUserHostVariableType (new_typename, color, child_type);
380 void instr_new_user_host_variable_type (const char *new_typename, const char *color)
382 recursiveNewUserHostVariableType (new_typename, color, getRootType());
385 int instr_platform_traced ()
387 return platform_created;
390 #endif /* HAVE_TRACING */