P4C
The P4 Compiler
Loading...
Searching...
No Matches
headerPacketMetrics.h
1/*
2Collects header manipulation metrics (extract, setValid and
3setInvalid calls), and header modification metrics (assignment
4statements where the left side is a header type). Before the main
5pass traverses the IR, the ParserAnalyzer pass is ran to determine
6cumulative packet types which can be created by the parser
7(for example, "ethernet", "ethernet-ipv4", "ethernet-ipv4-tcp").
8It does so by creating a parserCallGraph and then traversing it.
9Only limitation is that it cannot determine packet types created
10with loops completely (it stops when the same state is encountered
11in the current path).
12
13After the ParserAnalyzer finishes, the main pass collects the metrics
14on a global and per-packet type basis. An operation on a header type is
15added to the metrics of all packet types containing that type (so for
16hdr.ipv4.ttl = 64, the operation size will be added to both "ethernet-ipv4"
17and "ethernet-ipv4-tcp").
18*/
19
20#ifndef FRONTENDS_P4_METRICS_HEADERPACKETMETRICS_H_
21#define FRONTENDS_P4_METRICS_HEADERPACKETMETRICS_H_
22
23#include "frontends/common/resolveReferences/referenceMap.h"
24#include "frontends/p4/callGraph.h"
25#include "frontends/p4/methodInstance.h"
26#include "frontends/p4/metrics/metricsStructure.h"
27#include "frontends/p4/parserCallGraph.h"
28#include "frontends/p4/typeChecking/typeChecker.h"
29#include "ir/ir.h"
30
31using namespace P4::literals;
32
33namespace P4 {
34
36
37class ParserAnalyzer : public Inspector {
38 private:
39 ParserCallGraph &parserCallGraph;
40 std::set<cstring> &cumulativeTypes;
41 std::unordered_map<cstring, size_t> stateEncounters;
42
43 cstring getPacketType(const IR::ParserState *state) const;
44 void dfsCumulativeTypes(const IR::ParserState *state, const ParserCallGraph &pcg,
45 const cstring &parentType,
46 std::unordered_map<const IR::ParserState *, cstring> &types,
47 std::unordered_set<const IR::ParserState *> &currentPath);
48
49 public:
50 ParserAnalyzer(ParserCallGraph &graph, std::set<cstring> &types)
51 : parserCallGraph(graph), cumulativeTypes(types) {
52 setName("ParserAnalyzer");
53 }
54
55 bool preorder(const IR::P4Parser *parser) override;
56};
57
58class HeaderPacketMetricsPass : public Inspector {
59 private:
60 TypeMap *typeMap;
61 Metrics &metrics;
62 ParserCallGraph parserCallGraph;
63 std::set<cstring> cumulativeTypes;
64 bool insideParserState;
65 bool isValid;
66
67 void updateMetrics(const cstring &headerName, size_t size, bool isModification);
68 size_t getHeaderFieldSize(const IR::Type *type) const;
69 size_t getHeaderSize(const IR::Type_Header *header) const;
70
71 public:
72 HeaderPacketMetricsPass(TypeMap *typeMap, Metrics &metrics)
73 : typeMap(typeMap), metrics(metrics), parserCallGraph("parserCallGraph") {
74 setName("HeaderPacketMetricsPass");
75 }
76
77 bool preorder(const IR::P4Program *program) override;
78 void postorder(const IR::P4Program * /*program*/) override;
79 bool preorder(const IR::AssignmentStatement *assign) override;
80 bool preorder(const IR::MethodCallStatement *mcs) override;
81 bool preorder(const IR::ParserState * /*state*/) override;
82 void postorder(const IR::ParserState * /*state*/) override;
83};
84
85} // namespace P4
86
87#endif /* FRONTENDS_P4_METRICS_HEADERPACKETMETRICS_H_ */
Definition headerPacketMetrics.h:58
Definition visitor.h:413
Definition typeMap.h:41
Definition cstring.h:85
Definition cstring.h:80
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
Definition metricsStructure.h:119