Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] update examples with the new API for setting task category
[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 #include "instr/instr_private.h"
11 #include "surf/network_private.h"
12
13 typedef enum {
14   INSTR_US_DECLARE,
15   INSTR_US_SET,
16   INSTR_US_ADD,
17   INSTR_US_SUB
18 } InstrUserVariable;
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_api, instr, "API");
21
22 /** \ingroup TRACE_category
23  *  \brief Declare a new category with a random color.
24  *
25  *  This function should be used to define a user category. The
26  *  category can be used to differentiate the tasks that are created
27  *  during the simulation (for example, tasks from server1, server2,
28  *  or request tasks, computation tasks, communication tasks). All
29  *  resource utilization (host power and link bandwidth) will be
30  *  classified according to the task category. Tasks that do not
31  *  belong to a category are not traced. The color for the category
32  *  that is being declared is random. This function has no effect
33  *  if a category with the same name has been already declared.
34  *
35  * See \ref tracing_tracing for details on how to trace
36  * the (categorized) resource utilization.
37  *
38  *  \param category The name of the new tracing category to be created.
39  *
40  *  \see TRACE_category_with_color, MSG_task_set_category
41  */
42 void TRACE_category(const char *category)
43 {
44   TRACE_category_with_color (category, NULL);
45 }
46
47 /** \ingroup TRACE_category
48  *  \brief Declare a new category with a color.
49  *
50  *  Same as #TRACE_category, but let user specify a color encoded as a
51  *  RGB-like string with three floats from 0 to 1. So, to specify a
52  *  red color, pass "1 0 0" as color parameter. A light-gray color
53  *  can be specified using "0.7 0.7 0.7" as color. This function has
54  *  no effect if a category with the same name has been already declared.
55  *
56  * See \ref tracing_tracing for details on how to trace
57  * the (categorized) resource utilization.
58  *
59  *  \param category The name of the new tracing category to be created.
60  *  \param color The color of the category (see \ref tracing_tracing to
61  *  know how to correctly specify the color)
62  *
63  *  \see MSG_task_set_category
64  */
65 void TRACE_category_with_color (const char *category, const char *color)
66 {
67   /* safe switch */
68   if (!TRACE_is_enabled()) return;
69
70   if (!(TRACE_categorized() && category != NULL)) return;
71
72   /* if platform is not traced, we can't deal with categories */
73   if (!TRACE_needs_platform()) return;
74
75   //check if category is already created
76   char *created = xbt_dict_get_or_null(created_categories, category);
77   if (created) return;
78   xbt_dict_set (created_categories, category, xbt_strdup("1"), NULL);
79
80   //define final_color
81   char final_color[INSTR_DEFAULT_STR_SIZE];
82   if (!color){
83     //generate a random color
84     double red = drand48();
85     double green = drand48();
86     double blue = drand48();
87     snprintf (final_color, INSTR_DEFAULT_STR_SIZE, "%f %f %f", red, green, blue);
88   }else{
89     snprintf (final_color, INSTR_DEFAULT_STR_SIZE, "%s", color);
90   }
91
92   XBT_DEBUG("CAT,declare %s, %s", category, final_color);
93
94   //define the type of this category on top of hosts and links
95   instr_new_variable_type (category, final_color);
96 }
97
98 /** \ingroup TRACE_mark
99  * \brief Declare a new type for tracing mark.
100  *
101  * This function declares a new Paje event
102  * type in the trace file that can be used by
103  * simulators to declare application-level
104  * marks. This function is independent of
105  * which API is used in SimGrid.
106  *
107  * \param mark_type The name of the new type.
108  *
109  * \see TRACE_mark
110  */
111 void TRACE_declare_mark(const char *mark_type)
112 {
113   /* safe switch */
114   if (!TRACE_is_enabled()) return;
115
116   if (!mark_type) return;
117
118   XBT_DEBUG("MARK,declare %s", mark_type);
119   PJ_type_event_new(mark_type, NULL, PJ_type_get_root());
120 }
121
122 /**
123  * \ingroup TRACE_mark
124  * \brief Create a new instance of a tracing mark type.
125  *
126  * This function creates a mark in the trace file. The
127  * first parameter had to be previously declared using
128  * #TRACE_declare_mark, the second is the identifier
129  * for this mark instance. We recommend that the
130  * mark_value is a unique value for the whole simulation.
131  * Nevertheless, this is not a strong requirement: the
132  * trace will be valid even if there are multiple mark
133  * identifiers for the same trace.
134  *
135  * \param mark_type The name of the type for which the new instance will belong.
136  * \param mark_value The name of the new instance mark.
137  *
138  * \see TRACE_declare_mark
139  */
140 void TRACE_mark(const char *mark_type, const char *mark_value)
141 {
142   /* safe switch */
143   if (!TRACE_is_enabled()) return;
144
145   if (!mark_type || !mark_value) return;
146
147   XBT_DEBUG("MARK %s %s", mark_type, mark_value);
148   type_t type = PJ_type_get (mark_type, PJ_type_get_root());
149   if (type == NULL){
150     THROWF (tracing_error, 1, "mark_type with name (%s) not declared before", mark_type);
151   }
152   val_t value = PJ_value_get (mark_value, type);
153   if (value == NULL){
154     value = PJ_value_new (mark_value, NULL, type);
155   }
156   new_pajeNewEvent (MSG_get_clock(), PJ_container_get_root(), type, value);
157 }
158
159 static void instr_user_variable(double time,
160                          const char *resource,
161                          const char *variable,
162                          const char *father_type,
163                          double value,
164                          InstrUserVariable what,
165                          const char *color)
166 {
167   /* safe switch */
168   if (!TRACE_is_enabled()) return;
169
170   /* if platform is not traced, we can't deal user variables */
171   if (!TRACE_needs_platform()) return;
172
173   char valuestr[100];
174   snprintf(valuestr, 100, "%g", value);
175
176   switch (what){
177   case INSTR_US_DECLARE:
178     instr_new_user_variable_type (father_type, variable, color);
179     break;
180   case INSTR_US_SET:
181   {
182     container_t container = PJ_container_get(resource);
183     type_t type = PJ_type_get (variable, container->type);
184     new_pajeSetVariable(time, container, type, value);
185     break;
186   }
187   case INSTR_US_ADD:
188   {
189     container_t container = PJ_container_get(resource);
190     type_t type = PJ_type_get (variable, container->type);
191     new_pajeAddVariable(time, container, type, value);
192     break;
193   }
194   case INSTR_US_SUB:
195   {
196     container_t container = PJ_container_get(resource);
197     type_t type = PJ_type_get (variable, container->type);
198     new_pajeSubVariable(time, container, type, value);
199     break;
200   }
201   default:
202     //TODO: launch exception
203     break;
204   }
205 }
206
207 static void instr_user_srcdst_variable(double time,
208                               const char *src,
209                               const char *dst,
210                               const char *variable,
211                               const char *father_type,
212                               double value,
213                               InstrUserVariable what)
214 {
215   xbt_dynar_t route=NULL;
216   network_element_t src_elm = xbt_lib_get_or_null(host_lib,src,ROUTING_HOST_LEVEL);
217   if(!src_elm) src_elm = xbt_lib_get_or_null(as_router_lib,src,ROUTING_ASR_LEVEL);
218   if(!src_elm) xbt_die("Element '%s' not found!",src);
219
220   network_element_t dst_elm = xbt_lib_get_or_null(host_lib,dst,ROUTING_HOST_LEVEL);
221   if(!dst_elm) dst_elm = xbt_lib_get_or_null(as_router_lib,dst,ROUTING_ASR_LEVEL);
222   if(!dst_elm) xbt_die("Element '%s' not found!",dst);
223
224   routing_get_route_and_latency (src_elm, dst_elm, &route,NULL);
225   unsigned int i;
226   void *link;
227   xbt_dynar_foreach (route, i, link) {
228     char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
229     instr_user_variable (time, link_name, variable, father_type, value, what, NULL);
230   }
231 }
232
233 int TRACE_platform_graph_export_graphviz (const char *filename)
234 {
235   /* returns 1 if successful, 0 otherwise */
236   if (!TRACE_is_enabled()) return 0;
237   xbt_graph_t g = instr_routing_platform_graph();
238   if (g == NULL) return 0;
239   instr_routing_platform_graph_export_graphviz (g, filename);
240   xbt_graph_free_graph (g, xbt_free, xbt_free, NULL);
241   return 1;
242 }
243
244 /*
245  * Derived functions that use instr_user_variable and TRACE_user_srcdst_variable.
246  * They were previously defined as pre-processors directives, but were transformed
247  * into functions so the user can track them using gdb.
248  */
249
250 /* for host variables */
251 void TRACE_host_variable_declare (const char *var)
252 {
253   instr_user_variable(0, NULL, var, "HOST", 0, INSTR_US_DECLARE, NULL);
254 }
255
256 void TRACE_host_variable_declare_with_color (const char *var, const char *color)
257 {
258   instr_user_variable(0, NULL, var, "HOST", 0, INSTR_US_DECLARE, color);
259 }
260
261 void TRACE_host_variable_set (const char *host, const char *variable, double value)
262 {
263   TRACE_host_variable_set_with_time (MSG_get_clock(), host, variable, value);
264 }
265
266 void TRACE_host_variable_add (const char *host, const char *variable, double value)
267 {
268   TRACE_host_variable_add_with_time (MSG_get_clock(), host, variable, value);
269 }
270
271 void TRACE_host_variable_sub (const char *host, const char *variable, double value)
272 {
273   TRACE_host_variable_sub_with_time (MSG_get_clock(), host, variable, value);
274 }
275
276 void TRACE_host_variable_set_with_time (double time, const char *host, const char *variable, double value)
277 {
278   instr_user_variable(time, host, variable, "HOST", value, INSTR_US_SET, NULL);
279 }
280
281 void TRACE_host_variable_add_with_time (double time, const char *host, const char *variable, double value)
282 {
283   instr_user_variable(time, host, variable, "HOST", value, INSTR_US_ADD, NULL);
284 }
285
286 void TRACE_host_variable_sub_with_time (double time, const char *host, const char *variable, double value)
287 {
288   instr_user_variable(time, host, variable, "HOST", value, INSTR_US_SUB, NULL);
289 }
290
291 /* for link variables */
292 void TRACE_link_variable_declare (const char *var)
293 {
294   instr_user_variable (0, NULL, var, "LINK", 0, INSTR_US_DECLARE, NULL);
295 }
296
297 void TRACE_link_variable_declare_with_color (const char *var, const char *color)
298 {
299   instr_user_variable (0, NULL, var, "LINK", 0, INSTR_US_DECLARE, color);
300 }
301
302 void TRACE_link_variable_set (const char *link, const char *variable, double value)
303 {
304   TRACE_link_variable_set_with_time (MSG_get_clock(), link, variable, value);
305 }
306
307 void TRACE_link_variable_add (const char *link, const char *variable, double value)
308 {
309   TRACE_link_variable_add_with_time (MSG_get_clock(), link, variable, value);
310 }
311
312 void TRACE_link_variable_sub (const char *link, const char *variable, double value)
313 {
314   TRACE_link_variable_sub_with_time (MSG_get_clock(), link, variable, value);
315 }
316
317 void TRACE_link_variable_set_with_time (double time, const char *link, const char *variable, double value)
318 {
319   instr_user_variable (time, link, variable, "LINK", value, INSTR_US_SET, NULL);
320 }
321
322 void TRACE_link_variable_add_with_time (double time, const char *link, const char *variable, double value)
323 {
324   instr_user_variable (time, link, variable, "LINK", value, INSTR_US_ADD, NULL);
325 }
326
327 void TRACE_link_variable_sub_with_time (double time, const char *link, const char *variable, double value)
328 {
329   instr_user_variable (time, link, variable, "LINK", value, INSTR_US_SUB, NULL);
330 }
331
332 /* for link variables, but with src and dst used for get_route */
333 void TRACE_link_srcdst_variable_set (const char *src, const char *dst, const char *variable, double value)
334 {
335   TRACE_link_srcdst_variable_set_with_time (MSG_get_clock(), src, dst, variable, value);
336 }
337
338 void TRACE_link_srcdst_variable_add (const char *src, const char *dst, const char *variable, double value)
339 {
340   TRACE_link_srcdst_variable_add_with_time (MSG_get_clock(), src, dst, variable, value);
341 }
342
343 void TRACE_link_srcdst_variable_sub (const char *src, const char *dst, const char *variable, double value)
344 {
345   TRACE_link_srcdst_variable_sub_with_time (MSG_get_clock(), src, dst, variable, value);
346 }
347
348 void TRACE_link_srcdst_variable_set_with_time (double time, const char *src, const char *dst, const char *variable, double value)
349 {
350   instr_user_srcdst_variable (time, src, dst, variable, "LINK", value, INSTR_US_SET);
351 }
352
353 void TRACE_link_srcdst_variable_add_with_time (double time, const char *src, const char *dst, const char *variable, double value)
354 {
355   instr_user_srcdst_variable (time, src, dst, variable, "LINK", value, INSTR_US_ADD);
356 }
357
358 void TRACE_link_srcdst_variable_sub_with_time (double time, const char *src, const char *dst, const char *variable, double value)
359 {
360   instr_user_srcdst_variable (time, src, dst, variable, "LINK", value, INSTR_US_SUB);
361 }
362
363 #endif /* HAVE_TRACING */