P4C
The P4 Compiler
Loading...
Searching...
No Matches
allocator_metrics.h
1
19#ifndef BF_P4C_PHV_V2_ALLOCATOR_METRICS_H_
20#define BF_P4C_PHV_V2_ALLOCATOR_METRICS_H_
21
22#include <chrono>
23
24#include "backends/tofino/bf-p4c/phv/phv.h"
25
26namespace PHV {
27namespace v2 {
28
31 public:
32 enum ctype { ALLOC = 0, EQUIV = 1 };
33
34 private:
35 cstring name;
36
37 // Allocation
38 // PHV::Type (Kind, Size) -> pair < total, pass >
39 typedef std::map<PHV::Type, std::pair<unsigned long, unsigned long>> ContainerMetricsMap;
40 ContainerMetricsMap alloc_containers;
41 ContainerMetricsMap equiv_containers;
42
43 // Compilation Time
44 std::chrono::time_point<std::chrono::steady_clock> start_time;
45 std::chrono::time_point<std::chrono::steady_clock> stop_time;
46
47 public:
48 explicit AllocatorMetrics(cstring name = ""_cs) : name(name) {}
49
50 cstring get_name() const { return name; };
51
52 // Container Equivalence
53 void update_container_equivalence_metrics(const PHV::Container &c) {
54 ++equiv_containers[c.type()].first;
55 }
56
57 // Allocation
58 void update_containers_metrics(const PHV::Container &c, bool succ = false) {
59 ++alloc_containers[c.type()].first;
60 if (succ) ++alloc_containers[c.type()].second;
61 }
62
63 unsigned long get_containers(const ctype ct = ALLOC, const PHV::Kind *kind = nullptr,
64 const PHV::Size *size = nullptr, bool succ = false) const {
65 unsigned long containers = 0;
66
67 const ContainerMetricsMap *cmap = nullptr;
68 if (ct == ALLOC)
69 cmap = &alloc_containers;
70 else if (ct == EQUIV)
71 cmap = &equiv_containers;
72 else
73 return 0;
74
75 for (auto ct : *cmap) {
76 if (kind && (ct.first.kind() != *kind)) continue;
77 if (size && (ct.first.size() != *size)) continue;
78 if (succ)
79 containers += ct.second.second;
80 else
81 containers += ct.second.first;
82 }
83 return containers;
84 }
85
86 // Compilation time
87 void start_clock() { start_time = std::chrono::steady_clock::now(); }
88
89 void stop_clock() { stop_time = std::chrono::steady_clock::now(); }
90
91 std::string get_duration() const {
92 std::stringstream ss;
93 const auto duration = stop_time - start_time;
94 const auto hrs = std::chrono::duration_cast<std::chrono::hours>(duration);
95 const auto mins = std::chrono::duration_cast<std::chrono::minutes>(duration - hrs);
96 const auto secs = std::chrono::duration_cast<std::chrono::seconds>(duration - hrs - mins);
97 const auto msecs =
98 std::chrono::duration_cast<std::chrono::milliseconds>(duration - hrs - mins - secs);
99
100 ss << hrs.count() << "h " << mins.count() << "m " << secs.count() << "s " << msecs.count()
101 << "ms " << std::endl;
102 return ss.str();
103 }
104
105 void clear() {
106 alloc_containers.clear();
107 equiv_containers.clear();
108 }
109};
110
111std::ostream &operator<<(std::ostream &out, const AllocatorMetrics &am);
112
113} // namespace v2
114} // namespace PHV
115
116#endif /* BF_P4C_PHV_V2_ALLOCATOR_METRICS_H_ */
Definition cstring.h:85
Definition phv.h:176
AllocatorMetrics contains metrics useful in tracking Allocator efficiency and debug.
Definition allocator_metrics.h:30
The namespace encapsulating PHV-related stuff.
Definition gateway.h:32
Size
all possible PHV container sizes in BFN devices
Definition phv.h:110
Kind
Definition phv.h:44