Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7a27800d57c1df10fc4436ff31f61d88eec97cf3
[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"), xbt_free);
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   getEventType(mark_type, NULL, getRootType());
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 = getEventType (mark_type, NULL, getRootContainer()->type);
81   val_t value = getValue (mark_value, NULL, type);
82   new_pajeNewEvent (MSG_get_clock(), getRootContainer(), type, value);
83 }
84
85 static void instr_user_variable(double time,
86                          const char *resource,
87                          const char *variable,
88                          const char *father_type,
89                          double value,
90                          InstrUserVariable what,
91                          const char *color)
92 {
93   /* safe switch */
94   if (!TRACE_is_enabled()) return;
95
96   /* if platform is not traced, we can't deal user variables */
97   if (!TRACE_needs_platform()) return;
98
99   char valuestr[100];
100   snprintf(valuestr, 100, "%g", value);
101
102   switch (what){
103   case INSTR_US_DECLARE:
104     instr_new_user_variable_type (father_type, variable, color);
105     break;
106   case INSTR_US_SET:
107   {
108     container_t container = getContainerByName(resource);
109     type_t type = getVariableType (variable, NULL, container->type);
110     new_pajeSetVariable(time, container, type, value);
111     break;
112   }
113   case INSTR_US_ADD:
114   {
115     container_t container = getContainerByName(resource);
116     type_t type = getVariableType (variable, NULL, container->type);
117     new_pajeAddVariable(time, container, type, value);
118     break;
119   }
120   case INSTR_US_SUB:
121   {
122     container_t container = getContainerByName(resource);
123     type_t type = getVariableType (variable, NULL, container->type);
124     new_pajeSubVariable(time, container, type, value);
125     break;
126   }
127   default:
128     //TODO: launch exception
129     break;
130   }
131 }
132
133 static void instr_user_srcdst_variable(double time,
134                               const char *src,
135                               const char *dst,
136                               const char *variable,
137                               const char *father_type,
138                               double value,
139                               InstrUserVariable what)
140 {
141   xbt_dynar_t route=NULL;
142   routing_get_route_and_latency (src, dst, &route,NULL);
143   unsigned int i;
144   void *link;
145   xbt_dynar_foreach (route, i, link) {
146     char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
147     instr_user_variable (time, link_name, variable, father_type, value, what, NULL);
148   }
149 }
150
151 const char *TRACE_node_name (xbt_node_t node)
152 {
153   void *data = xbt_graph_node_get_data(node);
154   char *str = (char*)data;
155   return str;
156 }
157
158 xbt_graph_t TRACE_platform_graph (void)
159 {
160   if (!TRACE_is_enabled()) return NULL;
161   return instr_routing_platform_graph ();
162 }
163
164 void TRACE_platform_graph_export_graphviz (xbt_graph_t g, const char *filename)
165 {
166   instr_routing_platform_graph_export_graphviz (g, filename);
167 }
168
169
170 /*
171  * Derived functions that use instr_user_variable and TRACE_user_srcdst_variable.
172  * They were previously defined as pre-processors directives, but were transformed
173  * into functions so the user can track them using gdb.
174  */
175
176 /* for host variables */
177 void TRACE_host_variable_declare (const char *var)
178 {
179   instr_user_variable(0, NULL, var, "HOST", 0, INSTR_US_DECLARE, NULL);
180 }
181
182 void TRACE_host_variable_declare_with_color (const char *var, const char *color)
183 {
184   instr_user_variable(0, NULL, var, "HOST", 0, INSTR_US_DECLARE, color);
185 }
186
187 void TRACE_host_variable_set (const char *host, const char *variable, double value)
188 {
189   TRACE_host_variable_set_with_time (MSG_get_clock(), host, variable, value);
190 }
191
192 void TRACE_host_variable_add (const char *host, const char *variable, double value)
193 {
194   TRACE_host_variable_add_with_time (MSG_get_clock(), host, variable, value);
195 }
196
197 void TRACE_host_variable_sub (const char *host, const char *variable, double value)
198 {
199   TRACE_host_variable_sub_with_time (MSG_get_clock(), host, variable, value);
200 }
201
202 void TRACE_host_variable_set_with_time (double time, const char *host, const char *variable, double value)
203 {
204   instr_user_variable(time, host, variable, "HOST", value, INSTR_US_SET, NULL);
205 }
206
207 void TRACE_host_variable_add_with_time (double time, const char *host, const char *variable, double value)
208 {
209   instr_user_variable(time, host, variable, "HOST", value, INSTR_US_ADD, NULL);
210 }
211
212 void TRACE_host_variable_sub_with_time (double time, const char *host, const char *variable, double value)
213 {
214   instr_user_variable(time, host, variable, "HOST", value, INSTR_US_SUB, NULL);
215 }
216
217 /* for link variables */
218 void TRACE_link_variable_declare (const char *var)
219 {
220   instr_user_variable (0, NULL, var, "LINK", 0, INSTR_US_DECLARE, NULL);
221 }
222
223 void TRACE_link_variable_declare_with_color (const char *var, const char *color)
224 {
225   instr_user_variable (0, NULL, var, "LINK", 0, INSTR_US_DECLARE, color);
226 }
227
228 void TRACE_link_variable_set (const char *link, const char *variable, double value)
229 {
230   TRACE_link_variable_set_with_time (MSG_get_clock(), link, variable, value);
231 }
232
233 void TRACE_link_variable_add (const char *link, const char *variable, double value)
234 {
235   TRACE_link_variable_add_with_time (MSG_get_clock(), link, variable, value);
236 }
237
238 void TRACE_link_variable_sub (const char *link, const char *variable, double value)
239 {
240   TRACE_link_variable_sub_with_time (MSG_get_clock(), link, variable, value);
241 }
242
243 void TRACE_link_variable_set_with_time (double time, const char *link, const char *variable, double value)
244 {
245   instr_user_variable (time, link, variable, "LINK", value, INSTR_US_SET, NULL);
246 }
247
248 void TRACE_link_variable_add_with_time (double time, const char *link, const char *variable, double value)
249 {
250   instr_user_variable (time, link, variable, "LINK", value, INSTR_US_ADD, NULL);
251 }
252
253 void TRACE_link_variable_sub_with_time (double time, const char *link, const char *variable, double value)
254 {
255   instr_user_variable (time, link, variable, "LINK", value, INSTR_US_SUB, NULL);
256 }
257
258 /* for link variables, but with src and dst used for get_route */
259 void TRACE_link_srcdst_variable_set (const char *src, const char *dst, const char *variable, double value)
260 {
261   TRACE_link_srcdst_variable_set_with_time (MSG_get_clock(), src, dst, variable, value);
262 }
263
264 void TRACE_link_srcdst_variable_add (const char *src, const char *dst, const char *variable, double value)
265 {
266   TRACE_link_srcdst_variable_add_with_time (MSG_get_clock(), src, dst, variable, value);
267 }
268
269 void TRACE_link_srcdst_variable_sub (const char *src, const char *dst, const char *variable, double value)
270 {
271   TRACE_link_srcdst_variable_sub_with_time (MSG_get_clock(), src, dst, variable, value);
272 }
273
274 void TRACE_link_srcdst_variable_set_with_time (double time, const char *src, const char *dst, const char *variable, double value)
275 {
276   instr_user_srcdst_variable (time, src, dst, variable, "LINK", value, INSTR_US_SET);
277 }
278
279 void TRACE_link_srcdst_variable_add_with_time (double time, const char *src, const char *dst, const char *variable, double value)
280 {
281   instr_user_srcdst_variable (time, src, dst, variable, "LINK", value, INSTR_US_ADD);
282 }
283
284 void TRACE_link_srcdst_variable_sub_with_time (double time, const char *src, const char *dst, const char *variable, double value)
285 {
286   instr_user_srcdst_variable (time, src, dst, variable, "LINK", value, INSTR_US_SUB);
287 }
288
289 #endif /* HAVE_TRACING */