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 (container_t father, container_t src, container_t dst, xbt_dict_t filter)
53 if (strcmp (src->name, "__loopback__") == 0 || strcmp (dst->name, "__loopback__") == 0)
57 //check if we already register this pair (we only need one direction)
58 char aux1[INSTR_DEFAULT_STR_SIZE], aux2[INSTR_DEFAULT_STR_SIZE];
59 snprintf (aux1, INSTR_DEFAULT_STR_SIZE, "%s%s", src->name, dst->name);
60 snprintf (aux2, INSTR_DEFAULT_STR_SIZE, "%s%s", dst->name, src->name);
61 if (xbt_dict_get_or_null (filter, aux1)) return;
62 if (xbt_dict_get_or_null (filter, aux2)) return;
64 //ok, not found, register it
65 xbt_dict_set (filter, aux1, xbt_strdup ("1"), xbt_free);
66 xbt_dict_set (filter, aux2, xbt_strdup ("1"), xbt_free);
70 char link_typename[INSTR_DEFAULT_STR_SIZE];
71 snprintf (link_typename, INSTR_DEFAULT_STR_SIZE, "%s-%s", src->type->name, dst->type->name);
72 type_t link_type = getLinkType (link_typename, father->type, src->type, dst->type);
74 //register EDGE types for triva configuration
75 xbt_dict_set (trivaEdgeTypes, link_type->name, xbt_strdup("1"), xbt_free);
78 static long long counter = 0;
79 char key[INSTR_DEFAULT_STR_SIZE];
80 snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter++);
81 new_pajeStartLink(SIMIX_get_clock(), father, link_type, src, "G", key);
82 new_pajeEndLink(SIMIX_get_clock(), father, link_type, dst, "G", key);
85 static void recursiveGraphExtraction (routing_component_t rc, container_t container, xbt_dict_t filter)
87 if (xbt_dict_length (rc->routing_sons)){
88 xbt_dict_cursor_t cursor = NULL;
89 routing_component_t rc_son;
92 xbt_dict_foreach(rc->routing_sons, cursor, child_name, rc_son) {
93 container_t child_container = xbt_dict_get (container->children, rc_son->name);
94 recursiveGraphExtraction (rc_son, child_container, filter);
99 xbt_dict_cursor_t cursor1 = NULL, cursor2 = NULL;
100 container_t child1, child2;
101 const char *child1_name, *child2_name;
102 xbt_dict_foreach(container->children, cursor1, child1_name, child1) {
103 if (child1->kind == INSTR_LINK) continue;
104 xbt_dict_foreach(container->children, cursor2, child2_name, child2) {
105 if (child2->kind == INSTR_LINK) continue;
107 if ((child1->kind == INSTR_HOST || child1->kind == INSTR_ROUTER) &&
108 (child2->kind == INSTR_HOST || child2->kind == INSTR_ROUTER) &&
109 strcmp (child1_name, child2_name) != 0){
111 xbt_dynar_t route = global_routing->get_route (child1_name, child2_name);
114 container_t previous = child1;
115 xbt_dynar_foreach (route, cpt, link) {
116 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
117 container_t current = getContainerByName(link_name);
118 linkContainers(container, previous, current, filter);
121 linkContainers(container, previous, child2, filter);
123 }else if (child1->kind == INSTR_AS &&
124 child2->kind == INSTR_AS &&
125 strcmp(child1_name, child2_name) != 0){
127 route_extended_t route = rc->get_route (rc, child1_name, child2_name);
130 container_t previous = getContainerByName(route->src_gateway);
131 xbt_dynar_foreach (route->generic_route.link_list, cpt, link) {
132 char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
133 container_t current = getContainerByName(link_name);
134 linkContainers (container, previous, current, filter);
137 container_t last = getContainerByName(route->dst_gateway);
138 linkContainers (container, previous, last, filter);
147 static void instr_routing_parse_start_AS ()
149 if (getRootContainer() == NULL){
150 container_t root = newContainer (A_surfxml_AS_id, INSTR_AS, NULL);
151 instr_paje_init (root);
153 currentContainer = xbt_dynar_new (sizeof(container_t), NULL);
154 xbt_dynar_push (currentContainer, &root);
156 if (TRACE_smpi_is_enabled()) {
157 if (!TRACE_smpi_is_grouped()){
158 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
159 type_t mpi = getContainerType("MPI", father->type);
160 getStateType ("MPI_STATE", mpi);
161 getLinkType ("MPI_LINK", getRootType(), mpi, mpi);
167 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
168 container_t new = newContainer (A_surfxml_AS_id, INSTR_AS, father);
171 xbt_dynar_push (currentContainer, &new);
174 static void instr_routing_parse_end_AS ()
176 xbt_dynar_pop_ptr (currentContainer);
179 static void instr_routing_parse_start_link ()
181 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
182 const char *link_id = A_surfxml_link_id;
184 double bandwidth_value = atof(A_surfxml_link_bandwidth);
185 double latency_value = atof(A_surfxml_link_latency);
186 xbt_dynar_t links_to_create = xbt_dynar_new (sizeof(char*), &xbt_free_ref);
188 if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX){
189 char *up = bprintf("%s_UP", link_id);
190 char *down = bprintf("%s_DOWN", link_id);
191 xbt_dynar_push_as (links_to_create, char*, xbt_strdup(up));
192 xbt_dynar_push_as (links_to_create, char*, xbt_strdup(down));
196 xbt_dynar_push_as (links_to_create, char*, strdup(link_id));
199 char *link_name = NULL;
201 xbt_dynar_foreach (links_to_create, i, link_name){
203 container_t new = newContainer (link_name, 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, bandwidth_value);
208 new_pajeSetVariable (0, new, latency, latency_value);
209 if (TRACE_uncategorized()){
210 getVariableType ("bandwidth_used", "0.5 0.5 0.5", new->type);
214 xbt_dynar_free (&links_to_create);
217 static void instr_routing_parse_end_link ()
221 static void instr_routing_parse_start_host ()
223 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
224 container_t new = newContainer (A_surfxml_host_id, INSTR_HOST, father);
226 type_t power = getVariableType ("power", NULL, new->type);
227 new_pajeSetVariable (0, new, power, atof(A_surfxml_host_power));
228 if (TRACE_uncategorized()){
229 getVariableType ("power_used", "0.5 0.5 0.5", new->type);
232 if (TRACE_smpi_is_enabled()) {
233 if (TRACE_smpi_is_grouped()){
234 type_t mpi = getContainerType("MPI", new->type);
235 getStateType ("MPI_STATE", mpi);
236 getLinkType ("MPI_LINK", getRootType(), mpi, mpi);
240 if (TRACE_msg_process_is_enabled()) {
241 type_t msg_process = getContainerType("MSG_PROCESS", new->type);
242 type_t state = getStateType ("MSG_PROCESS_STATE", msg_process);
243 getValue ("executing", "0 1 0", state);
244 getValue ("suspend", "1 0 1", state);
245 getValue ("sleep", "1 1 0", state);
246 getValue ("receive", "1 0 0", state);
247 getValue ("send", "0 0 1", state);
248 getValue ("task_execute", "0 1 1", state);
249 getLinkType ("MSG_PROCESS_LINK", getRootType(), msg_process, msg_process);
250 getLinkType ("MSG_PROCESS_TASK_LINK", getRootType(), msg_process, msg_process);
253 if (TRACE_msg_task_is_enabled()) {
254 type_t msg_task = getContainerType ("MSG_TASK", new->type);
255 type_t state = getStateType ("MSG_TASK_STATE", msg_task);
256 getValue ("MSG_task_execute", "0 1 0", state);
257 getValue ("created", "1 1 0", state);
258 getLinkType ("MSG_TASK_LINK", getRootType(), msg_task, msg_task);
262 static void instr_routing_parse_end_host ()
266 static void instr_routing_parse_start_router ()
268 container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
269 newContainer (A_surfxml_router_id, INSTR_ROUTER, father);
272 static void instr_routing_parse_end_router ()
276 static void instr_routing_parse_end_platform ()
278 xbt_dynar_free(¤tContainer);
279 currentContainer = NULL;
280 xbt_dict_t filter = xbt_dict_new ();
281 recursiveGraphExtraction (global_routing->root, getRootContainer(), filter);
282 xbt_dict_free(&filter);
283 platform_created = 1;
284 TRACE_paje_dump_buffer(1);
287 void instr_routing_define_callbacks ()
289 if (!TRACE_is_active())
291 surfxml_add_callback(STag_surfxml_AS_cb_list, &instr_routing_parse_start_AS);
292 surfxml_add_callback(ETag_surfxml_AS_cb_list, &instr_routing_parse_end_AS);
293 surfxml_add_callback(STag_surfxml_link_cb_list, &instr_routing_parse_start_link);
294 surfxml_add_callback(ETag_surfxml_link_cb_list, &instr_routing_parse_end_link);
295 surfxml_add_callback(STag_surfxml_host_cb_list, &instr_routing_parse_start_host);
296 surfxml_add_callback(ETag_surfxml_host_cb_list, &instr_routing_parse_end_host);
297 surfxml_add_callback(STag_surfxml_router_cb_list, &instr_routing_parse_start_router);
298 surfxml_add_callback(ETag_surfxml_router_cb_list, &instr_routing_parse_end_router);
299 surfxml_add_callback(ETag_surfxml_platform_cb_list, &instr_routing_parse_end_platform);
303 * user categories support
305 static void recursiveNewUserVariableType (const char *new_typename, const char *color, type_t root)
307 if (!strcmp (root->name, "HOST")){
308 char tnstr[INSTR_DEFAULT_STR_SIZE];
309 snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "p%s", new_typename);
310 getVariableType(tnstr, color, root);
312 if (!strcmp (root->name, "LINK")){
313 char tnstr[INSTR_DEFAULT_STR_SIZE];
314 snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "b%s", new_typename);
315 getVariableType(tnstr, color, root);
317 xbt_dict_cursor_t cursor = NULL;
320 xbt_dict_foreach(root->children, cursor, name, child_type) {
321 recursiveNewUserVariableType (new_typename, color, child_type);
325 void instr_new_user_variable_type (const char *new_typename, const char *color)
327 recursiveNewUserVariableType (new_typename, color, getRootType());
330 static void recursiveNewUserLinkVariableType (const char *new_typename, const char *color, type_t root)
332 if (!strcmp (root->name, "LINK")){
333 getVariableType(new_typename, color, root);
335 xbt_dict_cursor_t cursor = NULL;
338 xbt_dict_foreach(root->children, cursor, name, child_type) {
339 recursiveNewUserLinkVariableType (new_typename, color, child_type);
343 void instr_new_user_link_variable_type (const char *new_typename, const char *color)
345 recursiveNewUserLinkVariableType (new_typename, color, getRootType());
349 static void recursiveNewUserHostVariableType (const char *new_typename, const char *color, type_t root)
351 if (!strcmp (root->name, "HOST")){
352 getVariableType(new_typename, color, root);
354 xbt_dict_cursor_t cursor = NULL;
357 xbt_dict_foreach(root->children, cursor, name, child_type) {
358 recursiveNewUserHostVariableType (new_typename, color, child_type);
362 void instr_new_user_host_variable_type (const char *new_typename, const char *color)
364 recursiveNewUserHostVariableType (new_typename, color, getRootType());
367 int instr_platform_traced ()
369 return platform_created;
372 #endif /* HAVE_TRACING */