Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/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   sg_netcard_t src_elm = sg_netcard_by_name_or_null(src);
368   if(!src_elm) xbt_die("Element '%s' not found!",src);
369
370   sg_netcard_t dst_elm = sg_netcard_by_name_or_null(dst);
371   if(!dst_elm) xbt_die("Element '%s' not found!",dst);
372
373   std::vector<Link*> *route = new std::vector<Link*>();
374   routing_platf->getRouteAndLatency (src_elm, dst_elm, route,NULL);
375   for (auto link : *route)
376     instr_user_variable (time, link->getName(), variable, father_type, value, what, NULL, user_link_variables);
377   delete route;
378 }
379
380 /** \ingroup TRACE_API
381  *  \brief Creates a file with the topology of the platform file used for the simulator.
382  *
383  *  The graph topology will have the following properties: all hosts, links and routers
384  *  of the platform file are mapped to graph nodes; routes are mapped to edges.
385  *  The platform's AS are not represented in the output.
386  *
387  *  \param filename The name of the file that will hold the graph.
388  *
389  *  \return 1 of successful, 0 otherwise.
390  */
391 int TRACE_platform_graph_export_graphviz (const char *filename)
392 {
393   /* returns 1 if successful, 0 otherwise */
394   if (!TRACE_is_enabled()) return 0;
395   xbt_graph_t g = instr_routing_platform_graph();
396   if (g == NULL) return 0;
397   instr_routing_platform_graph_export_graphviz (g, filename);
398   xbt_graph_free_graph(g, xbt_free_f, xbt_free_f, NULL);
399   return 1;
400 }
401
402 /*
403  * Derived functions that use instr_user_variable and TRACE_user_srcdst_variable.
404  * They were previously defined as pre-processors directives, but were transformed
405  * into functions so the user can track them using gdb.
406  */
407
408 /* for VM variables */
409 /** \ingroup TRACE_user_variables
410  *  \brief Declare a new user variable associated to VMs.
411  *
412  *  Declare a user variable that will be associated to VMs.
413  *  A user vm variable can be used to trace user variables
414  *  such as the number of tasks in a VM, the number of
415  *  clients in an application (for VMs), and so on. The color
416  *  associated to this new variable will be random.
417  *
418  *  \param variable The name of the new variable to be declared.
419  *
420  *  \see TRACE_vm_variable_declare_with_color
421  */
422 void TRACE_vm_variable_declare (const char *variable)
423 {
424   instr_user_variable(0, NULL, variable, "MSG_VM", 0, INSTR_US_DECLARE, NULL, user_vm_variables);
425 }
426
427 /** \ingroup TRACE_user_variables
428  *  \brief Declare a new user variable associated to VMs with a color.
429  *
430  *  Same as #TRACE_vm_variable_declare, but associated a color
431  *  to the newly created user host variable. The color needs to be
432  *  a string with three numbers separated by spaces in the range [0,1].
433  *  A light-gray color can be specified using "0.7 0.7 0.7" as color.
434  *
435  *  \param variable The name of the new variable to be declared.
436  *  \param color The color for the new variable.
437  *
438  */
439 void TRACE_vm_variable_declare_with_color (const char *variable, const char *color)
440 {
441    instr_user_variable(0, NULL, variable, "MSG_VM", 0, INSTR_US_DECLARE, color, user_vm_variables);
442 }
443
444 /** \ingroup TRACE_user_variables
445  *  \brief Set the value of a variable of a host.
446  *
447  *  \param vm The name of the VM to be considered.
448  *  \param variable The name of the variable to be considered.
449  *  \param value The new value of the variable.
450  *
451  *  \see TRACE_vm_variable_declare, TRACE_vm_variable_add, TRACE_vm_variable_sub
452  */
453 void TRACE_vm_variable_set (const char *vm, const char *variable, double value)
454 {
455
456   TRACE_vm_variable_set_with_time (MSG_get_clock(), vm, variable, value);
457 }
458
459 /** \ingroup TRACE_user_variables
460  *  \brief Add a value to a variable of a VM.
461  *
462  *  \param vm The name of the VM to be considered.
463  *  \param variable The name of the variable to be considered.
464  *  \param value The value to be added to the variable.
465  *
466  *  \see TRACE_vm_variable_declare, TRACE_vm_variable_set, TRACE_vm_variable_sub
467  */
468 void TRACE_vm_variable_add (const char *vm, const char *variable, double value)
469 {
470   TRACE_vm_variable_add_with_time (MSG_get_clock(), vm, variable, value);
471 }
472
473 /** \ingroup TRACE_user_variables
474  *  \brief Subtract a value from a variable of a VM.
475  *
476  *  \param vm The name of the vm to be considered.
477  *  \param variable The name of the variable to be considered.
478  *  \param value The value to be subtracted from the variable.
479  *
480  *  \see TRACE_vm_variable_declare, TRACE_vm_variable_set, TRACE_vm_variable_add
481  */
482 void TRACE_vm_variable_sub (const char *vm, const char *variable, double value)
483 {
484   TRACE_vm_variable_sub_with_time (MSG_get_clock(), vm, variable, value);
485 }
486
487 /** \ingroup TRACE_user_variables
488  *  \brief Set the value of a variable of a VM at a given timestamp.
489  *
490  *  Same as #TRACE_vm_variable_set, but let user specify
491  *  the time used to trace it. Users can specify a time that
492  *  is not the simulated clock time as defined by the core
493  *  simulator. This allows a fine-grain control of time
494  *  definition, but should be used with caution since the trace
495  *  can be inconsistent if resource utilization traces are also traced.
496  *
497  *  \param time The timestamp to be used to tag this change of value.
498  *  \param vm The name of the VM to be considered.
499  *  \param variable The name of the variable to be considered.
500  *  \param value The new value of the variable.
501  *
502  *  \see TRACE_vm_variable_declare, TRACE_vm_variable_add_with_time, TRACE_vm_variable_sub_with_time
503  */
504 void TRACE_vm_variable_set_with_time (double time, const char *vm, const char *variable, double value)
505 {
506   instr_user_variable(time, vm, variable, "MSG_VM", value, INSTR_US_SET, NULL, user_vm_variables);
507 }
508
509 /** \ingroup TRACE_user_variables
510  *  \brief Add a value to a variable of a VM at a given timestamp.
511  *
512  *  Same as #TRACE_vm_variable_add, but let user specify
513  *  the time used to trace it. Users can specify a time that
514  *  is not the simulated clock time as defined by the core
515  *  simulator. This allows a fine-grain control of time
516  *  definition, but should be used with caution since the trace
517  *  can be inconsistent if resource utilization traces are also traced.
518  *
519  *  \param time The timestamp to be used to tag this change of value.
520  *  \param vm The name of the VM to be considered.
521  *  \param variable The name of the variable to be considered.
522  *  \param value The value to be added to the variable.
523  *
524  *  \see TRACE_vm_variable_declare, TRACE_vm_variable_set_with_time, TRACE_vm_variable_sub_with_time
525  */
526 void TRACE_vm_variable_add_with_time (double time, const char *vm, const char *variable, double value)
527 {
528   instr_user_variable(time, vm, variable, "MSG_VM", value, INSTR_US_ADD, NULL, user_vm_variables);
529 }
530
531 /** \ingroup TRACE_user_variables
532  *  \brief Subtract a value from a variable of a VM at a given timestamp.
533  *
534  *  Same as #TRACE_vm_variable_sub, but let user specify
535  *  the time used to trace it. Users can specify a time that
536  *  is not the simulated clock time as defined by the core
537  *  simulator. This allows a fine-grain control of time
538  *  definition, but should be used with caution since the trace
539  *  can be inconsistent if resource utilization traces are also traced.
540  *
541  *  \param time The timestamp to be used to tag this change of value.
542  *  \param vm The name of the VM to be considered.
543  *  \param variable The name of the variable to be considered.
544  *  \param value The value to be subtracted from the variable.
545  *
546  *  \see TRACE_vm_variable_declare, TRACE_vm_variable_set_with_time, TRACE_vm_variable_add_with_time
547  */
548 void TRACE_vm_variable_sub_with_time (double time, const char *vm, const char *variable, double value)
549 {
550   instr_user_variable(time, vm, variable, "MSG_VM", value, INSTR_US_SUB, NULL, user_vm_variables);
551 }
552
553 /** \ingroup TRACE_user_variables
554  *  \brief Get declared user vm variables
555  *
556  * This function should be used to get VM variables that were already
557  * declared with #TRACE_vm_variable_declare or with #TRACE_vm_variable_declare_with_color.
558  *
559  * \return A dynar with the declared host variables, must be freed with xbt_dynar_free.
560  */
561 xbt_dynar_t TRACE_get_vm_variables (void)
562 {
563   return instr_dict_to_dynar (user_vm_variables);
564 }
565
566
567
568 /* for host variables */
569 /** \ingroup TRACE_user_variables
570  *  \brief Declare a new user variable associated to hosts.
571  *
572  *  Declare a user variable that will be associated to hosts.
573  *  A user host variable can be used to trace user variables
574  *  such as the number of tasks in a server, the number of
575  *  clients in an application (for hosts), and so on. The color
576  *  associated to this new variable will be random.
577  *
578  *  \param variable The name of the new variable to be declared.
579  *
580  *  \see TRACE_host_variable_declare_with_color
581  */
582 void TRACE_host_variable_declare (const char *variable)
583 {
584   instr_user_variable(0, NULL, variable, "HOST", 0, INSTR_US_DECLARE, NULL, user_host_variables);
585 }
586
587 /** \ingroup TRACE_user_variables
588  *  \brief Declare a new user variable associated to hosts with a color.
589  *
590  *  Same as #TRACE_host_variable_declare, but associated a color
591  *  to the newly created user host variable. The color needs to be
592  *  a string with three numbers separated by spaces in the range [0,1].
593  *  A light-gray color can be specified using "0.7 0.7 0.7" as color.
594  *
595  *  \param variable The name of the new variable to be declared.
596  *  \param color The color for the new variable.
597  *
598  */
599 void TRACE_host_variable_declare_with_color (const char *variable, const char *color)
600 {
601   instr_user_variable(0, NULL, variable, "HOST", 0, INSTR_US_DECLARE, color, user_host_variables);
602 }
603
604 /** \ingroup TRACE_user_variables
605  *  \brief Set the value of a variable of a host.
606  *
607  *  \param host The name of the host to be considered.
608  *  \param variable The name of the variable to be considered.
609  *  \param value The new value of the variable.
610  *
611  *  \see TRACE_host_variable_declare, TRACE_host_variable_add, TRACE_host_variable_sub
612  */
613 void TRACE_host_variable_set (const char *host, const char *variable, double value)
614 {
615   TRACE_host_variable_set_with_time (MSG_get_clock(), host, variable, value);
616 }
617
618 /** \ingroup TRACE_user_variables
619  *  \brief Add a value to a variable of a host.
620  *
621  *  \param host The name of the host to be considered.
622  *  \param variable The name of the variable to be considered.
623  *  \param value The value to be added to the variable.
624  *
625  *  \see TRACE_host_variable_declare, TRACE_host_variable_set, TRACE_host_variable_sub
626  */
627 void TRACE_host_variable_add (const char *host, const char *variable, double value)
628 {
629   TRACE_host_variable_add_with_time (MSG_get_clock(), host, variable, value);
630 }
631
632 /** \ingroup TRACE_user_variables
633  *  \brief Subtract a value from a variable of a host.
634  *
635  *  \param host The name of the host to be considered.
636  *  \param variable The name of the variable to be considered.
637  *  \param value The value to be subtracted from the variable.
638  *
639  *  \see TRACE_host_variable_declare, TRACE_host_variable_set, TRACE_host_variable_add
640  */
641 void TRACE_host_variable_sub (const char *host, const char *variable, double value)
642 {
643   TRACE_host_variable_sub_with_time (MSG_get_clock(), host, variable, value);
644 }
645
646 /** \ingroup TRACE_user_variables
647  *  \brief Set the value of a variable of a host at a given timestamp.
648  *
649  *  Same as #TRACE_host_variable_set, but let user specify
650  *  the time used to trace it. Users can specify a time that
651  *  is not the simulated clock time as defined by the core
652  *  simulator. This allows a fine-grain control of time
653  *  definition, but should be used with caution since the trace
654  *  can be inconsistent if resource utilization traces are also traced.
655  *
656  *  \param time The timestamp to be used to tag this change of value.
657  *  \param host The name of the host to be considered.
658  *  \param variable The name of the variable to be considered.
659  *  \param value The new value of the variable.
660  *
661  *  \see TRACE_host_variable_declare, TRACE_host_variable_add_with_time, TRACE_host_variable_sub_with_time
662  */
663 void TRACE_host_variable_set_with_time (double time, const char *host, const char *variable, double value)
664 {
665   instr_user_variable(time, host, variable, "HOST", value, INSTR_US_SET, NULL, user_host_variables);
666 }
667
668 /** \ingroup TRACE_user_variables
669  *  \brief Add a value to a variable of a host at a given timestamp.
670  *
671  *  Same as #TRACE_host_variable_add, but let user specify
672  *  the time used to trace it. Users can specify a time that
673  *  is not the simulated clock time as defined by the core
674  *  simulator. This allows a fine-grain control of time
675  *  definition, but should be used with caution since the trace
676  *  can be inconsistent if resource utilization traces are also traced.
677  *
678  *  \param time The timestamp to be used to tag this change of value.
679  *  \param host The name of the host to be considered.
680  *  \param variable The name of the variable to be considered.
681  *  \param value The value to be added to the variable.
682  *
683  *  \see TRACE_host_variable_declare, TRACE_host_variable_set_with_time, TRACE_host_variable_sub_with_time
684  */
685 void TRACE_host_variable_add_with_time (double time, const char *host, const char *variable, double value)
686 {
687   instr_user_variable(time, host, variable, "HOST", value, INSTR_US_ADD, NULL, user_host_variables);
688 }
689
690 /** \ingroup TRACE_user_variables
691  *  \brief Subtract a value from a variable of a host at a given timestamp.
692  *
693  *  Same as #TRACE_host_variable_sub, but let user specify
694  *  the time used to trace it. Users can specify a time that
695  *  is not the simulated clock time as defined by the core
696  *  simulator. This allows a fine-grain control of time
697  *  definition, but should be used with caution since the trace
698  *  can be inconsistent if resource utilization traces are also traced.
699  *
700  *  \param time The timestamp to be used to tag this change of value.
701  *  \param host The name of the host to be considered.
702  *  \param variable The name of the variable to be considered.
703  *  \param value The value to be subtracted from the variable.
704  *
705  *  \see TRACE_host_variable_declare, TRACE_host_variable_set_with_time, TRACE_host_variable_add_with_time
706  */
707 void TRACE_host_variable_sub_with_time (double time, const char *host, const char *variable, double value)
708 {
709   instr_user_variable(time, host, variable, "HOST", value, INSTR_US_SUB, NULL, user_host_variables);
710 }
711
712 /** \ingroup TRACE_user_variables
713  *  \brief Get declared user host variables
714  *
715  * This function should be used to get host variables that were already
716  * declared with #TRACE_host_variable_declare or with #TRACE_host_variable_declare_with_color.
717  *
718  * \return A dynar with the declared host variables, must be freed with xbt_dynar_free.
719  */
720 xbt_dynar_t TRACE_get_host_variables (void)
721 {
722   return instr_dict_to_dynar (user_host_variables);
723 }
724
725 /* for link variables */
726 /** \ingroup TRACE_user_variables
727  *  \brief Declare a new user variable associated to links.
728  *
729  *  Declare a user variable that will be associated to links.
730  *  A user link variable can be used, for example, to trace
731  *  user variables such as the number of messages being
732  *  transferred through network links. The color
733  *  associated to this new variable will be random.
734  *
735  *  \param variable The name of the new variable to be declared.
736  *
737  *  \see TRACE_link_variable_declare_with_color
738  */
739 void TRACE_link_variable_declare (const char *variable)
740 {
741   instr_user_variable (0, NULL, variable, "LINK", 0, INSTR_US_DECLARE, NULL, user_link_variables);
742 }
743
744 /** \ingroup TRACE_user_variables
745  *  \brief Declare a new user variable associated to links with a color.
746  *
747  *  Same as #TRACE_link_variable_declare, but associated a color
748  *  to the newly created user link variable. The color needs to be
749  *  a string with three numbers separated by spaces in the range [0,1].
750  *  A light-gray color can be specified using "0.7 0.7 0.7" as color.
751  *
752  *  \param variable The name of the new variable to be declared.
753  *  \param color The color for the new variable.
754  *
755  */
756 void TRACE_link_variable_declare_with_color (const char *variable, const char *color)
757 {
758   instr_user_variable (0, NULL, variable, "LINK", 0, INSTR_US_DECLARE, color, user_link_variables);
759 }
760
761 /** \ingroup TRACE_user_variables
762  *  \brief Set the value of a variable of a link.
763  *
764  *  \param link The name of the link to be considered.
765  *  \param variable The name of the variable to be considered.
766  *  \param value The new value of the variable.
767  *
768  *  \see TRACE_link_variable_declare, TRACE_link_variable_add, TRACE_link_variable_sub
769  */
770 void TRACE_link_variable_set (const char *link, const char *variable, double value)
771 {
772   TRACE_link_variable_set_with_time (MSG_get_clock(), link, variable, value);
773 }
774
775 /** \ingroup TRACE_user_variables
776  *  \brief Add a value to a variable of a link.
777  *
778  *  \param link The name of the link to be considered.
779  *  \param variable The name of the variable to be considered.
780  *  \param value The value to be added to the variable.
781  *
782  *  \see TRACE_link_variable_declare, TRACE_link_variable_set, TRACE_link_variable_sub
783  */
784 void TRACE_link_variable_add (const char *link, const char *variable, double value)
785 {
786   TRACE_link_variable_add_with_time (MSG_get_clock(), link, variable, value);
787 }
788
789 /** \ingroup TRACE_user_variables
790  *  \brief Subtract a value from a variable of a link.
791  *
792  *  \param link The name of the link to be considered.
793  *  \param variable The name of the variable to be considered.
794  *  \param value The value to be subtracted from the variable.
795  *
796  *  \see TRACE_link_variable_declare, TRACE_link_variable_set, TRACE_link_variable_add
797  */
798 void TRACE_link_variable_sub (const char *link, const char *variable, double value)
799 {
800   TRACE_link_variable_sub_with_time (MSG_get_clock(), link, variable, value);
801 }
802
803 /** \ingroup TRACE_user_variables
804  *  \brief Set the value of a variable of a link at a given timestamp.
805  *
806  *  Same as #TRACE_link_variable_set, but let user specify
807  *  the time used to trace it. Users can specify a time that
808  *  is not the simulated clock time as defined by the core
809  *  simulator. This allows a fine-grain control of time
810  *  definition, but should be used with caution since the trace
811  *  can be inconsistent if resource utilization traces are also traced.
812  *
813  *  \param time The timestamp to be used to tag this change of value.
814  *  \param link The name of the link to be considered.
815  *  \param variable The name of the variable to be considered.
816  *  \param value The new value of the variable.
817  *
818  *  \see TRACE_link_variable_declare, TRACE_link_variable_add_with_time, TRACE_link_variable_sub_with_time
819  */
820 void TRACE_link_variable_set_with_time (double time, const char *link, const char *variable, double value)
821 {
822   instr_user_variable (time, link, variable, "LINK", value, INSTR_US_SET, NULL, user_link_variables);
823 }
824
825 /** \ingroup TRACE_user_variables
826  *  \brief Add a value to a variable of a link at a given timestamp.
827  *
828  *  Same as #TRACE_link_variable_add, but let user specify
829  *  the time used to trace it. Users can specify a time that
830  *  is not the simulated clock time as defined by the core
831  *  simulator. This allows a fine-grain control of time
832  *  definition, but should be used with caution since the trace
833  *  can be inconsistent if resource utilization traces are also traced.
834  *
835  *  \param time The timestamp to be used to tag this change of value.
836  *  \param link The name of the link to be considered.
837  *  \param variable The name of the variable to be considered.
838  *  \param value The value to be added to the variable.
839  *
840  *  \see TRACE_link_variable_declare, TRACE_link_variable_set_with_time, TRACE_link_variable_sub_with_time
841  */
842 void TRACE_link_variable_add_with_time (double time, const char *link, const char *variable, double value)
843 {
844   instr_user_variable (time, link, variable, "LINK", value, INSTR_US_ADD, NULL, user_link_variables);
845 }
846
847 /** \ingroup TRACE_user_variables
848  *  \brief Subtract a value from a variable of a link at a given timestamp.
849  *
850  *  Same as #TRACE_link_variable_sub, but let user specify
851  *  the time used to trace it. Users can specify a time that
852  *  is not the simulated clock time as defined by the core
853  *  simulator. This allows a fine-grain control of time
854  *  definition, but should be used with caution since the trace
855  *  can be inconsistent if resource utilization traces are also traced.
856  *
857  *  \param time The timestamp to be used to tag this change of value.
858  *  \param link The name of the link to be considered.
859  *  \param variable The name of the variable to be considered.
860  *  \param value The value to be subtracted from the variable.
861  *
862  *  \see TRACE_link_variable_declare, TRACE_link_variable_set_with_time, TRACE_link_variable_add_with_time
863  */
864 void TRACE_link_variable_sub_with_time (double time, const char *link, const char *variable, double value)
865 {
866   instr_user_variable (time, link, variable, "LINK", value, INSTR_US_SUB, NULL, user_link_variables);
867 }
868
869 /* for link variables, but with src and dst used for get_route */
870 /** \ingroup TRACE_user_variables
871  *  \brief Set the value of the variable present in the links connecting source and destination.
872  *
873  *  Same as #TRACE_link_variable_set, but instead of providing the
874  *  name of link to be considered, provide the source and destination
875  *  hosts. All links that are part of the route between source and
876  *  destination will have the variable set to the provided value.
877  *
878  *  \param src The name of the source host for get route.
879  *  \param dst The name of the destination host for get route.
880  *  \param variable The name of the variable to be considered.
881  *  \param value The new value of the variable.
882  *
883  *  \see TRACE_link_variable_declare, TRACE_link_srcdst_variable_add, TRACE_link_srcdst_variable_sub
884  */
885 void TRACE_link_srcdst_variable_set (const char *src, const char *dst, const char *variable, double value)
886 {
887   TRACE_link_srcdst_variable_set_with_time (MSG_get_clock(), src, dst, variable, value);
888 }
889
890 /** \ingroup TRACE_user_variables
891  *  \brief Add a value to the variable present in the links connecting source and destination.
892  *
893  *  Same as #TRACE_link_variable_add, but instead of providing the
894  *  name of link to be considered, provide the source and destination
895  *  hosts. All links that are part of the route between source and
896  *  destination will have the value passed as parameter added to
897  *  the current value of the variable name to be considered.
898  *
899  *  \param src The name of the source host for get route.
900  *  \param dst The name of the destination host for get route.
901  *  \param variable The name of the variable to be considered.
902  *  \param value The value to be added to the variable.
903  *
904  *  \see TRACE_link_variable_declare, TRACE_link_srcdst_variable_set, TRACE_link_srcdst_variable_sub
905  */
906 void TRACE_link_srcdst_variable_add (const char *src, const char *dst, const char *variable, double value)
907 {
908   TRACE_link_srcdst_variable_add_with_time (MSG_get_clock(), src, dst, variable, value);
909 }
910
911 /** \ingroup TRACE_user_variables
912  *  \brief Subtract a value from the variable present in the links connecting source and destination.
913  *
914  *  Same as #TRACE_link_variable_sub, but instead of providing the
915  *  name of link to be considered, provide the source and destination
916  *  hosts. All links that are part of the route between source and
917  *  destination will have the value passed as parameter subtracted from
918  *  the current value of the variable name to be considered.
919  *
920  *  \param src The name of the source host for get route.
921  *  \param dst The name of the destination host for get route.
922  *  \param variable The name of the variable to be considered.
923  *  \param value The value to be subtracted from the variable.
924  *
925  *  \see TRACE_link_variable_declare, TRACE_link_srcdst_variable_set, TRACE_link_srcdst_variable_add
926  */
927 void TRACE_link_srcdst_variable_sub (const char *src, const char *dst, const char *variable, double value)
928 {
929   TRACE_link_srcdst_variable_sub_with_time (MSG_get_clock(), src, dst, variable, value);
930 }
931
932 /** \ingroup TRACE_user_variables
933  *  \brief Set the value of the variable present in the links connecting source and destination at a given timestamp.
934  *
935  *  Same as #TRACE_link_srcdst_variable_set, but let user specify
936  *  the time used to trace it. Users can specify a time that
937  *  is not the simulated clock time as defined by the core
938  *  simulator. This allows a fine-grain control of time
939  *  definition, but should be used with caution since the trace
940  *  can be inconsistent if resource utilization traces are also traced.
941  *
942  *  \param time The timestamp to be used to tag this change of value.
943  *  \param src The name of the source host for get route.
944  *  \param dst The name of the destination host for get route.
945  *  \param variable The name of the variable to be considered.
946  *  \param value The new value of the variable.
947  *
948  *  \see TRACE_link_variable_declare, TRACE_link_srcdst_variable_add_with_time, TRACE_link_srcdst_variable_sub_with_time
949  */
950 void TRACE_link_srcdst_variable_set_with_time (double time, const char *src, const char *dst, const char *variable, double value)
951 {
952   instr_user_srcdst_variable (time, src, dst, variable, "LINK", value, INSTR_US_SET);
953 }
954
955 /** \ingroup TRACE_user_variables
956  *  \brief Add a value to the variable present in the links connecting source and destination at a given timestamp.
957  *
958  *  Same as #TRACE_link_srcdst_variable_add, but let user specify
959  *  the time used to trace it. Users can specify a time that
960  *  is not the simulated clock time as defined by the core
961  *  simulator. This allows a fine-grain control of time
962  *  definition, but should be used with caution since the trace
963  *  can be inconsistent if resource utilization traces are also traced.
964  *
965  *  \param time The timestamp to be used to tag this change of value.
966  *  \param src The name of the source host for get route.
967  *  \param dst The name of the destination host for get route.
968  *  \param variable The name of the variable to be considered.
969  *  \param value The value to be added to the variable.
970  *
971  *  \see TRACE_link_variable_declare, TRACE_link_srcdst_variable_set_with_time, TRACE_link_srcdst_variable_sub_with_time
972  */
973 void TRACE_link_srcdst_variable_add_with_time (double time, const char *src, const char *dst, const char *variable, double value)
974 {
975   instr_user_srcdst_variable (time, src, dst, variable, "LINK", value, INSTR_US_ADD);
976 }
977
978 /** \ingroup TRACE_user_variables
979  *  \brief Subtract a value from the variable present in the links connecting source and destination at a given timestamp.
980  *
981  *  Same as #TRACE_link_srcdst_variable_sub, but let user specify
982  *  the time used to trace it. Users can specify a time that
983  *  is not the simulated clock time as defined by the core
984  *  simulator. This allows a fine-grain control of time
985  *  definition, but should be used with caution since the trace
986  *  can be inconsistent if resource utilization traces are also traced.
987  *
988  *  \param time The timestamp to be used to tag this change of value.
989  *  \param src The name of the source host for get route.
990  *  \param dst The name of the destination host for get route.
991  *  \param variable The name of the variable to be considered.
992  *  \param value The value to be subtracted from the variable.
993  *
994  *  \see TRACE_link_variable_declare, TRACE_link_srcdst_variable_set_with_time, TRACE_link_srcdst_variable_add_with_time
995  */
996 void TRACE_link_srcdst_variable_sub_with_time (double time, const char *src, const char *dst, const char *variable, double value)
997 {
998   instr_user_srcdst_variable (time, src, dst, variable, "LINK", value, INSTR_US_SUB);
999 }
1000
1001 /** \ingroup TRACE_user_variables
1002  *  \brief Get declared user link variables
1003  *
1004  * This function should be used to get link variables that were already
1005  * declared with #TRACE_link_variable_declare or with #TRACE_link_variable_declare_with_color.
1006  *
1007  * \return A dynar with the declared link variables, must be freed with xbt_dynar_free.
1008  */
1009 xbt_dynar_t TRACE_get_link_variables (void)
1010 {
1011   return instr_dict_to_dynar (user_link_variables);
1012 }
1013
1014 /** \ingroup TRACE_user_variables
1015  *  \brief Declare a new user state associated to hosts.
1016  *
1017  *  Declare a user state that will be associated to hosts.
1018  *  A user host state can be used to trace application states.
1019  *
1020  *  \param state The name of the new state to be declared.
1021  *
1022  *  \see TRACE_host_state_declare_value
1023  */
1024 void TRACE_host_state_declare (const char *state)
1025 {
1026   instr_new_user_state_type("HOST", state);
1027 }
1028
1029 /** \ingroup TRACE_user_variables
1030  *  \brief Declare a new value for a user state associated to hosts.
1031  *
1032  *  Declare a value for a state. The color needs to be
1033  *  a string with three numbers separated by spaces in the range [0,1].
1034  *  A light-gray color can be specified using "0.7 0.7 0.7" as color.
1035  *
1036  *  \param state The name of the new state to be declared.
1037  *  \param value The name of the value
1038  *  \param color The color of the value
1039  *
1040  *  \see TRACE_host_state_declare
1041  */
1042 void TRACE_host_state_declare_value (const char *state, const char *value, const char *color)
1043 {
1044   instr_new_value_for_user_state_type (state, value, color);
1045 }
1046
1047 /** \ingroup TRACE_user_variables
1048  *  \brief Set the user state to the given value.
1049  *
1050  *  Change a user state previously declared to the given value.
1051  *
1052  *  \param host The name of the host to be considered.
1053  *  \param state The name of the state previously declared.
1054  *  \param value The new value of the state.
1055  *
1056  *  \see TRACE_host_state_declare, TRACE_host_push_state, TRACE_host_pop_state, TRACE_host_reset_state
1057  */
1058 void TRACE_host_set_state (const char *host, const char *state, const char *value)
1059 {
1060   container_t container = PJ_container_get(host);
1061   type_t type = PJ_type_get (state, container->type);
1062   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 */
1063   new_pajeSetState(MSG_get_clock(), container, type, val);
1064 }
1065
1066 /** \ingroup TRACE_user_variables
1067  *  \brief Push a new value for a state of a given host.
1068  *
1069  *  Change a user state previously declared by pushing the new value to the state.
1070  *
1071  *  \param host The name of the host to be considered.
1072  *  \param state The name of the state previously declared.
1073  *  \param value The value to be pushed.
1074  *
1075  *  \see TRACE_host_state_declare, TRACE_host_set_state, TRACE_host_pop_state, TRACE_host_reset_state
1076  */
1077 void TRACE_host_push_state (const char *host, const char *state, const char *value)
1078 {
1079   container_t container = PJ_container_get(host);
1080   type_t type = PJ_type_get (state, container->type);
1081   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 */
1082   new_pajePushState(MSG_get_clock(), container, type, val);
1083 }
1084
1085 /** \ingroup TRACE_user_variables
1086  *  \brief Pop the last value of a state of a given host.
1087  *
1088  *  Change a user state previously declared by removing the last value of the state.
1089  *
1090  *  \param host The name of the host to be considered.
1091  *  \param state The name of the state to be popped.
1092  *
1093  *  \see TRACE_host_state_declare, TRACE_host_set_state, TRACE_host_push_state, TRACE_host_reset_state
1094  */
1095 void TRACE_host_pop_state (const char *host, const char *state)
1096 {
1097   container_t container = PJ_container_get(host);
1098   type_t type = PJ_type_get (state, container->type);
1099   new_pajePopState(MSG_get_clock(), container, type);
1100 }
1101
1102 /** \ingroup TRACE_user_variables
1103  *  \brief Reset the state of a given host.
1104  *
1105  *  Clear all previous values of a user state.
1106  *
1107  *  \param host The name of the host to be considered.
1108  *  \param state The name of the state to be cleared.
1109  *
1110  *  \see TRACE_host_state_declare, TRACE_host_set_state, TRACE_host_push_state, TRACE_host_pop_state
1111  */
1112 void TRACE_host_reset_state (const char *host, const char *state)
1113 {
1114   container_t container = PJ_container_get(host);
1115   type_t type = PJ_type_get (state, container->type);
1116   new_pajeResetState(MSG_get_clock(), container, type);
1117 }
1118
1119 /** \ingroup TRACE_API
1120  *  \brief Get Paje container types that can be mapped to the nodes of a graph.
1121  *
1122  *  This function can be used to create a user made
1123  *  graph configuration file for Triva. Normally, it is
1124  *  used with the functions defined in \ref TRACE_user_variables.
1125  *
1126  *  \return A dynar with the types, must be freed with xbt_dynar_free.
1127  */
1128 xbt_dynar_t TRACE_get_node_types (void)
1129 {
1130   return instr_dict_to_dynar (trivaNodeTypes);
1131 }
1132
1133 /** \ingroup TRACE_API
1134  *  \brief Get Paje container types that can be mapped to the edges of a graph.
1135  *
1136  *  This function can be used to create a user made
1137  *  graph configuration file for Triva. Normally, it is
1138  *  used with the functions defined in \ref TRACE_user_variables.
1139  *
1140  *  \return A dynar with the types, must be freed with xbt_dynar_free.
1141  */
1142 xbt_dynar_t TRACE_get_edge_types (void)
1143 {
1144   return instr_dict_to_dynar (trivaEdgeTypes);
1145 }
1146
1147 /** \ingroup TRACE_API
1148  *  \brief Pauses all tracing activities.
1149  *  \see TRACE_resume
1150  */
1151 void TRACE_pause (void)
1152 {
1153   instr_pause_tracing();
1154 }
1155
1156 /** \ingroup TRACE_API
1157  *  \brief Resumes all tracing activities.
1158  *  \see TRACE_pause
1159  */
1160 void TRACE_resume (void)
1161 {
1162   instr_resume_tracing();
1163 }