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 "simgrid_config.h"
10 #include "instr/instr_private.h"
11 #include "surf/network_private.h"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_api, instr, "API");
22 /** \ingroup TRACE_category
23 * \brief Declare a new category with a random color.
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.
35 * See \ref tracing_tracing for details on how to trace
36 * the (categorized) resource utilization.
38 * \param category The name of the new tracing category to be created.
40 * \see TRACE_category_with_color, MSG_task_set_category
42 void TRACE_category(const char *category)
44 TRACE_category_with_color (category, NULL);
47 /** \ingroup TRACE_category
48 * \brief Declare a new category with a color.
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.
56 * See \ref tracing_tracing for details on how to trace
57 * the (categorized) resource utilization.
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)
63 * \see MSG_task_set_category
65 void TRACE_category_with_color (const char *category, const char *color)
68 if (!TRACE_is_enabled()) return;
70 if (!(TRACE_categorized() && category != NULL)) return;
72 /* if platform is not traced, we can't deal with categories */
73 if (!TRACE_needs_platform()) return;
75 //check if category is already created
76 char *created = xbt_dict_get_or_null(created_categories, category);
78 xbt_dict_set (created_categories, category, xbt_strdup("1"), NULL);
81 char final_color[INSTR_DEFAULT_STR_SIZE];
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);
89 snprintf (final_color, INSTR_DEFAULT_STR_SIZE, "%s", color);
92 XBT_DEBUG("CAT,declare %s, %s", category, final_color);
94 //define the type of this category on top of hosts and links
95 instr_new_variable_type (category, final_color);
98 /** \ingroup TRACE_mark
99 * \brief Declare a new type for tracing mark.
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.
107 * \param mark_type The name of the new type.
111 void TRACE_declare_mark(const char *mark_type)
114 if (!TRACE_is_enabled()) return;
116 if (!mark_type) return;
118 XBT_DEBUG("MARK,declare %s", mark_type);
119 PJ_type_event_new(mark_type, NULL, PJ_type_get_root());
123 * \ingroup TRACE_mark
124 * \brief Create a new instance of a tracing mark type.
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.
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.
138 * \see TRACE_declare_mark
140 void TRACE_mark(const char *mark_type, const char *mark_value)
143 if (!TRACE_is_enabled()) return;
145 if (!mark_type || !mark_value) return;
147 XBT_DEBUG("MARK %s %s", mark_type, mark_value);
148 type_t type = PJ_type_get (mark_type, PJ_type_get_root());
150 THROWF (tracing_error, 1, "mark_type with name (%s) not declared before", mark_type);
152 val_t value = PJ_value_get (mark_value, type);
154 value = PJ_value_new (mark_value, NULL, type);
156 new_pajeNewEvent (MSG_get_clock(), PJ_container_get_root(), type, value);
159 static void instr_user_variable(double time,
160 const char *resource,
161 const char *variable,
162 const char *father_type,
164 InstrUserVariable what,
168 if (!TRACE_is_enabled()) return;
170 /* if platform is not traced, we can't deal user variables */
171 if (!TRACE_needs_platform()) return;
174 snprintf(valuestr, 100, "%g", value);
177 case INSTR_US_DECLARE:
178 instr_new_user_variable_type (father_type, variable, color);
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);
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);
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);
202 //TODO: launch exception
207 static void instr_user_srcdst_variable(double time,
210 const char *variable,
211 const char *father_type,
213 InstrUserVariable what)
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);
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);
224 routing_get_route_and_latency (src_elm, dst_elm, &route,NULL);
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);
233 int TRACE_platform_graph_export_graphviz (const char *filename)
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);
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.
250 /* for host variables */
251 void TRACE_host_variable_declare (const char *var)
253 instr_user_variable(0, NULL, var, "HOST", 0, INSTR_US_DECLARE, NULL);
256 void TRACE_host_variable_declare_with_color (const char *var, const char *color)
258 instr_user_variable(0, NULL, var, "HOST", 0, INSTR_US_DECLARE, color);
261 void TRACE_host_variable_set (const char *host, const char *variable, double value)
263 TRACE_host_variable_set_with_time (MSG_get_clock(), host, variable, value);
266 void TRACE_host_variable_add (const char *host, const char *variable, double value)
268 TRACE_host_variable_add_with_time (MSG_get_clock(), host, variable, value);
271 void TRACE_host_variable_sub (const char *host, const char *variable, double value)
273 TRACE_host_variable_sub_with_time (MSG_get_clock(), host, variable, value);
276 void TRACE_host_variable_set_with_time (double time, const char *host, const char *variable, double value)
278 instr_user_variable(time, host, variable, "HOST", value, INSTR_US_SET, NULL);
281 void TRACE_host_variable_add_with_time (double time, const char *host, const char *variable, double value)
283 instr_user_variable(time, host, variable, "HOST", value, INSTR_US_ADD, NULL);
286 void TRACE_host_variable_sub_with_time (double time, const char *host, const char *variable, double value)
288 instr_user_variable(time, host, variable, "HOST", value, INSTR_US_SUB, NULL);
291 /* for link variables */
292 void TRACE_link_variable_declare (const char *var)
294 instr_user_variable (0, NULL, var, "LINK", 0, INSTR_US_DECLARE, NULL);
297 void TRACE_link_variable_declare_with_color (const char *var, const char *color)
299 instr_user_variable (0, NULL, var, "LINK", 0, INSTR_US_DECLARE, color);
302 void TRACE_link_variable_set (const char *link, const char *variable, double value)
304 TRACE_link_variable_set_with_time (MSG_get_clock(), link, variable, value);
307 void TRACE_link_variable_add (const char *link, const char *variable, double value)
309 TRACE_link_variable_add_with_time (MSG_get_clock(), link, variable, value);
312 void TRACE_link_variable_sub (const char *link, const char *variable, double value)
314 TRACE_link_variable_sub_with_time (MSG_get_clock(), link, variable, value);
317 void TRACE_link_variable_set_with_time (double time, const char *link, const char *variable, double value)
319 instr_user_variable (time, link, variable, "LINK", value, INSTR_US_SET, NULL);
322 void TRACE_link_variable_add_with_time (double time, const char *link, const char *variable, double value)
324 instr_user_variable (time, link, variable, "LINK", value, INSTR_US_ADD, NULL);
327 void TRACE_link_variable_sub_with_time (double time, const char *link, const char *variable, double value)
329 instr_user_variable (time, link, variable, "LINK", value, INSTR_US_SUB, NULL);
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)
335 TRACE_link_srcdst_variable_set_with_time (MSG_get_clock(), src, dst, variable, value);
338 void TRACE_link_srcdst_variable_add (const char *src, const char *dst, const char *variable, double value)
340 TRACE_link_srcdst_variable_add_with_time (MSG_get_clock(), src, dst, variable, value);
343 void TRACE_link_srcdst_variable_sub (const char *src, const char *dst, const char *variable, double value)
345 TRACE_link_srcdst_variable_sub_with_time (MSG_get_clock(), src, dst, variable, value);
348 void TRACE_link_srcdst_variable_set_with_time (double time, const char *src, const char *dst, const char *variable, double value)
350 instr_user_srcdst_variable (time, src, dst, variable, "LINK", value, INSTR_US_SET);
353 void TRACE_link_srcdst_variable_add_with_time (double time, const char *src, const char *dst, const char *variable, double value)
355 instr_user_srcdst_variable (time, src, dst, variable, "LINK", value, INSTR_US_ADD);
358 void TRACE_link_srcdst_variable_sub_with_time (double time, const char *src, const char *dst, const char *variable, double value)
360 instr_user_srcdst_variable (time, src, dst, variable, "LINK", value, INSTR_US_SUB);
363 #endif /* HAVE_TRACING */