Logo AND Algorithmique Numérique Distribuée

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