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 void instr_routing_parse_start_AS (void);
21 static void instr_routing_parse_end_AS (void);
22 static void instr_routing_parse_start_link (void);
23 static void instr_routing_parse_end_link (void);
24 static void instr_routing_parse_start_host (void);
25 static void instr_routing_parse_end_host (void);
26 static void instr_routing_parse_start_router (void);
27 static void instr_routing_parse_end_router (void);
28 static void instr_routing_parse_end_platform (void);
30 void instr_routing_define_callbacks ()
32 if (!TRACE_is_active())
34 surfxml_add_callback(STag_surfxml_AS_cb_list, &instr_routing_parse_start_AS);
35 surfxml_add_callback(ETag_surfxml_AS_cb_list, &instr_routing_parse_end_AS);
36 surfxml_add_callback(STag_surfxml_link_cb_list, &instr_routing_parse_start_link);
37 surfxml_add_callback(ETag_surfxml_link_cb_list, &instr_routing_parse_end_link);
38 surfxml_add_callback(STag_surfxml_host_cb_list, &instr_routing_parse_start_host);
39 surfxml_add_callback(ETag_surfxml_host_cb_list, &instr_routing_parse_end_host);
40 surfxml_add_callback(STag_surfxml_router_cb_list, &instr_routing_parse_start_router);
41 surfxml_add_callback(ETag_surfxml_router_cb_list, &instr_routing_parse_end_router);
42 surfxml_add_callback(ETag_surfxml_platform_cb_list, &instr_routing_parse_end_platform);
45 static container_t findChild (container_t root, container_t a1)
47 if (root == a1) return root;
49 xbt_dict_cursor_t cursor = NULL;
52 xbt_dict_foreach(root->children, cursor, child_name, child) {
53 if (findChild (child, a1)) return child;
58 static container_t findCommonFather (container_t root, container_t a1, container_t a2)
60 if (a1->father == a2->father) return a1->father;
62 xbt_dict_cursor_t cursor = NULL;
65 container_t a1_try = NULL;
66 container_t a2_try = NULL;
67 xbt_dict_foreach(root->children, cursor, child_name, child) {
68 a1_try = findChild (child, a1);
69 a2_try = findChild (child, a2);
70 if (a1_try && a2_try) return child;
75 static void linkContainers (const char *a1, const char *a2)
78 if (strcmp (a1, "__loopback__") == 0 || strcmp (a2, "__loopback__") == 0)
81 container_t a1_container = getContainerByName (a1);
82 type_t a1_type = a1_container->type;
84 container_t a2_container = getContainerByName (a2);
85 type_t a2_type = a2_container->type;
87 container_t container = findCommonFather (getRootContainer(), a1_container, a2_container);
88 xbt_assert0 (container != NULL, "common father not found");
91 char link_typename[INSTR_DEFAULT_STR_SIZE];
92 snprintf (link_typename, INSTR_DEFAULT_STR_SIZE, "%s-%s", a1_type->name, a2_type->name);
93 type_t link_type = getLinkType (link_typename, container->type, a1_type, a2_type);
96 static long long counter = 0;
97 char key[INSTR_DEFAULT_STR_SIZE];
98 snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter++);
99 pajeStartLink(SIMIX_get_clock(), link_type->id, container->id, "G", a1_container->id, key);
100 pajeEndLink(SIMIX_get_clock(), link_type->id, container->id, "G", a2_container->id, key);
103 static void recursiveGraphExtraction (container_t container)
105 if (xbt_dict_length(container->children)){
106 xbt_dict_cursor_t cursor = NULL;
109 //bottom-up recursion
110 xbt_dict_foreach(container->children, cursor, child_name, child) {
111 recursiveGraphExtraction (child);
115 xbt_dict_cursor_t cursor1 = NULL, cursor2 = NULL;
116 container_t child1, child2;
117 const char *child_name1, *child_name2;
119 xbt_dict_t filter = xbt_dict_new ();
121 xbt_dict_foreach(container->children, cursor1, child_name1, child1) {
122 xbt_dict_foreach(container->children, cursor2, child_name2, child2) {
123 //check if we already register this pair (we only need one direction)
124 char aux1[INSTR_DEFAULT_STR_SIZE], aux2[INSTR_DEFAULT_STR_SIZE];
125 snprintf (aux1, INSTR_DEFAULT_STR_SIZE, "%s%s", child_name1, child_name2);
126 snprintf (aux2, INSTR_DEFAULT_STR_SIZE, "%s%s", child_name2, child_name1);
127 if (xbt_dict_get_or_null (filter, aux1)) continue;
128 if (xbt_dict_get_or_null (filter, aux2)) continue;
130 //ok, not found, register it
131 xbt_dict_set (filter, aux1, xbt_strdup ("1"), xbt_free);
132 xbt_dict_set (filter, aux2, xbt_strdup ("1"), xbt_free);
134 if ((child1->kind == INSTR_HOST || child1->kind == INSTR_ROUTER) &&
135 (child2->kind == INSTR_HOST || child2->kind == INSTR_ROUTER)){
138 xbt_dynar_t route = NULL;
141 route = global_routing->get_route (child_name1, child_name2);
143 //no route between them, that's possible
147 //link the route members
150 char *previous_entity_name = (char*)child_name1;
151 xbt_dynar_foreach (route, 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, child_name2);
157 }else if (child1->kind == INSTR_AS &&
158 child2->kind == INSTR_AS &&
159 strcmp(child_name1, child_name2) != 0){
162 routing_component_t root = global_routing->root;
163 route_extended_t route = NULL;
166 route = root->get_route (root, child_name1, child_name2);
168 //no route between them, that's possible
171 xbt_assert2(route!=NULL,
172 "there is no ASroute between %s and %s", child_name1, child_name2);
175 char *previous_entity_name = route->src_gateway;
176 xbt_dynar_foreach (route->generic_route.link_list, cpt, link) {
177 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
178 linkContainers (previous_entity_name, link_name);
179 previous_entity_name = link_name;
181 linkContainers (previous_entity_name, route->dst_gateway);
185 xbt_dict_free(&filter);
192 static void instr_routing_parse_start_AS ()
194 if (getRootContainer() == NULL){
195 container_t root = newContainer ("0", INSTR_AS, NULL);
196 instr_paje_init (root);
198 currentContainer = xbt_dynar_new (sizeof(container_t), NULL);
199 xbt_dynar_push (currentContainer, &root);
201 if (TRACE_smpi_is_enabled()) {
202 if (!TRACE_smpi_is_grouped()){
203 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
204 type_t mpi = getContainerType("MPI", father->type);
205 getStateType ("MPI_STATE", mpi);
206 getLinkType ("MPI_LINK", getRootType(), mpi, mpi);
210 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
211 container_t new = newContainer (A_surfxml_AS_id, INSTR_AS, father);
214 xbt_dynar_push (currentContainer, &new);
217 static void instr_routing_parse_end_AS ()
219 xbt_dynar_pop_ptr (currentContainer);
222 static void instr_routing_parse_start_link ()
224 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
225 container_t new = newContainer (A_surfxml_link_id, INSTR_LINK, father);
227 type_t bandwidth = getVariableType ("bandwidth", NULL, new->type);
228 type_t latency = getVariableType ("latency", NULL, new->type);
229 pajeSetVariable (0, bandwidth->id, new->id, A_surfxml_link_bandwidth);
230 pajeSetVariable (0, latency->id, new->id, A_surfxml_link_latency);
231 if (TRACE_uncategorized()){
232 getVariableType ("bandwidth_used", "0.5 0.5 0.5", new->type);
236 static void instr_routing_parse_end_link ()
240 static void instr_routing_parse_start_host ()
242 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
243 container_t new = newContainer (A_surfxml_host_id, INSTR_HOST, father);
245 type_t power = getVariableType ("power", NULL, new->type);
246 pajeSetVariable (0, power->id, new->id, A_surfxml_host_power);
247 if (TRACE_uncategorized()){
248 getVariableType ("power_used", "0.5 0.5 0.5", new->type);
251 if (TRACE_smpi_is_enabled()) {
252 if (TRACE_smpi_is_grouped()){
253 type_t mpi = getContainerType("MPI", new->type);
254 getStateType ("MPI_STATE", mpi);
255 getLinkType ("MPI_LINK", getRootType(), mpi, mpi);
259 if (TRACE_msg_process_is_enabled()) {
260 type_t msg_process = getContainerType("MSG_PROCESS", new->type);
261 getStateType ("MSG_PROCESS_STATE", msg_process);
262 getLinkType ("MSG_PROCESS_LINK", getRootType(), msg_process, msg_process);
265 if (TRACE_msg_task_is_enabled()) {
266 type_t msg_task = getContainerType ("MSG_TASK", new->type);
267 getStateType ("MSG_TASK_STATE", msg_task);
268 getLinkType ("MSG_TASK_LINK", getRootType(), msg_task, msg_task);
272 static void instr_routing_parse_end_host ()
276 static void instr_routing_parse_start_router ()
278 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
279 newContainer (A_surfxml_router_id, INSTR_ROUTER, father);
282 static void instr_routing_parse_end_router ()
286 static void instr_routing_parse_end_platform ()
288 xbt_dynar_free(¤tContainer);
289 currentContainer = NULL;
290 recursiveGraphExtraction (getRootContainer());
291 platform_created = 1;
297 int instr_link_is_traced (const char *name)
299 if (getContainerByName(name)){
306 char *instr_variable_type (const char *name, const char *resource)
308 container_t container = getContainerByName(resource);
309 xbt_dict_cursor_t cursor = NULL;
312 xbt_dict_foreach(container->type->children, cursor, type_name, type) {
313 if (strcmp (name, type->name) == 0) return type->id;
318 char *instr_resource_type (const char *resource_name)
320 return getContainerByName(resource_name)->id;
323 static void recursiveDestroyContainer (container_t container)
325 xbt_dict_cursor_t cursor = NULL;
328 xbt_dict_foreach(container->children, cursor, child_name, child) {
329 recursiveDestroyContainer (child);
331 destroyContainer (container);
334 static void recursiveDestroyType (type_t type)
336 xbt_dict_cursor_t cursor = NULL;
339 xbt_dict_foreach(type->children, cursor, child_name, child) {
340 recursiveDestroyType (child);
342 xbt_free (type->name);
344 xbt_free (type->children);
349 void instr_destroy_platform ()
351 if (getRootContainer()) recursiveDestroyContainer (getRootContainer());
352 if (getRootType()) recursiveDestroyType (getRootType());
356 * user categories support
358 static void recursiveNewUserVariableType (const char *new_typename, const char *color, type_t root)
360 if (!strcmp (root->name, "HOST") || !strcmp (root->name, "LINK")){
361 getVariableType(new_typename, color, root);
363 xbt_dict_cursor_t cursor = NULL;
366 xbt_dict_foreach(root->children, cursor, name, child_type) {
367 recursiveNewUserVariableType (new_typename, color, child_type);
371 void instr_new_user_variable_type (const char *new_typename, const char *color)
373 recursiveNewUserVariableType (new_typename, color, getRootType());
376 static void recursiveNewUserLinkVariableType (const char *new_typename, const char *color, type_t root)
378 if (!strcmp (root->name, "LINK")){
379 getVariableType(new_typename, color, root);
381 xbt_dict_cursor_t cursor = NULL;
384 xbt_dict_foreach(root->children, cursor, name, child_type) {
385 recursiveNewUserLinkVariableType (new_typename, color, child_type);
389 void instr_new_user_link_variable_type (const char *new_typename, const char *color)
391 recursiveNewUserLinkVariableType (new_typename, color, getRootType());
395 static void recursiveNewUserHostVariableType (const char *new_typename, const char *color, type_t root)
397 if (!strcmp (root->name, "HOST")){
398 getVariableType(new_typename, color, root);
400 xbt_dict_cursor_t cursor = NULL;
403 xbt_dict_foreach(root->children, cursor, name, child_type) {
404 recursiveNewUserHostVariableType (new_typename, color, child_type);
408 void instr_new_user_host_variable_type (const char *new_typename, const char *color)
410 recursiveNewUserHostVariableType (new_typename, color, getRootType());
413 int instr_platform_traced ()
415 return platform_created;
418 #endif /* HAVE_TRACING */