Logo AND Algorithmique Numérique Distribuée

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