The Sparta Modeling Framework
Loading...
Searching...
No Matches
ReportStatisticsHierTree.hpp
1// <ReportStatisticsHierTree> -*- C++ -*-
2
3#pragma once
4
5#include "sparta/report/Report.hpp"
6
7namespace sparta {
8namespace statistics {
9
46template <class NodeT, class LeafNodeT = NodeT>
48{
49public:
50 explicit ReportStatisticsHierTree(const Report * r) :
51 report_(r)
52 {
53 sparta_assert(report_, "You may not give a null report to a ReportStatisticsHierTree");
54 }
55
56 typedef std::pair<LeafNodeT*, const StatisticInstance*> LeafNodeSI;
57
62 std::vector<LeafNodeSI> buildFrom(std::shared_ptr<NodeT> & root,
63 std::vector<std::string> * si_locations = nullptr) const {
64 return buildFrom(root.get(), si_locations);
65 }
66
71 std::vector<LeafNodeSI> buildFrom(NodeT * root,
72 std::vector<std::string> * si_locations = nullptr) const {
73 std::vector<LeafNodeSI> flattened_leaves;
74 createSubreportHierTree_(root, *report_, flattened_leaves, si_locations, "");
75 return flattened_leaves;
76 }
77
78private:
79 void createSubreportHierTree_(NodeT * report_node,
80 const Report & report,
81 std::vector<LeafNodeSI> & flattened_leaves,
82 std::vector<std::string> * si_locations,
83 const std::string & si_location_prefix) const
84 {
85 const auto & stats = report.getStatistics();
86 const auto & subreports = report.getSubreports();
87 auto & children = report_node->getChildren();
88
89 if (!stats.empty()) {
90 for (const auto & stat : stats) {
91 std::string name = !stat.first.empty() ? stat.first : stat.second->getLocation();
92 if (si_locations) {
93 si_locations->emplace_back(si_location_prefix + name);
94 }
95 boost::replace_all(name, ".", "_");
96
97 std::shared_ptr<LeafNodeT> si_node(new LeafNodeT(name, stat.second.get()));
98 si_node->setParent(report_node);
99 children.emplace_back(si_node);
100 flattened_leaves.emplace_back(std::make_pair(si_node.get(), stat.second.get()));
101 }
102 }
103
104 if (!subreports.empty()) {
105 for (const auto & sr : subreports) {
106 std::vector<std::string> dot_delimited;
107 boost::split(dot_delimited, sr.getName(), boost::is_any_of("."));
108 const std::string & name = dot_delimited.back();
109
110 std::shared_ptr<NodeT> subreport_node(new NodeT(name, &sr));
111 subreport_node->setParent(report_node);
112 createSubreportHierTree_(subreport_node.get(), sr,
113 flattened_leaves,
114 si_locations,
115 sr.getName() + ".");
116
117 children.emplace_back(subreport_node);
118 }
119 }
120 }
121
122 const Report * report_ = nullptr;
123};
124
125} // namespace statistics
126} // namespace sparta
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
Helper class used to build up a "report statistics hierarchy tree", where the template types are:
std::vector< LeafNodeSI > buildFrom(std::shared_ptr< NodeT > &root, std::vector< std::string > *si_locations=nullptr) const
std::vector< LeafNodeSI > buildFrom(NodeT *root, std::vector< std::string > *si_locations=nullptr) const
Macros for handling exponential backoff.