Logo AND Algorithmique Numérique Distribuée

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