P4C
The P4 Compiler
Loading...
Searching...
No Matches
report.h
1
18
19#ifndef BACKENDS_TOFINO_BF_P4C_PHV_UTILS_REPORT_H_
20#define BACKENDS_TOFINO_BF_P4C_PHV_UTILS_REPORT_H_
21
22#include <boost/format.hpp>
23
24#include "backends/tofino/bf-p4c/phv/utils/utils.h"
25
26namespace PHV {
27
28class AllocationReport {
29 const PhvUse &uses;
30 const Allocation &alloc;
31 bool printMetricsOnly = false;
32
35 alloc_status;
36
37 ordered_map<PHV::Container, int> partial_containers_stat;
38
39 ordered_map<PHV::Container, int> container_to_bits_used;
40
41 ordered_map<PHV::Container, int> container_to_bits_allocated;
42
44
45 ordered_set<AllocSlice> alloc_slices;
46
47 int total_allocated_bits = 0;
48 int total_unallocated_bits = 0;
49 int valid_ingress_unallocated_bits = 0;
50 int valid_egress_unallocated_bits = 0;
51
52 static std::string formatPercent(int div, int den) {
53 std::stringstream ss;
54 ss << boost::format("(%=6.3g%%)") % (100.0 * div / den);
55 return ss.str();
56 }
57
58 static std::string formatUsage(int used, int total, bool show_total = false) {
59 std::stringstream ss;
60 ss << used << " ";
61 if (show_total) ss << "/ " << total << " ";
62 ss << formatPercent(used, total);
63 return ss.str();
64 }
65
66 struct PhvOccupancyMetricFields {
67 unsigned containersUsed = 0;
68 unsigned bitsUsed = 0;
69 unsigned bitsAllocated = 0;
70
71 inline PhvOccupancyMetricFields &operator+=(const PhvOccupancyMetricFields &src) {
72 containersUsed += src.containersUsed;
73 bitsUsed += src.bitsUsed;
74 bitsAllocated += src.bitsAllocated;
75
76 return *this;
77 }
78 };
79
81 struct PhvOccupancyMetric {
82 // in total
83 PhvOccupancyMetricFields total;
84 // per gress
85 std::map<gress_t, PhvOccupancyMetricFields> gress;
86
87 inline PhvOccupancyMetric &operator+=(const PhvOccupancyMetric &src) {
88 total += src.total;
89 for (const auto &gr : src.gress) {
90 gress[gr.first] += gr.second;
91 }
92
93 return *this;
94 }
95
96 inline std::string gressToSymbolString() const {
97 std::stringstream ss;
98 std::string sep = "";
99 for (const auto &gr : gress) {
100 ss << sep;
101 ss << toSymbol(gr.first);
102 sep = "/";
103 }
104 return ss.str();
105 }
106 };
107
109 struct MauGroupInfo {
110 size_t size = 0;
111 int groupID = 0;
112
113 PhvOccupancyMetric totalStats;
114 std::map<PHV::Kind, PhvOccupancyMetric, std::greater<PHV::Kind>> statsByContainerKind;
115
116 MauGroupInfo() {}
117
119 explicit MauGroupInfo(size_t sz, int i, PHV::Kind containerKind, bool cont, size_t used,
120 size_t allocated, std::optional<gress_t> gr)
121 : size(sz), groupID(i) {
122 update(containerKind, cont, used, allocated, gr);
123 }
124
126 void update(PHV::Kind containerKind, bool cont, size_t used, size_t allocated,
127 std::optional<gress_t> gr) {
128 auto &kindStats = statsByContainerKind[containerKind];
129
130 // Update total stats
131 auto &totalStatsTotal = totalStats.total;
132 auto &kindStatsTotal = kindStats.total;
133
134 if (cont) {
135 ++totalStatsTotal.containersUsed;
136 ++kindStatsTotal.containersUsed;
137 }
138
139 totalStatsTotal.bitsUsed += used;
140 kindStatsTotal.bitsUsed += used;
141
142 totalStatsTotal.bitsAllocated += allocated;
143 kindStatsTotal.bitsAllocated += allocated;
144
145 if (gr) {
146 // Update per gress stats
147 auto &totalStatsGress = totalStats.gress[*gr];
148 auto &kindStatsGress = kindStats.gress[*gr];
149
150 if (cont) {
151 ++totalStatsGress.containersUsed;
152 ++kindStatsGress.containersUsed;
153 }
154
155 totalStatsGress.bitsUsed += used;
156 kindStatsGress.bitsUsed += used;
157
158 totalStatsGress.bitsAllocated += allocated;
159 kindStatsGress.bitsAllocated += allocated;
160 }
161 }
162 };
163
165 struct TagalongCollectionInfo {
166 std::map<PHV::Type, size_t> containersUsed;
167 std::map<PHV::Type, size_t> bitsUsed;
168 std::map<PHV::Type, size_t> bitsAllocated;
169 std::map<PHV::Type, size_t> totalContainers;
170
171 std::optional<gress_t> gress;
172
173 size_t getTotalUsedBits() {
174 size_t rv = 0;
175 for (auto kv : bitsUsed) rv += kv.second;
176 return rv;
177 }
178
179 size_t getTotalAllocatedBits() {
180 size_t rv = 0;
181 for (auto kv : bitsAllocated) rv += kv.second;
182 return rv;
183 }
184
185 size_t getTotalAvailableBits() {
186 size_t rv = 0;
187 for (auto &kv : totalContainers) rv += (size_t)kv.first.size() * kv.second;
188 return rv;
189 }
190
191 std::string printUsage(PHV::Type type) {
192 auto used = containersUsed[type];
193 auto total = totalContainers[type];
194
195 return formatUsage(used, total);
196 }
197
198 std::string printTotalUsage() {
199 auto total = getTotalAvailableBits();
200 auto used = getTotalUsedBits();
201
202 return formatUsage(used, total);
203 }
204
205 std::string printTotalAlloc() {
206 auto total = getTotalAvailableBits();
207 auto alloced = getTotalAllocatedBits();
208
209 return formatUsage(alloced, total);
210 }
211 };
212
213 public:
214 explicit AllocationReport(const Allocation &alloc, bool printMetricsOnly = false)
215 : uses(*(alloc.uses_i)), alloc(alloc), printMetricsOnly(printMetricsOnly) {}
216
219 collectStatus();
220
221 std::stringstream ss;
222
223 if (!printMetricsOnly) ss << printAllocation() << std::endl;
224
225 if (!printMetricsOnly) ss << printAllocSlices() << std::endl;
226
227 ss << printContainerStatus() << std::endl;
228 ss << printOverlayStatus() << std::endl;
229 ss << printOccupancyMetrics() << std::endl;
230
231 return ss.str();
232 }
233
234 private:
235 void collectStatus();
236
237 cstring printAllocation() const;
238 cstring printAllocSlices() const;
239 cstring printOverlayStatus() const;
240 cstring printContainerStatus();
241 cstring printOccupancyMetrics() const;
242 cstring printMauGroupsOccupancyMetrics() const;
243 cstring printTagalongCollectionsOccupancyMetrics() const;
244};
245
246} // namespace PHV
247
248#endif /* BACKENDS_TOFINO_BF_P4C_PHV_UTILS_REPORT_H_ */
Definition cstring.h:76
Definition ordered_map.h:23
Definition ordered_set.h:23
Definition phv/utils/utils.h:117
cstring printSummary()
Definition report.h:218
Definition bf-p4c/specs/phv.h:109
Definition phv_parde_mau_use.h:154
The namespace encapsulating PHV-related stuff.
Definition bf-p4c/mau/gateway.h:32
Kind
Definition bf-p4c/specs/phv.h:39