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/surfxml_parse_private.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_routing, instr, "Tracing platform hierarchy");
14 extern routing_global_t global_routing; /* from surf/surf_routing.c */
15 extern xbt_dict_t defined_types; /* from instr_interface.c */
17 typedef struct s_container *container_t;
18 typedef struct s_container {
22 struct s_container *father;
26 static container_t rootContainer = NULL;
27 static xbt_dynar_t currentContainer = NULL;
28 static xbt_dict_t created_links = NULL;
29 static xbt_dict_t created_hosts = NULL;
30 xbt_dict_t hosts_types = NULL;
31 xbt_dict_t links_types = NULL;
33 static void instr_routing_parse_start_AS (void);
34 static void instr_routing_parse_end_AS (void);
35 static void instr_routing_parse_start_link (void);
36 static void instr_routing_parse_end_link (void);
37 static void instr_routing_parse_start_host (void);
38 static void instr_routing_parse_end_host (void);
39 static void instr_routing_parse_start_router (void);
40 static void instr_routing_parse_end_router (void);
41 static char *instr_AS_type (int level);
43 static char *instr_AS_type (int level)
45 char *ret = xbt_new (char, INSTR_DEFAULT_STR_SIZE);
47 snprintf (ret, INSTR_DEFAULT_STR_SIZE, "0");
49 snprintf (ret, INSTR_DEFAULT_STR_SIZE, "L%d", level);
54 static void newContainerType (const char *type, const char *parentType, const char *name)
56 char *defined = xbt_dict_get_or_null (defined_types, type);
58 pajeDefineContainerType(type, parentType, name);
59 xbt_dict_set(defined_types, type, xbt_strdup("1"), xbt_free);
64 static void newVariableType (const char *type, const char *parentType, const char *name, const char *color)
66 char *defined = xbt_dict_get_or_null (defined_types, type);
69 pajeDefineVariableTypeWithColor(type, parentType, name, color);
71 pajeDefineVariableType(type, parentType, name);
73 xbt_dict_set(defined_types, type, xbt_strdup("1"), xbt_free);
77 void instr_routing_define_callbacks ()
79 surfxml_add_callback(STag_surfxml_AS_cb_list, &instr_routing_parse_start_AS);
80 surfxml_add_callback(ETag_surfxml_AS_cb_list, &instr_routing_parse_end_AS);
81 surfxml_add_callback(STag_surfxml_link_cb_list, &instr_routing_parse_start_link);
82 surfxml_add_callback(ETag_surfxml_link_cb_list, &instr_routing_parse_end_link);
83 surfxml_add_callback(STag_surfxml_host_cb_list, &instr_routing_parse_start_host);
84 surfxml_add_callback(ETag_surfxml_host_cb_list, &instr_routing_parse_end_host);
85 surfxml_add_callback(STag_surfxml_router_cb_list, &instr_routing_parse_start_router);
86 surfxml_add_callback(ETag_surfxml_router_cb_list, &instr_routing_parse_end_router);
90 static container_t newContainer (const char *name, const char *type, const char *typename)
92 container_t newContainer = xbt_new0(s_container_t, 1);
93 newContainer->name = xbt_strdup (name);
94 newContainer->father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
95 newContainer->level = newContainer->father->level+1;
96 newContainer->type = xbt_strdup (type);
97 newContainer->children = xbt_dict_new();
98 xbt_dict_set(newContainer->father->children, newContainer->name, newContainer, NULL);
100 newContainerType (newContainer->type, newContainer->father->type, typename);
101 pajeCreateContainer (0, newContainer->name, newContainer->type, newContainer->father->name, newContainer->name);
106 static void recursiveDestroyContainer (container_t container)
108 xbt_dict_cursor_t cursor = NULL;
111 xbt_dict_foreach(container->children, cursor, child_name, child) {
112 recursiveDestroyContainer (child);
115 pajeDestroyContainer(SIMIX_get_clock(), container->type, container->name);
117 xbt_free (container->name);
118 xbt_free (container->type);
119 xbt_free (container->children);
120 xbt_free (container);
127 static void instr_routing_parse_start_AS ()
129 if (rootContainer == NULL){
130 rootContainer = xbt_new0(s_container_t, 1);
131 rootContainer->name = xbt_strdup ("0");
132 rootContainer->type = xbt_strdup ("0");
133 rootContainer->level = 0;
134 rootContainer->father = NULL;
135 rootContainer->children = xbt_dict_new();
137 currentContainer = xbt_dynar_new (sizeof(s_container_t), NULL);
138 xbt_dynar_push (currentContainer, rootContainer);
140 created_links = xbt_dict_new ();
141 created_hosts = xbt_dict_new ();
142 hosts_types = xbt_dict_new ();
143 links_types = xbt_dict_new ();
146 container_t newContainer = xbt_new0(s_container_t, 1);
147 newContainer->name = xbt_strdup (A_surfxml_AS_id);
148 newContainer->father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
149 newContainer->level = newContainer->father->level+1;
150 newContainer->type = instr_AS_type (newContainer->level);
151 newContainer->children = xbt_dict_new();
152 xbt_dict_set(newContainer->father->children, newContainer->name, newContainer, NULL);
155 newContainerType (newContainer->type, newContainer->father->type, newContainer->type);
156 pajeCreateContainer (0, newContainer->name, newContainer->type, newContainer->father->name, newContainer->name);
159 xbt_dynar_push (currentContainer, newContainer);
162 static void instr_routing_parse_end_AS ()
164 xbt_dynar_pop_ptr (currentContainer);
167 static void instr_routing_parse_start_link ()
169 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
170 char type[INSTR_DEFAULT_STR_SIZE];
171 snprintf (type, INSTR_DEFAULT_STR_SIZE, "LINK-%s", father->type);
172 container_t new = newContainer (A_surfxml_link_id, type, "LINK");
174 //bandwidth and latency
175 char bandwidth_type[INSTR_DEFAULT_STR_SIZE], latency_type[INSTR_DEFAULT_STR_SIZE];
176 snprintf (bandwidth_type, INSTR_DEFAULT_STR_SIZE, "bandwidth-%s", type);
177 snprintf (latency_type, INSTR_DEFAULT_STR_SIZE, "latency-%s", type);
178 newVariableType (bandwidth_type, type, "bandwidth", NULL);
179 newVariableType (latency_type, type, "latency", NULL);
180 pajeSetVariable(0, bandwidth_type, new->name, A_surfxml_link_bandwidth);
181 pajeSetVariable(0, latency_type, new->name, A_surfxml_link_latency);
183 if (TRACE_uncategorized()){
185 char bandwidth_used_type[INSTR_DEFAULT_STR_SIZE];
186 snprintf (bandwidth_used_type, INSTR_DEFAULT_STR_SIZE, "bandwidth_used-%s", type);
187 newVariableType (bandwidth_used_type, type, "bandwidth_used", "0.5 0.5 0.5");
190 //register created link on the dictionary
191 xbt_dict_set (created_links, A_surfxml_link_id, new, NULL);
193 //register this link type
194 xbt_dict_set (links_types, type, xbt_strdup("1"), xbt_free);
197 static void instr_routing_parse_end_link ()
201 static void instr_routing_parse_start_host ()
203 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
204 char type[INSTR_DEFAULT_STR_SIZE];
205 snprintf (type, INSTR_DEFAULT_STR_SIZE, "HOST-%s", father->type);
206 container_t new = newContainer (A_surfxml_host_id, type, "HOST");
209 char power_type[INSTR_DEFAULT_STR_SIZE];
210 snprintf (power_type, INSTR_DEFAULT_STR_SIZE, "power-%s", type);
211 newVariableType (power_type, type, "power", NULL);
212 pajeSetVariable(0, power_type, new->name, A_surfxml_host_power);
214 if (TRACE_uncategorized()){
216 char power_used_type[INSTR_DEFAULT_STR_SIZE];
217 snprintf (power_used_type, INSTR_DEFAULT_STR_SIZE, "power_used-%s", type);
218 newVariableType (power_used_type, type, "power_used", "0.5 0.5 0.5");
221 //register created host on the dictionary
222 xbt_dict_set (created_hosts, A_surfxml_host_id, new, NULL);
224 //register this link type
225 xbt_dict_set (hosts_types, type, xbt_strdup("1"), xbt_free);
228 static void instr_routing_parse_end_host ()
232 static void instr_routing_parse_start_router ()
234 container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
235 char type[INSTR_DEFAULT_STR_SIZE];
236 snprintf (type, INSTR_DEFAULT_STR_SIZE, "ROUTER-%s", father->type);
237 newContainer (A_surfxml_router_id, type, "ROUTER");
240 static void instr_routing_parse_end_router ()
247 int instr_link_is_traced (const char *name)
249 if (xbt_dict_get_or_null(created_links, name)) {
256 char *instr_link_type (const char *name)
258 container_t created_link = xbt_dict_get_or_null(created_links, name);
260 return created_link->type;
267 char *instr_host_type (const char *name)
269 container_t created_host = xbt_dict_get_or_null(created_hosts, name);
271 return created_host->type;
277 void instr_destroy_platform ()
279 recursiveDestroyContainer (rootContainer);
282 #endif /* HAVE_TRACING */