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 /** \ingroup TRACE_user_variables
252 * \brief Declare a new user variable associated to hosts.
254 * Declare a user variable that will be associated to hosts.
255 * A user host variable can be used to trace user variables
256 * such as the number of tasks in a server, the number of
257 * clients in an application (for hosts), and so on. The color
258 * associated to this new variable will be random.
260 * \param variable The name of the new variable to be declared.
262 * \see TRACE_host_variable_declare_with_color
264 void TRACE_host_variable_declare (const char *variable)
266 instr_user_variable(0, NULL, variable, "HOST", 0, INSTR_US_DECLARE, NULL);
269 /** \ingroup TRACE_user_variables
270 * \brief Declare a new user variable associated to hosts with a color.
272 * Same as #TRACE_host_variable_declare, but associated a color
273 * to the newly created user host variable. The color needs to be
274 * a string with three numbers separated by spaces in the range [0,1].
275 * A light-gray color can be specified using "0.7 0.7 0.7" as color.
277 * \param variable The name of the new variable to be declared.
278 * \param color The color for the new variable.
281 void TRACE_host_variable_declare_with_color (const char *variable, const char *color)
283 instr_user_variable(0, NULL, variable, "HOST", 0, INSTR_US_DECLARE, color);
286 /** \ingroup TRACE_user_variables
287 * \brief Set the value of a variable of a host.
289 * \param host The name of the host to be considered.
290 * \param variable The name of the variable to be considered.
291 * \param value The new value of the variable.
293 * \see TRACE_host_variable_declare, TRACE_host_variable_add, TRACE_host_variable_sub
295 void TRACE_host_variable_set (const char *host, const char *variable, double value)
297 TRACE_host_variable_set_with_time (MSG_get_clock(), host, variable, value);
300 /** \ingroup TRACE_user_variables
301 * \brief Add a value to a variable of a host.
303 * \param host The name of the host to be considered.
304 * \param variable The name of the variable to be considered.
305 * \param value The value to be added to the variable.
307 * \see TRACE_host_variable_declare, TRACE_host_variable_set, TRACE_host_variable_sub
309 void TRACE_host_variable_add (const char *host, const char *variable, double value)
311 TRACE_host_variable_add_with_time (MSG_get_clock(), host, variable, value);
314 /** \ingroup TRACE_user_variables
315 * \brief Subtract a value from a variable of a host.
317 * \param host The name of the host to be considered.
318 * \param variable The name of the variable to be considered.
319 * \param value The value to be subtracted from the variable.
321 * \see TRACE_host_variable_declare, TRACE_host_variable_set, TRACE_host_variable_add
323 void TRACE_host_variable_sub (const char *host, const char *variable, double value)
325 TRACE_host_variable_sub_with_time (MSG_get_clock(), host, variable, value);
328 /** \ingroup TRACE_user_variables
329 * \brief Set the value of a variable of a host at a given timestamp.
331 * Same as #TRACE_host_variable_set, but let user specify
332 * the time used to trace it. Users can specify a time that
333 * is not the simulated clock time as defined by the core
334 * simulator. This allows a fine-grain control of time
335 * definition, but should be used with caution since the trace
336 * can be inconsistent if resource utilization traces are also traced.
338 * \param time The timestamp to be used to tag this change of value.
339 * \param host The name of the host to be considered.
340 * \param variable The name of the variable to be considered.
341 * \param value The new value of the variable.
343 * \see TRACE_host_variable_declare, TRACE_host_variable_add_with_time, TRACE_host_variable_sub_with_time
345 void TRACE_host_variable_set_with_time (double time, const char *host, const char *variable, double value)
347 instr_user_variable(time, host, variable, "HOST", value, INSTR_US_SET, NULL);
350 /** \ingroup TRACE_user_variables
351 * \brief Add a value to a variable of a host at a given timestamp.
353 * Same as #TRACE_host_variable_add, but let user specify
354 * the time used to trace it. Users can specify a time that
355 * is not the simulated clock time as defined by the core
356 * simulator. This allows a fine-grain control of time
357 * definition, but should be used with caution since the trace
358 * can be inconsistent if resource utilization traces are also traced.
360 * \param time The timestamp to be used to tag this change of value.
361 * \param host The name of the host to be considered.
362 * \param variable The name of the variable to be considered.
363 * \param value The value to be added to the variable.
365 * \see TRACE_host_variable_declare, TRACE_host_variable_set_with_time, TRACE_host_variable_sub_with_time
367 void TRACE_host_variable_add_with_time (double time, const char *host, const char *variable, double value)
369 instr_user_variable(time, host, variable, "HOST", value, INSTR_US_ADD, NULL);
372 /** \ingroup TRACE_user_variables
373 * \brief Subtract a value from a variable of a host at a given timestamp.
375 * Same as #TRACE_host_variable_sub, but let user specify
376 * the time used to trace it. Users can specify a time that
377 * is not the simulated clock time as defined by the core
378 * simulator. This allows a fine-grain control of time
379 * definition, but should be used with caution since the trace
380 * can be inconsistent if resource utilization traces are also traced.
382 * \param time The timestamp to be used to tag this change of value.
383 * \param host The name of the host to be considered.
384 * \param variable The name of the variable to be considered.
385 * \param value The value to be subtracted from the variable.
387 * \see TRACE_host_variable_declare, TRACE_host_variable_set_with_time, TRACE_host_variable_add_with_time
389 void TRACE_host_variable_sub_with_time (double time, const char *host, const char *variable, double value)
391 instr_user_variable(time, host, variable, "HOST", value, INSTR_US_SUB, NULL);
394 /* for link variables */
395 /** \ingroup TRACE_user_variables
396 * \brief Declare a new user variable associated to links.
398 * Declare a user variable that will be associated to links.
399 * A user link variable can be used, for example, to trace
400 * user variables such as the number of messages being
401 * transferred through network links. The color
402 * associated to this new variable will be random.
404 * \param variable The name of the new variable to be declared.
406 * \see TRACE_link_variable_declare_with_color
408 void TRACE_link_variable_declare (const char *variable)
410 instr_user_variable (0, NULL, variable, "LINK", 0, INSTR_US_DECLARE, NULL);
413 /** \ingroup TRACE_user_variables
414 * \brief Declare a new user variable associated to links with a color.
416 * Same as #TRACE_link_variable_declare, but associated a color
417 * to the newly created user link variable. The color needs to be
418 * a string with three numbers separated by spaces in the range [0,1].
419 * A light-gray color can be specified using "0.7 0.7 0.7" as color.
421 * \param variable The name of the new variable to be declared.
422 * \param color The color for the new variable.
425 void TRACE_link_variable_declare_with_color (const char *variable, const char *color)
427 instr_user_variable (0, NULL, variable, "LINK", 0, INSTR_US_DECLARE, color);
430 /** \ingroup TRACE_user_variables
431 * \brief Set the value of a variable of a link.
433 * \param link The name of the link to be considered.
434 * \param variable The name of the variable to be considered.
435 * \param value The new value of the variable.
437 * \see TRACE_link_variable_declare, TRACE_link_variable_add, TRACE_link_variable_sub
439 void TRACE_link_variable_set (const char *link, const char *variable, double value)
441 TRACE_link_variable_set_with_time (MSG_get_clock(), link, variable, value);
444 /** \ingroup TRACE_user_variables
445 * \brief Add a value to a variable of a link.
447 * \param link The name of the link to be considered.
448 * \param variable The name of the variable to be considered.
449 * \param value The value to be added to the variable.
451 * \see TRACE_link_variable_declare, TRACE_link_variable_set, TRACE_link_variable_sub
453 void TRACE_link_variable_add (const char *link, const char *variable, double value)
455 TRACE_link_variable_add_with_time (MSG_get_clock(), link, variable, value);
458 /** \ingroup TRACE_user_variables
459 * \brief Subtract a value from a variable of a link.
461 * \param link The name of the link to be considered.
462 * \param variable The name of the variable to be considered.
463 * \param value The value to be subtracted from the variable.
465 * \see TRACE_link_variable_declare, TRACE_link_variable_set, TRACE_link_variable_add
467 void TRACE_link_variable_sub (const char *link, const char *variable, double value)
469 TRACE_link_variable_sub_with_time (MSG_get_clock(), link, variable, value);
472 /** \ingroup TRACE_user_variables
473 * \brief Set the value of a variable of a link at a given timestamp.
475 * Same as #TRACE_link_variable_set, but let user specify
476 * the time used to trace it. Users can specify a time that
477 * is not the simulated clock time as defined by the core
478 * simulator. This allows a fine-grain control of time
479 * definition, but should be used with caution since the trace
480 * can be inconsistent if resource utilization traces are also traced.
482 * \param time The timestamp to be used to tag this change of value.
483 * \param link The name of the link to be considered.
484 * \param variable The name of the variable to be considered.
485 * \param value The new value of the variable.
487 * \see TRACE_link_variable_declare, TRACE_link_variable_add_with_time, TRACE_link_variable_sub_with_time
489 void TRACE_link_variable_set_with_time (double time, const char *link, const char *variable, double value)
491 instr_user_variable (time, link, variable, "LINK", value, INSTR_US_SET, NULL);
494 /** \ingroup TRACE_user_variables
495 * \brief Add a value to a variable of a link at a given timestamp.
497 * Same as #TRACE_link_variable_add, but let user specify
498 * the time used to trace it. Users can specify a time that
499 * is not the simulated clock time as defined by the core
500 * simulator. This allows a fine-grain control of time
501 * definition, but should be used with caution since the trace
502 * can be inconsistent if resource utilization traces are also traced.
504 * \param time The timestamp to be used to tag this change of value.
505 * \param link The name of the link to be considered.
506 * \param variable The name of the variable to be considered.
507 * \param value The value to be added to the variable.
509 * \see TRACE_link_variable_declare, TRACE_link_variable_set_with_time, TRACE_link_variable_sub_with_time
511 void TRACE_link_variable_add_with_time (double time, const char *link, const char *variable, double value)
513 instr_user_variable (time, link, variable, "LINK", value, INSTR_US_ADD, NULL);
516 /** \ingroup TRACE_user_variables
517 * \brief Subtract a value from a variable of a link at a given timestamp.
519 * Same as #TRACE_link_variable_sub, but let user specify
520 * the time used to trace it. Users can specify a time that
521 * is not the simulated clock time as defined by the core
522 * simulator. This allows a fine-grain control of time
523 * definition, but should be used with caution since the trace
524 * can be inconsistent if resource utilization traces are also traced.
526 * \param time The timestamp to be used to tag this change of value.
527 * \param link The name of the link to be considered.
528 * \param variable The name of the variable to be considered.
529 * \param value The value to be subtracted from the variable.
531 * \see TRACE_link_variable_declare, TRACE_link_variable_set_with_time, TRACE_link_variable_add_with_time
533 void TRACE_link_variable_sub_with_time (double time, const char *link, const char *variable, double value)
535 instr_user_variable (time, link, variable, "LINK", value, INSTR_US_SUB, NULL);
538 /* for link variables, but with src and dst used for get_route */
539 /** \ingroup TRACE_user_variables
540 * \brief Set the value of the variable present in the links connecting source and destination.
542 * Same as #TRACE_link_variable_set, but instead of providing the
543 * name of link to be considered, provide the source and destination
544 * hosts. All links that are part of the route between source and
545 * destination will have the variable set to the provided value.
547 * \param src The name of the source host for get route.
548 * \param dst The name of the destination host for get route.
549 * \param variable The name of the variable to be considered.
550 * \param value The new value of the variable.
552 * \see TRACE_link_variable_declare, TRACE_link_srcdst_variable_add, TRACE_link_srcdst_variable_sub
554 void TRACE_link_srcdst_variable_set (const char *src, const char *dst, const char *variable, double value)
556 TRACE_link_srcdst_variable_set_with_time (MSG_get_clock(), src, dst, variable, value);
559 /** \ingroup TRACE_user_variables
560 * \brief Add a value to the variable present in the links connecting source and destination.
562 * Same as #TRACE_link_variable_add, but instead of providing the
563 * name of link to be considered, provide the source and destination
564 * hosts. All links that are part of the route between source and
565 * destination will have the value passed as parameter added to
566 * the current value of the variable name to be considered.
568 * \param src The name of the source host for get route.
569 * \param dst The name of the destination host for get route.
570 * \param variable The name of the variable to be considered.
571 * \param value The value to be added to the variable.
573 * \see TRACE_link_variable_declare, TRACE_link_srcdst_variable_set, TRACE_link_srcdst_variable_sub
575 void TRACE_link_srcdst_variable_add (const char *src, const char *dst, const char *variable, double value)
577 TRACE_link_srcdst_variable_add_with_time (MSG_get_clock(), src, dst, variable, value);
580 /** \ingroup TRACE_user_variables
581 * \brief Subtract a value from the variable present in the links connecting source and destination.
583 * Same as #TRACE_link_variable_sub, but instead of providing the
584 * name of link to be considered, provide the source and destination
585 * hosts. All links that are part of the route between source and
586 * destination will have the value passed as parameter subtracted from
587 * the current value of the variable name to be considered.
589 * \param src The name of the source host for get route.
590 * \param dst The name of the destination host for get route.
591 * \param variable The name of the variable to be considered.
592 * \param value The value to be subtracted from the variable.
594 * \see TRACE_link_variable_declare, TRACE_link_srcdst_variable_set, TRACE_link_srcdst_variable_add
596 void TRACE_link_srcdst_variable_sub (const char *src, const char *dst, const char *variable, double value)
598 TRACE_link_srcdst_variable_sub_with_time (MSG_get_clock(), src, dst, variable, value);
601 /** \ingroup TRACE_user_variables
602 * \brief Set the value of the variable present in the links connecting source and destination at a given timestamp.
604 * Same as #TRACE_link_srcdst_variable_set, but let user specify
605 * the time used to trace it. Users can specify a time that
606 * is not the simulated clock time as defined by the core
607 * simulator. This allows a fine-grain control of time
608 * definition, but should be used with caution since the trace
609 * can be inconsistent if resource utilization traces are also traced.
611 * \param time The timestamp to be used to tag this change of value.
612 * \param src The name of the source host for get route.
613 * \param dst The name of the destination host for get route.
614 * \param variable The name of the variable to be considered.
615 * \param value The new value of the variable.
617 * \see TRACE_link_variable_declare, TRACE_link_srcdst_variable_add_with_time, TRACE_link_srcdst_variable_sub_with_time
619 void TRACE_link_srcdst_variable_set_with_time (double time, const char *src, const char *dst, const char *variable, double value)
621 instr_user_srcdst_variable (time, src, dst, variable, "LINK", value, INSTR_US_SET);
624 /** \ingroup TRACE_user_variables
625 * \brief Add a value to the variable present in the links connecting source and destination at a given timestamp.
627 * Same as #TRACE_link_srcdst_variable_add, but let user specify
628 * the time used to trace it. Users can specify a time that
629 * is not the simulated clock time as defined by the core
630 * simulator. This allows a fine-grain control of time
631 * definition, but should be used with caution since the trace
632 * can be inconsistent if resource utilization traces are also traced.
634 * \param time The timestamp to be used to tag this change of value.
635 * \param src The name of the source host for get route.
636 * \param dst The name of the destination host for get route.
637 * \param variable The name of the variable to be considered.
638 * \param value The value to be added to the variable.
640 * \see TRACE_link_variable_declare, TRACE_link_srcdst_variable_set_with_time, TRACE_link_srcdst_variable_sub_with_time
642 void TRACE_link_srcdst_variable_add_with_time (double time, const char *src, const char *dst, const char *variable, double value)
644 instr_user_srcdst_variable (time, src, dst, variable, "LINK", value, INSTR_US_ADD);
647 /** \ingroup TRACE_user_variables
648 * \brief Subtract a value from the variable present in the links connecting source and destination at a given timestamp.
650 * Same as #TRACE_link_srcdst_variable_sub, but let user specify
651 * the time used to trace it. Users can specify a time that
652 * is not the simulated clock time as defined by the core
653 * simulator. This allows a fine-grain control of time
654 * definition, but should be used with caution since the trace
655 * can be inconsistent if resource utilization traces are also traced.
657 * \param time The timestamp to be used to tag this change of value.
658 * \param src The name of the source host for get route.
659 * \param dst The name of the destination host for get route.
660 * \param variable The name of the variable to be considered.
661 * \param value The value to be subtracted from the variable.
663 * \see TRACE_link_variable_declare, TRACE_link_srcdst_variable_set_with_time, TRACE_link_srcdst_variable_add_with_time
665 void TRACE_link_srcdst_variable_sub_with_time (double time, const char *src, const char *dst, const char *variable, double value)
667 instr_user_srcdst_variable (time, src, dst, variable, "LINK", value, INSTR_US_SUB);
670 #endif /* HAVE_TRACING */