Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
include cleanups (mostly surf/surf.hpp)
[simgrid.git] / src / instr / instr_interface.cpp
1 /* Copyright (c) 2010-2021. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include <simgrid/Exception.hpp>
7 #include <simgrid/kernel/routing/NetPoint.hpp>
8 #include <xbt/random.hpp>
9
10 #include "src/instr/instr_private.hpp"
11 #include "src/surf/network_interface.hpp"
12 #include "src/surf/surf_private.hpp"
13 #include <algorithm>
14 #include <cmath>
15
16 enum class InstrUserVariable { DECLARE, SET, ADD, SUB };
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_api, instr, "API");
19
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;
25
26 static xbt_dynar_t instr_set_to_dynar(const std::set<std::string, std::less<>>& filter)
27 {
28   if (not TRACE_is_enabled() || not TRACE_needs_platform())
29     return nullptr;
30
31   xbt_dynar_t ret = xbt_dynar_new (sizeof(char*), &xbt_free_ref);
32   for (auto const& name : filter)
33     xbt_dynar_push_as(ret, char*, xbt_strdup(name.c_str()));
34
35   return ret;
36 }
37
38 /** @ingroup TRACE_category
39  *  @brief Declare a new category with a random color.
40  *
41  *  This function should be used to define a user category. The category can be used to differentiate the tasks that
42  *  are created during the simulation (for example, tasks from server1, server2, or request tasks, computation tasks,
43  *  communication tasks). All resource utilization (host power and link bandwidth) will be classified according to the
44  *  task category. Tasks that do not belong to a category are not traced. The color for the category that is being
45  *  declared is random. This function has no effect if a category with the same name has been already declared.
46  *
47  * See @ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
48  *
49  *  @param category The name of the new tracing category to be created.
50  *
51  *  @see TRACE_category_with_color, MSG_task_set_category, SD_task_set_category
52  */
53 void TRACE_category(const char *category)
54 {
55   TRACE_category_with_color (category, nullptr);
56 }
57
58 /** @ingroup TRACE_category
59  *  @brief Declare a new category with a color.
60  *
61  *  Same as #TRACE_category, but let user specify a color encoded as an RGB-like string with three floats from 0 to 1.
62  *  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"
63  *   as color. This function has no effect if a category with the same name has been already declared.
64  *
65  * See @ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
66  *
67  *  @param category The name of the new tracing category to be created.
68  *  @param color The color of the category (see @ref outcomes_vizu to
69  *  know how to correctly specify the color)
70  *
71  *  @see MSG_task_set_category, SD_task_set_category
72  */
73 void TRACE_category_with_color (const char *category, const char *color)
74 {
75   /* safe switches. tracing has to be activated and if platform is not traced, we can't deal with categories */
76   if (not TRACE_is_enabled() || not TRACE_needs_platform())
77     return;
78
79   if (not(TRACE_categorized() && category != nullptr))
80     return;
81
82   //check if category is already created
83   if (created_categories.find(category) != created_categories.end())
84     return;
85
86   created_categories.emplace(category);
87
88   //define final_color
89   std::string final_color;
90   if (not color) {
91     //generate a random color
92     double red   = simgrid::xbt::random::uniform_real(0.0, std::nextafter(1.0, 2.0));
93     double green = simgrid::xbt::random::uniform_real(0.0, std::nextafter(1.0, 2.0));
94     double blue  = simgrid::xbt::random::uniform_real(0.0, std::nextafter(1.0, 2.0));
95     final_color  = std::to_string(red) + " " + std::to_string(green) + " " + std::to_string(blue);
96   }else{
97     final_color = std::string(color);
98   }
99
100   XBT_DEBUG("CAT,declare %s, \"%s\" \"%s\"", category, color, final_color.c_str());
101
102   //define the type of this category on top of hosts and links
103   instr_new_variable_type (category, final_color);
104 }
105
106 /** @ingroup TRACE_category
107  *  @brief Get declared categories
108  *
109  * This function should be used to get categories that were already declared with #TRACE_category or with
110  * #TRACE_category_with_color.
111  *
112  * See @ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
113  *
114  * @return A dynar with the declared categories, must be freed with xbt_dynar_free.
115  *
116  *  @see MSG_task_set_category, SD_task_set_category
117  */
118 xbt_dynar_t TRACE_get_categories ()
119 {
120   if (not TRACE_is_enabled() || not TRACE_categorized())
121     return nullptr;
122   return instr_set_to_dynar(created_categories);
123 }
124
125 /** @ingroup TRACE_mark
126  * @brief Declare a new type for tracing mark.
127  *
128  * This function declares a new Paje event type in the trace file that can be used by simulators to declare
129  * application-level marks. This function is independent of which API is used in SimGrid.
130  *
131  * @param mark_type The name of the new type.
132  *
133  * @see TRACE_mark
134  */
135 void TRACE_declare_mark(const char *mark_type)
136 {
137   /* safe switches. tracing has to be activated and if platform is not traced, we can't deal with marks */
138   if (not TRACE_is_enabled() || not TRACE_needs_platform())
139     return;
140
141   xbt_assert(mark_type, "mark_type is nullptr");
142
143   //check if mark_type is already declared
144   if (declared_marks.find(mark_type) != declared_marks.end()) {
145     throw simgrid::TracingError(XBT_THROW_POINT,
146                                 simgrid::xbt::string_printf("mark_type with name (%s) is already declared", mark_type));
147   }
148
149   XBT_DEBUG("MARK,declare %s", mark_type);
150   simgrid::instr::Container::get_root()->get_type()->by_name_or_create<simgrid::instr::EventType>(mark_type);
151   declared_marks.emplace(mark_type);
152 }
153
154 /** @ingroup TRACE_mark
155  * @brief Declare a new colored value for a previously declared mark type.
156  *
157  * This function declares a new colored value for a Paje event type in the trace file that can be used by simulators to
158  * declare application-level marks. This function is independent of which API is used in SimGrid. The color needs to be
159  * a string with three numbers separated by spaces in the range [0,1].
160  * 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
161  * be white ("1 1 1").
162  *
163  * @param mark_type The name of the new type.
164  * @param mark_value The name of the new value for this type.
165  * @param mark_color The color of the new value for this type.
166  *
167  * @see TRACE_mark
168  */
169 void TRACE_declare_mark_value_with_color (const char *mark_type, const char *mark_value, const char *mark_color)
170 {
171   /* safe switches. tracing has to be activated and if platform is not traced, we can't deal with marks */
172   if (not TRACE_is_enabled() || not TRACE_needs_platform())
173     return;
174
175   xbt_assert(mark_type, "mark_type is nullptr");
176   xbt_assert(mark_value, "mark_value is nullptr");
177
178   auto* type =
179       static_cast<simgrid::instr::EventType*>(simgrid::instr::Container::get_root()->get_type()->by_name(mark_type));
180   if (not type) {
181     throw simgrid::TracingError(XBT_THROW_POINT,
182                                 simgrid::xbt::string_printf("mark_type with name (%s) is not declared", mark_type));
183   } else {
184     if (not mark_color)
185       mark_color = "1.0 1.0 1.0" /*white*/;
186
187     XBT_DEBUG("MARK,declare_value %s %s %s", mark_type, mark_value, mark_color);
188     type->add_entity_value(mark_value, mark_color);
189   }
190 }
191
192 /** @ingroup TRACE_mark
193  * @brief Declare a new value for a previously declared mark type.
194  *
195  * This function declares a new value for a Paje event type in the trace file that can be used by simulators to declare
196  * application-level marks. This function is independent of which API is used in SimGrid. Calling this function is the
197  * same as calling @ref TRACE_declare_mark_value_with_color with a nullptr color.
198  *
199  * @param mark_type The name of the new type.
200  * @param mark_value The name of the new value for this type.
201  *
202  * @see TRACE_mark
203  */
204 void TRACE_declare_mark_value (const char *mark_type, const char *mark_value)
205 {
206   TRACE_declare_mark_value_with_color (mark_type, mark_value, nullptr);
207 }
208
209 /**
210  * @ingroup TRACE_mark
211  * @brief Create a new instance of a tracing mark type.
212  *
213  * This function creates a mark in the trace file. The first parameter had to be previously declared using
214  * #TRACE_declare_mark, the second is the identifier for this mark instance. We recommend that the mark_value is a
215  * unique value for the whole simulation. Nevertheless, this is not a strong requirement: the trace will be valid even
216  * if there are multiple mark identifiers for the same trace.
217  *
218  * @param mark_type The name of the type for which the new instance will belong.
219  * @param mark_value The name of the new instance mark.
220  *
221  * @see TRACE_declare_mark
222  */
223 void TRACE_mark(const char *mark_type, const char *mark_value)
224 {
225   /* safe switches. tracing has to be activated and if platform is not traced, we can't deal with marks */
226   if (not TRACE_is_enabled() || not TRACE_needs_platform())
227     return;
228
229   xbt_assert(mark_type, "mark_type is nullptr");
230   xbt_assert(mark_value, "mark_value is nullptr");
231
232   //check if mark_type is already declared
233   auto* type =
234       static_cast<simgrid::instr::EventType*>(simgrid::instr::Container::get_root()->get_type()->by_name(mark_type));
235   if (not type) {
236     throw simgrid::TracingError(XBT_THROW_POINT,
237                                 simgrid::xbt::string_printf("mark_type with name (%s) is not declared", mark_type));
238   } else {
239     XBT_DEBUG("MARK %s %s", mark_type, mark_value);
240     new simgrid::instr::NewEvent(simgrid_get_clock(), simgrid::instr::Container::get_root(), type,
241                                  type->get_entity_value(mark_value));
242   }
243 }
244
245 /** @ingroup TRACE_mark
246  *  @brief Get declared marks
247  *
248  * This function should be used to get marks that were already declared with #TRACE_declare_mark.
249  *
250  * @return A dynar with the declared marks, must be freed with xbt_dynar_free.
251  */
252 xbt_dynar_t TRACE_get_marks ()
253 {
254   if (not TRACE_is_enabled())
255     return nullptr;
256
257   return instr_set_to_dynar(declared_marks);
258 }
259
260 static void instr_user_variable(double time, const char* resource, const char* variable_name, const char* parent_type,
261                                 double value, InstrUserVariable what, const char* color,
262                                 std::set<std::string, std::less<>>* filter)
263 {
264   /* safe switches. tracing has to be activated and if platform is not traced, we don't allow user variables */
265   if (not TRACE_is_enabled() || not TRACE_needs_platform())
266     return;
267
268   //check if variable is already declared
269   auto created = filter->find(variable_name);
270   if (what == InstrUserVariable::DECLARE) {
271     if (created == filter->end()) { // not declared yet
272       filter->insert(variable_name);
273       instr_new_user_variable_type(parent_type, variable_name, color == nullptr ? "" : color);
274     }
275   }else{
276     if (created != filter->end()) { // declared, let's work
277       simgrid::instr::VariableType* variable =
278           simgrid::instr::Container::by_name(resource)->get_variable(variable_name);
279       switch (what){
280         case InstrUserVariable::SET:
281           variable->set_event(time, value);
282           break;
283         case InstrUserVariable::ADD:
284           variable->add_event(time, value);
285           break;
286         case InstrUserVariable::SUB:
287           variable->sub_event(time, value);
288           break;
289         default:
290           THROW_IMPOSSIBLE;
291       }
292     }
293   }
294 }
295
296 static void instr_user_srcdst_variable(double time, const char* src, const char* dst, const char* variable,
297                                        const char* parent_type, double value, InstrUserVariable what)
298 {
299   const simgrid::kernel::routing::NetPoint* src_elm = sg_netpoint_by_name_or_null(src);
300   xbt_assert(src_elm, "Element '%s' not found!", src);
301
302   const simgrid::kernel::routing::NetPoint* dst_elm = sg_netpoint_by_name_or_null(dst);
303   xbt_assert(dst_elm, "Element '%s' not found!", dst);
304
305   std::vector<simgrid::kernel::resource::LinkImpl*> route;
306   simgrid::kernel::routing::NetZoneImpl::get_global_route(src_elm, dst_elm, route, nullptr);
307   for (auto const& link : route)
308     instr_user_variable(time, link->get_cname(), variable, parent_type, value, what, nullptr, &user_link_variables);
309 }
310
311 /** @ingroup TRACE_API
312  *  @brief Creates a file with the topology of the platform file used for the simulator.
313  *
314  *  The graph topology will have the following properties: all hosts, links and routers of the platform file are mapped
315  *  to graph nodes; routes are mapped to edges.
316  *  The platform's zones are not represented in the output.
317  *
318  *  @param filename The name of the file that will hold the graph.
319  *
320  *  @return 1 of successful, 0 otherwise.
321  */
322 int TRACE_platform_graph_export_graphviz (const char *filename)
323 {
324   simgrid::instr::platform_graph_export_graphviz(filename);
325   return 1;
326 }
327
328 /*
329  * Derived functions that use instr_user_variable and TRACE_user_srcdst_variable. They were previously defined as
330  * pre-processors directives, but were transformed into functions so the user can track them using gdb.
331  */
332
333 /* for VM variables */
334 /** @ingroup TRACE_user_variables
335  *  @brief Declare a new user variable associated to VMs.
336  *
337  *  Declare a user variable that will be associated to VMs. A user vm variable can be used to trace user variables
338  *  such as the number of tasks in a VM, the number of clients in an application (for VMs), and so on. The color
339  *  associated to this new variable will be random.
340  *
341  *  @param variable The name of the new variable to be declared.
342  *
343  *  @see TRACE_vm_variable_declare_with_color
344  */
345 void TRACE_vm_variable_declare (const char *variable)
346 {
347   instr_user_variable(0, nullptr, variable, "VM", 0, InstrUserVariable::DECLARE, nullptr, &user_vm_variables);
348 }
349
350 /** @ingroup TRACE_user_variables
351  *  @brief Declare a new user variable associated to VMs with a color.
352  *
353  *  Same as #TRACE_vm_variable_declare, but associated a color to the newly created user host variable. The color needs
354  *  to be a string with three numbers separated by spaces in the range [0,1].
355  *  A light-gray color can be specified using "0.7 0.7 0.7" as color.
356  *
357  *  @param variable The name of the new variable to be declared.
358  *  @param color The color for the new variable.
359  */
360 void TRACE_vm_variable_declare_with_color (const char *variable, const char *color)
361 {
362   instr_user_variable(0, nullptr, variable, "VM", 0, InstrUserVariable::DECLARE, color, &user_vm_variables);
363 }
364
365 /** @ingroup TRACE_user_variables
366  *  @brief Set the value of a variable of a host.
367  *
368  *  @param vm The name of the VM to be considered.
369  *  @param variable The name of the variable to be considered.
370  *  @param value The new value of the variable.
371  *
372  *  @see TRACE_vm_variable_declare, TRACE_vm_variable_add, TRACE_vm_variable_sub
373  */
374 void TRACE_vm_variable_set (const char *vm, const char *variable, double value)
375 {
376   TRACE_vm_variable_set_with_time(simgrid_get_clock(), vm, variable, value);
377 }
378
379 /** @ingroup TRACE_user_variables
380  *  @brief Add a value to a variable of a VM.
381  *
382  *  @param vm The name of the VM to be considered.
383  *  @param variable The name of the variable to be considered.
384  *  @param value The value to be added to the variable.
385  *
386  *  @see TRACE_vm_variable_declare, TRACE_vm_variable_set, TRACE_vm_variable_sub
387  */
388 void TRACE_vm_variable_add (const char *vm, const char *variable, double value)
389 {
390   TRACE_vm_variable_add_with_time(simgrid_get_clock(), vm, variable, value);
391 }
392
393 /** @ingroup TRACE_user_variables
394  *  @brief Subtract a value from a variable of a VM.
395  *
396  *  @param vm The name of the vm to be considered.
397  *  @param variable The name of the variable to be considered.
398  *  @param value The value to be subtracted from the variable.
399  *
400  *  @see TRACE_vm_variable_declare, TRACE_vm_variable_set, TRACE_vm_variable_add
401  */
402 void TRACE_vm_variable_sub (const char *vm, const char *variable, double value)
403 {
404   TRACE_vm_variable_sub_with_time(simgrid_get_clock(), vm, variable, value);
405 }
406
407 /** @ingroup TRACE_user_variables
408  *  @brief Set the value of a variable of a VM at a given timestamp.
409  *
410  *  Same as #TRACE_vm_variable_set, but let user specify  the time used to trace it. Users can specify a time that
411  *  is not the simulated clock time as defined by the core  simulator. This allows a fine-grain control of time
412  *  definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
413  *  also traced.
414  *
415  *  @param time The timestamp to be used to tag this change of value.
416  *  @param vm The name of the VM to be considered.
417  *  @param variable The name of the variable to be considered.
418  *  @param value The new value of the variable.
419  *
420  *  @see TRACE_vm_variable_declare, TRACE_vm_variable_add_with_time, TRACE_vm_variable_sub_with_time
421  */
422 void TRACE_vm_variable_set_with_time (double time, const char *vm, const char *variable, double value)
423 {
424   instr_user_variable(time, vm, variable, "VM", value, InstrUserVariable::SET, nullptr, &user_vm_variables);
425 }
426
427 /** @ingroup TRACE_user_variables
428  *  @brief Add a value to a variable of a VM at a given timestamp.
429  *
430  *  Same as #TRACE_vm_variable_add, but let user specify the time used to trace it. Users can specify a time that
431  *  is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
432  *  definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
433  *  also traced.
434  *
435  *  @param time The timestamp to be used to tag this change of value.
436  *  @param vm The name of the VM to be considered.
437  *  @param variable The name of the variable to be considered.
438  *  @param value The value to be added to the variable.
439  *
440  *  @see TRACE_vm_variable_declare, TRACE_vm_variable_set_with_time, TRACE_vm_variable_sub_with_time
441  */
442 void TRACE_vm_variable_add_with_time (double time, const char *vm, const char *variable, double value)
443 {
444   instr_user_variable(time, vm, variable, "VM", value, InstrUserVariable::ADD, nullptr, &user_vm_variables);
445 }
446
447 /** @ingroup TRACE_user_variables
448  *  @brief Subtract a value from a variable of a VM at a given timestamp.
449  *
450  *  Same as #TRACE_vm_variable_sub, but let user specify the time used to trace it. Users can specify a time that
451  *  is not the simulated clock time as defined by the core  simulator. This allows a fine-grain control of time
452  *  definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
453  *  also traced.
454  *
455  *  @param time The timestamp to be used to tag this change of value.
456  *  @param vm The name of the VM to be considered.
457  *  @param variable The name of the variable to be considered.
458  *  @param value The value to be subtracted from the variable.
459  *
460  *  @see TRACE_vm_variable_declare, TRACE_vm_variable_set_with_time, TRACE_vm_variable_add_with_time
461  */
462 void TRACE_vm_variable_sub_with_time (double time, const char *vm, const char *variable, double value)
463 {
464   instr_user_variable(time, vm, variable, "VM", value, InstrUserVariable::SUB, nullptr, &user_vm_variables);
465 }
466
467 /* for host variables */
468 /** @ingroup TRACE_user_variables
469  *  @brief Declare a new user variable associated to hosts.
470  *
471  *  Declare a user variable that will be associated to hosts.
472  *  A user host variable can be used to trace user variables such as the number of tasks in a server, the number of
473  *  clients in an application (for hosts), and so on. The color associated to this new variable will be random.
474  *
475  *  @param variable The name of the new variable to be declared.
476  *
477  *  @see TRACE_host_variable_declare_with_color
478  */
479 void TRACE_host_variable_declare (const char *variable)
480 {
481   instr_user_variable(0, nullptr, variable, "HOST", 0, InstrUserVariable::DECLARE, nullptr, &user_host_variables);
482 }
483
484 /** @ingroup TRACE_user_variables
485  *  @brief Declare a new user variable associated to hosts with a color.
486  *
487  *  Same as #TRACE_host_variable_declare, but associated a color to the newly created user host variable. The color
488  *  needs to be a string with three numbers separated by spaces in the range [0,1].
489  *  A light-gray color can be specified using "0.7 0.7 0.7" as color.
490  *
491  *  @param variable The name of the new variable to be declared.
492  *  @param color The color for the new variable.
493  */
494 void TRACE_host_variable_declare_with_color (const char *variable, const char *color)
495 {
496   instr_user_variable(0, nullptr, variable, "HOST", 0, InstrUserVariable::DECLARE, color, &user_host_variables);
497 }
498
499 /** @ingroup TRACE_user_variables
500  *  @brief Set the value of a variable of a host.
501  *
502  *  @param host The name of the host to be considered.
503  *  @param variable The name of the variable to be considered.
504  *  @param value The new value of the variable.
505  *
506  *  @see TRACE_host_variable_declare, TRACE_host_variable_add, TRACE_host_variable_sub
507  */
508 void TRACE_host_variable_set (const char *host, const char *variable, double value)
509 {
510   TRACE_host_variable_set_with_time(simgrid_get_clock(), host, variable, value);
511 }
512
513 /** @ingroup TRACE_user_variables
514  *  @brief Add a value to a variable of a host.
515  *
516  *  @param host The name of the host 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.
519  *
520  *  @see TRACE_host_variable_declare, TRACE_host_variable_set, TRACE_host_variable_sub
521  */
522 void TRACE_host_variable_add (const char *host, const char *variable, double value)
523 {
524   TRACE_host_variable_add_with_time(simgrid_get_clock(), host, variable, value);
525 }
526
527 /** @ingroup TRACE_user_variables
528  *  @brief Subtract a value from a variable of a host.
529  *
530  *  @param host The name of the host to be considered.
531  *  @param variable The name of the variable to be considered.
532  *  @param value The value to be subtracted from the variable.
533  *
534  *  @see TRACE_host_variable_declare, TRACE_host_variable_set, TRACE_host_variable_add
535  */
536 void TRACE_host_variable_sub (const char *host, const char *variable, double value)
537 {
538   TRACE_host_variable_sub_with_time(simgrid_get_clock(), host, variable, value);
539 }
540
541 /** @ingroup TRACE_user_variables
542  *  @brief Set the value of a variable of a host at a given timestamp.
543  *
544  *  Same as #TRACE_host_variable_set, but let user specify  the time used to trace it. Users can specify a time that
545  *  is not the simulated clock time as defined by the core  simulator. This allows a fine-grain control of time
546  *  definition, but should be used with caution since the trace  can be inconsistent if resource utilization traces are
547  *  also traced.
548  *
549  *  @param time The timestamp to be used to tag this change of value.
550  *  @param host The name of the host to be considered.
551  *  @param variable The name of the variable to be considered.
552  *  @param value The new value of the variable.
553  *
554  *  @see TRACE_host_variable_declare, TRACE_host_variable_add_with_time, TRACE_host_variable_sub_with_time
555  */
556 void TRACE_host_variable_set_with_time (double time, const char *host, const char *variable, double value)
557 {
558   instr_user_variable(time, host, variable, "HOST", value, InstrUserVariable::SET, nullptr, &user_host_variables);
559 }
560
561 /** @ingroup TRACE_user_variables
562  *  @brief Add a value to a variable of a host at a given timestamp.
563  *
564  *  Same as #TRACE_host_variable_add, but let user specify the time used to trace it. Users can specify a time that
565  *  is not the simulated clock time as defined by the core  simulator. This allows a fine-grain control of time
566  *  definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
567  *  also traced.
568  *
569  *  @param time The timestamp to be used to tag this change of value.
570  *  @param host The name of the host to be considered.
571  *  @param variable The name of the variable to be considered.
572  *  @param value The value to be added to the variable.
573  *
574  *  @see TRACE_host_variable_declare, TRACE_host_variable_set_with_time, TRACE_host_variable_sub_with_time
575  */
576 void TRACE_host_variable_add_with_time (double time, const char *host, const char *variable, double value)
577 {
578   instr_user_variable(time, host, variable, "HOST", value, InstrUserVariable::ADD, nullptr, &user_host_variables);
579 }
580
581 /** @ingroup TRACE_user_variables
582  *  @brief Subtract a value from a variable of a host at a given timestamp.
583  *
584  *  Same as #TRACE_host_variable_sub, but let user specify the time used to trace it. Users can specify a time that
585  *  is not the simulated clock time as defined by the core  simulator. This allows a fine-grain control of time
586  *  definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
587  *  also traced.
588  *
589  *  @param time The timestamp to be used to tag this change of value.
590  *  @param host The name of the host to be considered.
591  *  @param variable The name of the variable to be considered.
592  *  @param value The value to be subtracted from the variable.
593  *
594  *  @see TRACE_host_variable_declare, TRACE_host_variable_set_with_time, TRACE_host_variable_add_with_time
595  */
596 void TRACE_host_variable_sub_with_time (double time, const char *host, const char *variable, double value)
597 {
598   instr_user_variable(time, host, variable, "HOST", value, InstrUserVariable::SUB, nullptr, &user_host_variables);
599 }
600
601 /** @ingroup TRACE_user_variables
602  *  @brief Get declared user host variables
603  *
604  * This function should be used to get host variables that were already declared with #TRACE_host_variable_declare or
605  * with #TRACE_host_variable_declare_with_color.
606  *
607  * @return A dynar with the declared host variables, must be freed with xbt_dynar_free.
608  */
609 xbt_dynar_t TRACE_get_host_variables ()
610 {
611   return instr_set_to_dynar(user_host_variables);
612 }
613
614 /* for link variables */
615 /** @ingroup TRACE_user_variables
616  *  @brief Declare a new user variable associated to links.
617  *
618  *  Declare a user variable that will be associated to links.
619  *  A user link variable can be used, for example, to trace user variables such as the number of messages being
620  *  transferred through network links. The color associated to this new variable will be random.
621  *
622  *  @param variable The name of the new variable to be declared.
623  *
624  *  @see TRACE_link_variable_declare_with_color
625  */
626 void TRACE_link_variable_declare (const char *variable)
627 {
628   instr_user_variable(0, nullptr, variable, "LINK", 0, InstrUserVariable::DECLARE, nullptr, &user_link_variables);
629 }
630
631 /** @ingroup TRACE_user_variables
632  *  @brief Declare a new user variable associated to links with a color.
633  *
634  *  Same as #TRACE_link_variable_declare, but associated a color to the newly created user link variable. The color
635  *  needs to be a string with three numbers separated by spaces in the range [0,1].
636  *  A light-gray color can be specified using "0.7 0.7 0.7" as color.
637  *
638  *  @param variable The name of the new variable to be declared.
639  *  @param color The color for the new variable.
640  */
641 void TRACE_link_variable_declare_with_color (const char *variable, const char *color)
642 {
643   instr_user_variable(0, nullptr, variable, "LINK", 0, InstrUserVariable::DECLARE, color, &user_link_variables);
644 }
645
646 /** @ingroup TRACE_user_variables
647  *  @brief Set the value of a variable of a link.
648  *
649  *  @param link The name of the link to be considered.
650  *  @param variable The name of the variable to be considered.
651  *  @param value The new value of the variable.
652  *
653  *  @see TRACE_link_variable_declare, TRACE_link_variable_add, TRACE_link_variable_sub
654  */
655 void TRACE_link_variable_set (const char *link, const char *variable, double value)
656 {
657   TRACE_link_variable_set_with_time(simgrid_get_clock(), link, variable, value);
658 }
659
660 /** @ingroup TRACE_user_variables
661  *  @brief Add a value to a variable of a link.
662  *
663  *  @param link The name of the link to be considered.
664  *  @param variable The name of the variable to be considered.
665  *  @param value The value to be added to the variable.
666  *
667  *  @see TRACE_link_variable_declare, TRACE_link_variable_set, TRACE_link_variable_sub
668  */
669 void TRACE_link_variable_add (const char *link, const char *variable, double value)
670 {
671   TRACE_link_variable_add_with_time(simgrid_get_clock(), link, variable, value);
672 }
673
674 /** @ingroup TRACE_user_variables
675  *  @brief Subtract a value from a variable of a link.
676  *
677  *  @param link The name of the link to be considered.
678  *  @param variable The name of the variable to be considered.
679  *  @param value The value to be subtracted from the variable.
680  *
681  *  @see TRACE_link_variable_declare, TRACE_link_variable_set, TRACE_link_variable_add
682  */
683 void TRACE_link_variable_sub (const char *link, const char *variable, double value)
684 {
685   TRACE_link_variable_sub_with_time(simgrid_get_clock(), link, variable, value);
686 }
687
688 /** @ingroup TRACE_user_variables
689  *  @brief Set the value of a variable of a link at a given timestamp.
690  *
691  *  Same as #TRACE_link_variable_set, but let user specify the time used to trace it. Users can specify a time that
692  *  is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
693  *  definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
694  *  also traced.
695  *
696  *  @param time The timestamp to be used to tag this change of value.
697  *  @param link The name of the link to be considered.
698  *  @param variable The name of the variable to be considered.
699  *  @param value The new value of the variable.
700  *
701  *  @see TRACE_link_variable_declare, TRACE_link_variable_add_with_time, TRACE_link_variable_sub_with_time
702  */
703 void TRACE_link_variable_set_with_time (double time, const char *link, const char *variable, double value)
704 {
705   instr_user_variable(time, link, variable, "LINK", value, InstrUserVariable::SET, nullptr, &user_link_variables);
706 }
707
708 /** @ingroup TRACE_user_variables
709  *  @brief Add a value to a variable of a link at a given timestamp.
710  *
711  *  Same as #TRACE_link_variable_add, but let user specify the time used to trace it. Users can specify a time that
712  *  is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
713  *  definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
714  *  also traced.
715  *
716  *  @param time The timestamp to be used to tag this change of value.
717  *  @param link The name of the link to be considered.
718  *  @param variable The name of the variable to be considered.
719  *  @param value The value to be added to the variable.
720  *
721  *  @see TRACE_link_variable_declare, TRACE_link_variable_set_with_time, TRACE_link_variable_sub_with_time
722  */
723 void TRACE_link_variable_add_with_time (double time, const char *link, const char *variable, double value)
724 {
725   instr_user_variable(time, link, variable, "LINK", value, InstrUserVariable::ADD, nullptr, &user_link_variables);
726 }
727
728 /** @ingroup TRACE_user_variables
729  *  @brief Subtract a value from a variable of a link at a given timestamp.
730  *
731  *  Same as #TRACE_link_variable_sub, but let user specify the time used to trace it. Users can specify a time that
732  *  is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
733  *  definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
734  *  also traced.
735  *
736  *  @param time The timestamp to be used to tag this change of value.
737  *  @param link The name of the link to be considered.
738  *  @param variable The name of the variable to be considered.
739  *  @param value The value to be subtracted from the variable.
740  *
741  *  @see TRACE_link_variable_declare, TRACE_link_variable_set_with_time, TRACE_link_variable_add_with_time
742  */
743 void TRACE_link_variable_sub_with_time (double time, const char *link, const char *variable, double value)
744 {
745   instr_user_variable(time, link, variable, "LINK", value, InstrUserVariable::SUB, nullptr, &user_link_variables);
746 }
747
748 /* for link variables, but with src and dst used for get_route */
749 /** @ingroup TRACE_user_variables
750  *  @brief Set the value of the variable present in the links connecting source and destination.
751  *
752  *  Same as #TRACE_link_variable_set, but instead of providing the name of link to be considered, provide the source
753  *  and destination hosts. All links that are part of the route between source and destination will have the variable
754  *  set to the provided value.
755  *
756  *  @param src The name of the source host for get route.
757  *  @param dst The name of the destination host for get route.
758  *  @param variable The name of the variable to be considered.
759  *  @param value The new value of the variable.
760  *
761  *  @see TRACE_link_variable_declare, TRACE_link_srcdst_variable_add, TRACE_link_srcdst_variable_sub
762  */
763 void TRACE_link_srcdst_variable_set (const char *src, const char *dst, const char *variable, double value)
764 {
765   TRACE_link_srcdst_variable_set_with_time(simgrid_get_clock(), src, dst, variable, value);
766 }
767
768 /** @ingroup TRACE_user_variables
769  *  @brief Add a value to the variable present in the links connecting source and destination.
770  *
771  *  Same as #TRACE_link_variable_add, but instead of providing the name of link to be considered, provide the source
772  *  and destination hosts. All links that are part of the route between source and destination will have the value
773  *  passed as parameter added to the current value of the variable name to be considered.
774  *
775  *  @param src The name of the source host for get route.
776  *  @param dst The name of the destination host for get route.
777  *  @param variable The name of the variable to be considered.
778  *  @param value The value to be added to the variable.
779  *
780  *  @see TRACE_link_variable_declare, TRACE_link_srcdst_variable_set, TRACE_link_srcdst_variable_sub
781  */
782 void TRACE_link_srcdst_variable_add (const char *src, const char *dst, const char *variable, double value)
783 {
784   TRACE_link_srcdst_variable_add_with_time(simgrid_get_clock(), src, dst, variable, value);
785 }
786
787 /** @ingroup TRACE_user_variables
788  *  @brief Subtract a value from the variable present in the links connecting source and destination.
789  *
790  *  Same as #TRACE_link_variable_sub, but instead of providing the name of link to be considered, provide the source
791  *  and destination hosts. All links that are part of the route between source and destination will have the value
792  *  passed as parameter subtracted from the current value of the variable name to be considered.
793  *
794  *  @param src The name of the source host for get route.
795  *  @param dst The name of the destination host for get route.
796  *  @param variable The name of the variable to be considered.
797  *  @param value The value to be subtracted from the variable.
798  *
799  *  @see TRACE_link_variable_declare, TRACE_link_srcdst_variable_set, TRACE_link_srcdst_variable_add
800  */
801 void TRACE_link_srcdst_variable_sub (const char *src, const char *dst, const char *variable, double value)
802 {
803   TRACE_link_srcdst_variable_sub_with_time(simgrid_get_clock(), src, dst, variable, value);
804 }
805
806 /** @ingroup TRACE_user_variables
807  *  @brief Set the value of the variable present in the links connecting source and destination at a given timestamp.
808  *
809  *  Same as #TRACE_link_srcdst_variable_set, but let user specify the time used to trace it. Users can specify a time
810  *  that is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
811  *  definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
812  *  also traced.
813  *
814  *  @param time The timestamp to be used to tag this change of value.
815  *  @param src The name of the source host for get route.
816  *  @param dst The name of the destination host for get route.
817  *  @param variable The name of the variable to be considered.
818  *  @param value The new value of the variable.
819  *
820  *  @see TRACE_link_variable_declare, TRACE_link_srcdst_variable_add_with_time, TRACE_link_srcdst_variable_sub_with_time
821  */
822 void TRACE_link_srcdst_variable_set_with_time (double time, const char *src, const char *dst, const char *variable,
823                                                double value)
824 {
825   instr_user_srcdst_variable(time, src, dst, variable, "LINK", value, InstrUserVariable::SET);
826 }
827
828 /** @ingroup TRACE_user_variables
829  *  @brief Add a value to the variable present in the links connecting source and destination at a given timestamp.
830  *
831  *  Same as #TRACE_link_srcdst_variable_add, but let user specify the time used to trace it. Users can specify a time
832  *  that is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
833  *  definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
834  *  also traced.
835  *
836  *  @param time The timestamp to be used to tag this change of value.
837  *  @param src The name of the source host for get route.
838  *  @param dst The name of the destination host for get route.
839  *  @param variable The name of the variable to be considered.
840  *  @param value The value to be added to the variable.
841  *
842  *  @see TRACE_link_variable_declare, TRACE_link_srcdst_variable_set_with_time, TRACE_link_srcdst_variable_sub_with_time
843  */
844 void TRACE_link_srcdst_variable_add_with_time (double time, const char *src, const char *dst, const char *variable,
845                                                double value)
846 {
847   instr_user_srcdst_variable(time, src, dst, variable, "LINK", value, InstrUserVariable::ADD);
848 }
849
850 /** @ingroup TRACE_user_variables
851  *  @brief Subtract a value from the variable present in the links connecting source and dest. at a given timestamp.
852  *
853  *  Same as #TRACE_link_srcdst_variable_sub, but let user specify the time used to trace it. Users can specify a time
854  *  that is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
855  *  definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
856  *  also traced.
857  *
858  *  @param time The timestamp to be used to tag this change of value.
859  *  @param src The name of the source host for get route.
860  *  @param dst The name of the destination host for get route.
861  *  @param variable The name of the variable to be considered.
862  *  @param value The value to be subtracted from the variable.
863  *
864  *  @see TRACE_link_variable_declare, TRACE_link_srcdst_variable_set_with_time, TRACE_link_srcdst_variable_add_with_time
865  */
866 void TRACE_link_srcdst_variable_sub_with_time (double time, const char *src, const char *dst, const char *variable,
867                                                double value)
868 {
869   instr_user_srcdst_variable(time, src, dst, variable, "LINK", value, InstrUserVariable::SUB);
870 }
871
872 /** @ingroup TRACE_user_variables
873  *  @brief Get declared user link variables
874  *
875  * This function should be used to get link variables that were already declared with #TRACE_link_variable_declare or
876  * with #TRACE_link_variable_declare_with_color.
877  *
878  * @return A dynar with the declared link variables, must be freed with xbt_dynar_free.
879  */
880 xbt_dynar_t TRACE_get_link_variables ()
881 {
882   return instr_set_to_dynar(user_link_variables);
883 }
884
885 /** @ingroup TRACE_user_variables
886  *  @brief Declare a new user state associated to hosts.
887  *
888  *  Declare a user state that will be associated to hosts.
889  *  A user host state can be used to trace application states.
890  *
891  *  @param state The name of the new state to be declared.
892  *
893  *  @see TRACE_host_state_declare_value
894  */
895 void TRACE_host_state_declare (const char *state)
896 {
897   instr_new_user_state_type("HOST", state);
898 }
899
900 /** @ingroup TRACE_user_variables
901  *  @brief Declare a new value for a user state associated to hosts.
902  *
903  *  Declare a value for a state. The color needs to be a string with 3 numbers separated by spaces in the range [0,1].
904  *  A light-gray color can be specified using "0.7 0.7 0.7" as color.
905  *
906  *  @param state The name of the new state to be declared.
907  *  @param value The name of the value
908  *  @param color The color of the value
909  *
910  *  @see TRACE_host_state_declare
911  */
912 void TRACE_host_state_declare_value (const char *state, const char *value, const char *color)
913 {
914   instr_new_value_for_user_state_type (state, value, color);
915 }
916
917 /** @ingroup TRACE_user_variables
918  *  @brief Set the user state to the given value.
919  *
920  *  Change a user state previously declared to the given value.
921  *
922  *  @param host The name of the host to be considered.
923  *  @param state_name The name of the state previously declared.
924  *  @param value_name The new value of the state.
925  *
926  *  @see TRACE_host_state_declare, TRACE_host_push_state, TRACE_host_pop_state, TRACE_host_reset_state
927  */
928 void TRACE_host_set_state(const char* host, const char* state_name, const char* value_name)
929 {
930   simgrid::instr::StateType* state = simgrid::instr::Container::by_name(host)->get_state(state_name);
931   state->add_entity_value(value_name);
932   state->set_event(value_name);
933 }
934
935 /** @ingroup TRACE_user_variables
936  *  @brief Push a new value for a state of a given host.
937  *
938  *  Change a user state previously declared by pushing the new value to the state.
939  *
940  *  @param host The name of the host to be considered.
941  *  @param state_name The name of the state previously declared.
942  *  @param value_name The value to be pushed.
943  *
944  *  @see TRACE_host_state_declare, TRACE_host_set_state, TRACE_host_pop_state, TRACE_host_reset_state
945  */
946 void TRACE_host_push_state(const char* host, const char* state_name, const char* value_name)
947 {
948   simgrid::instr::Container::by_name(host)->get_state(state_name)->push_event(value_name);
949 }
950
951 /** @ingroup TRACE_user_variables
952  *  @brief Pop the last value of a state of a given host.
953  *
954  *  Change a user state previously declared by removing the last value of the state.
955  *
956  *  @param host The name of the host to be considered.
957  *  @param state_name The name of the state to be popped.
958  *
959  *  @see TRACE_host_state_declare, TRACE_host_set_state, TRACE_host_push_state, TRACE_host_reset_state
960  */
961 void TRACE_host_pop_state(const char* host, const char* state_name)
962 {
963   simgrid::instr::Container::by_name(host)->get_state(state_name)->pop_event();
964 }