Logo AND Algorithmique Numérique Distribuée

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