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, SD_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, SD_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 /** \ingroup TRACE_API
234 * \brief Creates a file with the topology of the platform file used for the simulator.
236 * The graph topology will have the following properties: all hosts, links and routers
237 * of the platform file are mapped to graph nodes; routes are mapped to edges.
238 * The platform's AS are not represented in the output.
240 * \param filename The name of the file that will hold the graph.
242 * \return 1 of successful, 0 otherwise.
244 int TRACE_platform_graph_export_graphviz (const char *filename)
246 /* returns 1 if successful, 0 otherwise */
247 if (!TRACE_is_enabled()) return 0;
248 xbt_graph_t g = instr_routing_platform_graph();
249 if (g == NULL) return 0;
250 instr_routing_platform_graph_export_graphviz (g, filename);
251 xbt_graph_free_graph (g, xbt_free, xbt_free, NULL);
256 * Derived functions that use instr_user_variable and TRACE_user_srcdst_variable.
257 * They were previously defined as pre-processors directives, but were transformed
258 * into functions so the user can track them using gdb.
261 /* for host variables */
262 /** \ingroup TRACE_user_variables
263 * \brief Declare a new user variable associated to hosts.
265 * Declare a user variable that will be associated to hosts.
266 * A user host variable can be used to trace user variables
267 * such as the number of tasks in a server, the number of
268 * clients in an application (for hosts), and so on. The color
269 * associated to this new variable will be random.
271 * \param variable The name of the new variable to be declared.
273 * \see TRACE_host_variable_declare_with_color
275 void TRACE_host_variable_declare (const char *variable)
277 instr_user_variable(0, NULL, variable, "HOST", 0, INSTR_US_DECLARE, NULL);
280 /** \ingroup TRACE_user_variables
281 * \brief Declare a new user variable associated to hosts with a color.
283 * Same as #TRACE_host_variable_declare, but associated a color
284 * to the newly created user host variable. The color needs to be
285 * a string with three numbers separated by spaces in the range [0,1].
286 * A light-gray color can be specified using "0.7 0.7 0.7" as color.
288 * \param variable The name of the new variable to be declared.
289 * \param color The color for the new variable.
292 void TRACE_host_variable_declare_with_color (const char *variable, const char *color)
294 instr_user_variable(0, NULL, variable, "HOST", 0, INSTR_US_DECLARE, color);
297 /** \ingroup TRACE_user_variables
298 * \brief Set the value of a variable of a host.
300 * \param host The name of the host to be considered.
301 * \param variable The name of the variable to be considered.
302 * \param value The new value of the variable.
304 * \see TRACE_host_variable_declare, TRACE_host_variable_add, TRACE_host_variable_sub
306 void TRACE_host_variable_set (const char *host, const char *variable, double value)
308 TRACE_host_variable_set_with_time (MSG_get_clock(), host, variable, value);
311 /** \ingroup TRACE_user_variables
312 * \brief Add a value to a variable of a host.
314 * \param host The name of the host to be considered.
315 * \param variable The name of the variable to be considered.
316 * \param value The value to be added to the variable.
318 * \see TRACE_host_variable_declare, TRACE_host_variable_set, TRACE_host_variable_sub
320 void TRACE_host_variable_add (const char *host, const char *variable, double value)
322 TRACE_host_variable_add_with_time (MSG_get_clock(), host, variable, value);
325 /** \ingroup TRACE_user_variables
326 * \brief Subtract a value from a variable of a host.
328 * \param host The name of the host to be considered.
329 * \param variable The name of the variable to be considered.
330 * \param value The value to be subtracted from the variable.
332 * \see TRACE_host_variable_declare, TRACE_host_variable_set, TRACE_host_variable_add
334 void TRACE_host_variable_sub (const char *host, const char *variable, double value)
336 TRACE_host_variable_sub_with_time (MSG_get_clock(), host, variable, value);
339 /** \ingroup TRACE_user_variables
340 * \brief Set the value of a variable of a host at a given timestamp.
342 * Same as #TRACE_host_variable_set, but let user specify
343 * the time used to trace it. Users can specify a time that
344 * is not the simulated clock time as defined by the core
345 * simulator. This allows a fine-grain control of time
346 * definition, but should be used with caution since the trace
347 * can be inconsistent if resource utilization traces are also traced.
349 * \param time The timestamp to be used to tag this change of value.
350 * \param host The name of the host to be considered.
351 * \param variable The name of the variable to be considered.
352 * \param value The new value of the variable.
354 * \see TRACE_host_variable_declare, TRACE_host_variable_add_with_time, TRACE_host_variable_sub_with_time
356 void TRACE_host_variable_set_with_time (double time, const char *host, const char *variable, double value)
358 instr_user_variable(time, host, variable, "HOST", value, INSTR_US_SET, NULL);
361 /** \ingroup TRACE_user_variables
362 * \brief Add a value to a variable of a host at a given timestamp.
364 * Same as #TRACE_host_variable_add, but let user specify
365 * the time used to trace it. Users can specify a time that
366 * is not the simulated clock time as defined by the core
367 * simulator. This allows a fine-grain control of time
368 * definition, but should be used with caution since the trace
369 * can be inconsistent if resource utilization traces are also traced.
371 * \param time The timestamp to be used to tag this change of value.
372 * \param host The name of the host to be considered.
373 * \param variable The name of the variable to be considered.
374 * \param value The value to be added to the variable.
376 * \see TRACE_host_variable_declare, TRACE_host_variable_set_with_time, TRACE_host_variable_sub_with_time
378 void TRACE_host_variable_add_with_time (double time, const char *host, const char *variable, double value)
380 instr_user_variable(time, host, variable, "HOST", value, INSTR_US_ADD, NULL);
383 /** \ingroup TRACE_user_variables
384 * \brief Subtract a value from a variable of a host at a given timestamp.
386 * Same as #TRACE_host_variable_sub, but let user specify
387 * the time used to trace it. Users can specify a time that
388 * is not the simulated clock time as defined by the core
389 * simulator. This allows a fine-grain control of time
390 * definition, but should be used with caution since the trace
391 * can be inconsistent if resource utilization traces are also traced.
393 * \param time The timestamp to be used to tag this change of value.
394 * \param host The name of the host to be considered.
395 * \param variable The name of the variable to be considered.
396 * \param value The value to be subtracted from the variable.
398 * \see TRACE_host_variable_declare, TRACE_host_variable_set_with_time, TRACE_host_variable_add_with_time
400 void TRACE_host_variable_sub_with_time (double time, const char *host, const char *variable, double value)
402 instr_user_variable(time, host, variable, "HOST", value, INSTR_US_SUB, NULL);
405 /* for link variables */
406 /** \ingroup TRACE_user_variables
407 * \brief Declare a new user variable associated to links.
409 * Declare a user variable that will be associated to links.
410 * A user link variable can be used, for example, to trace
411 * user variables such as the number of messages being
412 * transferred through network links. The color
413 * associated to this new variable will be random.
415 * \param variable The name of the new variable to be declared.
417 * \see TRACE_link_variable_declare_with_color
419 void TRACE_link_variable_declare (const char *variable)
421 instr_user_variable (0, NULL, variable, "LINK", 0, INSTR_US_DECLARE, NULL);
424 /** \ingroup TRACE_user_variables
425 * \brief Declare a new user variable associated to links with a color.
427 * Same as #TRACE_link_variable_declare, but associated a color
428 * to the newly created user link variable. The color needs to be
429 * a string with three numbers separated by spaces in the range [0,1].
430 * A light-gray color can be specified using "0.7 0.7 0.7" as color.
432 * \param variable The name of the new variable to be declared.
433 * \param color The color for the new variable.
436 void TRACE_link_variable_declare_with_color (const char *variable, const char *color)
438 instr_user_variable (0, NULL, variable, "LINK", 0, INSTR_US_DECLARE, color);
441 /** \ingroup TRACE_user_variables
442 * \brief Set the value of a variable of a link.
444 * \param link The name of the link to be considered.
445 * \param variable The name of the variable to be considered.
446 * \param value The new value of the variable.
448 * \see TRACE_link_variable_declare, TRACE_link_variable_add, TRACE_link_variable_sub
450 void TRACE_link_variable_set (const char *link, const char *variable, double value)
452 TRACE_link_variable_set_with_time (MSG_get_clock(), link, variable, value);
455 /** \ingroup TRACE_user_variables
456 * \brief Add a value to a variable of a link.
458 * \param link The name of the link to be considered.
459 * \param variable The name of the variable to be considered.
460 * \param value The value to be added to the variable.
462 * \see TRACE_link_variable_declare, TRACE_link_variable_set, TRACE_link_variable_sub
464 void TRACE_link_variable_add (const char *link, const char *variable, double value)
466 TRACE_link_variable_add_with_time (MSG_get_clock(), link, variable, value);
469 /** \ingroup TRACE_user_variables
470 * \brief Subtract a value from a variable of a link.
472 * \param link The name of the link to be considered.
473 * \param variable The name of the variable to be considered.
474 * \param value The value to be subtracted from the variable.
476 * \see TRACE_link_variable_declare, TRACE_link_variable_set, TRACE_link_variable_add
478 void TRACE_link_variable_sub (const char *link, const char *variable, double value)
480 TRACE_link_variable_sub_with_time (MSG_get_clock(), link, variable, value);
483 /** \ingroup TRACE_user_variables
484 * \brief Set the value of a variable of a link at a given timestamp.
486 * Same as #TRACE_link_variable_set, but let user specify
487 * the time used to trace it. Users can specify a time that
488 * is not the simulated clock time as defined by the core
489 * simulator. This allows a fine-grain control of time
490 * definition, but should be used with caution since the trace
491 * can be inconsistent if resource utilization traces are also traced.
493 * \param time The timestamp to be used to tag this change of value.
494 * \param link The name of the link to be considered.
495 * \param variable The name of the variable to be considered.
496 * \param value The new value of the variable.
498 * \see TRACE_link_variable_declare, TRACE_link_variable_add_with_time, TRACE_link_variable_sub_with_time
500 void TRACE_link_variable_set_with_time (double time, const char *link, const char *variable, double value)
502 instr_user_variable (time, link, variable, "LINK", value, INSTR_US_SET, NULL);
505 /** \ingroup TRACE_user_variables
506 * \brief Add a value to a variable of a link at a given timestamp.
508 * Same as #TRACE_link_variable_add, but let user specify
509 * the time used to trace it. Users can specify a time that
510 * is not the simulated clock time as defined by the core
511 * simulator. This allows a fine-grain control of time
512 * definition, but should be used with caution since the trace
513 * can be inconsistent if resource utilization traces are also traced.
515 * \param time The timestamp to be used to tag this change of value.
516 * \param link The name of the link to be considered.
517 * \param variable The name of the variable to be considered.
518 * \param value The value to be added to the variable.
520 * \see TRACE_link_variable_declare, TRACE_link_variable_set_with_time, TRACE_link_variable_sub_with_time
522 void TRACE_link_variable_add_with_time (double time, const char *link, const char *variable, double value)
524 instr_user_variable (time, link, variable, "LINK", value, INSTR_US_ADD, NULL);
527 /** \ingroup TRACE_user_variables
528 * \brief Subtract a value from a variable of a link at a given timestamp.
530 * Same as #TRACE_link_variable_sub, but let user specify
531 * the time used to trace it. Users can specify a time that
532 * is not the simulated clock time as defined by the core
533 * simulator. This allows a fine-grain control of time
534 * definition, but should be used with caution since the trace
535 * can be inconsistent if resource utilization traces are also traced.
537 * \param time The timestamp to be used to tag this change of value.
538 * \param link The name of the link to be considered.
539 * \param variable The name of the variable to be considered.
540 * \param value The value to be subtracted from the variable.
542 * \see TRACE_link_variable_declare, TRACE_link_variable_set_with_time, TRACE_link_variable_add_with_time
544 void TRACE_link_variable_sub_with_time (double time, const char *link, const char *variable, double value)
546 instr_user_variable (time, link, variable, "LINK", value, INSTR_US_SUB, NULL);
549 /* for link variables, but with src and dst used for get_route */
550 /** \ingroup TRACE_user_variables
551 * \brief Set the value of the variable present in the links connecting source and destination.
553 * Same as #TRACE_link_variable_set, but instead of providing the
554 * name of link to be considered, provide the source and destination
555 * hosts. All links that are part of the route between source and
556 * destination will have the variable set to the provided value.
558 * \param src The name of the source host for get route.
559 * \param dst The name of the destination host for get route.
560 * \param variable The name of the variable to be considered.
561 * \param value The new value of the variable.
563 * \see TRACE_link_variable_declare, TRACE_link_srcdst_variable_add, TRACE_link_srcdst_variable_sub
565 void TRACE_link_srcdst_variable_set (const char *src, const char *dst, const char *variable, double value)
567 TRACE_link_srcdst_variable_set_with_time (MSG_get_clock(), src, dst, variable, value);
570 /** \ingroup TRACE_user_variables
571 * \brief Add a value to the variable present in the links connecting source and destination.
573 * Same as #TRACE_link_variable_add, but instead of providing the
574 * name of link to be considered, provide the source and destination
575 * hosts. All links that are part of the route between source and
576 * destination will have the value passed as parameter added to
577 * the current value of the variable name to be considered.
579 * \param src The name of the source host for get route.
580 * \param dst The name of the destination host for get route.
581 * \param variable The name of the variable to be considered.
582 * \param value The value to be added to the variable.
584 * \see TRACE_link_variable_declare, TRACE_link_srcdst_variable_set, TRACE_link_srcdst_variable_sub
586 void TRACE_link_srcdst_variable_add (const char *src, const char *dst, const char *variable, double value)
588 TRACE_link_srcdst_variable_add_with_time (MSG_get_clock(), src, dst, variable, value);
591 /** \ingroup TRACE_user_variables
592 * \brief Subtract a value from the variable present in the links connecting source and destination.
594 * Same as #TRACE_link_variable_sub, but instead of providing the
595 * name of link to be considered, provide the source and destination
596 * hosts. All links that are part of the route between source and
597 * destination will have the value passed as parameter subtracted from
598 * the current value of the variable name to be considered.
600 * \param src The name of the source host for get route.
601 * \param dst The name of the destination host for get route.
602 * \param variable The name of the variable to be considered.
603 * \param value The value to be subtracted from the variable.
605 * \see TRACE_link_variable_declare, TRACE_link_srcdst_variable_set, TRACE_link_srcdst_variable_add
607 void TRACE_link_srcdst_variable_sub (const char *src, const char *dst, const char *variable, double value)
609 TRACE_link_srcdst_variable_sub_with_time (MSG_get_clock(), src, dst, variable, value);
612 /** \ingroup TRACE_user_variables
613 * \brief Set the value of the variable present in the links connecting source and destination at a given timestamp.
615 * Same as #TRACE_link_srcdst_variable_set, but let user specify
616 * the time used to trace it. Users can specify a time that
617 * is not the simulated clock time as defined by the core
618 * simulator. This allows a fine-grain control of time
619 * definition, but should be used with caution since the trace
620 * can be inconsistent if resource utilization traces are also traced.
622 * \param time The timestamp to be used to tag this change of value.
623 * \param src The name of the source host for get route.
624 * \param dst The name of the destination host for get route.
625 * \param variable The name of the variable to be considered.
626 * \param value The new value of the variable.
628 * \see TRACE_link_variable_declare, TRACE_link_srcdst_variable_add_with_time, TRACE_link_srcdst_variable_sub_with_time
630 void TRACE_link_srcdst_variable_set_with_time (double time, const char *src, const char *dst, const char *variable, double value)
632 instr_user_srcdst_variable (time, src, dst, variable, "LINK", value, INSTR_US_SET);
635 /** \ingroup TRACE_user_variables
636 * \brief Add a value to the variable present in the links connecting source and destination at a given timestamp.
638 * Same as #TRACE_link_srcdst_variable_add, but let user specify
639 * the time used to trace it. Users can specify a time that
640 * is not the simulated clock time as defined by the core
641 * simulator. This allows a fine-grain control of time
642 * definition, but should be used with caution since the trace
643 * can be inconsistent if resource utilization traces are also traced.
645 * \param time The timestamp to be used to tag this change of value.
646 * \param src The name of the source host for get route.
647 * \param dst The name of the destination host for get route.
648 * \param variable The name of the variable to be considered.
649 * \param value The value to be added to the variable.
651 * \see TRACE_link_variable_declare, TRACE_link_srcdst_variable_set_with_time, TRACE_link_srcdst_variable_sub_with_time
653 void TRACE_link_srcdst_variable_add_with_time (double time, const char *src, const char *dst, const char *variable, double value)
655 instr_user_srcdst_variable (time, src, dst, variable, "LINK", value, INSTR_US_ADD);
658 /** \ingroup TRACE_user_variables
659 * \brief Subtract a value from the variable present in the links connecting source and destination at a given timestamp.
661 * Same as #TRACE_link_srcdst_variable_sub, but let user specify
662 * the time used to trace it. Users can specify a time that
663 * is not the simulated clock time as defined by the core
664 * simulator. This allows a fine-grain control of time
665 * definition, but should be used with caution since the trace
666 * can be inconsistent if resource utilization traces are also traced.
668 * \param time The timestamp to be used to tag this change of value.
669 * \param src The name of the source host for get route.
670 * \param dst The name of the destination host for get route.
671 * \param variable The name of the variable to be considered.
672 * \param value The value to be subtracted from the variable.
674 * \see TRACE_link_variable_declare, TRACE_link_srcdst_variable_set_with_time, TRACE_link_srcdst_variable_add_with_time
676 void TRACE_link_srcdst_variable_sub_with_time (double time, const char *src, const char *dst, const char *variable, double value)
678 instr_user_srcdst_variable (time, src, dst, variable, "LINK", value, INSTR_US_SUB);
681 #endif /* HAVE_TRACING */