bmv2
Designing your own switch target with bmv2
Loading...
Searching...
No Matches
counters.h
Go to the documentation of this file.
1/* Copyright 2013-present Barefoot Networks, Inc.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16/*
17 * Antonin Bas (antonin@barefootnetworks.com)
18 *
19 */
20
34
35#ifndef BM_BM_SIM_COUNTERS_H_
36#define BM_BM_SIM_COUNTERS_H_
37
38#include <atomic>
39#include <iosfwd>
40#include <string>
41#include <utility>
42#include <vector>
43
44#include "named_p4object.h"
45#include "packet.h"
46
47namespace bm {
48
53class Counter {
54 public:
56 using counter_value_t = uint64_t;
57
58 enum CounterErrorCode {
59 SUCCESS = 0,
60 INVALID_COUNTER_NAME,
61 INVALID_INDEX,
62 ERROR
63 };
64
66 void increment_counter(const Packet &pkt) {
67 bytes += pkt.get_ingress_length();
68 packets += 1;
69 }
70
71 CounterErrorCode query_counter(counter_value_t *bytes,
72 counter_value_t *packets) const;
73
74 CounterErrorCode reset_counter();
75
76 // in case something more general than reset is needed
77 CounterErrorCode write_counter(counter_value_t bytes,
78 counter_value_t packets);
79
80 void serialize(std::ostream *out) const;
81 void deserialize(std::istream *in);
82
83 private:
84 std::atomic<std::uint_fast64_t> bytes{0u};
85 std::atomic<std::uint_fast64_t> packets{0u};
86};
87
88using meter_array_id_t = p4object_id_t;
89
101 public:
102 using CounterErrorCode = Counter::CounterErrorCode;
103
104 using iterator = std::vector<Counter>::iterator;
105 using const_iterator = std::vector<Counter>::const_iterator;
106
107 public:
108 CounterArray(const std::string &name, p4object_id_t id, size_t size)
109 : NamedP4Object(name, id), counters(size) { }
110
111 CounterErrorCode reset_counters();
112
114 Counter &get_counter(size_t idx) {
115 return counters[idx];
116 }
117
119 const Counter &get_counter(size_t idx) const {
120 return counters[idx];
121 }
122
124 Counter &operator[](size_t idx) {
125 assert(idx < size());
126 return counters[idx];
127 }
128
130 const Counter &operator[](size_t idx) const {
131 assert(idx < size());
132 return counters[idx];
133 }
134
135 // iterators
136
138 iterator begin() { return counters.begin(); }
139
141 const_iterator begin() const { return counters.begin(); }
142
144 iterator end() { return counters.end(); }
145
147 const_iterator end() const { return counters.end(); }
148
150 size_t size() const { return counters.size(); }
151
152 void reset_state() { reset_counters(); }
153
154 private:
155 std::vector<Counter> counters;
156};
157
158} // namespace bm
159
160#endif // BM_BM_SIM_COUNTERS_H_
Definition counters.h:100
Counter & get_counter(size_t idx)
Access the counter at position idx, asserts if bad idx.
Definition counters.h:114
const Counter & operator[](size_t idx) const
Access the counter at position idx, asserts if bad idx.
Definition counters.h:130
const Counter & get_counter(size_t idx) const
Access the counter at position idx, asserts if bad idx.
Definition counters.h:119
iterator begin()
NC.
Definition counters.h:138
iterator end()
NC.
Definition counters.h:144
const_iterator begin() const
NC.
Definition counters.h:141
size_t size() const
Return the size of the CounterArray (i.e. number of counters it includes)
Definition counters.h:150
const_iterator end() const
NC.
Definition counters.h:147
Counter & operator[](size_t idx)
Access the counter at position idx, asserts if bad idx.
Definition counters.h:124
Definition counters.h:53
void increment_counter(const Packet &pkt)
Increments both counter values (bytes and packets)
Definition counters.h:66
uint64_t counter_value_t
A counter value (measuring bytes or packets) is a uint64_t.
Definition counters.h:56
Definition named_p4object.h:39
Definition packet.h:98
int get_ingress_length() const
Definition packet.h:151