PerSystSqlOperator.cpp 7.68 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
39
40
#include <string>

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

47
PerSystSqlOperator::PerSystSqlOperator(const std::string& name) :
48
49
50
		OperatorTemplate(name), JobOperatorTemplate(name), _number_of_even_quantiles(0),
		_severity_formula(NOFORMULA), _severity_threshold(0), _severity_exponent(0),
		_severity_max_memory(0) {
51
52
}

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

56
void PerSystSqlOperator::compute(U_Ptr unit, qeJobData& jobData) {
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    // Clearing the buffer, if already allocated
	_buffer.clear();
    size_t elCtr=0;
    uint64_t my_timestamp = getTimestamp(); //TODO minus 10 seconds or so...
    // 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);
}

80
void PerSystSqlOperator::compute_internal(U_Ptr unit, vector<reading_t> buffer) {
Carla Guillen Carias's avatar
Carla Guillen Carias committed
81
	_quantileSensors.clear();
82
83
    reading_t reading;
    AggregatorSensorBase::aggregationOps_t op;
84
    reading.timestamp = getTimestamp() - 10e9;
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
    // Performing the actual aggregation operation
    for(const auto& out : unit->getOutputs()) {
        op = out->getOperation();
        if(op!=AggregatorSensorBase::QTL) {
            switch (op) {
                case AggregatorSensorBase::SUM:
                    reading.value = computeSum(buffer);
                    break;
                case AggregatorSensorBase::AVG:
                    reading.value = computeAvg(buffer);
                    break;
                case AggregatorSensorBase::MIN:
                    reading.value = computeMin(buffer);
                    break;
                case AggregatorSensorBase::MAX:
                    reading.value = computeMax(buffer);
                    break;
                case AggregatorSensorBase::STD:
                    reading.value = computeStd(buffer);
                    break;
                case AggregatorSensorBase::OBS:
                    reading.value = computeObs(buffer);
                    break;
108
                case AggregatorSensorBase::AVG_SEV:
109
                	reading.value = computeSeverityAverage(buffer); //ToDo an appropriate cast needed here
110
                	break;
111
112
113
114
115
116
                default:
                    LOG(warning) << _name << ": Encountered unknown operation!";
                    reading.value = 0;
                    break;
            }
            out->storeReading(reading);
Carla Guillen Carias's avatar
Carla Guillen Carias committed
117
118
        } else {
        	_quantileSensors.push_back(out);
119
120
121
122
123
        }
    }

    if(!_quantileSensors.empty()) {
    	vector<int64_t> quantiles;
124
      	computeEvenQuantiles(buffer, _number_of_even_quantiles, quantiles);
125
126
127
128
129
130
131
        for(unsigned idx=0; idx<quantiles.size(); idx++) {
            reading.value = quantiles[idx];
            _quantileSensors[idx]->storeReading(reading);
        }
    }
}

132
void PerSystSqlOperator::compute(U_Ptr unit){
133
134
//nothing here!
}
Carla Guillen Carias's avatar
Carla Guillen Carias committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193

float severity_formula1(float metric, float threshold, float exponent){
	float val = metric - threshold;
	if (val > 0) {
		float ret = (pow(val, exponent));
		if(ret > 1){
			return 1;
		}
		return ret;
	}
	return 0;
}

float severity_formula2(float metric, float threshold, float exponent){
	if(!threshold){
		return -1;
	}
	float val = metric / threshold - 1;
	if (val > 0) {
		float ret= (pow(val, exponent));
		if(ret > 1){
			return 1;
		}
		return ret;
	}
	return 0;
}

float severity_formula3(float metric, float threshold, float exponent){
	if (!threshold) {
		return -1;
	}
	float val = metric / threshold;
	if (val > 0) {
		float ret= (1 - pow(val, exponent));
		if(ret > 1 ){
			return 1;
		}
		if( ret < 0 ){
			return 0;
		}
		return ret;
	}
	return 0;
}

float severity_memory(float metric, float threshold, float max_memory){
	float denominator = max_memory - threshold;
	float severity = -1;
	if(denominator){
		severity = metric - threshold/(max_memory - threshold);
		if(severity > 1) {
			severity = 1;
		} else if(severity < 0){
			severity = 0;
		}
	}
	return severity;
}
194

195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
float PerSystSqlOperator::computeSeverityAverage(vector<reading_t> & buffer){
	std::vector<float> severities;
	switch( _severity_formula ) {
		case (FORMULA1):
			for(auto reading: buffer){
				auto severity = severity_formula1(reading.value, _severity_threshold, _severity_exponent);
				severities.push_back(severity);
			}
		break;
		case (FORMULA2):
			for(auto reading: buffer){
				auto severity = severity_formula2(reading.value, _severity_threshold, _severity_exponent);
				severities.push_back(severity);
			}
		break;
		case (FORMULA3):
			for(auto reading: buffer){
				auto severity = severity_formula3(reading.value, _severity_threshold, _severity_exponent);
				severities.push_back(severity);
			}
		break;
		case (MEMORY_FORMULA):
			for(auto reading: buffer){
				auto severity = severity_memory(reading.value, _severity_threshold, _severity_max_memory);
				severities.push_back(severity);
			}
		break;
		case (NOFORMULA):
		default:
			return 0.0;
			break;
	}
	if (severities.size()){
		return (std::accumulate(severities.begin(),severities.end(), 0.0f) / severities.size());
	}
	return 0.0;
231
}