Logo AND Algorithmique Numérique Distribuée

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