2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

sensorcache.h 5.2 KB
Newer Older
1
2
3
//================================================================================
// Name        : sensorcache.h
// Author      : Michael Ott
Micha Müller's avatar
Micha Müller committed
4
// Contact     : info@dcdb.it
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Copyright   : Leibniz Supercomputing Centre
// Description :
//================================================================================

//================================================================================
// This file is part of DCDB (DataCenter DataBase)
// Copyright (C) 2016-2019 Leibniz Supercomputing Centre
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//================================================================================
Alessio Netti's avatar
Alessio Netti committed
27
28
29
30
31

#ifndef COLLECTAGENT_SENSORCACHE_H_
#define COLLECTAGENT_SENSORCACHE_H_

#include <map>
32
#include <atomic>
Alessio Netti's avatar
Alessio Netti committed
33
34
#include <dcdb/sensorid.h>
#include <dcdb/timestamp.h>
35
#include <dcdb/sensordatastore.h>
Alessio Netti's avatar
Alessio Netti committed
36
37
38
39
40
41
#include "cacheentry.h"

using namespace DCDB;

typedef std::map<SensorId, CacheEntry> sensorCache_t;

42
43
44
45
46
/**
 * @brief
 *
 * @ingroup ca
 */
Alessio Netti's avatar
Alessio Netti committed
47
48
49
50
51
52
class SensorCache {
public:
	SensorCache(uint64_t maxHistory=60000000000);
	virtual ~SensorCache();

	/**
53
	* @brief        Returns a reference to the internal sensor cache map.
Alessio Netti's avatar
Alessio Netti committed
54
	**/
55
	sensorCache_t& getSensorMap();
Alessio Netti's avatar
Alessio Netti committed
56
57
58
59
60
61
62
63
64
65
66

	/**
	* @brief        Store a sensor reading in the SensorCache.
	*
	* @param sid    The SensorId of the sensor to be cached.
    * @param ts     The timestamp of the sensor reading.
    * @param val    The actual sensor reading.
	* @return       Returns true if the topic string was valid and the data field of the object was populated.
	**/
	void storeSensor(SensorId sid, uint64_t ts, int64_t val);

67
68
69
70
71
72
73
	/**
	* @brief        Store a sensor reading in the SensorCache.
	*
	* @param s    The SensorDataStoreReading object of the sensor data to be cached.
	**/
	void storeSensor(const SensorDataStoreReading& s);
	
Alessio Netti's avatar
Alessio Netti committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
	/**
    * @brief        Return a sensor reading or the average of the last readings
    *               from the SensorCache.
    *
    * @param sid    The SensorId of the sensor to be looked up in the cache.
    * @param avg    If avg > 0: denotes the length of the average aggregation window in nanoseconds.
    * @return       If avg == 0 :The sensor reading of the corresponding cache entry.
    *               If avg > 0 the average of the last readings is returned.
    * @throws       std::invalid_argument if the SensorId doesn't exist in the SensorCache.
    * @throws       std::out_of_range if the sid was found in the cache entry but is outdated.
    **/
	int64_t getSensor(SensorId sid, uint64_t avg=0);

	/**
	* @brief        Return a sensor reading or the average of the last readings
    *               from the SensorCache.
	*
	* @param topic  The topic of the sensor to be looked up in the cache. May contain wildcards.
	* @param avg    If avg > 0: denotes the length of the average aggregation window in nanoseconds.
	* @return       If avg == 0 :The sensor reading of the corresponding cache entry.
    *               If avg > 0 the average of the last readings is returned.
	* @throws       std::invalid_argument if the topic couldn't be found in the SensorCache.
	* @throws       std::out_of_range if the topic was found in the cache entry but is outdated.
	**/
98
//	int64_t getSensor(std::string topic, uint64_t avg=0);
Alessio Netti's avatar
Alessio Netti committed
99
100
101
102
103
104
105
106
107

	/**
    * @brief        Dump the contents of the SensorCache to stdout.
    **/
	void dump();

	/**
	* @brief        Removes all obsolete entries from the cache
	*
108
	* @details      entries in the cache whose latest sensor reading is older than "now" - t nanoseconds are
Alessio Netti's avatar
Alessio Netti committed
109
110
111
112
113
114
115
	*				removed.
	*
	* @param t		The threshold in nanoseconds for entries that must be removed
	* @return		The number of purged cache entries
	**/
	uint64_t clean(uint64_t t);

116
117
118
119
120
	/**
	* @brief               Waits for internal updates to finish.
	*/
	const void wait() {
		while(_updating.load()) {}
121
122
123
124
125
126
127
128
		++_access;
	}

	/**
	* @brief               Reduces the internal reading counter.
	*/
	const void release() {
		--_access;
129
130
	}

Alessio Netti's avatar
Alessio Netti committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
    /**
    * @brief  Set a new maximum cache length.
    *
    * @param maxHistory: new sensor cache length value.
    **/
	void setMaxHistory(uint64_t maxHistory) { this->_maxHistory = maxHistory; }

    /**
    * @brief  Returns the current maximum sensor cache length
    *
    * @return Current maximum sensor cache length
    */
	uint64_t getMaxHistory() { return this->_maxHistory; }

private:
	// Map containing the single sensor caches
	sensorCache_t sensorCache;
	// Global maximum allowed time frame for the sensor caches
	uint64_t _maxHistory;
150
151
	// Spinlock to regulate map modifications
	std::atomic<bool> _updating;
152
	std::atomic<int> _access;
Alessio Netti's avatar
Alessio Netti committed
153
154
155
156

};

#endif /* COLLECTAGENT_SENSORCACHE_H_ */