Logo AND Algorithmique Numérique Distribuée

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