PerfSensorBase.h 3.83 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 "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
	  //default delta to true, as perfevent has only monotonic sensors usually
	  _delta = true;
	}
25

26
27
28
	PerfSensorBase(const PerfSensorBase& other) :
	  SensorBase(other),
	  _type(other._type),
29
	  _config(other._config) {}
30

31
32
	virtual ~PerfSensorBase() {}

33
34
35
36
37
38
39
40
	PerfSensorBase& operator=(const PerfSensorBase& other) {
	  SensorBase::operator=(other);
	  _type = other._type;
	  _config = other._config;

	  return *this;
	}

41
42
43
44
45
46
	unsigned getType() 		const { return _type; }
	unsigned getConfig()	const { return _config; }

	void setType(unsigned type)		{ _type = type; }
	void setConfig(unsigned config)	{ _config = config;	}

47
48
49
50
	void printConfig(LOG_LEVEL ll, LOGGER& lg, unsigned leadingSpaces=16) {
		std::string leading(leadingSpaces, ' ');
        LOG_VAR(ll) << leading << "    Type:   " <<  _type;
        LOG_VAR(ll) << leading << "    Config: " << _config;
Micha Mueller's avatar
Micha Mueller committed
51
52
    }

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
	/**
	 * Required for hyper-threading aggregation feature.
	 *
	 * FIXME: logic should be kept in sync with storeReading of common sensorbase
	 *
	 * Store reading within the sensor, but do not put it in the readingQueue
	 * so the reading does not get pushed but the caches are still updated.
	 */
	void storeReadingLocal(ureading_t rawReading, double factor=1.0, unsigned long long maxValue=ULLONG_MAX) {
        reading_t reading;
        reading.timestamp = rawReading.timestamp;
        if( _delta ) {
            if (!_firstReading) {
              if (rawReading.value < _lastRawUValue.value)
                  reading.value = (rawReading.value + (maxValue - _lastRawUValue.value)) * factor;
              else
                  reading.value = (rawReading.value - _lastRawUValue.value) * factor;
            } else {
              _firstReading = false;
              _lastRawUValue = rawReading;
              return;
            }
            _lastRawUValue = rawReading;
        }
        else
            reading.value = rawReading.value * factor;

        if (_sinkFile) {
            try {
                _sinkFile->seekp(0, std::ios::beg);
                *_sinkFile << reading.value << std::endl;
            } catch(const std::exception &e) { _sinkFile->close(); _sinkFile.reset(nullptr); }
        }

        _cache->store(reading);
        _latestValue = reading;
    }

	/**
	 * Required for hyper-threading aggregation feature.
	 *
	 * FIXME: logic should be kept in sync with storeReading of common sensorbase
	 *
	 * Store the reading in the readingQueue so it can get pushed.
	 */
	void storeReadingGlobal(reading_t reading) {
        if( _delta )
            // If in delta mode, _accumulator acts as a buffer, summing all deltas for the subsampling period
            _accumulator.value += reading.value;
        else
            _accumulator.value = reading.value;

        if (_subsamplingIndex++ % _subsamplingFactor == 0) {
            _accumulator.timestamp = reading.timestamp;
            //TODO: if sensor starts with values of 0, these won't be pushed. This should be fixed
            if( !(_skipConstVal && (_accumulator.value == _lastSentValue.value)) ) {
                _readingQueue->push(_accumulator);
                _lastSentValue = _accumulator;
            }
            // We reset the accumulator's value for the correct accumulation of deltas
            _accumulator.value = 0;
        }
    }

117
118
119
120
121
protected:
	unsigned int _type;
	unsigned int _config;
};

122
123
using PerfSBPtr = std::shared_ptr<PerfSensorBase>;

124
#endif /* PERFEVENT_PERFSENSORBASE_H_ */