Commit 0562b9b0 authored by Micha Mueller's avatar Micha Mueller
Browse files

Move storeReading() to Sensor and make it public. Break up friendships with PerfCounterGroup

parent 271a187e
......@@ -79,6 +79,10 @@ const reading_t * const Sensor::getCache() const {
return _cache;
}
const reading_t Sensor::getLatestValue() const {
return _latestValue;
}
const std::size_t Sensor::getSizeOfReadingQueue() const {
return _readingQueue->read_available();
}
......@@ -111,6 +115,8 @@ void Sensor::storeReading(reading_t reading) {
_readingQueue->push(reading);
_cache[_cacheIndex] = reading;
_cacheIndex = (_cacheIndex + 1) % _cacheSize;
_latestValue.value = reading.value;
_latestValue.timestamp = reading.timestamp;
}
void Sensor::wait() {
......
......@@ -25,8 +25,6 @@ typedef struct {
*/
class Sensor {
friend class PerfCounterGroup;
public:
Sensor(const std::string name);
......@@ -51,12 +49,15 @@ public:
const reading_t * const getCache() const;
const reading_t getLatestValue() const;
const std::size_t getSizeOfReadingQueue() const;
std::size_t popReadingQueue(reading_t *reads, std::size_t max) const;
void pushReadingQueue(reading_t *reads, std::size_t count) const;
//can be overwritten methods
virtual void init(boost::asio::io_service& io);
virtual void storeReading(reading_t reading);
//have to be overwritten methods
virtual void read() = 0;
......@@ -70,7 +71,6 @@ public:
void wait();
protected:
void storeReading(reading_t reading);
std::string _name;
std::string _mqtt;
......
......@@ -31,8 +31,6 @@ void BACnetSensor::read() {
try {
reading.value = _bacClient->readProperty(_deviceInstance, _objectInstance, _objectType, _propertyId) * _factor;
storeReading(reading);
_latestValue.value = reading.value;
_latestValue.timestamp = reading.timestamp;
#ifdef DEBUG
LOG(debug) << _name << ": \"" << reading.value << "\"";
#endif
......
......@@ -25,8 +25,6 @@ void PDUSensor::read() {
try {
reading.value = _pdu->readValue(_xmlPath);
storeReading(reading);
_latestValue.value = reading.value;
_latestValue.timestamp = reading.timestamp;
#ifdef DEBUG
LOG(debug) << _pdu->getHost() << "::" << _name << ": \"" << reading.value << "\"";
#endif
......
......@@ -49,8 +49,6 @@ void PerfCounter::read() {
LOG(debug) << _name << ": \"" << reading.value << "\"";
#endif
storeReading(reading);
_latestValue.value = count;
_latestValue.timestamp = reading.timestamp;
}
void PerfCounter::readAsync() {
......@@ -108,3 +106,16 @@ void PerfCounter::stopPolling() {
}
LOG(info) << "Sensor " << _name << " stopped.";
}
void PerfCounter::storeReading(reading_t reading) {
_readingQueue->push(reading);
_cache[_cacheIndex] = reading;
_cacheIndex = (_cacheIndex + 1) % _cacheSize;
//want to store absolute values here
if (ULLONG_MAX - _latestValue.value < reading.value) {
_latestValue.value = reading.value - (ULLONG_MAX - _latestValue.value);
} else {
_latestValue.value += reading.value;
}
_latestValue.timestamp = reading.timestamp;
}
......@@ -12,8 +12,6 @@
class PerfCounter : public Sensor{
friend class PerfCounterGroup;
public:
PerfCounter(const std::string& name);
virtual ~PerfCounter();
......@@ -50,6 +48,8 @@ public:
void stopPolling();
void storeReading(reading_t reading);
private:
int _cpuId;
unsigned int _type;
......
......@@ -79,21 +79,19 @@ void PerfCounterGroup::read() {
//iterate over all counters and find the one with matching id
for (unsigned j = 0; j < _sensors.size(); j++) {
if (rf->values[i].id == _ids[j]) {
if (val >= _sensors[j]->_latestValue.value) {
reading.value = val - _sensors[j]->_latestValue.value;
reading_t lV = _sensors[j]->getLatestValue();
if (val >= lV.value) {
reading.value = val - lV.value;
} else {
//the counter overflow since last read
//according to perf_event_open() man-page u64 (=64bit) is used for event values
//--> unsigned long long is usually also 64bit
reading.value = val + (ULLONG_MAX - _sensors[j]->_latestValue.value);
reading.value = val + (ULLONG_MAX - lV.value);
}
#ifdef DEBUG
LOG(debug) << _name << "::" << _sensors[j]->getName() << ": \"" << reading.value << "\"";
#endif
_sensors[j]->storeReading(reading);
//TODO make latestValue assignment in storeReading()
_sensors[j]->_latestValue.value = val;
_sensors[j]->_latestValue.timestamp = reading.timestamp;
break;
}
}
......@@ -128,8 +126,8 @@ void PerfCounterGroup::startPolling() {
memset(&pe, 0, sizeof(struct perf_event_attr));
pe.size = sizeof(struct perf_event_attr);
pe.type = pc->_type;
pe.config = pc->_config;
pe.type = pc->getType();
pe.config = pc->getConfig();
pe.disabled = 1;
pe.exclude_kernel = 0;
pe.exclude_hv = 1;
......@@ -153,8 +151,8 @@ void PerfCounterGroup::startPolling() {
for(; iter != _sensors.end(); iter++) {
pc = (PerfCounter*) (*iter);
int fd;
pe.type = pc->_type;
pe.config = pc->_config;
pe.type = pc->getType();
pe.config = pc->getConfig();
fd = syscall(__NR_perf_event_open, &pe, -1, _cpuId, _fd, 0);
//store id, so that we can match counters with values later (see read())
......
......@@ -26,8 +26,6 @@ void SNMPSensor::read() {
try {
reading.value = _connection->issueGet(_oid, _oidLen);
storeReading(reading);
_latestValue.value = reading.value;
_latestValue.timestamp = reading.timestamp;
#ifdef DEBUG
LOG(debug) << _name << ": \"" << reading.value << "\"";
#endif
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment