Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] add some comments, cosmetics
[simgrid.git] / src / instr / instr_interface.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "simgrid_config.h"
8
9 #ifdef HAVE_TRACING
10
11 #include "instr/instr_private.h"
12 #include "surf/network_private.h"
13
14 typedef enum {
15   INSTR_US_DECLARE,
16   INSTR_US_SET,
17   INSTR_US_ADD,
18   INSTR_US_SUB
19 } InstrUserVariable;
20
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_api, instr, "API");
22
23 void TRACE_category(const char *category)
24 {
25   TRACE_category_with_color (category, NULL);
26 }
27
28 void TRACE_category_with_color (const char *category, const char *color)
29 {
30   /* safe switch */
31   if (!TRACE_is_enabled()) return;
32
33   if (!(TRACE_categorized() && category != NULL)) return;
34
35   /* if platform is not traced, we can't deal with categories */
36   if (!TRACE_needs_platform()) return;
37
38   //check if category is already created
39   char *created = xbt_dict_get_or_null(created_categories, category);
40   if (created) return;
41   xbt_dict_set (created_categories, category, xbt_strdup("1"), NULL);
42
43   //define final_color
44   char final_color[INSTR_DEFAULT_STR_SIZE];
45   if (!color){
46     //generate a random color
47     double red = drand48();
48     double green = drand48();
49     double blue = drand48();
50     snprintf (final_color, INSTR_DEFAULT_STR_SIZE, "%f %f %f", red, green, blue);
51   }else{
52     snprintf (final_color, INSTR_DEFAULT_STR_SIZE, "%s", color);
53   }
54
55   XBT_DEBUG("CAT,declare %s, %s", category, final_color);
56
57   //define the type of this category on top of hosts and links
58   instr_new_variable_type (category, final_color);
59 }
60
61 void TRACE_declare_mark(const char *mark_type)
62 {
63   /* safe switch */
64   if (!TRACE_is_enabled()) return;
65
66   if (!mark_type) return;
67
68   XBT_DEBUG("MARK,declare %s", mark_type);
69   PJ_type_event_new(mark_type, NULL, PJ_type_get_root());
70 }
71
72 void TRACE_mark(const char *mark_type, const char *mark_value)
73 {
74   /* safe switch */
75   if (!TRACE_is_enabled()) return;
76
77   if (!mark_type || !mark_value) return;
78
79   XBT_DEBUG("MARK %s %s", mark_type, mark_value);
80   type_t type = PJ_type_get (mark_type, PJ_type_get_root());
81   if (type == NULL){
82     THROWF (tracing_error, 1, "mark_type with name (%s) not declared before", mark_type);
83   }
84   val_t value = PJ_value_get (mark_value, type);
85   if (value == NULL){
86     value = PJ_value_new (mark_value, NULL, type);
87   }
88   new_pajeNewEvent (MSG_get_clock(), PJ_container_get_root(), type, value);
89 }
90
91 static void instr_user_variable(double time,
92                          const char *resource,
93                          const char *variable,
94                          const char *father_type,
95                          double value,
96                          InstrUserVariable what,
97                          const char *color)
98 {
99   /* safe switch */
100   if (!TRACE_is_enabled()) return;
101
102   /* if platform is not traced, we can't deal user variables */
103   if (!TRACE_needs_platform()) return;
104
105   char valuestr[100];
106   snprintf(valuestr, 100, "%g", value);
107
108   switch (what){
109   case INSTR_US_DECLARE:
110     instr_new_user_variable_type (father_type, variable, color);
111     break;
112   case INSTR_US_SET:
113   {
114     container_t container = PJ_container_get(resource);
115     type_t type = PJ_type_get (variable, container->type);
116     new_pajeSetVariable(time, container, type, value);
117     break;
118   }
119   case INSTR_US_ADD:
120   {
121     container_t container = PJ_container_get(resource);
122     type_t type = PJ_type_get (variable, container->type);
123     new_pajeAddVariable(time, container, type, value);
124     break;
125   }
126   case INSTR_US_SUB:
127   {
128     container_t container = PJ_container_get(resource);
129     type_t type = PJ_type_get (variable, container->type);
130     new_pajeSubVariable(time, container, type, value);
131     break;
132   }
133   default:
134     //TODO: launch exception
135     break;
136   }
137 }
138
139 static void instr_user_srcdst_variable(double time,
140                               const char *src,
141                               const char *dst,
142                               const char *variable,
143                               const char *father_type,
144                               double value,
145                               InstrUserVariable what)
146 {
147   xbt_dynar_t route=NULL;
148   network_element_t src_elm = xbt_lib_get_or_null(host_lib,src,ROUTING_HOST_LEVEL);
149   if(!src_elm) src_elm = xbt_lib_get_or_null(as_router_lib,src,ROUTING_ASR_LEVEL);
150   if(!src_elm) xbt_die("Element '%s' not found!",src);
151
152   network_element_t dst_elm = xbt_lib_get_or_null(host_lib,dst,ROUTING_HOST_LEVEL);
153   if(!dst_elm) dst_elm = xbt_lib_get_or_null(as_router_lib,dst,ROUTING_ASR_LEVEL);
154   if(!dst_elm) xbt_die("Element '%s' not found!",dst);
155
156   routing_get_route_and_latency (src_elm, dst_elm, &route,NULL);
157   unsigned int i;
158   void *link;
159   xbt_dynar_foreach (route, i, link) {
160     char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
161     instr_user_variable (time, link_name, variable, father_type, value, what, NULL);
162   }
163 }
164
165 const char *TRACE_node_name (xbt_node_t node)
166 {
167   void *data = xbt_graph_node_get_data(node);
168   char *str = (char*)data;
169   return str;
170 }
171
172 xbt_graph_t TRACE_platform_graph (void)
173 {
174   if (!TRACE_is_enabled()) return NULL;
175   return instr_routing_platform_graph ();
176 }
177
178 void TRACE_platform_graph_export_graphviz (xbt_graph_t g, const char *filename)
179 {
180   instr_routing_platform_graph_export_graphviz (g, filename);
181 }
182
183
184 /*
185  * Derived functions that use instr_user_variable and TRACE_user_srcdst_variable.
186  * They were previously defined as pre-processors directives, but were transformed
187  * into functions so the user can track them using gdb.
188  */
189
190 /* for host variables */
191 void TRACE_host_variable_declare (const char *var)
192 {
193   instr_user_variable(0, NULL, var, "HOST", 0, INSTR_US_DECLARE, NULL);
194 }
195
196 void TRACE_host_variable_declare_with_color (const char *var, const char *color)
197 {
198   instr_user_variable(0, NULL, var, "HOST", 0, INSTR_US_DECLARE, color);
199 }
200
201 void TRACE_host_variable_set (const char *host, const char *variable, double value)
202 {
203   TRACE_host_variable_set_with_time (MSG_get_clock(), host, variable, value);
204 }
205
206 void TRACE_host_variable_add (const char *host, const char *variable, double value)
207 {
208   TRACE_host_variable_add_with_time (MSG_get_clock(), host, variable, value);
209 }
210
211 void TRACE_host_variable_sub (const char *host, const char *variable, double value)
212 {
213   TRACE_host_variable_sub_with_time (MSG_get_clock(), host, variable, value);
214 }
215
216 void TRACE_host_variable_set_with_time (double time, const char *host, const char *variable, double value)
217 {
218   instr_user_variable(time, host, variable, "HOST", value, INSTR_US_SET, NULL);
219 }
220
221 void TRACE_host_variable_add_with_time (double time, const char *host, const char *variable, double value)
222 {
223   instr_user_variable(time, host, variable, "HOST", value, INSTR_US_ADD, NULL);
224 }
225
226 void TRACE_host_variable_sub_with_time (double time, const char *host, const char *variable, double value)
227 {
228   instr_user_variable(time, host, variable, "HOST", value, INSTR_US_SUB, NULL);
229 }
230
231 /* for link variables */
232 void TRACE_link_variable_declare (const char *var)
233 {
234   instr_user_variable (0, NULL, var, "LINK", 0, INSTR_US_DECLARE, NULL);
235 }
236
237 void TRACE_link_variable_declare_with_color (const char *var, const char *color)
238 {
239   instr_user_variable (0, NULL, var, "LINK", 0, INSTR_US_DECLARE, color);
240 }
241
242 void TRACE_link_variable_set (const char *link, const char *variable, double value)
243 {
244   TRACE_link_variable_set_with_time (MSG_get_clock(), link, variable, value);
245 }
246
247 void TRACE_link_variable_add (const char *link, const char *variable, double value)
248 {
249   TRACE_link_variable_add_with_time (MSG_get_clock(), link, variable, value);
250 }
251
252 void TRACE_link_variable_sub (const char *link, const char *variable, double value)
253 {
254   TRACE_link_variable_sub_with_time (MSG_get_clock(), link, variable, value);
255 }
256
257 void TRACE_link_variable_set_with_time (double time, const char *link, const char *variable, double value)
258 {
259   instr_user_variable (time, link, variable, "LINK", value, INSTR_US_SET, NULL);
260 }
261
262 void TRACE_link_variable_add_with_time (double time, const char *link, const char *variable, double value)
263 {
264   instr_user_variable (time, link, variable, "LINK", value, INSTR_US_ADD, NULL);
265 }
266
267 void TRACE_link_variable_sub_with_time (double time, const char *link, const char *variable, double value)
268 {
269   instr_user_variable (time, link, variable, "LINK", value, INSTR_US_SUB, NULL);
270 }
271
272 /* for link variables, but with src and dst used for get_route */
273 void TRACE_link_srcdst_variable_set (const char *src, const char *dst, const char *variable, double value)
274 {
275   TRACE_link_srcdst_variable_set_with_time (MSG_get_clock(), src, dst, variable, value);
276 }
277
278 void TRACE_link_srcdst_variable_add (const char *src, const char *dst, const char *variable, double value)
279 {
280   TRACE_link_srcdst_variable_add_with_time (MSG_get_clock(), src, dst, variable, value);
281 }
282
283 void TRACE_link_srcdst_variable_sub (const char *src, const char *dst, const char *variable, double value)
284 {
285   TRACE_link_srcdst_variable_sub_with_time (MSG_get_clock(), src, dst, variable, value);
286 }
287
288 void TRACE_link_srcdst_variable_set_with_time (double time, const char *src, const char *dst, const char *variable, double value)
289 {
290   instr_user_srcdst_variable (time, src, dst, variable, "LINK", value, INSTR_US_SET);
291 }
292
293 void TRACE_link_srcdst_variable_add_with_time (double time, const char *src, const char *dst, const char *variable, double value)
294 {
295   instr_user_srcdst_variable (time, src, dst, variable, "LINK", value, INSTR_US_ADD);
296 }
297
298 void TRACE_link_srcdst_variable_sub_with_time (double time, const char *src, const char *dst, const char *variable, double value)
299 {
300   instr_user_srcdst_variable (time, src, dst, variable, "LINK", value, INSTR_US_SUB);
301 }
302
303 #endif /* HAVE_TRACING */