Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into actor-yield
[simgrid.git] / src / instr / instr_resource_utilization.cpp
1 /* Copyright (c) 2010-2017. 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.hpp"
8 #include <set>
9 #include <string>
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 std::set<std::string> platform_variables;
15
16 static void instr_event(double now, double delta, simgrid::instr::VariableType* variable, container_t resource,
17                         double value)
18 {
19   /* To trace resource utilization, we use AddEvent and SubEvent only. This implies to add a SetEvent first to set the
20    * initial value of all variables for subsequent adds/subs. If we don't do so, the first AddEvent would be added to a
21    * non-determined value, hence causing analysis problems.
22    */
23
24   // create a key considering the resource and variable
25   std::string key = resource->getName() + variable->getName();
26
27   // check if key exists: if it doesn't, set the variable to zero and mark this in the global map.
28   if (platform_variables.find(key) == platform_variables.end()) {
29     variable->setEvent(now, 0);
30     platform_variables.insert(key);
31   }
32
33   variable->addEvent(now, value);
34   variable->subEvent(now + delta, value);
35 }
36
37 static void TRACE_surf_resource_set_utilization(const char* type, const char* name, const char* resource,
38                                                 const char* category, double value, double now, double delta)
39 {
40   // only trace resource utilization if resource is known by tracing mechanism
41   container_t container = simgrid::instr::Container::byNameOrNull(resource);
42   if (not container || not value)
43     return;
44
45   // trace uncategorized resource utilization
46   if (TRACE_uncategorized()){
47     XBT_DEBUG("UNCAT %s [%f - %f] %s %s %f", type, now, now + delta, resource, name, value);
48     simgrid::instr::VariableType* variable = container->getVariable(name);
49     instr_event(now, delta, variable, container, value);
50   }
51
52   // trace categorized resource utilization
53   if (TRACE_categorized()){
54     if (not category)
55       return;
56     std::string category_type = name[0] + std::string(category);
57     XBT_DEBUG("CAT %s [%f - %f] %s %s %f", type, now, now + delta, resource, category_type.c_str(), value);
58     simgrid::instr::VariableType* variable = container->getVariable(category_type);
59     instr_event(now, delta, variable, container, value);
60   }
61 }
62
63 /* TRACE_surf_link_set_utilization: entry point from SimGrid */
64 void TRACE_surf_link_set_utilization(const char* resource, const char* category, double value, double now, double delta)
65 {
66   TRACE_surf_resource_set_utilization("LINK", "bandwidth_used", resource, category, value, now, delta);
67 }
68
69 /* TRACE_surf_host_set_utilization: entry point from SimGrid */
70 void TRACE_surf_host_set_utilization(const char* resource, const char* category, double value, double now, double delta)
71 {
72   TRACE_surf_resource_set_utilization("HOST", "power_used", resource, category, value, now, delta);
73 }