Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move collective algorithms to separate folders
[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 (!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, 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 (!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     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 (!TRACE_is_enabled() || !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 (!TRACE_is_enabled() || !TRACE_needs_platform())
148     return;
149
150   if (!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 (!TRACE_is_enabled() || !TRACE_needs_platform())
182     return;
183
184   if (!mark_type)
185     THROWF (tracing_error, 1, "mark_type is nullptr");
186   if (!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 (!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 (!mark_color)
196     mark_color = white;
197
198   XBT_DEBUG("MARK,declare_value %s %s %s", mark_type, mark_value, mark_color);
199   PJ_value_new (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 (!TRACE_is_enabled() || !TRACE_needs_platform())
237     return;
238
239   if (!mark_type)
240     THROWF (tracing_error, 1, "mark_type is nullptr");
241   if (!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 (!type){
247     THROWF (tracing_error, 1, "mark_type with name (%s) is not declared", mark_type);
248   }
249
250   val_t value = PJ_value_get (mark_value, type);
251   XBT_DEBUG("MARK %s %s", mark_type, mark_value);
252   new_pajeNewEvent (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 (!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 (!TRACE_is_enabled() || !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 (!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_pajeSetVariable(time, container, type, value);
293         break;
294       case INSTR_US_ADD:
295         new_pajeAddVariable(time, container, type, value);
296         break;
297       case INSTR_US_SUB:
298         new_pajeSubVariable(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(!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(!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 (!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 /** \ingroup TRACE_user_variables
489  *  \brief Get declared user vm variables
490  *
491  * This function should be used to get VM variables that were already declared with #TRACE_vm_variable_declare or with
492  * #TRACE_vm_variable_declare_with_color.
493  *
494  * \return A dynar with the declared host variables, must be freed with xbt_dynar_free.
495  */
496 xbt_dynar_t TRACE_get_vm_variables ()
497 {
498   return instr_dict_to_dynar (user_vm_variables);
499 }
500
501 /* for host variables */
502 /** \ingroup TRACE_user_variables
503  *  \brief Declare a new user variable associated to hosts.
504  *
505  *  Declare a user variable that will be associated to hosts.
506  *  A user host variable can be used to trace user variables such as the number of tasks in a server, the number of
507  *  clients in an application (for hosts), and so on. The color associated to this new variable will be random.
508  *
509  *  \param variable The name of the new variable to be declared.
510  *
511  *  \see TRACE_host_variable_declare_with_color
512  */
513 void TRACE_host_variable_declare (const char *variable)
514 {
515   instr_user_variable(0, nullptr, variable, "HOST", 0, INSTR_US_DECLARE, nullptr, user_host_variables);
516 }
517
518 /** \ingroup TRACE_user_variables
519  *  \brief Declare a new user variable associated to hosts with a color.
520  *
521  *  Same as #TRACE_host_variable_declare, but associated a color to the newly created user host variable. The color
522  *  needs to be a string with three numbers separated by spaces in the range [0,1].
523  *  A light-gray color can be specified using "0.7 0.7 0.7" as color.
524  *
525  *  \param variable The name of the new variable to be declared.
526  *  \param color The color for the new variable.
527  */
528 void TRACE_host_variable_declare_with_color (const char *variable, const char *color)
529 {
530   instr_user_variable(0, nullptr, variable, "HOST", 0, INSTR_US_DECLARE, color, user_host_variables);
531 }
532
533 /** \ingroup TRACE_user_variables
534  *  \brief Set the value of a variable of a host.
535  *
536  *  \param host The name of the host to be considered.
537  *  \param variable The name of the variable to be considered.
538  *  \param value The new value of the variable.
539  *
540  *  \see TRACE_host_variable_declare, TRACE_host_variable_add, TRACE_host_variable_sub
541  */
542 void TRACE_host_variable_set (const char *host, const char *variable, double value)
543 {
544   TRACE_host_variable_set_with_time (MSG_get_clock(), host, variable, value);
545 }
546
547 /** \ingroup TRACE_user_variables
548  *  \brief Add a value to a variable of a host.
549  *
550  *  \param host The name of the host to be considered.
551  *  \param variable The name of the variable to be considered.
552  *  \param value The value to be added to the variable.
553  *
554  *  \see TRACE_host_variable_declare, TRACE_host_variable_set, TRACE_host_variable_sub
555  */
556 void TRACE_host_variable_add (const char *host, const char *variable, double value)
557 {
558   TRACE_host_variable_add_with_time (MSG_get_clock(), host, variable, value);
559 }
560
561 /** \ingroup TRACE_user_variables
562  *  \brief Subtract a value from a variable of a host.
563  *
564  *  \param host The name of the host to be considered.
565  *  \param variable The name of the variable to be considered.
566  *  \param value The value to be subtracted from the variable.
567  *
568  *  \see TRACE_host_variable_declare, TRACE_host_variable_set, TRACE_host_variable_add
569  */
570 void TRACE_host_variable_sub (const char *host, const char *variable, double value)
571 {
572   TRACE_host_variable_sub_with_time (MSG_get_clock(), host, variable, value);
573 }
574
575 /** \ingroup TRACE_user_variables
576  *  \brief Set the value of a variable of a host at a given timestamp.
577  *
578  *  Same as #TRACE_host_variable_set, but let user specify  the time used to trace it. Users can specify a time that
579  *  is not the simulated clock time as defined by the core  simulator. This allows a fine-grain control of time
580  *  definition, but should be used with caution since the trace  can be inconsistent if resource utilization traces are
581  *  also traced.
582  *
583  *  \param time The timestamp to be used to tag this change of value.
584  *  \param host The name of the host to be considered.
585  *  \param variable The name of the variable to be considered.
586  *  \param value The new value of the variable.
587  *
588  *  \see TRACE_host_variable_declare, TRACE_host_variable_add_with_time, TRACE_host_variable_sub_with_time
589  */
590 void TRACE_host_variable_set_with_time (double time, const char *host, const char *variable, double value)
591 {
592   instr_user_variable(time, host, variable, "HOST", value, INSTR_US_SET, nullptr, user_host_variables);
593 }
594
595 /** \ingroup TRACE_user_variables
596  *  \brief Add a value to a variable of a host at a given timestamp.
597  *
598  *  Same as #TRACE_host_variable_add, but let user specify the time used to trace it. Users can specify a time that
599  *  is not the simulated clock time as defined by the core  simulator. This allows a fine-grain control of time
600  *  definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
601  *  also traced.
602  *
603  *  \param time The timestamp to be used to tag this change of value.
604  *  \param host The name of the host to be considered.
605  *  \param variable The name of the variable to be considered.
606  *  \param value The value to be added to the variable.
607  *
608  *  \see TRACE_host_variable_declare, TRACE_host_variable_set_with_time, TRACE_host_variable_sub_with_time
609  */
610 void TRACE_host_variable_add_with_time (double time, const char *host, const char *variable, double value)
611 {
612   instr_user_variable(time, host, variable, "HOST", value, INSTR_US_ADD, nullptr, user_host_variables);
613 }
614
615 /** \ingroup TRACE_user_variables
616  *  \brief Subtract a value from a variable of a host at a given timestamp.
617  *
618  *  Same as #TRACE_host_variable_sub, but let user specify the time used to trace it. Users can specify a time that
619  *  is not the simulated clock time as defined by the core  simulator. This allows a fine-grain control of time
620  *  definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
621  *  also traced.
622  *
623  *  \param time The timestamp to be used to tag this change of value.
624  *  \param host The name of the host to be considered.
625  *  \param variable The name of the variable to be considered.
626  *  \param value The value to be subtracted from the variable.
627  *
628  *  \see TRACE_host_variable_declare, TRACE_host_variable_set_with_time, TRACE_host_variable_add_with_time
629  */
630 void TRACE_host_variable_sub_with_time (double time, const char *host, const char *variable, double value)
631 {
632   instr_user_variable(time, host, variable, "HOST", value, INSTR_US_SUB, nullptr, user_host_variables);
633 }
634
635 /** \ingroup TRACE_user_variables
636  *  \brief Get declared user host variables
637  *
638  * This function should be used to get host variables that were already declared with #TRACE_host_variable_declare or
639  * with #TRACE_host_variable_declare_with_color.
640  *
641  * \return A dynar with the declared host variables, must be freed with xbt_dynar_free.
642  */
643 xbt_dynar_t TRACE_get_host_variables ()
644 {
645   return instr_dict_to_dynar (user_host_variables);
646 }
647
648 /* for link variables */
649 /** \ingroup TRACE_user_variables
650  *  \brief Declare a new user variable associated to links.
651  *
652  *  Declare a user variable that will be associated to links.
653  *  A user link variable can be used, for example, to trace user variables such as the number of messages being
654  *  transferred through network links. The color associated to this new variable will be random.
655  *
656  *  \param variable The name of the new variable to be declared.
657  *
658  *  \see TRACE_link_variable_declare_with_color
659  */
660 void TRACE_link_variable_declare (const char *variable)
661 {
662   instr_user_variable (0, nullptr, variable, "LINK", 0, INSTR_US_DECLARE, nullptr, user_link_variables);
663 }
664
665 /** \ingroup TRACE_user_variables
666  *  \brief Declare a new user variable associated to links with a color.
667  *
668  *  Same as #TRACE_link_variable_declare, but associated a color to the newly created user link variable. The color
669  *  needs to be a string with three numbers separated by spaces in the range [0,1].
670  *  A light-gray color can be specified using "0.7 0.7 0.7" as color.
671  *
672  *  \param variable The name of the new variable to be declared.
673  *  \param color The color for the new variable.
674  */
675 void TRACE_link_variable_declare_with_color (const char *variable, const char *color)
676 {
677   instr_user_variable (0, nullptr, variable, "LINK", 0, INSTR_US_DECLARE, color, user_link_variables);
678 }
679
680 /** \ingroup TRACE_user_variables
681  *  \brief Set the value of a variable of a link.
682  *
683  *  \param link The name of the link to be considered.
684  *  \param variable The name of the variable to be considered.
685  *  \param value The new value of the variable.
686  *
687  *  \see TRACE_link_variable_declare, TRACE_link_variable_add, TRACE_link_variable_sub
688  */
689 void TRACE_link_variable_set (const char *link, const char *variable, double value)
690 {
691   TRACE_link_variable_set_with_time (MSG_get_clock(), link, variable, value);
692 }
693
694 /** \ingroup TRACE_user_variables
695  *  \brief Add a value to a variable of a link.
696  *
697  *  \param link The name of the link to be considered.
698  *  \param variable The name of the variable to be considered.
699  *  \param value The value to be added to the variable.
700  *
701  *  \see TRACE_link_variable_declare, TRACE_link_variable_set, TRACE_link_variable_sub
702  */
703 void TRACE_link_variable_add (const char *link, const char *variable, double value)
704 {
705   TRACE_link_variable_add_with_time (MSG_get_clock(), link, variable, value);
706 }
707
708 /** \ingroup TRACE_user_variables
709  *  \brief Subtract a value from a variable of a link.
710  *
711  *  \param link The name of the link to be considered.
712  *  \param variable The name of the variable to be considered.
713  *  \param value The value to be subtracted from the variable.
714  *
715  *  \see TRACE_link_variable_declare, TRACE_link_variable_set, TRACE_link_variable_add
716  */
717 void TRACE_link_variable_sub (const char *link, const char *variable, double value)
718 {
719   TRACE_link_variable_sub_with_time (MSG_get_clock(), link, variable, value);
720 }
721
722 /** \ingroup TRACE_user_variables
723  *  \brief Set the value of a variable of a link at a given timestamp.
724  *
725  *  Same as #TRACE_link_variable_set, but let user specify the time used to trace it. Users can specify a time that
726  *  is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
727  *  definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
728  *  also traced.
729  *
730  *  \param time The timestamp to be used to tag this change of value.
731  *  \param link The name of the link to be considered.
732  *  \param variable The name of the variable to be considered.
733  *  \param value The new value of the variable.
734  *
735  *  \see TRACE_link_variable_declare, TRACE_link_variable_add_with_time, TRACE_link_variable_sub_with_time
736  */
737 void TRACE_link_variable_set_with_time (double time, const char *link, const char *variable, double value)
738 {
739   instr_user_variable (time, link, variable, "LINK", value, INSTR_US_SET, nullptr, user_link_variables);
740 }
741
742 /** \ingroup TRACE_user_variables
743  *  \brief Add a value to a variable of a link at a given timestamp.
744  *
745  *  Same as #TRACE_link_variable_add, but let user specify the time used to trace it. Users can specify a time that
746  *  is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
747  *  definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
748  *  also traced.
749  *
750  *  \param time The timestamp to be used to tag this change of value.
751  *  \param link The name of the link to be considered.
752  *  \param variable The name of the variable to be considered.
753  *  \param value The value to be added to the variable.
754  *
755  *  \see TRACE_link_variable_declare, TRACE_link_variable_set_with_time, TRACE_link_variable_sub_with_time
756  */
757 void TRACE_link_variable_add_with_time (double time, const char *link, const char *variable, double value)
758 {
759   instr_user_variable (time, link, variable, "LINK", value, INSTR_US_ADD, nullptr, user_link_variables);
760 }
761
762 /** \ingroup TRACE_user_variables
763  *  \brief Subtract a value from a variable of a link at a given timestamp.
764  *
765  *  Same as #TRACE_link_variable_sub, but let user specify the time used to trace it. Users can specify a time that
766  *  is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
767  *  definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
768  *  also traced.
769  *
770  *  \param time The timestamp to be used to tag this change of value.
771  *  \param link The name of the link to be considered.
772  *  \param variable The name of the variable to be considered.
773  *  \param value The value to be subtracted from the variable.
774  *
775  *  \see TRACE_link_variable_declare, TRACE_link_variable_set_with_time, TRACE_link_variable_add_with_time
776  */
777 void TRACE_link_variable_sub_with_time (double time, const char *link, const char *variable, double value)
778 {
779   instr_user_variable (time, link, variable, "LINK", value, INSTR_US_SUB, nullptr, user_link_variables);
780 }
781
782 /* for link variables, but with src and dst used for get_route */
783 /** \ingroup TRACE_user_variables
784  *  \brief Set the value of the variable present in the links connecting source and destination.
785  *
786  *  Same as #TRACE_link_variable_set, but instead of providing the name of link to be considered, provide the source
787  *  and destination hosts. All links that are part of the route between source and destination will have the variable
788  *  set to the provided value.
789  *
790  *  \param src The name of the source host for get route.
791  *  \param dst The name of the destination host for get route.
792  *  \param variable The name of the variable to be considered.
793  *  \param value The new value of the variable.
794  *
795  *  \see TRACE_link_variable_declare, TRACE_link_srcdst_variable_add, TRACE_link_srcdst_variable_sub
796  */
797 void TRACE_link_srcdst_variable_set (const char *src, const char *dst, const char *variable, double value)
798 {
799   TRACE_link_srcdst_variable_set_with_time (MSG_get_clock(), src, dst, variable, value);
800 }
801
802 /** \ingroup TRACE_user_variables
803  *  \brief Add a value to the variable present in the links connecting source and destination.
804  *
805  *  Same as #TRACE_link_variable_add, but instead of providing the name of link to be considered, provide the source
806  *  and destination hosts. All links that are part of the route between source and destination will have the value
807  *  passed as parameter added to the current value of the variable name to be considered.
808  *
809  *  \param src The name of the source host for get route.
810  *  \param dst The name of the destination host for get route.
811  *  \param variable The name of the variable to be considered.
812  *  \param value The value to be added to the variable.
813  *
814  *  \see TRACE_link_variable_declare, TRACE_link_srcdst_variable_set, TRACE_link_srcdst_variable_sub
815  */
816 void TRACE_link_srcdst_variable_add (const char *src, const char *dst, const char *variable, double value)
817 {
818   TRACE_link_srcdst_variable_add_with_time (MSG_get_clock(), src, dst, variable, value);
819 }
820
821 /** \ingroup TRACE_user_variables
822  *  \brief Subtract a value from the variable present in the links connecting source and destination.
823  *
824  *  Same as #TRACE_link_variable_sub, but instead of providing the name of link to be considered, provide the source
825  *  and destination hosts. All links that are part of the route between source and destination will have the value
826  *  passed as parameter subtracted from the current value of the variable name to be considered.
827  *
828  *  \param src The name of the source host for get route.
829  *  \param dst The name of the destination host for get route.
830  *  \param variable The name of the variable to be considered.
831  *  \param value The value to be subtracted from the variable.
832  *
833  *  \see TRACE_link_variable_declare, TRACE_link_srcdst_variable_set, TRACE_link_srcdst_variable_add
834  */
835 void TRACE_link_srcdst_variable_sub (const char *src, const char *dst, const char *variable, double value)
836 {
837   TRACE_link_srcdst_variable_sub_with_time (MSG_get_clock(), src, dst, variable, value);
838 }
839
840 /** \ingroup TRACE_user_variables
841  *  \brief Set the value of the variable present in the links connecting source and destination at a given timestamp.
842  *
843  *  Same as #TRACE_link_srcdst_variable_set, but let user specify the time used to trace it. Users can specify a time
844  *  that is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
845  *  definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
846  *  also traced.
847  *
848  *  \param time The timestamp to be used to tag this change of value.
849  *  \param src The name of the source host for get route.
850  *  \param dst The name of the destination host for get route.
851  *  \param variable The name of the variable to be considered.
852  *  \param value The new value of the variable.
853  *
854  *  \see TRACE_link_variable_declare, TRACE_link_srcdst_variable_add_with_time, TRACE_link_srcdst_variable_sub_with_time
855  */
856 void TRACE_link_srcdst_variable_set_with_time (double time, const char *src, const char *dst, const char *variable,
857                                                double value)
858 {
859   instr_user_srcdst_variable (time, src, dst, variable, "LINK", value, INSTR_US_SET);
860 }
861
862 /** \ingroup TRACE_user_variables
863  *  \brief Add a value to the variable present in the links connecting source and destination at a given timestamp.
864  *
865  *  Same as #TRACE_link_srcdst_variable_add, but let user specify the time used to trace it. Users can specify a time
866  *  that is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
867  *  definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
868  *  also traced.
869  *
870  *  \param time The timestamp to be used to tag this change of value.
871  *  \param src The name of the source host for get route.
872  *  \param dst The name of the destination host for get route.
873  *  \param variable The name of the variable to be considered.
874  *  \param value The value to be added to the variable.
875  *
876  *  \see TRACE_link_variable_declare, TRACE_link_srcdst_variable_set_with_time, TRACE_link_srcdst_variable_sub_with_time
877  */
878 void TRACE_link_srcdst_variable_add_with_time (double time, const char *src, const char *dst, const char *variable,
879                                                double value)
880 {
881   instr_user_srcdst_variable (time, src, dst, variable, "LINK", value, INSTR_US_ADD);
882 }
883
884 /** \ingroup TRACE_user_variables
885  *  \brief Subtract a value from the variable present in the links connecting source and dest. at a given timestamp.
886  *
887  *  Same as #TRACE_link_srcdst_variable_sub, but let user specify the time used to trace it. Users can specify a time
888  *  that is not the simulated clock time as defined by the core simulator. This allows a fine-grain control of time
889  *  definition, but should be used with caution since the trace can be inconsistent if resource utilization traces are
890  *  also traced.
891  *
892  *  \param time The timestamp to be used to tag this change of value.
893  *  \param src The name of the source host for get route.
894  *  \param dst The name of the destination host for get route.
895  *  \param variable The name of the variable to be considered.
896  *  \param value The value to be subtracted from the variable.
897  *
898  *  \see TRACE_link_variable_declare, TRACE_link_srcdst_variable_set_with_time, TRACE_link_srcdst_variable_add_with_time
899  */
900 void TRACE_link_srcdst_variable_sub_with_time (double time, const char *src, const char *dst, const char *variable,
901                                                double value)
902 {
903   instr_user_srcdst_variable (time, src, dst, variable, "LINK", value, INSTR_US_SUB);
904 }
905
906 /** \ingroup TRACE_user_variables
907  *  \brief Get declared user link variables
908  *
909  * This function should be used to get link variables that were already declared with #TRACE_link_variable_declare or
910  * with #TRACE_link_variable_declare_with_color.
911  *
912  * \return A dynar with the declared link variables, must be freed with xbt_dynar_free.
913  */
914 xbt_dynar_t TRACE_get_link_variables ()
915 {
916   return instr_dict_to_dynar (user_link_variables);
917 }
918
919 /** \ingroup TRACE_user_variables
920  *  \brief Declare a new user state associated to hosts.
921  *
922  *  Declare a user state that will be associated to hosts.
923  *  A user host state can be used to trace application states.
924  *
925  *  \param state The name of the new state to be declared.
926  *
927  *  \see TRACE_host_state_declare_value
928  */
929 void TRACE_host_state_declare (const char *state)
930 {
931   instr_new_user_state_type("HOST", state);
932 }
933
934 /** \ingroup TRACE_user_variables
935  *  \brief Declare a new value for a user state associated to hosts.
936  *
937  *  Declare a value for a state. The color needs to be a string with 3 numbers separated by spaces in the range [0,1].
938  *  A light-gray color can be specified using "0.7 0.7 0.7" as color.
939  *
940  *  \param state The name of the new state to be declared.
941  *  \param value The name of the value
942  *  \param color The color of the value
943  *
944  *  \see TRACE_host_state_declare
945  */
946 void TRACE_host_state_declare_value (const char *state, const char *value, const char *color)
947 {
948   instr_new_value_for_user_state_type (state, value, color);
949 }
950
951 /** \ingroup TRACE_user_variables
952  *  \brief Set the user state to the given value.
953  *
954  *  Change a user state previously declared to the given value.
955  *
956  *  \param host The name of the host to be considered.
957  *  \param state The name of the state previously declared.
958  *  \param value The new value of the state.
959  *
960  *  \see TRACE_host_state_declare, TRACE_host_push_state, TRACE_host_pop_state, TRACE_host_reset_state
961  */
962 void TRACE_host_set_state (const char *host, const char *state, const char *value)
963 {
964   container_t container = PJ_container_get(host);
965   type_t type = PJ_type_get (state, container->type);
966   val_t val = PJ_value_get_or_new (value, nullptr, type); /* if user didn't declare a value with a color, use nullptr color */
967   new_pajeSetState(MSG_get_clock(), container, type, val);
968 }
969
970 /** \ingroup TRACE_user_variables
971  *  \brief Push a new value for a state of a given host.
972  *
973  *  Change a user state previously declared by pushing the new value to the state.
974  *
975  *  \param host The name of the host to be considered.
976  *  \param state The name of the state previously declared.
977  *  \param value The value to be pushed.
978  *
979  *  \see TRACE_host_state_declare, TRACE_host_set_state, TRACE_host_pop_state, TRACE_host_reset_state
980  */
981 void TRACE_host_push_state (const char *host, const char *state, const char *value)
982 {
983   container_t container = PJ_container_get(host);
984   type_t type = PJ_type_get (state, container->type);
985   val_t val = PJ_value_get_or_new (value, nullptr, type); /* if user didn't declare a value with a color, use nullptr color */
986   new_pajePushState(MSG_get_clock(), container, type, val);
987 }
988
989 /** \ingroup TRACE_user_variables
990  *  \brief Pop the last value of a state of a given host.
991  *
992  *  Change a user state previously declared by removing the last value of the state.
993  *
994  *  \param host The name of the host to be considered.
995  *  \param state The name of the state to be popped.
996  *
997  *  \see TRACE_host_state_declare, TRACE_host_set_state, TRACE_host_push_state, TRACE_host_reset_state
998  */
999 void TRACE_host_pop_state (const char *host, const char *state)
1000 {
1001   container_t container = PJ_container_get(host);
1002   type_t type = PJ_type_get (state, container->type);
1003   new_pajePopState(MSG_get_clock(), container, type);
1004 }
1005
1006 /** \ingroup TRACE_user_variables
1007  *  \brief Reset the state of a given host.
1008  *
1009  *  Clear all previous values of a user state.
1010  *
1011  *  \param host The name of the host to be considered.
1012  *  \param state The name of the state to be cleared.
1013  *
1014  *  \see TRACE_host_state_declare, TRACE_host_set_state, TRACE_host_push_state, TRACE_host_pop_state
1015  */
1016 void TRACE_host_reset_state (const char *host, const char *state)
1017 {
1018   container_t container = PJ_container_get(host);
1019   type_t type = PJ_type_get (state, container->type);
1020   new_pajeResetState(MSG_get_clock(), container, type);
1021 }
1022
1023 /** \ingroup TRACE_API
1024  *  \brief Get Paje container types that can be mapped to the nodes of a graph.
1025  *
1026  *  This function can be used to create a user made  graph configuration file for Triva. Normally, it is used with the
1027  *  functions defined in \ref TRACE_user_variables.
1028  *
1029  *  \return A dynar with the types, must be freed with xbt_dynar_free.
1030  */
1031 xbt_dynar_t TRACE_get_node_types ()
1032 {
1033   return instr_dict_to_dynar (trivaNodeTypes);
1034 }
1035
1036 /** \ingroup TRACE_API
1037  *  \brief Get Paje container types that can be mapped to the edges of a graph.
1038  *
1039  *  This function can be used to create a user made graph configuration file for Triva. Normally, it is used with the
1040  *  functions defined in \ref TRACE_user_variables.
1041  *
1042  *  \return A dynar with the types, must be freed with xbt_dynar_free.
1043  */
1044 xbt_dynar_t TRACE_get_edge_types ()
1045 {
1046   return instr_dict_to_dynar (trivaEdgeTypes);
1047 }
1048
1049 /** \ingroup TRACE_API
1050  *  \brief Pauses all tracing activities.
1051  *  \see TRACE_resume
1052  */
1053 void TRACE_pause ()
1054 {
1055   instr_pause_tracing();
1056 }
1057
1058 /** \ingroup TRACE_API
1059  *  \brief Resumes all tracing activities.
1060  *  \see TRACE_pause
1061  */
1062 void TRACE_resume ()
1063 {
1064   instr_resume_tracing();
1065 }