Logo AND Algorithmique Numérique Distribuée

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