Logo AND Algorithmique Numérique Distribuée

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