Logo AND Algorithmique Numérique Distribuée

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