ZeroTierOne/node/RingBuffer.hpp

337 lines
7.4 KiB
C++
Raw Normal View History

2018-05-01 23:32:15 +00:00
/*
2020-05-12 08:35:48 +00:00
* Copyright (c)2013-2020 ZeroTier, Inc.
2018-05-01 23:32:15 +00:00
*
2019-08-23 16:23:39 +00:00
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file in the project's root directory.
2018-05-01 23:32:15 +00:00
*
2020-05-12 08:35:48 +00:00
* Change Date: 2024-01-01
2018-05-01 23:32:15 +00:00
*
2019-08-23 16:23:39 +00:00
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2.0 of the Apache License.
2018-05-01 23:32:15 +00:00
*/
2019-08-23 16:23:39 +00:00
/****/
2018-05-01 23:32:15 +00:00
#ifndef ZT_RINGBUFFER_H
#define ZT_RINGBUFFER_H
#include <typeinfo>
#include <cstdint>
#include <stdlib.h>
#include <memory.h>
#include <algorithm>
#include <math.h>
namespace ZeroTier {
/**
* A circular buffer
2018-05-01 23:32:15 +00:00
*
* For fast handling of continuously-evolving variables (such as path quality metrics).
2019-08-23 16:23:39 +00:00
* Using this, we can maintain longer sliding historical windows for important path
2018-05-01 23:32:15 +00:00
* metrics without the need for potentially expensive calls to memcpy/memmove.
*
* Some basic statistical functionality is implemented here in an attempt
* to reduce the complexity of code needed to interact with this type of buffer.
*/
template <class T,size_t S>
2018-05-01 23:32:15 +00:00
class RingBuffer
{
private:
T buf[S];
2018-05-01 23:32:15 +00:00
size_t begin;
size_t end;
bool wrap;
public:
RingBuffer() :
2018-05-01 23:32:15 +00:00
begin(0),
end(0),
wrap(false)
{
memset(buf,0,sizeof(T)*S);
}
2018-05-01 23:32:15 +00:00
/**
* @return A pointer to the underlying buffer
*/
inline T *get_buf()
2018-05-01 23:32:15 +00:00
{
return buf + begin;
}
2019-08-23 16:23:39 +00:00
/**
2018-05-01 23:32:15 +00:00
* Adjust buffer index pointer as if we copied data in
* @param n Number of elements to copy in
* @return Number of elements we copied in
*/
inline size_t produce(size_t n)
2018-05-01 23:32:15 +00:00
{
n = std::min(n, getFree());
if (n == 0) {
return n;
}
const size_t first_chunk = std::min(n, S - end);
end = (end + first_chunk) % S;
2018-05-01 23:32:15 +00:00
if (first_chunk < n) {
const size_t second_chunk = n - first_chunk;
end = (end + second_chunk) % S;
2018-05-01 23:32:15 +00:00
}
if (begin == end) {
wrap = true;
}
return n;
}
2019-08-23 16:23:39 +00:00
/**
* Fast erase, O(1).
2018-05-01 23:32:15 +00:00
* Merely reset the buffer pointer, doesn't erase contents
*/
inline void reset() { consume(count()); }
2018-05-01 23:32:15 +00:00
2019-08-23 16:23:39 +00:00
/**
2018-05-01 23:32:15 +00:00
* adjust buffer index pointer as if we copied data out
* @param n Number of elements we copied from the buffer
* @return Number of elements actually available from the buffer
*/
inline size_t consume(size_t n)
2018-05-01 23:32:15 +00:00
{
n = std::min(n, count());
if (n == 0) {
return n;
}
if (wrap) {
wrap = false;
}
const size_t first_chunk = std::min(n, S - begin);
begin = (begin + first_chunk) % S;
2018-05-01 23:32:15 +00:00
if (first_chunk < n) {
const size_t second_chunk = n - first_chunk;
begin = (begin + second_chunk) % S;
2018-05-01 23:32:15 +00:00
}
return n;
}
/**
* @param data Buffer that is to be written to the ring
* @param n Number of elements to write to the buffer
*/
inline size_t write(const T * data, size_t n)
2018-05-01 23:32:15 +00:00
{
n = std::min(n, getFree());
if (n == 0) {
return n;
}
const size_t first_chunk = std::min(n, S - end);
2018-05-01 23:32:15 +00:00
memcpy(buf + end, data, first_chunk * sizeof(T));
end = (end + first_chunk) % S;
2018-05-01 23:32:15 +00:00
if (first_chunk < n) {
const size_t second_chunk = n - first_chunk;
memcpy(buf + end, data + first_chunk, second_chunk * sizeof(T));
end = (end + second_chunk) % S;
2018-05-01 23:32:15 +00:00
}
if (begin == end) {
wrap = true;
}
return n;
}
/**
* Place a single value on the buffer. If the buffer is full, consume a value first.
*
* @param value A single value to be placed in the buffer
*/
inline void push(const T value)
2018-05-01 23:32:15 +00:00
{
if (count() == S) {
2018-05-01 23:32:15 +00:00
consume(1);
}
const size_t first_chunk = std::min((size_t)1, S - end);
*(buf + end) = value;
end = (end + first_chunk) % S;
if (begin == end) {
wrap = true;
}
2018-05-01 23:32:15 +00:00
}
/**
* @return The most recently pushed element on the buffer
*/
inline T get_most_recent() { return *(buf + end); }
2018-05-01 23:32:15 +00:00
/**
* @param dest Destination buffer
* @param n Size (in terms of number of elements) of the destination buffer
* @return Number of elements read from the buffer
*/
inline size_t read(T *dest,size_t n)
2018-05-01 23:32:15 +00:00
{
n = std::min(n, count());
if (n == 0) {
return n;
}
if (wrap) {
wrap = false;
}
const size_t first_chunk = std::min(n, S - begin);
2018-05-01 23:32:15 +00:00
memcpy(dest, buf + begin, first_chunk * sizeof(T));
begin = (begin + first_chunk) % S;
2018-05-01 23:32:15 +00:00
if (first_chunk < n) {
const size_t second_chunk = n - first_chunk;
memcpy(dest + first_chunk, buf + begin, second_chunk * sizeof(T));
begin = (begin + second_chunk) % S;
2018-05-01 23:32:15 +00:00
}
return n;
}
/**
* Return how many elements are in the buffer, O(1).
*
* @return The number of elements in the buffer
*/
inline size_t count()
2018-05-01 23:32:15 +00:00
{
if (end == begin) {
return wrap ? S : 0;
2018-05-01 23:32:15 +00:00
}
else if (end > begin) {
return end - begin;
}
else {
return S + end - begin;
2018-05-01 23:32:15 +00:00
}
}
/**
* @return The number of slots that are unused in the buffer
*/
inline size_t getFree() { return S - count(); }
2018-05-01 23:32:15 +00:00
/**
* @return The arithmetic mean of the contents of the buffer
*/
inline float mean()
2018-05-01 23:32:15 +00:00
{
size_t iterator = begin;
float subtotal = 0;
size_t curr_cnt = count();
for (size_t i=0; i<curr_cnt; i++) {
iterator = (iterator + S - 1) % curr_cnt;
subtotal += (float)*(buf + iterator);
2018-05-01 23:32:15 +00:00
}
return curr_cnt ? subtotal / (float)curr_cnt : 0;
2018-05-01 23:32:15 +00:00
}
/**
* @return The arithmetic mean of the most recent 'n' elements of the buffer
2018-05-01 23:32:15 +00:00
*/
inline float mean(size_t n)
{
n = n < S ? n : S;
size_t iterator = begin;
float subtotal = 0;
size_t curr_cnt = count();
for (size_t i=0; i<n; i++) {
iterator = (iterator + S - 1) % curr_cnt;
subtotal += (float)*(buf + iterator);
}
return curr_cnt ? subtotal / (float)curr_cnt : 0;
}
2020-05-12 08:35:48 +00:00
/**
* @return The sum of the contents of the buffer
*/
inline float sum()
{
size_t iterator = begin;
float total = 0;
size_t curr_cnt = count();
for (size_t i=0; i<curr_cnt; i++) {
iterator = (iterator + S - 1) % curr_cnt;
total += (float)*(buf + iterator);
}
return total;
}
/**
* @return The sample standard deviation of element values
*/
inline float stddev() { return sqrt(variance()); }
/**
* @return The variance of element values
*/
inline float variance()
2018-05-01 23:32:15 +00:00
{
size_t iterator = begin;
float cached_mean = mean();
size_t curr_cnt = count();
T sum_of_squared_deviations = 0;
for (size_t i=0; i<curr_cnt; i++) {
iterator = (iterator + S - 1) % curr_cnt;
float deviation = (buf[i] - cached_mean);
2019-06-13 17:27:54 +00:00
sum_of_squared_deviations += (T)(deviation*deviation);
2018-05-01 23:32:15 +00:00
}
float variance = (float)sum_of_squared_deviations / (float)(S - 1);
return variance;
2018-05-01 23:32:15 +00:00
}
/**
* @return The number of elements of zero value
2018-05-01 23:32:15 +00:00
*/
inline size_t zeroCount()
2018-05-01 23:32:15 +00:00
{
size_t iterator = begin;
size_t zeros = 0;
size_t curr_cnt = count();
for (size_t i=0; i<curr_cnt; i++) {
iterator = (iterator + S - 1) % curr_cnt;
2018-05-01 23:32:15 +00:00
if (*(buf + iterator) == 0) {
zeros++;
}
}
return zeros;
}
/**
* @param value Value to match against in buffer
* @return The number of values held in the ring buffer which match a given value
*/
inline size_t countValue(T value)
2018-05-01 23:32:15 +00:00
{
size_t iterator = begin;
size_t cnt = 0;
size_t curr_cnt = count();
for (size_t i=0; i<curr_cnt; i++) {
iterator = (iterator + S - 1) % curr_cnt;
2018-05-01 23:32:15 +00:00
if (*(buf + iterator) == value) {
cnt++;
2018-05-01 23:32:15 +00:00
}
}
return cnt;
2018-05-01 23:32:15 +00:00
}
/**
* Print the contents of the buffer
*/
/*
inline void dump()
2018-05-01 23:32:15 +00:00
{
size_t iterator = begin;
for (size_t i=0; i<S; i++) {
iterator = (iterator + S - 1) % S;
2018-05-01 23:32:15 +00:00
if (typeid(T) == typeid(int)) {
2020-05-12 08:35:48 +00:00
fprintf(stderr, "buf[%2zu]=%2d\n", iterator, (int)*(buf + iterator));
2018-05-01 23:32:15 +00:00
}
else {
2020-05-12 08:35:48 +00:00
fprintf(stderr, "buf[%2zu]=%2f\n", iterator, (float)*(buf + iterator));
2018-05-01 23:32:15 +00:00
}
}
}
*/
2018-05-01 23:32:15 +00:00
};
} // namespace ZeroTier
#endif