Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove useless type aliases.
[simgrid.git] / src / instr / instr_interface.cpp
1 /* Copyright (c) 2010-2020. 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 "src/instr/instr_private.hpp"
9 #include "src/surf/network_interface.hpp"
10 #include "src/surf/surf_private.hpp"
11 #include "surf/surf.hpp"
12 #include "xbt/random.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> created_categories;
21 std::set<std::string> declared_marks;
22 std::set<std::string> user_host_variables;
23 std::set<std::string> user_vm_variables;
24 std::set<std::string> user_link_variables;
25
26 static xbt_dynar_t instr_set_to_dynar(const std::set<std::string>& 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 a 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   else
86     created_categories.insert(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()->type_->by_name_or_create<simgrid::instr::EventType>(mark_type);
151   declared_marks.insert(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   simgrid::instr::EventType* type =
179       static_cast<simgrid::instr::EventType*>(simgrid::instr::Container::get_root()->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   simgrid::instr::EventType* type =
234       static_cast<simgrid::instr::EventType*>(simgrid::instr::Container::get_root()->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* father_type,
261                                 double value, InstrUserVariable what, const char* color, std::set<std::string>* filter)
262 {
263   /* safe switches. tracing has to be activated and if platform is not traced, we don't allow user variables */
264   if (not TRACE_is_enabled() || not TRACE_needs_platform())
265     return;
266
267   //check if variable is already declared
268   auto created = filter->find(variable_name);
269   if (what == InstrUserVariable::DECLARE) {
270     if (created == filter->end()) { // not declared yet
271       filter->insert(variable_name);
272       instr_new_user_variable_type(father_type, variable_name, color == nullptr ? "" : color);
273     }
274   }else{
275     if (created != filter->end()) { // declared, let's work
276       simgrid::instr::VariableType* variable =
277           simgrid::instr::Container::by_name(resource)->get_variable(variable_name);
278       switch (what){
279         case InstrUserVariable::SET:
280           variable->set_event(time, value);
281           break;
282         case InstrUserVariable::ADD:
283           variable->add_event(time, value);
284           break;
285         case InstrUserVariable::SUB:
286           variable->sub_event(time, value);
287           break;
288         default:
289           THROW_IMPOSSIBLE;
290       }
291     }
292   }
293 }
294
295 static void instr_user_srcdst_variable(double time, const char *src, const char *dst, const char *variable,
296                               const char *father_type, double value, InstrUserVariable what)
297 {
298   simgrid::kernel::routing::NetPoint* src_elm = sg_netpoint_by_name_or_null(src);
299   if (not src_elm)
300     xbt_die("Element '%s' not found!",src);
301
302   simgrid::kernel::routing::NetPoint* dst_elm = sg_netpoint_by_name_or_null(dst);
303   if (not dst_elm)
304     xbt_die("Element '%s' not found!",dst);
305
306   std::vector<simgrid::kernel::resource::LinkImpl*> route;
307   simgrid::kernel::routing::NetZoneImpl::get_global_route(src_elm, dst_elm, route, nullptr);
308   for (auto const& link : route)
309     instr_user_variable(time, link->get_cname(), variable, father_type, value, what, nullptr, &user_link_variables);
310 }
311
312 /** @ingroup TRACE_API
313  *  @brief Creates a file with the topology of the platform file used for the simulator.
314  *
315  *  The graph topology will have the following properties: all hosts, links and routers of the platform file are mapped
316  *  to graph nodes; routes are mapped to edges.
317  *  The platform's zones are not represented in the output.
318  *
319  *  @param filename The name of the file that will hold the graph.
320  *
321  *  @return 1 of successful, 0 otherwise.
322  */
323 int TRACE_platform_graph_export_graphviz (const char *filename)
324 {
325   simgrid::instr::platform_graph_export_graphviz(filename);
326   return 1;
327 }
328
329 /*
330  * Derived functions that use instr_user_variable and TRACE_user_srcdst_variable. They were previously defined as
331  * pre-processors directives, but were transformed into functions so the user can track them using gdb.
332  */
333
334 /* for VM variables */
335 /** @ingroup TRACE_user_variables
336  *  @brief Declare a new user variable associated to VMs.
337  *
338  *  Declare a user variable that will be associated to VMs. A user vm variable can be used to trace user variables
339  *  such as the number of tasks in a VM, the number of clients in an application (for VMs), and so on. The color
340  *  associated to this new variable will be random.
341  *
342  *  @param variable The name of the new variable to be declared.
343  *
344  *  @see TRACE_vm_variable_declare_with_color
345  */
346 void TRACE_vm_variable_declare (const char *variable)
347 {
348   instr_user_variable(0, nullptr, variable, "VM", 0, InstrUserVariable::DECLARE, nullptr, &user_vm_variables);
349 }
350
351 /** @ingroup TRACE_user_variables
352  *  @brief Declare a new user variable associated to VMs with a color.
353  *
354  *  Same as #TRACE_vm_variable_declare, but associated a color to the newly created user host variable. The color needs
355  *  to be a string with three numbers separated by spaces in the range [0,1].
356  *  A light-gray color can be specified using "0.7 0.7 0.7" as color.
357  *
358  *  @param variable The name of the new variable to be declared.
359  *  @param color The color for the new variable.
360  */
361 void TRACE_vm_variable_declare_with_color (const char *variable, const char *color)
362 {
363   instr_user_variable(0, nullptr, variable, "VM", 0, InstrUserVariable::DECLARE, color, &user_vm_variables);
364 }
365
366 /** @ingroup TRACE_user_variables
367  *  @brief Set the value of a variable of a host.
368  *
369  *  @param vm The name of the VM to be considered.
370  *  @param variable The name of the variable to be considered.
371  *  @param value The new value of the variable.
372  *
373  *  @see TRACE_vm_variable_declare, TRACE_vm_variable_add, TRACE_vm_variable_sub
374  */
375 void TRACE_vm_variable_set (const char *vm, const char *variable, double value)
376 {
377   TRACE_vm_variable_set_with_time(simgrid_get_clock(), vm, variable, value);
378 }
379
380 /** @ingroup TRACE_user_variables
381  *  @brief Add a value to a variable of a VM.
382  *
383  *  @param vm The name of the VM to be considered.
384  *  @param variable The name of the variable to be considered.
385  *  @param value The value to be added to the variable.
386  *
387  *  @see TRACE_vm_variable_declare, TRACE_vm_variable_set, TRACE_vm_variable_sub
388  */
389 void TRACE_vm_variable_add (const char *vm, const char *variable, double value)
390 {
391   TRACE_vm_variable_add_with_time(simgrid_get_clock(), vm, variable, value);
392 }
393
394 /** @ingroup TRACE_user_variables
395  *  @brief Subtract a value from a variable of a VM.
396  *
397  *  @param vm The name of the vm to be considered.
398  *  @param variable The name of the variable to be considered.
399  *  @param value The value to be subtracted from the variable.
400  *
401  *  @see TRACE_vm_variable_declare, TRACE_vm_variable_set, TRACE_vm_variable_add
402  */
403 void TRACE_vm_variable_sub (const char *vm, const char *variable, double value)
404 {
405   TRACE_vm_variable_sub_with_time(simgrid_get_clock(), vm, variable, value);
406 }
407
408 /** @ingroup TRACE_user_variables
409  *  @brief Set the value of a variable of a VM at a given timestamp.
410  *
411  *  Same as #TRACE_vm_variable_set, but let user specify  the time used to trace it. Users can specify a time that
412  *  is not the simulated clock time as defined by the core  simulator. This allows a fine-grain control of time
413  *  definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
414  *  also traced.
415  *
416  *  @param time The timestamp to be used to tag this change of value.
417  *  @param vm The name of the VM to be considered.
418  *  @param variable The name of the variable to be considered.
419  *  @param value The new value of the variable.
420  *
421  *  @see TRACE_vm_variable_declare, TRACE_vm_variable_add_with_time, TRACE_vm_variable_sub_with_time
422  */
423 void TRACE_vm_variable_set_with_time (double time, const char *vm, const char *variable, double value)
424 {
425   instr_user_variable(time, vm, variable, "VM", value, InstrUserVariable::SET, nullptr, &user_vm_variables);
426 }
427
428 /** @ingroup TRACE_user_variables
429  *  @brief Add a value to a variable of a VM at a given timestamp.
430  *
431  *  Same as #TRACE_vm_variable_add, but let user specify the time used to trace it. Users can specify a time that
432  *  is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
433  *  definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
434  *  also traced.
435  *
436  *  @param time The timestamp to be used to tag this change of value.
437  *  @param vm The name of the VM to be considered.
438  *  @param variable The name of the variable to be considered.
439  *  @param value The value to be added to the variable.
440  *
441  *  @see TRACE_vm_variable_declare, TRACE_vm_variable_set_with_time, TRACE_vm_variable_sub_with_time
442  */
443 void TRACE_vm_variable_add_with_time (double time, const char *vm, const char *variable, double value)
444 {
445   instr_user_variable(time, vm, variable, "VM", value, InstrUserVariable::ADD, nullptr, &user_vm_variables);
446 }
447
448 /** @ingroup TRACE_user_variables
449  *  @brief Subtract a value from a variable of a VM at a given timestamp.
450  *
451  *  Same as #TRACE_vm_variable_sub, but let user specify the time used to trace it. Users can specify a time that
452  *  is not the simulated clock time as defined by the core  simulator. This allows a fine-grain control of time
453  *  definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
454  *  also traced.
455  *
456  *  @param time The timestamp to be used to tag this change of value.
457  *  @param vm The name of the VM to be considered.
458  *  @param variable The name of the variable to be considered.
459  *  @param value The value to be subtracted from the variable.
460  *
461  *  @see TRACE_vm_variable_declare, TRACE_vm_variable_set_with_time, TRACE_vm_variable_add_with_time
462  */
463 void TRACE_vm_variable_sub_with_time (double time, const char *vm, const char *variable, double value)
464 {
465   instr_user_variable(time, vm, variable, "VM", value, InstrUserVariable::SUB, nullptr, &user_vm_variables);
466 }
467
468 /* for host variables */
469 /** @ingroup TRACE_user_variables
470  *  @brief Declare a new user variable associated to hosts.
471  *
472  *  Declare a user variable that will be associated to hosts.
473  *  A user host variable can be used to trace user variables such as the number of tasks in a server, the number of
474  *  clients in an application (for hosts), and so on. The color associated to this new variable will be random.
475  *
476  *  @param variable The name of the new variable to be declared.
477  *
478  *  @see TRACE_host_variable_declare_with_color
479  */
480 void TRACE_host_variable_declare (const char *variable)
481 {
482   instr_user_variable(0, nullptr, variable, "HOST", 0, InstrUserVariable::DECLARE, nullptr, &user_host_variables);
483 }
484
485 /** @ingroup TRACE_user_variables
486  *  @brief Declare a new user variable associated to hosts with a color.
487  *
488  *  Same as #TRACE_host_variable_declare, but associated a color to the newly created user host variable. The color
489  *  needs to be a string with three numbers separated by spaces in the range [0,1].
490  *  A light-gray color can be specified using "0.7 0.7 0.7" as color.
491  *
492  *  @param variable The name of the new variable to be declared.
493  *  @param color The color for the new variable.
494  */
495 void TRACE_host_variable_declare_with_color (const char *variable, const char *color)
496 {
497   instr_user_variable(0, nullptr, variable, "HOST", 0, InstrUserVariable::DECLARE, color, &user_host_variables);
498 }
499
500 /** @ingroup TRACE_user_variables
501  *  @brief Set the value of a variable of a host.
502  *
503  *  @param host The name of the host to be considered.
504  *  @param variable The name of the variable to be considered.
505  *  @param value The new value of the variable.
506  *
507  *  @see TRACE_host_variable_declare, TRACE_host_variable_add, TRACE_host_variable_sub
508  */
509 void TRACE_host_variable_set (const char *host, const char *variable, double value)
510 {
511   TRACE_host_variable_set_with_time(simgrid_get_clock(), host, variable, value);
512 }
513
514 /** @ingroup TRACE_user_variables
515  *  @brief Add a value to a variable of a host.
516  *
517  *  @param host The name of the host to be considered.
518  *  @param variable The name of the variable to be considered.
519  *  @param value The value to be added to the variable.
520  *
521  *  @see TRACE_host_variable_declare, TRACE_host_variable_set, TRACE_host_variable_sub
522  */
523 void TRACE_host_variable_add (const char *host, const char *variable, double value)
524 {
525   TRACE_host_variable_add_with_time(simgrid_get_clock(), host, variable, value);
526 }
527
528 /** @ingroup TRACE_user_variables
529  *  @brief Subtract a value from a variable of a host.
530  *
531  *  @param host The name of the host to be considered.
532  *  @param variable The name of the variable to be considered.
533  *  @param value The value to be subtracted from the variable.
534  *
535  *  @see TRACE_host_variable_declare, TRACE_host_variable_set, TRACE_host_variable_add
536  */
537 void TRACE_host_variable_sub (const char *host, const char *variable, double value)
538 {
539   TRACE_host_variable_sub_with_time(simgrid_get_clock(), host, variable, value);
540 }
541
542 /** @ingroup TRACE_user_variables
543  *  @brief Set the value of a variable of a host at a given timestamp.
544  *
545  *  Same as #TRACE_host_variable_set, but let user specify  the time used to trace it. Users can specify a time that
546  *  is not the simulated clock time as defined by the core  simulator. This allows a fine-grain control of time
547  *  definition, but should be used with caution since the trace  can be inconsistent if resource utilization traces are
548  *  also traced.
549  *
550  *  @param time The timestamp to be used to tag this change of value.
551  *  @param host The name of the host to be considered.
552  *  @param variable The name of the variable to be considered.
553  *  @param value The new value of the variable.
554  *
555  *  @see TRACE_host_variable_declare, TRACE_host_variable_add_with_time, TRACE_host_variable_sub_with_time
556  */
557 void TRACE_host_variable_set_with_time (double time, const char *host, const char *variable, double value)
558 {
559   instr_user_variable(time, host, variable, "HOST", value, InstrUserVariable::SET, nullptr, &user_host_variables);
560 }
561
562 /** @ingroup TRACE_user_variables
563  *  @brief Add a value to a variable of a host at a given timestamp.
564  *
565  *  Same as #TRACE_host_variable_add, but let user specify the time used to trace it. Users can specify a time that
566  *  is not the simulated clock time as defined by the core  simulator. This allows a fine-grain control of time
567  *  definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
568  *  also traced.
569  *
570  *  @param time The timestamp to be used to tag this change of value.
571  *  @param host The name of the host to be considered.
572  *  @param variable The name of the variable to be considered.
573  *  @param value The value to be added to the variable.
574  *
575  *  @see TRACE_host_variable_declare, TRACE_host_variable_set_with_time, TRACE_host_variable_sub_with_time
576  */
577 void TRACE_host_variable_add_with_time (double time, const char *host, const char *variable, double value)
578 {
579   instr_user_variable(time, host, variable, "HOST", value, InstrUserVariable::ADD, nullptr, &user_host_variables);
580 }
581
582 /** @ingroup TRACE_user_variables
583  *  @brief Subtract a value from a variable of a host at a given timestamp.
584  *
585  *  Same as #TRACE_host_variable_sub, but let user specify the time used to trace it. Users can specify a time that
586  *  is not the simulated clock time as defined by the core  simulator. This allows a fine-grain control of time
587  *  definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
588  *  also traced.
589  *
590  *  @param time The timestamp to be used to tag this change of value.
591  *  @param host The name of the host to be considered.
592  *  @param variable The name of the variable to be considered.
593  *  @param value The value to be subtracted from the variable.
594  *
595  *  @see TRACE_host_variable_declare, TRACE_host_variable_set_with_time, TRACE_host_variable_add_with_time
596  */
597 void TRACE_host_variable_sub_with_time (double time, const char *host, const char *variable, double value)
598 {
599   instr_user_variable(time, host, variable, "HOST", value, InstrUserVariable::SUB, nullptr, &user_host_variables);
600 }
601
602 /** @ingroup TRACE_user_variables
603  *  @brief Get declared user host variables
604  *
605  * This function should be used to get host variables that were already declared with #TRACE_host_variable_declare or
606  * with #TRACE_host_variable_declare_with_color.
607  *
608  * @return A dynar with the declared host variables, must be freed with xbt_dynar_free.
609  */
610 xbt_dynar_t TRACE_get_host_variables ()
611 {
612   return instr_set_to_dynar(user_host_variables);
613 }
614
615 /* for link variables */
616 /** @ingroup TRACE_user_variables
617  *  @brief Declare a new user variable associated to links.
618  *
619  *  Declare a user variable that will be associated to links.
620  *  A user link variable can be used, for example, to trace user variables such as the number of messages being
621  *  transferred through network links. The color associated to this new variable will be random.
622  *
623  *  @param variable The name of the new variable to be declared.
624  *
625  *  @see TRACE_link_variable_declare_with_color
626  */
627 void TRACE_link_variable_declare (const char *variable)
628 {
629   instr_user_variable(0, nullptr, variable, "LINK", 0, InstrUserVariable::DECLARE, nullptr, &user_link_variables);
630 }
631
632 /** @ingroup TRACE_user_variables
633  *  @brief Declare a new user variable associated to links with a color.
634  *
635  *  Same as #TRACE_link_variable_declare, but associated a color to the newly created user link variable. The color
636  *  needs to be a string with three numbers separated by spaces in the range [0,1].
637  *  A light-gray color can be specified using "0.7 0.7 0.7" as color.
638  *
639  *  @param variable The name of the new variable to be declared.
640  *  @param color The color for the new variable.
641  */
642 void TRACE_link_variable_declare_with_color (const char *variable, const char *color)
643 {
644   instr_user_variable(0, nullptr, variable, "LINK", 0, InstrUserVariable::DECLARE, color, &user_link_variables);
645 }
646
647 /** @ingroup TRACE_user_variables
648  *  @brief Set the value of a variable of a link.
649  *
650  *  @param link The name of the link to be considered.
651  *  @param variable The name of the variable to be considered.
652  *  @param value The new value of the variable.
653  *
654  *  @see TRACE_link_variable_declare, TRACE_link_variable_add, TRACE_link_variable_sub
655  */
656 void TRACE_link_variable_set (const char *link, const char *variable, double value)
657 {
658   TRACE_link_variable_set_with_time(simgrid_get_clock(), link, variable, value);
659 }
660
661 /** @ingroup TRACE_user_variables
662  *  @brief Add a value to a variable of a link.
663  *
664  *  @param link The name of the link to be considered.
665  *  @param variable The name of the variable to be considered.
666  *  @param value The value to be added to the variable.
667  *
668  *  @see TRACE_link_variable_declare, TRACE_link_variable_set, TRACE_link_variable_sub
669  */
670 void TRACE_link_variable_add (const char *link, const char *variable, double value)
671 {
672   TRACE_link_variable_add_with_time(simgrid_get_clock(), link, variable, value);
673 }
674
675 /** @ingroup TRACE_user_variables
676  *  @brief Subtract a value from a variable of a link.
677  *
678  *  @param link The name of the link to be considered.
679  *  @param variable The name of the variable to be considered.
680  *  @param value The value to be subtracted from the variable.
681  *
682  *  @see TRACE_link_variable_declare, TRACE_link_variable_set, TRACE_link_variable_add
683  */
684 void TRACE_link_variable_sub (const char *link, const char *variable, double value)
685 {
686   TRACE_link_variable_sub_with_time(simgrid_get_clock(), link, variable, value);
687 }
688
689 /** @ingroup TRACE_user_variables
690  *  @brief Set the value of a variable of a link at a given timestamp.
691  *
692  *  Same as #TRACE_link_variable_set, but let user specify the time used to trace it. Users can specify a time that
693  *  is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
694  *  definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
695  *  also traced.
696  *
697  *  @param time The timestamp to be used to tag this change of value.
698  *  @param link The name of the link to be considered.
699  *  @param variable The name of the variable to be considered.
700  *  @param value The new value of the variable.
701  *
702  *  @see TRACE_link_variable_declare, TRACE_link_variable_add_with_time, TRACE_link_variable_sub_with_time
703  */
704 void TRACE_link_variable_set_with_time (double time, const char *link, const char *variable, double value)
705 {
706   instr_user_variable(time, link, variable, "LINK", value, InstrUserVariable::SET, nullptr, &user_link_variables);
707 }
708
709 /** @ingroup TRACE_user_variables
710  *  @brief Add a value to a variable of a link at a given timestamp.
711  *
712  *  Same as #TRACE_link_variable_add, but let user specify the time used to trace it. Users can specify a time that
713  *  is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
714  *  definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
715  *  also traced.
716  *
717  *  @param time The timestamp to be used to tag this change of value.
718  *  @param link The name of the link to be considered.
719  *  @param variable The name of the variable to be considered.
720  *  @param value The value to be added to the variable.
721  *
722  *  @see TRACE_link_variable_declare, TRACE_link_variable_set_with_time, TRACE_link_variable_sub_with_time
723  */
724 void TRACE_link_variable_add_with_time (double time, const char *link, const char *variable, double value)
725 {
726   instr_user_variable(time, link, variable, "LINK", value, InstrUserVariable::ADD, nullptr, &user_link_variables);
727 }
728
729 /** @ingroup TRACE_user_variables
730  *  @brief Subtract a value from a variable of a link at a given timestamp.
731  *
732  *  Same as #TRACE_link_variable_sub, but let user specify the time used to trace it. Users can specify a time that
733  *  is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
734  *  definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
735  *  also traced.
736  *
737  *  @param time The timestamp to be used to tag this change of value.
738  *  @param link The name of the link to be considered.
739  *  @param variable The name of the variable to be considered.
740  *  @param value The value to be subtracted from the variable.
741  *
742  *  @see TRACE_link_variable_declare, TRACE_link_variable_set_with_time, TRACE_link_variable_add_with_time
743  */
744 void TRACE_link_variable_sub_with_time (double time, const char *link, const char *variable, double value)
745 {
746   instr_user_variable(time, link, variable, "LINK", value, InstrUserVariable::SUB, nullptr, &user_link_variables);
747 }
748
749 /* for link variables, but with src and dst used for get_route */
750 /** @ingroup TRACE_user_variables
751  *  @brief Set the value of the variable present in the links connecting source and destination.
752  *
753  *  Same as #TRACE_link_variable_set, but instead of providing the name of link to be considered, provide the source
754  *  and destination hosts. All links that are part of the route between source and destination will have the variable
755  *  set to the provided value.
756  *
757  *  @param src The name of the source host for get route.
758  *  @param dst The name of the destination host for get route.
759  *  @param variable The name of the variable to be considered.
760  *  @param value The new value of the variable.
761  *
762  *  @see TRACE_link_variable_declare, TRACE_link_srcdst_variable_add, TRACE_link_srcdst_variable_sub
763  */
764 void TRACE_link_srcdst_variable_set (const char *src, const char *dst, const char *variable, double value)
765 {
766   TRACE_link_srcdst_variable_set_with_time(simgrid_get_clock(), src, dst, variable, value);
767 }
768
769 /** @ingroup TRACE_user_variables
770  *  @brief Add a value to the variable present in the links connecting source and destination.
771  *
772  *  Same as #TRACE_link_variable_add, but instead of providing the name of link to be considered, provide the source
773  *  and destination hosts. All links that are part of the route between source and destination will have the value
774  *  passed as parameter added to the current value of the variable name to be considered.
775  *
776  *  @param src The name of the source host for get route.
777  *  @param dst The name of the destination host for get route.
778  *  @param variable The name of the variable to be considered.
779  *  @param value The value to be added to the variable.
780  *
781  *  @see TRACE_link_variable_declare, TRACE_link_srcdst_variable_set, TRACE_link_srcdst_variable_sub
782  */
783 void TRACE_link_srcdst_variable_add (const char *src, const char *dst, const char *variable, double value)
784 {
785   TRACE_link_srcdst_variable_add_with_time(simgrid_get_clock(), src, dst, variable, value);
786 }
787
788 /** @ingroup TRACE_user_variables
789  *  @brief Subtract a value from the variable present in the links connecting source and destination.
790  *
791  *  Same as #TRACE_link_variable_sub, but instead of providing the name of link to be considered, provide the source
792  *  and destination hosts. All links that are part of the route between source and destination will have the value
793  *  passed as parameter subtracted from the current value of the variable name to be considered.
794  *
795  *  @param src The name of the source host for get route.
796  *  @param dst The name of the destination host for get route.
797  *  @param variable The name of the variable to be considered.
798  *  @param value The value to be subtracted from the variable.
799  *
800  *  @see TRACE_link_variable_declare, TRACE_link_srcdst_variable_set, TRACE_link_srcdst_variable_add
801  */
802 void TRACE_link_srcdst_variable_sub (const char *src, const char *dst, const char *variable, double value)
803 {
804   TRACE_link_srcdst_variable_sub_with_time(simgrid_get_clock(), src, dst, variable, value);
805 }
806
807 /** @ingroup TRACE_user_variables
808  *  @brief Set the value of the variable present in the links connecting source and destination at a given timestamp.
809  *
810  *  Same as #TRACE_link_srcdst_variable_set, but let user specify the time used to trace it. Users can specify a time
811  *  that is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
812  *  definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
813  *  also traced.
814  *
815  *  @param time The timestamp to be used to tag this change of value.
816  *  @param src The name of the source host for get route.
817  *  @param dst The name of the destination host for get route.
818  *  @param variable The name of the variable to be considered.
819  *  @param value The new value of the variable.
820  *
821  *  @see TRACE_link_variable_declare, TRACE_link_srcdst_variable_add_with_time, TRACE_link_srcdst_variable_sub_with_time
822  */
823 void TRACE_link_srcdst_variable_set_with_time (double time, const char *src, const char *dst, const char *variable,
824                                                double value)
825 {
826   instr_user_srcdst_variable(time, src, dst, variable, "LINK", value, InstrUserVariable::SET);
827 }
828
829 /** @ingroup TRACE_user_variables
830  *  @brief Add a value to the variable present in the links connecting source and destination at a given timestamp.
831  *
832  *  Same as #TRACE_link_srcdst_variable_add, but let user specify the time used to trace it. Users can specify a time
833  *  that is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
834  *  definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
835  *  also traced.
836  *
837  *  @param time The timestamp to be used to tag this change of value.
838  *  @param src The name of the source host for get route.
839  *  @param dst The name of the destination host for get route.
840  *  @param variable The name of the variable to be considered.
841  *  @param value The value to be added to the variable.
842  *
843  *  @see TRACE_link_variable_declare, TRACE_link_srcdst_variable_set_with_time, TRACE_link_srcdst_variable_sub_with_time
844  */
845 void TRACE_link_srcdst_variable_add_with_time (double time, const char *src, const char *dst, const char *variable,
846                                                double value)
847 {
848   instr_user_srcdst_variable(time, src, dst, variable, "LINK", value, InstrUserVariable::ADD);
849 }
850
851 /** @ingroup TRACE_user_variables
852  *  @brief Subtract a value from the variable present in the links connecting source and dest. at a given timestamp.
853  *
854  *  Same as #TRACE_link_srcdst_variable_sub, but let user specify the time used to trace it. Users can specify a time
855  *  that is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
856  *  definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
857  *  also traced.
858  *
859  *  @param time The timestamp to be used to tag this change of value.
860  *  @param src The name of the source host for get route.
861  *  @param dst The name of the destination host for get route.
862  *  @param variable The name of the variable to be considered.
863  *  @param value The value to be subtracted from the variable.
864  *
865  *  @see TRACE_link_variable_declare, TRACE_link_srcdst_variable_set_with_time, TRACE_link_srcdst_variable_add_with_time
866  */
867 void TRACE_link_srcdst_variable_sub_with_time (double time, const char *src, const char *dst, const char *variable,
868                                                double value)
869 {
870   instr_user_srcdst_variable(time, src, dst, variable, "LINK", value, InstrUserVariable::SUB);
871 }
872
873 /** @ingroup TRACE_user_variables
874  *  @brief Get declared user link variables
875  *
876  * This function should be used to get link variables that were already declared with #TRACE_link_variable_declare or
877  * with #TRACE_link_variable_declare_with_color.
878  *
879  * @return A dynar with the declared link variables, must be freed with xbt_dynar_free.
880  */
881 xbt_dynar_t TRACE_get_link_variables ()
882 {
883   return instr_set_to_dynar(user_link_variables);
884 }
885
886 /** @ingroup TRACE_user_variables
887  *  @brief Declare a new user state associated to hosts.
888  *
889  *  Declare a user state that will be associated to hosts.
890  *  A user host state can be used to trace application states.
891  *
892  *  @param state The name of the new state to be declared.
893  *
894  *  @see TRACE_host_state_declare_value
895  */
896 void TRACE_host_state_declare (const char *state)
897 {
898   instr_new_user_state_type("HOST", state);
899 }
900
901 /** @ingroup TRACE_user_variables
902  *  @brief Declare a new value for a user state associated to hosts.
903  *
904  *  Declare a value for a state. The color needs to be a string with 3 numbers separated by spaces in the range [0,1].
905  *  A light-gray color can be specified using "0.7 0.7 0.7" as color.
906  *
907  *  @param state The name of the new state to be declared.
908  *  @param value The name of the value
909  *  @param color The color of the value
910  *
911  *  @see TRACE_host_state_declare
912  */
913 void TRACE_host_state_declare_value (const char *state, const char *value, const char *color)
914 {
915   instr_new_value_for_user_state_type (state, value, color);
916 }
917
918 /** @ingroup TRACE_user_variables
919  *  @brief Set the user state to the given value.
920  *
921  *  Change a user state previously declared to the given value.
922  *
923  *  @param host The name of the host to be considered.
924  *  @param state_name The name of the state previously declared.
925  *  @param value_name The new value of the state.
926  *
927  *  @see TRACE_host_state_declare, TRACE_host_push_state, TRACE_host_pop_state, TRACE_host_reset_state
928  */
929 void TRACE_host_set_state(const char* host, const char* state_name, const char* value_name)
930 {
931   simgrid::instr::StateType* state = simgrid::instr::Container::by_name(host)->get_state(state_name);
932   state->add_entity_value(value_name);
933   state->set_event(value_name);
934 }
935
936 /** @ingroup TRACE_user_variables
937  *  @brief Push a new value for a state of a given host.
938  *
939  *  Change a user state previously declared by pushing the new value to the state.
940  *
941  *  @param host The name of the host to be considered.
942  *  @param state_name The name of the state previously declared.
943  *  @param value_name The value to be pushed.
944  *
945  *  @see TRACE_host_state_declare, TRACE_host_set_state, TRACE_host_pop_state, TRACE_host_reset_state
946  */
947 void TRACE_host_push_state(const char* host, const char* state_name, const char* value_name)
948 {
949   simgrid::instr::Container::by_name(host)->get_state(state_name)->push_event(value_name);
950 }
951
952 /** @ingroup TRACE_user_variables
953  *  @brief Pop the last value of a state of a given host.
954  *
955  *  Change a user state previously declared by removing the last value of the state.
956  *
957  *  @param host The name of the host to be considered.
958  *  @param state_name The name of the state to be popped.
959  *
960  *  @see TRACE_host_state_declare, TRACE_host_set_state, TRACE_host_push_state, TRACE_host_reset_state
961  */
962 void TRACE_host_pop_state(const char* host, const char* state_name)
963 {
964   simgrid::instr::Container::by_name(host)->get_state(state_name)->pop_event();
965 }