Logo AND Algorithmique Numérique Distribuée

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