Logo AND Algorithmique Numérique Distribuée

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