1 /* Copyright (c) 2010-2022. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include <simgrid/Exception.hpp>
7 #include <simgrid/kernel/routing/NetPoint.hpp>
8 #include <simgrid/s4u/Host.hpp>
9 #include <xbt/random.hpp>
11 #include "src/instr/instr_private.hpp"
12 #include "src/kernel/resource/StandardLinkImpl.hpp"
16 enum class InstrUserVariable { DECLARE, SET, ADD, SUB };
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_api, instr, "API");
20 std::set<std::string, std::less<>> created_categories;
21 std::set<std::string, std::less<>> declared_marks;
22 std::set<std::string, std::less<>> user_host_variables;
23 std::set<std::string, std::less<>> user_vm_variables;
24 std::set<std::string, std::less<>> user_link_variables;
26 static void instr_user_variable(double time, const char* resource, const std::string& variable_name,
27 const std::string& parent_type, double value, InstrUserVariable what,
28 const std::string& color, std::set<std::string, std::less<>>* filter)
30 /* safe switches. tracing has to be activated and if platform is not traced, we don't allow user variables */
31 if (not TRACE_is_enabled() || not TRACE_needs_platform())
34 // check if variable is already declared
35 auto created = filter->find(variable_name);
36 if (what == InstrUserVariable::DECLARE) {
37 if (created == filter->end()) { // not declared yet
38 filter->insert(variable_name);
39 instr_new_user_variable_type(parent_type, variable_name, color);
42 if (created != filter->end()) { // declared, let's work
43 simgrid::instr::VariableType* variable =
44 simgrid::instr::Container::by_name(resource)->get_variable(variable_name);
46 case InstrUserVariable::SET:
47 variable->set_event(time, value);
49 case InstrUserVariable::ADD:
50 variable->add_event(time, value);
52 case InstrUserVariable::SUB:
53 variable->sub_event(time, value);
62 static void instr_user_srcdst_variable(double time, const char* src, const char* dst, const std::string& variable,
63 const std::string& parent_type, double value, InstrUserVariable what)
65 const simgrid::kernel::routing::NetPoint* src_elm = sg_netpoint_by_name_or_null(src);
66 xbt_assert(src_elm, "Element '%s' not found!", src);
68 const simgrid::kernel::routing::NetPoint* dst_elm = sg_netpoint_by_name_or_null(dst);
69 xbt_assert(dst_elm, "Element '%s' not found!", dst);
71 std::vector<simgrid::kernel::resource::StandardLinkImpl*> route;
72 simgrid::kernel::routing::NetZoneImpl::get_global_route(src_elm, dst_elm, route, nullptr);
73 for (auto const& link : route)
74 instr_user_variable(time, link->get_cname(), variable, parent_type, value, what, "", &user_link_variables);
80 void declare_host_variable(const std::string& variable, const std::string& color)
82 instr_user_variable(0, nullptr, variable, "HOST", 0, InstrUserVariable::DECLARE, color, &user_host_variables);
85 void set_host_variable(const s4u::Host* host, const std::string& variable, double value, double time)
87 instr_user_variable(time, host->get_cname(), variable, "HOST", value, InstrUserVariable::SET, "",
88 &user_host_variables);
91 void add_host_variable(const s4u::Host* host, const std::string& variable, double value, double time)
93 instr_user_variable(time, host->get_cname(), variable, "HOST", value, InstrUserVariable::ADD, "",
94 &user_host_variables);
97 void sub_host_variable(const s4u::Host* host, const std::string& variable, double value, double time)
99 instr_user_variable(time, host->get_cname(), variable, "HOST", value, InstrUserVariable::SUB, "",
100 &user_host_variables);
103 const std::set<std::string, std::less<>>& get_host_variables()
105 return user_host_variables;
108 void declare_link_variable(const std::string& variable, const std::string& color)
110 instr_user_variable(0, nullptr, variable, "LINK", 0, InstrUserVariable::DECLARE, color, &user_link_variables);
113 void set_link_variable(const s4u::Link* link, const std::string& variable, double value, double time)
115 instr_user_variable(time, link->get_cname(), variable, "LINK", value, InstrUserVariable::SET, "",
116 &user_link_variables);
119 void set_link_variable(const s4u::Host* src, const s4u::Host* dst, const std::string& variable, double value,
122 instr_user_srcdst_variable(time, src->get_cname(), dst->get_cname(), variable, "LINK", value, InstrUserVariable::SET);
125 void add_link_variable(const s4u::Link* link, const std::string& variable, double value, double time)
127 instr_user_variable(time, link->get_cname(), variable, "LINK", value, InstrUserVariable::ADD, "",
128 &user_link_variables);
131 void add_link_variable(const s4u::Host* src, const s4u::Host* dst, const std::string& variable, double value,
134 instr_user_srcdst_variable(time, src->get_cname(), dst->get_cname(), variable, "LINK", value, InstrUserVariable::ADD);
137 void sub_link_variable(const s4u::Link* link, const std::string& variable, double value, double time)
139 instr_user_variable(time, link->get_cname(), variable, "LINK", value, InstrUserVariable::SUB, "",
140 &user_link_variables);
143 void sub_link_variable(const s4u::Host* src, const s4u::Host* dst, const std::string& variable, double value,
146 instr_user_srcdst_variable(time, src->get_cname(), dst->get_cname(), variable, "LINK", value, InstrUserVariable::SUB);
149 const std::set<std::string, std::less<>>& get_link_variables()
151 return user_link_variables;
154 void declare_mark(const std::string& mark_type)
156 /* safe switches. tracing has to be activated and if platform is not traced, we can't deal with marks */
157 if (not TRACE_is_enabled() || not TRACE_needs_platform())
160 // check if mark_type is already declared
161 if (declared_marks.find(mark_type) != declared_marks.end()) {
162 throw TracingError(XBT_THROW_POINT,
163 xbt::string_printf("mark_type with name (%s) is already declared", mark_type.c_str()));
166 XBT_DEBUG("MARK,declare %s", mark_type.c_str());
167 Container::get_root()->get_type()->by_name_or_create<EventType>(mark_type);
168 declared_marks.emplace(mark_type);
171 void declare_mark_value(const std::string& mark_type, const std::string& mark_value, const std::string& mark_color)
173 /* safe switches. tracing has to be activated and if platform is not traced, we can't deal with marks */
174 if (not TRACE_is_enabled() || not TRACE_needs_platform())
177 auto* type = static_cast<EventType*>(Container::get_root()->get_type()->by_name(mark_type));
179 throw TracingError(XBT_THROW_POINT,
180 xbt::string_printf("mark_type with name (%s) is not declared", mark_type.c_str()));
182 XBT_DEBUG("MARK, declare_value %s %s %s", mark_type.c_str(), mark_value.c_str(), mark_color.c_str());
183 type->add_entity_value(mark_value, mark_color);
187 void mark(const std::string& mark_type, const std::string& mark_value)
189 /* safe switches. tracing has to be activated and if platform is not traced, we can't deal with marks */
190 if (not TRACE_is_enabled() || not TRACE_needs_platform())
193 // check if mark_type is already declared
194 auto* type = static_cast<EventType*>(Container::get_root()->get_type()->by_name(mark_type));
196 throw TracingError(XBT_THROW_POINT,
197 xbt::string_printf("mark_type with name (%s) is not declared", mark_type.c_str()));
199 XBT_DEBUG("MARK %s %s", mark_type.c_str(), mark_value.c_str());
200 new NewEvent(simgrid_get_clock(), Container::get_root(), type, type->get_entity_value(mark_value));
204 const std::set<std::string, std::less<>>& get_marks()
206 return declared_marks;
209 void declare_tracing_category(const std::string& name, const std::string& color)
211 /* safe switches. tracing has to be activated and if platform is not traced, we can't deal with categories */
212 if (not TRACE_is_enabled() || not TRACE_needs_platform() || not TRACE_categorized())
215 // check if category is already created
216 if (created_categories.find(name) != created_categories.end())
219 created_categories.emplace(name);
221 // define final_color
222 std::string final_color;
224 // generate a random color
225 double red = simgrid::xbt::random::uniform_real(0.0, std::nextafter(1.0, 2.0));
226 double green = simgrid::xbt::random::uniform_real(0.0, std::nextafter(1.0, 2.0));
227 double blue = simgrid::xbt::random::uniform_real(0.0, std::nextafter(1.0, 2.0));
228 final_color = std::to_string(red) + " " + std::to_string(green) + " " + std::to_string(blue);
230 final_color = std::string(color);
233 XBT_DEBUG("CAT,declare %s, \"%s\" \"%s\"", name.c_str(), color.c_str(), final_color.c_str());
235 // define the type of this category on top of hosts and links
236 instr_new_variable_type(name, final_color);
239 const std::set<std::string, std::less<>>& get_tracing_categories()
241 return created_categories;
245 } // namespace simgrid
247 static xbt_dynar_t instr_set_to_dynar(const std::set<std::string, std::less<>>& filter)
249 if (not TRACE_is_enabled() || not TRACE_needs_platform())
252 xbt_dynar_t ret = xbt_dynar_new (sizeof(char*), &xbt_free_ref);
253 for (auto const& name : filter)
254 xbt_dynar_push_as(ret, char*, xbt_strdup(name.c_str()));
259 /** @ingroup TRACE_category
260 * @brief Declare a new category with a random color.
262 * This function should be used to define a user category. The category can be used to differentiate the tasks that
263 * are created during the simulation (for example, tasks from server1, server2, or request tasks, computation tasks,
264 * communication tasks). All resource utilization (host power and link bandwidth) will be classified according to the
265 * task category. Tasks that do not belong to a category are not traced. The color for the category that is being
266 * declared is random. This function has no effect if a category with the same name has been already declared.
268 * See @ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
270 * @param category The name of the new tracing category to be created.
272 * @see TRACE_category_with_color
274 void TRACE_category(const char *category)
276 simgrid::instr::declare_tracing_category(category);
279 /** @ingroup TRACE_category
280 * @brief Declare a new category with a color.
282 * Same as #TRACE_category, but let user specify a color encoded as an RGB-like string with three floats from 0 to 1.
283 * So, to specify a red color, pass "1 0 0" as color parameter. A light-gray color can be specified using "0.7 0.7 0.7"
284 * as color. This function has no effect if a category with the same name has been already declared.
286 * See @ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
288 * @param category The name of the new tracing category to be created.
289 * @param color The color of the category (see @ref outcomes_vizu to
290 * know how to correctly specify the color)
293 void TRACE_category_with_color (const char *category, const char *color)
295 simgrid::instr::declare_tracing_category(category, color);
298 /** @ingroup TRACE_category
299 * @brief Get declared categories
301 * This function should be used to get categories that were already declared with #TRACE_category or with
302 * #TRACE_category_with_color.
304 * See @ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
306 * @return A dynar with the declared categories, must be freed with xbt_dynar_free.
309 xbt_dynar_t TRACE_get_categories ()
311 if (not TRACE_is_enabled() || not TRACE_categorized())
313 return instr_set_to_dynar(created_categories);
316 /** @ingroup TRACE_mark
317 * @brief Declare a new type for tracing mark.
319 * This function declares a new Paje event type in the trace file that can be used by simulators to declare
320 * application-level marks. This function is independent of which API is used in SimGrid.
322 * @param mark_type The name of the new type.
326 void TRACE_declare_mark(const char *mark_type)
328 simgrid::instr::declare_mark(mark_type);
331 /** @ingroup TRACE_mark
332 * @brief Declare a new colored value for a previously declared mark type.
334 * This function declares a new colored value for a Paje event type in the trace file that can be used by simulators to
335 * declare application-level marks. This function is independent of which API is used in SimGrid. The color needs to be
336 * a string with three numbers separated by spaces in the range [0,1].
337 * A light-gray color can be specified using "0.7 0.7 0.7" as color. If a nullptr color is provided, the color used will
338 * be white ("1 1 1").
340 * @param mark_type The name of the new type.
341 * @param mark_value The name of the new value for this type.
342 * @param mark_color The color of the new value for this type.
346 void TRACE_declare_mark_value_with_color (const char *mark_type, const char *mark_value, const char *mark_color)
348 simgrid::instr::declare_mark_value(mark_type, mark_value, mark_color);
351 /** @ingroup TRACE_mark
352 * @brief Declare a new value for a previously declared mark type.
354 * This function declares a new value for a Paje event type in the trace file that can be used by simulators to declare
355 * application-level marks. This function is independent of which API is used in SimGrid. Calling this function is the
356 * same as calling @ref TRACE_declare_mark_value_with_color with a nullptr color.
358 * @param mark_type The name of the new type.
359 * @param mark_value The name of the new value for this type.
363 void TRACE_declare_mark_value (const char *mark_type, const char *mark_value)
365 simgrid::instr::declare_mark_value(mark_type, mark_value);
369 * @ingroup TRACE_mark
370 * @brief Create a new instance of a tracing mark type.
372 * This function creates a mark in the trace file. The first parameter had to be previously declared using
373 * #TRACE_declare_mark, the second is the identifier for this mark instance. We recommend that the mark_value is a
374 * unique value for the whole simulation. Nevertheless, this is not a strong requirement: the trace will be valid even
375 * if there are multiple mark identifiers for the same trace.
377 * @param mark_type The name of the type for which the new instance will belong.
378 * @param mark_value The name of the new instance mark.
380 * @see TRACE_declare_mark
382 void TRACE_mark(const char *mark_type, const char *mark_value)
384 simgrid::instr::mark(mark_type, mark_value);
387 /** @ingroup TRACE_mark
388 * @brief Get declared marks
390 * This function should be used to get marks that were already declared with #TRACE_declare_mark.
392 * @return A dynar with the declared marks, must be freed with xbt_dynar_free.
394 xbt_dynar_t TRACE_get_marks ()
396 if (not TRACE_is_enabled())
399 return instr_set_to_dynar(declared_marks);
402 /** @ingroup TRACE_API
403 * @brief Creates a file with the topology of the platform file used for the simulator.
405 * The graph topology will have the following properties: all hosts, links and routers of the platform file are mapped
406 * to graph nodes; routes are mapped to edges.
407 * The platform's zones are not represented in the output.
409 * @param filename The name of the file that will hold the graph.
411 * @return 1 of successful, 0 otherwise.
413 int TRACE_platform_graph_export_graphviz (const char *filename)
415 simgrid::instr::platform_graph_export_graphviz(filename);
420 * Derived functions that use instr_user_variable and TRACE_user_srcdst_variable. They were previously defined as
421 * pre-processors directives, but were transformed into functions so the user can track them using gdb.
424 /* for VM variables */
425 /** @ingroup TRACE_user_variables
426 * @brief Declare a new user variable associated to VMs.
428 * Declare a user variable that will be associated to VMs. A user vm variable can be used to trace user variables
429 * such as the number of tasks in a VM, the number of clients in an application (for VMs), and so on. The color
430 * associated to this new variable will be random.
432 * @param variable The name of the new variable to be declared.
434 * @see TRACE_vm_variable_declare_with_color
436 void TRACE_vm_variable_declare (const char *variable)
438 instr_user_variable(0, nullptr, variable, "VM", 0, InstrUserVariable::DECLARE, nullptr, &user_vm_variables);
441 /** @ingroup TRACE_user_variables
442 * @brief Declare a new user variable associated to VMs with a color.
444 * Same as #TRACE_vm_variable_declare, but associated a color to the newly created user host variable. The color needs
445 * to be a string with three numbers separated by spaces in the range [0,1].
446 * A light-gray color can be specified using "0.7 0.7 0.7" as color.
448 * @param variable The name of the new variable to be declared.
449 * @param color The color for the new variable.
451 void TRACE_vm_variable_declare_with_color (const char *variable, const char *color)
453 instr_user_variable(0, nullptr, variable, "VM", 0, InstrUserVariable::DECLARE, color, &user_vm_variables);
456 /** @ingroup TRACE_user_variables
457 * @brief Set the value of a variable of a host.
459 * @param vm The name of the VM to be considered.
460 * @param variable The name of the variable to be considered.
461 * @param value The new value of the variable.
463 * @see TRACE_vm_variable_declare, TRACE_vm_variable_add, TRACE_vm_variable_sub
465 void TRACE_vm_variable_set (const char *vm, const char *variable, double value)
467 TRACE_vm_variable_set_with_time(simgrid_get_clock(), vm, variable, value);
470 /** @ingroup TRACE_user_variables
471 * @brief Add a value to a variable of a VM.
473 * @param vm The name of the VM to be considered.
474 * @param variable The name of the variable to be considered.
475 * @param value The value to be added to the variable.
477 * @see TRACE_vm_variable_declare, TRACE_vm_variable_set, TRACE_vm_variable_sub
479 void TRACE_vm_variable_add (const char *vm, const char *variable, double value)
481 TRACE_vm_variable_add_with_time(simgrid_get_clock(), vm, variable, value);
484 /** @ingroup TRACE_user_variables
485 * @brief Subtract a value from a variable of a VM.
487 * @param vm The name of the vm to be considered.
488 * @param variable The name of the variable to be considered.
489 * @param value The value to be subtracted from the variable.
491 * @see TRACE_vm_variable_declare, TRACE_vm_variable_set, TRACE_vm_variable_add
493 void TRACE_vm_variable_sub (const char *vm, const char *variable, double value)
495 TRACE_vm_variable_sub_with_time(simgrid_get_clock(), vm, variable, value);
498 /** @ingroup TRACE_user_variables
499 * @brief Set the value of a variable of a VM at a given timestamp.
501 * Same as #TRACE_vm_variable_set, but let user specify the time used to trace it. Users can specify a time that
502 * is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
503 * definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
506 * @param time The timestamp to be used to tag this change of value.
507 * @param vm The name of the VM to be considered.
508 * @param variable The name of the variable to be considered.
509 * @param value The new value of the variable.
511 * @see TRACE_vm_variable_declare, TRACE_vm_variable_add_with_time, TRACE_vm_variable_sub_with_time
513 void TRACE_vm_variable_set_with_time (double time, const char *vm, const char *variable, double value)
515 instr_user_variable(time, vm, variable, "VM", value, InstrUserVariable::SET, nullptr, &user_vm_variables);
518 /** @ingroup TRACE_user_variables
519 * @brief Add a value to a variable of a VM at a given timestamp.
521 * Same as #TRACE_vm_variable_add, but let user specify the time used to trace it. Users can specify a time that
522 * is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
523 * definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
526 * @param time The timestamp to be used to tag this change of value.
527 * @param vm The name of the VM to be considered.
528 * @param variable The name of the variable to be considered.
529 * @param value The value to be added to the variable.
531 * @see TRACE_vm_variable_declare, TRACE_vm_variable_set_with_time, TRACE_vm_variable_sub_with_time
533 void TRACE_vm_variable_add_with_time (double time, const char *vm, const char *variable, double value)
535 instr_user_variable(time, vm, variable, "VM", value, InstrUserVariable::ADD, nullptr, &user_vm_variables);
538 /** @ingroup TRACE_user_variables
539 * @brief Subtract a value from a variable of a VM at a given timestamp.
541 * Same as #TRACE_vm_variable_sub, but let user specify the time used to trace it. Users can specify a time that
542 * is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
543 * definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
546 * @param time The timestamp to be used to tag this change of value.
547 * @param vm The name of the VM to be considered.
548 * @param variable The name of the variable to be considered.
549 * @param value The value to be subtracted from the variable.
551 * @see TRACE_vm_variable_declare, TRACE_vm_variable_set_with_time, TRACE_vm_variable_add_with_time
553 void TRACE_vm_variable_sub_with_time (double time, const char *vm, const char *variable, double value)
555 instr_user_variable(time, vm, variable, "VM", value, InstrUserVariable::SUB, nullptr, &user_vm_variables);
558 /* for host variables */
559 /** @ingroup TRACE_user_variables
560 * @brief Declare a new user variable associated to hosts.
562 * Declare a user variable that will be associated to hosts.
563 * A user host variable can be used to trace user variables such as the number of tasks in a server, the number of
564 * clients in an application (for hosts), and so on. The color associated to this new variable will be random.
566 * @param variable The name of the new variable to be declared.
568 * @see TRACE_host_variable_declare_with_color
570 void TRACE_host_variable_declare (const char *variable)
572 instr_user_variable(0, nullptr, variable, "HOST", 0, InstrUserVariable::DECLARE, nullptr, &user_host_variables);
575 /** @ingroup TRACE_user_variables
576 * @brief Declare a new user variable associated to hosts with a color.
578 * Same as #TRACE_host_variable_declare, but associated a color to the newly created user host variable. The color
579 * needs to be a string with three numbers separated by spaces in the range [0,1].
580 * A light-gray color can be specified using "0.7 0.7 0.7" as color.
582 * @param variable The name of the new variable to be declared.
583 * @param color The color for the new variable.
585 void TRACE_host_variable_declare_with_color (const char *variable, const char *color)
587 instr_user_variable(0, nullptr, variable, "HOST", 0, InstrUserVariable::DECLARE, color, &user_host_variables);
590 /** @ingroup TRACE_user_variables
591 * @brief Set the value of a variable of a host.
593 * @param host The name of the host to be considered.
594 * @param variable The name of the variable to be considered.
595 * @param value The new value of the variable.
597 * @see TRACE_host_variable_declare, TRACE_host_variable_add, TRACE_host_variable_sub
599 void TRACE_host_variable_set (const char *host, const char *variable, double value)
601 TRACE_host_variable_set_with_time(simgrid_get_clock(), host, variable, value);
604 /** @ingroup TRACE_user_variables
605 * @brief Add a value to a variable of a host.
607 * @param host The name of the host to be considered.
608 * @param variable The name of the variable to be considered.
609 * @param value The value to be added to the variable.
611 * @see TRACE_host_variable_declare, TRACE_host_variable_set, TRACE_host_variable_sub
613 void TRACE_host_variable_add (const char *host, const char *variable, double value)
615 TRACE_host_variable_add_with_time(simgrid_get_clock(), host, variable, value);
618 /** @ingroup TRACE_user_variables
619 * @brief Subtract a value from a variable of a host.
621 * @param host The name of the host to be considered.
622 * @param variable The name of the variable to be considered.
623 * @param value The value to be subtracted from the variable.
625 * @see TRACE_host_variable_declare, TRACE_host_variable_set, TRACE_host_variable_add
627 void TRACE_host_variable_sub (const char *host, const char *variable, double value)
629 TRACE_host_variable_sub_with_time(simgrid_get_clock(), host, variable, value);
632 /** @ingroup TRACE_user_variables
633 * @brief Set the value of a variable of a host at a given timestamp.
635 * Same as #TRACE_host_variable_set, but let user specify the time used to trace it. Users can specify a time that
636 * is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
637 * definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
640 * @param time The timestamp to be used to tag this change of value.
641 * @param host The name of the host to be considered.
642 * @param variable The name of the variable to be considered.
643 * @param value The new value of the variable.
645 * @see TRACE_host_variable_declare, TRACE_host_variable_add_with_time, TRACE_host_variable_sub_with_time
647 void TRACE_host_variable_set_with_time (double time, const char *host, const char *variable, double value)
649 instr_user_variable(time, host, variable, "HOST", value, InstrUserVariable::SET, nullptr, &user_host_variables);
652 /** @ingroup TRACE_user_variables
653 * @brief Add a value to a variable of a host at a given timestamp.
655 * Same as #TRACE_host_variable_add, but let user specify the time used to trace it. Users can specify a time that
656 * is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
657 * definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
660 * @param time The timestamp to be used to tag this change of value.
661 * @param host The name of the host to be considered.
662 * @param variable The name of the variable to be considered.
663 * @param value The value to be added to the variable.
665 * @see TRACE_host_variable_declare, TRACE_host_variable_set_with_time, TRACE_host_variable_sub_with_time
667 void TRACE_host_variable_add_with_time (double time, const char *host, const char *variable, double value)
669 instr_user_variable(time, host, variable, "HOST", value, InstrUserVariable::ADD, nullptr, &user_host_variables);
672 /** @ingroup TRACE_user_variables
673 * @brief Subtract a value from a variable of a host at a given timestamp.
675 * Same as #TRACE_host_variable_sub, but let user specify the time used to trace it. Users can specify a time that
676 * is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
677 * definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
680 * @param time The timestamp to be used to tag this change of value.
681 * @param host The name of the host to be considered.
682 * @param variable The name of the variable to be considered.
683 * @param value The value to be subtracted from the variable.
685 * @see TRACE_host_variable_declare, TRACE_host_variable_set_with_time, TRACE_host_variable_add_with_time
687 void TRACE_host_variable_sub_with_time (double time, const char *host, const char *variable, double value)
689 instr_user_variable(time, host, variable, "HOST", value, InstrUserVariable::SUB, nullptr, &user_host_variables);
692 /** @ingroup TRACE_user_variables
693 * @brief Get declared user host variables
695 * This function should be used to get host variables that were already declared with #TRACE_host_variable_declare or
696 * with #TRACE_host_variable_declare_with_color.
698 * @return A dynar with the declared host variables, must be freed with xbt_dynar_free.
700 xbt_dynar_t TRACE_get_host_variables ()
702 return instr_set_to_dynar(user_host_variables);
705 /* for link variables */
706 /** @ingroup TRACE_user_variables
707 * @brief Declare a new user variable associated to links.
709 * Declare a user variable that will be associated to links.
710 * A user link variable can be used, for example, to trace user variables such as the number of messages being
711 * transferred through network links. The color associated to this new variable will be random.
713 * @param variable The name of the new variable to be declared.
715 * @see TRACE_link_variable_declare_with_color
717 void TRACE_link_variable_declare (const char *variable)
719 instr_user_variable(0, nullptr, variable, "LINK", 0, InstrUserVariable::DECLARE, nullptr, &user_link_variables);
722 /** @ingroup TRACE_user_variables
723 * @brief Declare a new user variable associated to links with a color.
725 * Same as #TRACE_link_variable_declare, but associated a color to the newly created user link variable. The color
726 * needs to be a string with three numbers separated by spaces in the range [0,1].
727 * A light-gray color can be specified using "0.7 0.7 0.7" as color.
729 * @param variable The name of the new variable to be declared.
730 * @param color The color for the new variable.
732 void TRACE_link_variable_declare_with_color (const char *variable, const char *color)
734 instr_user_variable(0, nullptr, variable, "LINK", 0, InstrUserVariable::DECLARE, color, &user_link_variables);
737 /** @ingroup TRACE_user_variables
738 * @brief Set the value of a variable of a link.
740 * @param link The name of the link to be considered.
741 * @param variable The name of the variable to be considered.
742 * @param value The new value of the variable.
744 * @see TRACE_link_variable_declare, TRACE_link_variable_add, TRACE_link_variable_sub
746 void TRACE_link_variable_set (const char *link, const char *variable, double value)
748 TRACE_link_variable_set_with_time(simgrid_get_clock(), link, variable, value);
751 /** @ingroup TRACE_user_variables
752 * @brief Add a value to a variable of a link.
754 * @param link The name of the link to be considered.
755 * @param variable The name of the variable to be considered.
756 * @param value The value to be added to the variable.
758 * @see TRACE_link_variable_declare, TRACE_link_variable_set, TRACE_link_variable_sub
760 void TRACE_link_variable_add (const char *link, const char *variable, double value)
762 TRACE_link_variable_add_with_time(simgrid_get_clock(), link, variable, value);
765 /** @ingroup TRACE_user_variables
766 * @brief Subtract a value from a variable of a link.
768 * @param link The name of the link to be considered.
769 * @param variable The name of the variable to be considered.
770 * @param value The value to be subtracted from the variable.
772 * @see TRACE_link_variable_declare, TRACE_link_variable_set, TRACE_link_variable_add
774 void TRACE_link_variable_sub (const char *link, const char *variable, double value)
776 TRACE_link_variable_sub_with_time(simgrid_get_clock(), link, variable, value);
779 /** @ingroup TRACE_user_variables
780 * @brief Set the value of a variable of a link at a given timestamp.
782 * Same as #TRACE_link_variable_set, but let user specify the time used to trace it. Users can specify a time that
783 * is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
784 * definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
787 * @param time The timestamp to be used to tag this change of value.
788 * @param link The name of the link to be considered.
789 * @param variable The name of the variable to be considered.
790 * @param value The new value of the variable.
792 * @see TRACE_link_variable_declare, TRACE_link_variable_add_with_time, TRACE_link_variable_sub_with_time
794 void TRACE_link_variable_set_with_time (double time, const char *link, const char *variable, double value)
796 instr_user_variable(time, link, variable, "LINK", value, InstrUserVariable::SET, nullptr, &user_link_variables);
799 /** @ingroup TRACE_user_variables
800 * @brief Add a value to a variable of a link at a given timestamp.
802 * Same as #TRACE_link_variable_add, but let user specify the time used to trace it. Users can specify a time that
803 * is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
804 * definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
807 * @param time The timestamp to be used to tag this change of value.
808 * @param link The name of the link to be considered.
809 * @param variable The name of the variable to be considered.
810 * @param value The value to be added to the variable.
812 * @see TRACE_link_variable_declare, TRACE_link_variable_set_with_time, TRACE_link_variable_sub_with_time
814 void TRACE_link_variable_add_with_time (double time, const char *link, const char *variable, double value)
816 instr_user_variable(time, link, variable, "LINK", value, InstrUserVariable::ADD, nullptr, &user_link_variables);
819 /** @ingroup TRACE_user_variables
820 * @brief Subtract a value from a variable of a link at a given timestamp.
822 * Same as #TRACE_link_variable_sub, but let user specify the time used to trace it. Users can specify a time that
823 * is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
824 * definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
827 * @param time The timestamp to be used to tag this change of value.
828 * @param link The name of the link to be considered.
829 * @param variable The name of the variable to be considered.
830 * @param value The value to be subtracted from the variable.
832 * @see TRACE_link_variable_declare, TRACE_link_variable_set_with_time, TRACE_link_variable_add_with_time
834 void TRACE_link_variable_sub_with_time (double time, const char *link, const char *variable, double value)
836 instr_user_variable(time, link, variable, "LINK", value, InstrUserVariable::SUB, nullptr, &user_link_variables);
839 /* for link variables, but with src and dst used for get_route */
840 /** @ingroup TRACE_user_variables
841 * @brief Set the value of the variable present in the links connecting source and destination.
843 * Same as #TRACE_link_variable_set, but instead of providing the name of link to be considered, provide the source
844 * and destination hosts. All links that are part of the route between source and destination will have the variable
845 * set to the provided value.
847 * @param src The name of the source host for get route.
848 * @param dst The name of the destination host for get route.
849 * @param variable The name of the variable to be considered.
850 * @param value The new value of the variable.
852 * @see TRACE_link_variable_declare, TRACE_link_srcdst_variable_add, TRACE_link_srcdst_variable_sub
854 void TRACE_link_srcdst_variable_set (const char *src, const char *dst, const char *variable, double value)
856 TRACE_link_srcdst_variable_set_with_time(simgrid_get_clock(), src, dst, variable, value);
859 /** @ingroup TRACE_user_variables
860 * @brief Add a value to the variable present in the links connecting source and destination.
862 * Same as #TRACE_link_variable_add, but instead of providing the name of link to be considered, provide the source
863 * and destination hosts. All links that are part of the route between source and destination will have the value
864 * passed as parameter added to the current value of the variable name to be considered.
866 * @param src The name of the source host for get route.
867 * @param dst The name of the destination host for get route.
868 * @param variable The name of the variable to be considered.
869 * @param value The value to be added to the variable.
871 * @see TRACE_link_variable_declare, TRACE_link_srcdst_variable_set, TRACE_link_srcdst_variable_sub
873 void TRACE_link_srcdst_variable_add (const char *src, const char *dst, const char *variable, double value)
875 TRACE_link_srcdst_variable_add_with_time(simgrid_get_clock(), src, dst, variable, value);
878 /** @ingroup TRACE_user_variables
879 * @brief Subtract a value from the variable present in the links connecting source and destination.
881 * Same as #TRACE_link_variable_sub, but instead of providing the name of link to be considered, provide the source
882 * and destination hosts. All links that are part of the route between source and destination will have the value
883 * passed as parameter subtracted from the current value of the variable name to be considered.
885 * @param src The name of the source host for get route.
886 * @param dst The name of the destination host for get route.
887 * @param variable The name of the variable to be considered.
888 * @param value The value to be subtracted from the variable.
890 * @see TRACE_link_variable_declare, TRACE_link_srcdst_variable_set, TRACE_link_srcdst_variable_add
892 void TRACE_link_srcdst_variable_sub (const char *src, const char *dst, const char *variable, double value)
894 TRACE_link_srcdst_variable_sub_with_time(simgrid_get_clock(), src, dst, variable, value);
897 /** @ingroup TRACE_user_variables
898 * @brief Set the value of the variable present in the links connecting source and destination at a given timestamp.
900 * Same as #TRACE_link_srcdst_variable_set, but let user specify the time used to trace it. Users can specify a time
901 * that is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
902 * definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
905 * @param time The timestamp to be used to tag this change of value.
906 * @param src The name of the source host for get route.
907 * @param dst The name of the destination host for get route.
908 * @param variable The name of the variable to be considered.
909 * @param value The new value of the variable.
911 * @see TRACE_link_variable_declare, TRACE_link_srcdst_variable_add_with_time, TRACE_link_srcdst_variable_sub_with_time
913 void TRACE_link_srcdst_variable_set_with_time (double time, const char *src, const char *dst, const char *variable,
916 instr_user_srcdst_variable(time, src, dst, variable, "LINK", value, InstrUserVariable::SET);
919 /** @ingroup TRACE_user_variables
920 * @brief Add a value to the variable present in the links connecting source and destination at a given timestamp.
922 * Same as #TRACE_link_srcdst_variable_add, but let user specify the time used to trace it. Users can specify a time
923 * that is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
924 * definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
927 * @param time The timestamp to be used to tag this change of value.
928 * @param src The name of the source host for get route.
929 * @param dst The name of the destination host for get route.
930 * @param variable The name of the variable to be considered.
931 * @param value The value to be added to the variable.
933 * @see TRACE_link_variable_declare, TRACE_link_srcdst_variable_set_with_time, TRACE_link_srcdst_variable_sub_with_time
935 void TRACE_link_srcdst_variable_add_with_time (double time, const char *src, const char *dst, const char *variable,
938 instr_user_srcdst_variable(time, src, dst, variable, "LINK", value, InstrUserVariable::ADD);
941 /** @ingroup TRACE_user_variables
942 * @brief Subtract a value from the variable present in the links connecting source and dest. at a given timestamp.
944 * Same as #TRACE_link_srcdst_variable_sub, but let user specify the time used to trace it. Users can specify a time
945 * that is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
946 * definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
949 * @param time The timestamp to be used to tag this change of value.
950 * @param src The name of the source host for get route.
951 * @param dst The name of the destination host for get route.
952 * @param variable The name of the variable to be considered.
953 * @param value The value to be subtracted from the variable.
955 * @see TRACE_link_variable_declare, TRACE_link_srcdst_variable_set_with_time, TRACE_link_srcdst_variable_add_with_time
957 void TRACE_link_srcdst_variable_sub_with_time (double time, const char *src, const char *dst, const char *variable,
960 instr_user_srcdst_variable(time, src, dst, variable, "LINK", value, InstrUserVariable::SUB);
963 /** @ingroup TRACE_user_variables
964 * @brief Get declared user link variables
966 * This function should be used to get link variables that were already declared with #TRACE_link_variable_declare or
967 * with #TRACE_link_variable_declare_with_color.
969 * @return A dynar with the declared link variables, must be freed with xbt_dynar_free.
971 xbt_dynar_t TRACE_get_link_variables ()
973 return instr_set_to_dynar(user_link_variables);
976 /** @ingroup TRACE_user_variables
977 * @brief Declare a new user state associated to hosts.
979 * Declare a user state that will be associated to hosts.
980 * A user host state can be used to trace application states.
982 * @param state The name of the new state to be declared.
984 * @see TRACE_host_state_declare_value
986 void TRACE_host_state_declare (const char *state)
988 instr_new_user_state_type("HOST", state);
991 /** @ingroup TRACE_user_variables
992 * @brief Declare a new value for a user state associated to hosts.
994 * Declare a value for a state. The color needs to be a string with 3 numbers separated by spaces in the range [0,1].
995 * A light-gray color can be specified using "0.7 0.7 0.7" as color.
997 * @param state The name of the new state to be declared.
998 * @param value The name of the value
999 * @param color The color of the value
1001 * @see TRACE_host_state_declare
1003 void TRACE_host_state_declare_value (const char *state, const char *value, const char *color)
1005 instr_new_value_for_user_state_type (state, value, color);
1008 /** @ingroup TRACE_user_variables
1009 * @brief Set the user state to the given value.
1011 * Change a user state previously declared to the given value.
1013 * @param host The name of the host to be considered.
1014 * @param state_name The name of the state previously declared.
1015 * @param value_name The new value of the state.
1017 * @see TRACE_host_state_declare, TRACE_host_push_state, TRACE_host_pop_state, TRACE_host_reset_state
1019 void TRACE_host_set_state(const char* host, const char* state_name, const char* value_name)
1021 simgrid::instr::StateType* state = simgrid::instr::Container::by_name(host)->get_state(state_name);
1022 state->add_entity_value(value_name);
1023 state->set_event(value_name);
1026 /** @ingroup TRACE_user_variables
1027 * @brief Push a new value for a state of a given host.
1029 * Change a user state previously declared by pushing the new value to the state.
1031 * @param host The name of the host to be considered.
1032 * @param state_name The name of the state previously declared.
1033 * @param value_name The value to be pushed.
1035 * @see TRACE_host_state_declare, TRACE_host_set_state, TRACE_host_pop_state, TRACE_host_reset_state
1037 void TRACE_host_push_state(const char* host, const char* state_name, const char* value_name)
1039 simgrid::instr::Container::by_name(host)->get_state(state_name)->push_event(value_name);
1042 /** @ingroup TRACE_user_variables
1043 * @brief Pop the last value of a state of a given host.
1045 * Change a user state previously declared by removing the last value of the state.
1047 * @param host The name of the host to be considered.
1048 * @param state_name The name of the state to be popped.
1050 * @see TRACE_host_state_declare, TRACE_host_set_state, TRACE_host_push_state, TRACE_host_reset_state
1052 void TRACE_host_pop_state(const char* host, const char* state_name)
1054 simgrid::instr::Container::by_name(host)->get_state(state_name)->pop_event();