2019-02-25 08:48:22 -05:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2012-07-09 14:32:02 -04:00
|
|
|
|
2012-06-28 09:43:49 -04:00
|
|
|
#ifndef RINGBUFFER_H
|
|
|
|
|
#define RINGBUFFER_H
|
|
|
|
|
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "base/i2-base.hpp"
|
|
|
|
|
#include "base/object.hpp"
|
2013-03-16 16:18:53 -04:00
|
|
|
#include <vector>
|
2021-02-02 04:16:04 -05:00
|
|
|
#include <mutex>
|
2013-03-16 16:18:53 -04:00
|
|
|
|
2012-06-28 09:43:49 -04:00
|
|
|
namespace icinga
|
|
|
|
|
{
|
|
|
|
|
|
2012-09-17 07:35:55 -04:00
|
|
|
/**
|
|
|
|
|
* A ring buffer that holds a pre-defined number of integers.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup base
|
|
|
|
|
*/
|
2018-01-10 10:44:48 -05:00
|
|
|
class RingBuffer final
|
2012-06-28 09:43:49 -04:00
|
|
|
{
|
|
|
|
|
public:
|
2014-11-07 06:32:25 -05:00
|
|
|
DECLARE_PTR_TYPEDEFS(RingBuffer);
|
2013-03-01 06:07:52 -05:00
|
|
|
|
2013-03-16 16:18:53 -04:00
|
|
|
typedef std::vector<int>::size_type SizeType;
|
2012-06-28 09:43:49 -04:00
|
|
|
|
2012-08-07 15:02:12 -04:00
|
|
|
RingBuffer(SizeType slots);
|
|
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
SizeType GetLength() const;
|
2012-08-07 15:02:12 -04:00
|
|
|
void InsertValue(SizeType tv, int num);
|
2017-11-13 10:17:59 -05:00
|
|
|
int UpdateAndGetValues(SizeType tv, SizeType span);
|
2017-11-14 05:03:05 -05:00
|
|
|
double CalculateRate(SizeType tv, SizeType span);
|
2012-06-28 09:43:49 -04:00
|
|
|
|
|
|
|
|
private:
|
2021-02-02 04:16:04 -05:00
|
|
|
mutable std::mutex m_Mutex;
|
2013-03-16 16:18:53 -04:00
|
|
|
std::vector<int> m_Slots;
|
2013-03-06 05:03:50 -05:00
|
|
|
SizeType m_TimeValue;
|
2017-11-14 05:03:05 -05:00
|
|
|
SizeType m_InsertedValues;
|
2018-01-10 10:44:48 -05:00
|
|
|
|
|
|
|
|
void InsertValueUnlocked(SizeType tv, int num);
|
|
|
|
|
int UpdateAndGetValuesUnlocked(SizeType tv, SizeType span);
|
2012-06-28 09:43:49 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif /* RINGBUFFER_H */
|