Logo AND Algorithmique Numérique Distribuée

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