Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : merge conflict resolved
[simgrid.git] / src / instr / instr_resource_utilization.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5   * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "instr/instr_private.h"
8
9 #ifdef HAVE_TRACING
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_resource, instr, "tracing (un)-categorized resource utilization");
12
13 //to check if variables were previously set to 0, otherwise paje won't simulate them
14 static xbt_dict_t platform_variables;   /* host or link name -> array of categories */
15
16 //used by all methods
17 static void __TRACE_surf_check_variable_set_to_zero(double now,
18                                                     const char *variable,
19                                                     const char *resource)
20 {
21   /*
22    * To trace resource utilization, we use pajeAddVariable and pajeSubVariable only.
23    * The Paje simulator needs a pajeSetVariable in the first place so it knows
24    * the initial value of all variables for subsequent adds/subs. If we don't do
25    * so, the first pajeAddVariable is added to a non-determined value within
26    * the Paje simulator, causing analysis problems.
27    */
28
29   // create a key considering the resource and variable
30   int n = strlen(variable)+strlen(resource)+1;
31   char *key = (char*)xbt_malloc(n*sizeof(char));
32   snprintf (key, n, "%s%s", resource, variable);
33
34   // check if key exists: if it doesn't, set the variable to zero and mark this in the dict
35   if (!xbt_dict_get_or_null(platform_variables, key)) {
36     container_t container = PJ_container_get (resource);
37     type_t type = PJ_type_get (variable, container->type);
38     new_pajeSetVariable (now, container, type, 0);
39     xbt_dict_set(platform_variables, key, (char*)"", NULL);
40   }
41   xbt_free(key);
42 }
43
44 /*
45 static void __TRACE_A_event(smx_action_t action, double now, double delta,
46                             const char *variable, const char *resource,
47                             double value)
48 {
49   char valuestr[100];
50   snprintf(valuestr, 100, "%f", value);
51
52   __TRACE_surf_check_variable_set_to_zero(now, variable, resource);
53   container_t container = PJ_container_get (resource);
54   type_t type = getVariableType (variable, NULL, container->type);
55   new_pajeAddVariable(now, container, type, value);
56   new_pajeSubVariable(now + delta, container, type, value);
57 }
58 */
59
60 static void instr_event (double now, double delta, type_t variable, container_t resource, double value)
61 {
62   __TRACE_surf_check_variable_set_to_zero(now, variable->name, resource->name);
63   new_pajeAddVariable(now, resource, variable, value);
64   new_pajeSubVariable(now + delta, resource, variable, value);
65 }
66
67 /*
68  * TRACE_surf_link_set_utilization: entry point from SimGrid
69  */
70 void TRACE_surf_link_set_utilization(const char *resource, smx_action_t smx_action,
71                                      surf_action_t surf_action,
72                                      double value, double now,
73                                      double delta)
74 {
75   //only trace link utilization if link is known by tracing mechanism
76   if (!PJ_container_get_or_null(resource))
77     return;
78   if (!value)
79     return;
80
81   //trace uncategorized link utilization
82   if (TRACE_uncategorized()){
83     XBT_DEBUG("UNCAT LINK [%f - %f] %s bandwidth_used %f", now, now+delta, resource, value);
84     container_t container = PJ_container_get (resource);
85     type_t type = PJ_type_get ("bandwidth_used", container->type);
86     instr_event (now, delta, type, container, value);
87   }
88
89   //trace categorized utilization
90   if (TRACE_categorized()){
91     if (!surf_action->category)
92       return;
93     //variable of this category starts by 'b', because we have a link here
94     char category_type[INSTR_DEFAULT_STR_SIZE];
95     snprintf (category_type, INSTR_DEFAULT_STR_SIZE, "b%s", surf_action->category);
96     XBT_DEBUG("CAT LINK [%f - %f] %s %s %f", now, now+delta, resource, category_type, value);
97     container_t container = PJ_container_get (resource);
98     type_t type = PJ_type_get (category_type, container->type);
99     instr_event (now, delta, type, container, value);
100   }
101   return;
102 }
103
104 /*
105  * TRACE_surf_host_set_utilization: entry point from SimGrid
106  */
107 void TRACE_surf_host_set_utilization(const char *resource,
108                                      smx_action_t smx_action,
109                                      surf_action_t surf_action,
110                                      double value, double now,
111                                      double delta)
112 {
113   //only trace host utilization if host is known by tracing mechanism
114   if (!PJ_container_get_or_null(resource))
115     return;
116   if (!value)
117     return;
118
119   //trace uncategorized host utilization
120   if (TRACE_uncategorized()){
121     XBT_DEBUG("UNCAT HOST [%f - %f] %s power_used %f", now, now+delta, resource, value);
122     container_t container = PJ_container_get (resource);
123     type_t type = PJ_type_get ("power_used", container->type);
124     instr_event (now, delta, type, container, value);
125   }
126
127   //trace categorized utilization
128   if (TRACE_categorized()){
129     if (!surf_action->category)
130       return;
131     //variable of this category starts by 'p', because we have a host here
132     char category_type[INSTR_DEFAULT_STR_SIZE];
133     snprintf (category_type, INSTR_DEFAULT_STR_SIZE, "p%s", surf_action->category);
134     XBT_DEBUG("CAT HOST [%f - %f] %s %s %f", now, now+delta, resource, category_type, value);
135     container_t container = PJ_container_get (resource);
136     type_t type = PJ_type_get (category_type, container->type);
137     instr_event (now, delta, type, container, value);
138   }
139   return;
140 }
141
142 void TRACE_surf_resource_utilization_alloc()
143 {
144   platform_variables = xbt_dict_new_homogeneous(NULL);
145 }
146
147 void TRACE_surf_resource_utilization_release()
148 {
149   xbt_dict_free(&platform_variables);
150 }
151 #endif /* HAVE_TRACING */