OpaSensorGroup.cpp 5.18 KB
Newer Older
Micha Müller's avatar
Micha Müller committed
1 2 3 4 5 6 7 8 9 10
/*
 * OpaSensorGroup.cpp
 *
 *  Created on: 10.09.2018
 *      Author: Micha Mueller
 */

#include "OpaSensorGroup.h"

OpaSensorGroup::OpaSensorGroup(const std::string name) :
11 12
	SensorGroupTemplate(name),
	_hfiNum(0),
13 14 15 16 17 18 19 20 21 22 23 24 25
	_portNum(0),
	_port(nullptr) {
	_imageId = {0};
	_imageInfo = {0};
}

OpaSensorGroup::OpaSensorGroup(const OpaSensorGroup& other) :
    SensorGroupTemplate(other),
    _hfiNum(other._hfiNum),
    _portNum(other._portNum),
    _port(nullptr) {
  _imageId = {0};
  _imageInfo = {0};
26
}
Micha Müller's avatar
Micha Müller committed
27

28 29
OpaSensorGroup::~OpaSensorGroup() {
	if(_port) {
Alessio Netti's avatar
Alessio Netti committed
30
		omgt_close_port(_port);
Micha Müller's avatar
Micha Müller committed
31 32 33
	}
}

34 35 36 37 38 39 40 41 42 43 44
OpaSensorGroup& OpaSensorGroup::operator=(const OpaSensorGroup& other) {
  SensorGroupTemplate::operator =(other);
  _hfiNum = other._hfiNum;
  _portNum = other._portNum;
  _port = nullptr;
  _imageId = {0};
  _imageInfo = {0};

  return *this;
}

Micha Müller's avatar
Micha Müller committed
45 46 47 48 49 50 51
void OpaSensorGroup::start() {
	if (_keepRunning) {
		//we have been started already
		LOG(info) << "Sensorgroup " << _groupName << " already running.";
		return;
	}

Micha Müller's avatar
Micha Müller committed
52 53 54 55 56 57
	if (omgt_open_port_by_num(&_port, _hfiNum, _portNum, NULL) != OMGT_STATUS_SUCCESS) {
		LOG(error) << "Sensorgroup " << _groupName << " failed to open port or initialize PA connection";
		_port = nullptr;
		return;
	}

58
	if (omgt_pa_get_image_info(_port, _imageId, &_imageInfo)) {
Micha Müller's avatar
Micha Müller committed
59 60 61 62 63
		LOG(error) << "Sensorgroup " << _groupName << " failed to get PA image";
		omgt_close_port(_port);
		_port = nullptr;
		return;
	}
Micha Müller's avatar
Micha Müller committed
64 65 66 67 68 69 70 71 72 73

	_keepRunning = 1;
	_pendingTasks++;
	_timer->async_wait(std::bind(&OpaSensorGroup::readAsync, this));
	LOG(info) << "Sensorgroup " << _groupName << " started.";
}

void OpaSensorGroup::stop() {
	_keepRunning = 0;
	
Micha Müller's avatar
Micha Müller committed
74 75 76 77
	if (_port) {
		omgt_close_port(_port);
		_port = nullptr;
	}
Micha Müller's avatar
Micha Müller committed
78 79 80 81 82
	 
	LOG(info) << "Sensorgroup " << _groupName << " stopped.";
}

void OpaSensorGroup::read() {
83
	ureading_t reading;
Micha Müller's avatar
Micha Müller committed
84 85
	reading.timestamp = getTimestamp();

86
	STL_PORT_COUNTERS_DATA portCounters;
Micha Müller's avatar
Micha Müller committed
87
	try {
88
		if (omgt_pa_get_port_stats(_port, _imageId, 1, _portNum, &_imageId, &portCounters, NULL, 0, 1)) {
Micha Müller's avatar
Micha Müller committed
89 90
			throw std::runtime_error("Failed to get port counters");
		}
Micha Müller's avatar
Micha Müller committed
91
	} catch (const std::exception& e) {
Micha Müller's avatar
Micha Müller committed
92
		LOG(error) << "Sensorgroup" << _groupName << " could not read value: " << e.what();
Micha Müller's avatar
Micha Müller committed
93 94
		return;
	}
95

96
	for(const auto& s : _sensors) {
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 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
		switch(s->getCounterData()) {
		case(portXmitData) :
			reading.value = portCounters.portXmitData;
			break;
		case(portRcvData) :
			reading.value = portCounters.portRcvData;
			break;
		case(portXmitPkts) :
			reading.value = portCounters.portXmitPkts;
			break;
		case(portRcvPkts) :
			reading.value = portCounters.portRcvPkts;
			break;
		case(portMulticastXmitPkts) :
			reading.value = portCounters.portMulticastXmitPkts;
			break;
		case(portMulticastRcvPkts) :
			reading.value = portCounters.portMulticastRcvPkts;
			break;
		case(localLinkIntegrityErrors) :
			reading.value = portCounters.localLinkIntegrityErrors;
			break;
		case(fmConfigErrors) :
			reading.value = portCounters.fmConfigErrors;
			break;
		case(portRcvErrors) :
			reading.value = portCounters.portRcvErrors;
			break;
		case(excessiveBufferOverruns) :
			reading.value = portCounters.excessiveBufferOverruns;
			break;
		case(portRcvConstraintErrors) :
			reading.value = portCounters.portRcvConstraintErrors;
			break;
		case(portRcvSwitchRelayErrors) :
			reading.value = portCounters.portRcvSwitchRelayErrors;
			break;
		case(portXmitDiscards) :
			reading.value = portCounters.portXmitDiscards;
			break;
		case(portXmitConstraintErrors) :
			reading.value = portCounters.portXmitConstraintErrors;
			break;
		case(portRcvRemotePhysicalErrors) :
			reading.value = portCounters.portRcvRemotePhysicalErrors;
			break;
		case(swPortCongestion) :
			reading.value = portCounters.swPortCongestion;
			break;
		case(portXmitWait) :
			reading.value = portCounters.portXmitWait;
			break;
		case(portRcvFECN) :
			reading.value = portCounters.portRcvFECN;
			break;
		case(portRcvBECN) :
			reading.value = portCounters.portRcvBECN;
			break;
		case(portXmitTimeCong) :
			reading.value = portCounters.portXmitTimeCong;
			break;
		case(portXmitWastedBW) :
			reading.value = portCounters.portXmitWastedBW;
			break;
		case(portXmitWaitData) :
			reading.value = portCounters.portXmitWaitData;
			break;
		case(portRcvBubble) :
			reading.value = portCounters.portRcvBubble;
			break;
		case(portMarkFECN) :
			reading.value = portCounters.portMarkFECN;
			break;
		case(linkErrorRecovery) :
			reading.value = portCounters.linkErrorRecovery;
			break;
		case(linkDowned) :
			reading.value = portCounters.linkDowned;
			break;
		case(uncorrectableErrors) :
			reading.value = portCounters.uncorrectableErrors;
			break;
		default:
			LOG(error) << _groupName << "::" << s->getName() << " could not read value!";
181
			continue;
182 183
			break;
		}
184
		s->storeReading(reading);
185
#ifdef DEBUG
Micha Müller's avatar
Micha Müller committed
186
		LOG(debug) << _groupName << "::" << s->getName() << " raw reading: \"" << reading.value << "\"";
187 188
#endif
	}
Micha Müller's avatar
Micha Müller committed
189 190 191 192 193
}

void OpaSensorGroup::readAsync() {
	read();
	if (_timer && _keepRunning) {
194
		_timer->expires_at(timestamp2ptime(nextReadingTime()));
Micha Müller's avatar
Micha Müller committed
195 196 197 198 199
		_pendingTasks++;
		_timer->async_wait(std::bind(&OpaSensorGroup::readAsync, this));
	}
	_pendingTasks--;
}
Micha Müller's avatar
Micha Müller committed
200 201

void OpaSensorGroup::printConfig(LOG_LEVEL ll) {
202 203
    LOG_VAR(ll) << "            HFI Num:  " << _hfiNum;
    LOG_VAR(ll) << "            Port Num: " << _portNum;
Micha Müller's avatar
Micha Müller committed
204
}