blob: eeb98dc64a68050b4e2f6337952ac0d858de2989 [file] [log] [blame]
Junxiao Shi57df2882015-11-11 06:12:35 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento2cae8ca2019-04-18 20:48:05 -04002/*
Davide Pesavento8f0b8b62023-08-29 21:04:08 -04003 * Copyright (c) 2014-2023, Regents of the University of California,
Junxiao Shi57df2882015-11-11 06:12:35 -07004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040026#ifndef NFD_DAEMON_COMMON_COUNTER_HPP
27#define NFD_DAEMON_COMMON_COUNTER_HPP
Junxiao Shi57df2882015-11-11 06:12:35 -070028
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040029#include "core/common.hpp"
Junxiao Shi57df2882015-11-11 06:12:35 -070030
31namespace nfd {
32
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040033/**
34 * \brief Represents a counter that encloses an integer value.
Junxiao Shi57df2882015-11-11 06:12:35 -070035 *
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040036 * SimpleCounter is noncopyable, because increment should be called on the counter,
Davide Pesavento8f0b8b62023-08-29 21:04:08 -040037 * not a copy of it. It's implicitly convertible to an integral type to be observed.
Junxiao Shi57df2882015-11-11 06:12:35 -070038 */
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040039class SimpleCounter : noncopyable
Junxiao Shi57df2882015-11-11 06:12:35 -070040{
41public:
Davide Pesavento8f0b8b62023-08-29 21:04:08 -040042 using rep = uint64_t;
Junxiao Shi57df2882015-11-11 06:12:35 -070043
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040044 /**
Davide Pesavento8f0b8b62023-08-29 21:04:08 -040045 * \brief Return the counter's value.
Junxiao Shi57df2882015-11-11 06:12:35 -070046 */
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040047 operator rep() const noexcept
Junxiao Shi57df2882015-11-11 06:12:35 -070048 {
49 return m_value;
50 }
51
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040052 /**
53 * \brief Replace the counter's value.
Junxiao Shi57df2882015-11-11 06:12:35 -070054 */
55 void
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040056 set(rep value) noexcept
Junxiao Shi57df2882015-11-11 06:12:35 -070057 {
58 m_value = value;
59 }
60
61protected:
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040062 rep m_value = 0;
Junxiao Shi57df2882015-11-11 06:12:35 -070063};
64
Davide Pesavento8f0b8b62023-08-29 21:04:08 -040065/**
66 * \brief Represents a counter of number of packets.
Junxiao Shi57df2882015-11-11 06:12:35 -070067 *
Davide Pesavento8f0b8b62023-08-29 21:04:08 -040068 * \warning The counter value may wrap after exceeding the range of the underlying integer type.
Junxiao Shi57df2882015-11-11 06:12:35 -070069 */
70class PacketCounter : public SimpleCounter
71{
72public:
Davide Pesavento8f0b8b62023-08-29 21:04:08 -040073 /**
74 * \brief Increment the counter by one.
Junxiao Shi57df2882015-11-11 06:12:35 -070075 */
76 PacketCounter&
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040077 operator++() noexcept
Junxiao Shi57df2882015-11-11 06:12:35 -070078 {
79 ++m_value;
80 return *this;
81 }
Junxiao Shi57df2882015-11-11 06:12:35 -070082};
83
Davide Pesavento8f0b8b62023-08-29 21:04:08 -040084/**
85 * \brief Represents a counter of number of bytes.
Junxiao Shi57df2882015-11-11 06:12:35 -070086 *
Davide Pesavento8f0b8b62023-08-29 21:04:08 -040087 * \warning The counter value may wrap after exceeding the range of the underlying integer type.
Junxiao Shi57df2882015-11-11 06:12:35 -070088 */
89class ByteCounter : public SimpleCounter
90{
91public:
Davide Pesavento8f0b8b62023-08-29 21:04:08 -040092 /**
93 * \brief Increase the counter.
Junxiao Shi57df2882015-11-11 06:12:35 -070094 */
95 ByteCounter&
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040096 operator+=(rep n) noexcept
Junxiao Shi57df2882015-11-11 06:12:35 -070097 {
98 m_value += n;
99 return *this;
100 }
101};
102
Davide Pesavento8f0b8b62023-08-29 21:04:08 -0400103/**
104 * \brief Provides a counter that observes the size of a table.
105 * \tparam T a type that provides a `size()` const member function
Eric Newberry73bcad32017-04-25 17:57:35 -0700106 *
Davide Pesavento8f0b8b62023-08-29 21:04:08 -0400107 * If the table is not specified in the constructor, it can be added later by calling observe().
Junxiao Shi57df2882015-11-11 06:12:35 -0700108 */
109template<typename T>
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400110class SizeCounter : noncopyable
Junxiao Shi57df2882015-11-11 06:12:35 -0700111{
112public:
Davide Pesavento8f0b8b62023-08-29 21:04:08 -0400113 using rep = size_t;
Junxiao Shi57df2882015-11-11 06:12:35 -0700114
Eric Newberry73bcad32017-04-25 17:57:35 -0700115 explicit constexpr
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400116 SizeCounter(const T* table = nullptr) noexcept
Junxiao Shi57df2882015-11-11 06:12:35 -0700117 : m_table(table)
118 {
119 }
120
Eric Newberry73bcad32017-04-25 17:57:35 -0700121 void
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400122 observe(const T* table) noexcept
Eric Newberry73bcad32017-04-25 17:57:35 -0700123 {
124 m_table = table;
125 }
126
Davide Pesavento8f0b8b62023-08-29 21:04:08 -0400127 /**
128 * \brief Return the counter's value, i.e., the current size of the table being observed.
Junxiao Shi57df2882015-11-11 06:12:35 -0700129 */
Davide Pesavento8f0b8b62023-08-29 21:04:08 -0400130 operator rep() const
Junxiao Shi57df2882015-11-11 06:12:35 -0700131 {
Eric Newberry73bcad32017-04-25 17:57:35 -0700132 BOOST_ASSERT(m_table != nullptr);
133 return m_table->size();
Junxiao Shi57df2882015-11-11 06:12:35 -0700134 }
135
136private:
Davide Pesavento8f0b8b62023-08-29 21:04:08 -0400137 const T* m_table = nullptr;
Junxiao Shi57df2882015-11-11 06:12:35 -0700138};
139
140} // namespace nfd
141
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400142#endif // NFD_DAEMON_COMMON_COUNTER_HPP