PerSystSqlOperator.cpp 9.23 KB
Newer Older
1
//================================================================================
2
// Name        : PerSystSqlOperator.cpp
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Author      : Carla Guillen
// Contact     : info@dcdb.it
// Copyright   : Leibniz Supercomputing Centre
// Description : Template implementing features to use Units in Operators.
//================================================================================

//================================================================================
// This file is part of DCDB (DataCenter DataBase)
// Copyright (C) 2018-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.
//================================================================================

28
29
#include "PerSystSqlOperator.h"

30
31
32
33
34
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/utility/formatting_ostream.hpp>
#include <boost/parameter/keyword.hpp>
#include <stddef.h>
35
#include <cmath>
36
#include <cstdint>
37
#include <memory>
38
#include <string>
39
#include <numeric>
40
41

#include "../../../common/include/logging.h"
42
#include "../../../common/include/sensorbase.h"
43
44
45
#include "../../../common/include/timestamp.h"
#include "../../includes/CommonStatistics.h"
#include "../../includes/QueryEngine.h"
46
#include "../../includes/UnitTemplate.h"
47

48
PerSystSqlOperator::PerSystSqlOperator(const std::string& name) :
49
50
		OperatorTemplate(name), JobOperatorTemplate(name), _number_of_even_quantiles(0),
		_severity_formula(NOFORMULA), _severity_threshold(0), _severity_exponent(0),
Carla Guillen's avatar
Carla Guillen committed
51
		_severity_max_memory(0), _go_back_ns(0) {
52
53
}

54
PerSystSqlOperator::~PerSystSqlOperator() {
55
56
}

57
void PerSystSqlOperator::compute(U_Ptr unit, qeJobData& jobData) {
58
59
60
    // Clearing the buffer, if already allocated
	_buffer.clear();
    size_t elCtr=0;
Carla Guillen's avatar
Carla Guillen committed
61
    uint64_t my_timestamp = getTimestamp() - _go_back_ns;
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    // Making sure that the aggregation boundaries do not go past the job start/end time
    uint64_t jobEnd   = jobData.endTime!=0 && my_timestamp > jobData.endTime ? jobData.endTime : my_timestamp;
    uint64_t jobStart = jobEnd-my_timestamp < jobData.startTime ? jobData.startTime : jobEnd-my_timestamp;
    // Job units are hierarchical, and thus we iterate over all sub-units associated to each single node
    for(const auto& subUnit : unit->getSubUnits()) {
        // Getting the most recent values as specified in _window
        // Since we do not clear the internal buffer, all sensor readings will be accumulated in the same vector
        for(const auto& in : subUnit->getInputs()) {
            elCtr = _buffer.size();
            _queryEngine.querySensor(in->getName(), my_timestamp, my_timestamp, _buffer, false);
            if (_buffer.size() <= elCtr) {
                LOG(debug) << "Job Operator " << _name << " cannot read from sensor " << in->getName() << "!";
                return;
            }
        }
    }
    compute_internal(unit, _buffer);
}

Carla Guillen's avatar
Carla Guillen committed
81
void PerSystSqlOperator::compute_internal(U_Ptr& unit, vector<reading_t>& buffer) {
Carla Guillen Carias's avatar
Carla Guillen Carias committed
82
	_quantileSensors.clear();
83
84
    reading_t reading;
    AggregatorSensorBase::aggregationOps_t op;
Carla Guillen's avatar
Carla Guillen committed
85
    reading.timestamp = getTimestamp() - _go_back_ns;
86
87
88

    std::vector<double> douBuffer;
    punToDoubles(buffer, douBuffer);
89
90
91
92
93
94
    // Performing the actual aggregation operation
    for(const auto& out : unit->getOutputs()) {
        op = out->getOperation();
        if(op!=AggregatorSensorBase::QTL) {
            switch (op) {
                case AggregatorSensorBase::SUM:
95
                    reading.value = punDoubleToLL(std::accumulate(douBuffer.begin(), douBuffer.end(), 0.0));
96
97
                    break;
                case AggregatorSensorBase::AVG:
98
                    reading.value = punDoubleToLL(std::accumulate(douBuffer.begin(), douBuffer.end(), 0.0)/douBuffer.size());
99
100
101
102
                    break;
                case AggregatorSensorBase::OBS:
                    reading.value = computeObs(buffer);
                    break;
103
                case AggregatorSensorBase::AVG_SEV:
104
105
                    reading.value = punDoubleToLL(computeSeverityAverage(douBuffer));
                    break;
106
                default:
107
                    LOG(warning) << _name << ": Operation " << op << " not supported!";
108
109
110
111
                    reading.value = 0;
                    break;
            }
            out->storeReading(reading);
Carla Guillen Carias's avatar
Carla Guillen Carias committed
112
        } else {
Carla Guillen's avatar
Carla Guillen committed
113
            _quantileSensors.push_back(out);
114
115
116
117
        }
    }

    if(!_quantileSensors.empty()) {
118
119
    	vector<double> quantiles;
      	computeEvenQuantiles(douBuffer, _number_of_even_quantiles, quantiles);
120
        for(unsigned idx=0; idx<quantiles.size(); idx++) {
121
            reading.value = punDoubleToLL(quantiles[idx]);
122
123
124
125
126
            _quantileSensors[idx]->storeReading(reading);
        }
    }
}

127
void PerSystSqlOperator::compute(U_Ptr unit){
128
129
//nothing here!
}
Carla Guillen Carias's avatar
Carla Guillen Carias committed
130

131
132
double severity_formula1(double metric, double threshold, double exponent){
	double val = metric - threshold;
Carla Guillen Carias's avatar
Carla Guillen Carias committed
133
	if (val > 0) {
134
		double ret = (pow(val, exponent));
Carla Guillen Carias's avatar
Carla Guillen Carias committed
135
136
137
138
139
140
141
142
		if(ret > 1){
			return 1;
		}
		return ret;
	}
	return 0;
}

143
double severity_formula2(double metric, double threshold, double exponent){
Carla Guillen Carias's avatar
Carla Guillen Carias committed
144
145
146
	if(!threshold){
		return -1;
	}
147
	double val = metric / threshold - 1;
Carla Guillen Carias's avatar
Carla Guillen Carias committed
148
	if (val > 0) {
149
		double ret= (pow(val, exponent));
Carla Guillen Carias's avatar
Carla Guillen Carias committed
150
151
152
153
154
155
156
157
		if(ret > 1){
			return 1;
		}
		return ret;
	}
	return 0;
}

158
double severity_formula3(double metric, double threshold, double exponent){
Carla Guillen Carias's avatar
Carla Guillen Carias committed
159
160
161
	if (!threshold) {
		return -1;
	}
162
	double val = metric / threshold;
Carla Guillen Carias's avatar
Carla Guillen Carias committed
163
	if (val > 0) {
164
		double ret= (1 - pow(val, exponent));
Carla Guillen Carias's avatar
Carla Guillen Carias committed
165
166
167
168
169
170
171
172
173
174
175
		if(ret > 1 ){
			return 1;
		}
		if( ret < 0 ){
			return 0;
		}
		return ret;
	}
	return 0;
}

176
177
178
double severity_memory(double metric, double threshold, double max_memory){
	double denominator = max_memory - threshold;
	double severity = -1;
Carla Guillen Carias's avatar
Carla Guillen Carias committed
179
180
181
182
183
184
185
186
187
188
	if(denominator){
		severity = metric - threshold/(max_memory - threshold);
		if(severity > 1) {
			severity = 1;
		} else if(severity < 0){
			severity = 0;
		}
	}
	return severity;
}
189

190
191
double PerSystSqlOperator::computeSeverityAverage(std::vector<double> & buffer){
	std::vector<double> severities;
192
193
	switch( _severity_formula ) {
		case (FORMULA1):
194
195
			for(auto val : buffer){
				auto severity = severity_formula1(val, _severity_threshold, _severity_exponent);
196
197
198
199
				severities.push_back(severity);
			}
		break;
		case (FORMULA2):
200
201
			for(auto val: buffer){
				auto severity = severity_formula2(val, _severity_threshold, _severity_exponent);
202
203
204
205
				severities.push_back(severity);
			}
		break;
		case (FORMULA3):
206
207
			for(auto val: buffer){
				auto severity = severity_formula3(val, _severity_threshold, _severity_exponent);
208
209
210
211
				severities.push_back(severity);
			}
		break;
		case (MEMORY_FORMULA):
212
213
			for(auto val: buffer){
				auto severity = severity_memory(val, _severity_threshold, _severity_max_memory);
214
215
216
217
				severities.push_back(severity);
			}
		break;
		case (NOFORMULA):
218
219
220
			for(auto val: buffer){
				severities.push_back(severity_noformula());
			}
Carla Guillen Carias's avatar
Carla Guillen Carias committed
221
		break;
222
223
224
225
226
		default:
			return 0.0;
			break;
	}
	if (severities.size()){
227
		return (std::accumulate(severities.begin(),severities.end(), 0.0) / severities.size());
228
229
	}
	return 0.0;
230
}
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277


void punToDoubles(std::vector<reading_t> & buffer, std::vector<double> & outDoubleVec){
	for(auto & reading: buffer){
		outDoubleVec.push_back(punLLToDouble(reading.value));
	}
}

double punLLToDouble(long long value){
        double * returnval;
        returnval = (double *)(&value);
        return *returnval;
}

long long punDoubleToLL(double value){
        long long * returnval;
        returnval = (long long *)(&value);

        return *returnval;
}


void computeEvenQuantiles(std::vector<double> &data, const unsigned int NUMBER_QUANTILES, std::vector<double> &quantiles) {
        if (data.empty() || NUMBER_QUANTILES == 0) {
                return;
        }
        std::sort(data.begin(), data.end());
        int elementNumber = data.size();
        quantiles.resize(NUMBER_QUANTILES + 1); //+min
        double factor = elementNumber/static_cast<double>(NUMBER_QUANTILES);
        quantiles[0] = data[0]; //minimum
        quantiles[NUMBER_QUANTILES] = data[data.size() - 1]; //maximum
        for (unsigned int i = 1; i < NUMBER_QUANTILES; i++) {
                if (elementNumber > 1) {
                        int idx = static_cast<int>(std::floor(i * factor));
                        if(idx == 0){
                                quantiles[i] = data[0];
                        } else {
                                double rest = (i * factor) - idx;
                                quantiles[i] = data[idx - 1] + rest * (data[idx] - data[idx - 1]); //ToDo scaling factor??
                        }
                } else { //optimization, we don't need to calculate all the quantiles
                        quantiles[i] = data[0];
                }
        }
}