Logo AND Algorithmique Numérique Distribuée

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