bmv2
Designing your own switch target with bmv2
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 
47 namespace bm {
48 
53 class 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 
88 using meter_array_id_t = p4object_id_t;
89 
100 class CounterArray : public NamedP4Object {
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_
bm::NamedP4Object
Definition: named_p4object.h:39
bm::Counter::increment_counter
void increment_counter(const Packet &pkt)
Increments both counter values (bytes and packets)
Definition: counters.h:66
bm::Counter::counter_value_t
uint64_t counter_value_t
A counter value (measuring bytes or packets) is a uint64_t.
Definition: counters.h:56
bm::CounterArray::operator[]
Counter & operator[](size_t idx)
Access the counter at position idx, asserts if bad idx.
Definition: counters.h:124
bm::CounterArray::begin
iterator begin()
NC.
Definition: counters.h:138
named_p4object.h
bm::Packet
Definition: packet.h:98
bm::CounterArray::end
iterator end()
NC.
Definition: counters.h:144
bm::CounterArray::get_counter
const Counter & get_counter(size_t idx) const
Access the counter at position idx, asserts if bad idx.
Definition: counters.h:119
bm::CounterArray::begin
const_iterator begin() const
NC.
Definition: counters.h:141
bm::Counter
Definition: counters.h:53
packet.h
bm::Packet::get_ingress_length
int get_ingress_length() const
Definition: packet.h:151
bm::CounterArray::size
size_t size() const
Return the size of the CounterArray (i.e. number of counters it includes)
Definition: counters.h:150
bm::CounterArray::end
const_iterator end() const
NC.
Definition: counters.h:147
bm::CounterArray::get_counter
Counter & get_counter(size_t idx)
Access the counter at position idx, asserts if bad idx.
Definition: counters.h:114
bm::CounterArray::operator[]
const Counter & operator[](size_t idx) const
Access the counter at position idx, asserts if bad idx.
Definition: counters.h:130
bm::CounterArray
Definition: counters.h:100