Logo AND Algorithmique Numérique Distribuée

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