PerfSensorBase.h 1.96 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/*
 * PerfSensorBase.h
 *
 *  Created on: 09.08.2018
 *      Author: Micha Mueller
 */

#ifndef PERFEVENT_PERFSENSORBASE_H_
#define PERFEVENT_PERFSENSORBASE_H_

11
#include "../../includes/SensorBase.h"
12
13
#include <limits.h>

14
class PerfSensorBase : public SensorBase {
15
public:
16
	static constexpr int64_t MAXCOUNTERVALUE = LLONG_MAX; //ToDo check if it is not LLONG_MAX, or make it configurable!
17

18
19
20
	PerfSensorBase(const std::string& name) :
		SensorBase(name),
		_type(0),
21
		_config(0),
22
23
24
25
		_lastRawPerf(0) {
	  //default delta to true, as perfevent has only monotonic sensors usually
	  _delta = true;
	}
26
27
28
29
30
31
32
33

	virtual ~PerfSensorBase() {}

	unsigned getType() 		const { return _type; }
	unsigned getConfig()	const { return _config; }

	void setType(unsigned type)		{ _type = type; }
	void setConfig(unsigned config)	{ _config = config;	}
34
35
36
37
38
39
40
41
42
43
44
45
46
47
	void setLastRaw(uint64_t raw)   { _lastRawPerf = raw; }

	void storeReading(uint64_t value, uint64_t timestamp, double factor=1.0, unsigned long long maxValue=ULLONG_MAX) {
        _latestValue.timestamp = timestamp;
        if( _delta ) {
            if (value < _lastRawPerf)
                _latestValue.value = (value + (maxValue - _lastRawPerf)) * factor;
            else
                _latestValue.value = (value - _lastRawPerf) * factor;
            _lastRawPerf = value;
        }
        else
            _latestValue.value = value * factor;

48
49
50
51
52
53
54
55
56
		if (!(_skipConstVal && (_latestValue.value == _lastSentValue.value)) && _subsamplingIndex++ % _subsamplingFactor == 0) {
			_readingQueue->push(_latestValue);
			_lastSentValue = _latestValue;
		}
		if (_sinkFile) {
			_sinkFile->seekp(0, std::ios::beg);
			*_sinkFile << _latestValue.value;
		}

57
58
59
60
        _readingQueue->push(_latestValue);
        _cache[_cacheIndex] = _latestValue;
        _cacheIndex = (_cacheIndex + 1) % _cacheSize;
    }
61
62
63
64

protected:
	unsigned int _type;
	unsigned int _config;
65
	uint64_t     _lastRawPerf;
66
67
};

68
69
using PerfSBPtr = std::shared_ptr<PerfSensorBase>;

70
#endif /* PERFEVENT_PERFSENSORBASE_H_ */