CaliperSensorGroup.cpp 5.25 KB
Newer Older
1
2
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
28
29
30
31
//================================================================================
// Name        : CaliperSensorGroup.cpp
// Author      : Micha Mueller
// Copyright   : Leibniz Supercomputing Centre
// Description : Source file for Caliper sensor group class.
//================================================================================

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

#include "CaliperSensorGroup.h"

#include <errno.h>
#include <stdio.h>
#include <sys/socket.h>
32
#include <sys/un.h>
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

#include "timestamp.h"

CaliperSensorGroup::CaliperSensorGroup(const std::string& name) :
	SensorGroupTemplate(name),
	_socket(-1),
	_connection(-1) {
}

CaliperSensorGroup::CaliperSensorGroup(const CaliperSensorGroup& other) :
  SensorGroupTemplate(other),
  _socket(-1),
  _connection(-1) {
}

CaliperSensorGroup::~CaliperSensorGroup() {
	if(_keepRunning) {
	    stop();
	}
}

CaliperSensorGroup& CaliperSensorGroup::operator=(const CaliperSensorGroup& other) {
  SensorGroupTemplate::operator=(other); 
  _socket = -1;
  _connection = -1;
  
  return *this;
}

void CaliperSensorGroup::start() {
	if (_keepRunning) {
		//we have been started already
		LOG(info) << "Sensorgroup " << _groupName << " already running.";
		return;
	}

	_socket = socket(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK, 0);

	if(_socket == -1) {
72
	    LOG(error) << _groupName << ": Failed to open socket: " << strerror(errno);
73
74
75
76
77
78
79
80
81
82
	    return;
	}

	sockaddr_un addr;
	memset(&addr, 0, sizeof(struct sockaddr_un));

	addr.sun_family = AF_UNIX;
	snprintf(&addr.sun_path[1], 91, "DCDBPusherCaliSocket");

	if(bind(_socket, (struct sockaddr*) &addr, sizeof(addr))) {
83
	    LOG(error) << _groupName << ": Failed to bind socket: " << strerror(errno);
84
85
86
87
88
89
	    close(_socket);
	    _socket = -1;
	    return;
	}

	if(listen(_socket, 1)) {
90
	    LOG(error) << _groupName << ": Can not listen on socket: " << strerror(errno);
91
92
93
94
95
96
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
	    close(_socket);
	    _socket = -1;
	    return;
	}

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

void CaliperSensorGroup::stop() {
    _keepRunning = 0;

    //wait before closing _file
    wait();

    if(_connection != -1) {
        close(_connection);
        _connection = -1;
    }
    if(_socket != -1) {
        close(_socket);
        _socket = -1;
    }

    LOG(info) << "Sensorgroup " << _groupName << " stopped.";
}

void CaliperSensorGroup::read() {
    if (_connection == -1) {
        _connection = accept(_socket, NULL, NULL);
        if (_connection == -1) {
            if (errno != EAGAIN && errno != EWOULDBLOCK) {
125
                LOG(error) << _groupName << ": Accept failed: " << strerror(errno);
126
127
128
129
130
131
132
133
            }
            return;
        }
    }

    const size_t bufSize = 2048;
    char buf[bufSize];
    const ssize_t nrec = recv(_connection, (void *) buf, bufSize, MSG_DONTWAIT);
134
    LOG(debug) << _groupName << ": Receiving message...";
135
136
137
138

    if (nrec == 0) {
        close(_connection);
        _connection = -1;
139
        LOG(debug) << _groupName << ": Connection closed";
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
        return;
    } else if (nrec == -1) {
        if (errno != EAGAIN && errno != EWOULDBLOCK) {
            LOG(error) << _groupName << ": Recv failed: " << strerror(errno);
        }
        return;
    } else {
        std::string timestamp(buf);
        std::string function(&(buf[timestamp.length()+1]));
        LOG(info) << _groupName << ": Received value " << function << " (" << timestamp << ")";
    }


/*    reading_t reading;
	reading.timestamp = getTimestamp();

	try {
		for(auto s : _sensors) {
			reading.value =  *
							 *  TODO
							 *  Read a value for every sensor affiliated with this group and store
							 *  it with the appropriate sensor.
							 * 0;
			s->storeReading(reading);
#ifdef DEBUG
			LOG(debug) << _groupName << "::" << s->getName() << " raw reading: \"" << reading.value << "\"";
#endif
		}
	} catch (const std::exception& e) {
		LOG(error) << "Sensorgroup" << _groupName << " could not read value: " << e.what();
	}*/
}

void CaliperSensorGroup::readAsync() {
	uint64_t now = getTimestamp();
	read();
	if (_timer && _keepRunning) {
		_timer->expires_at(timestamp2ptime(nextReadingTime()));
		_pendingTasks++;
		_timer->async_wait(std::bind(&CaliperSensorGroup::readAsync, this));
	}
	_pendingTasks--;
}

void CaliperSensorGroup::printConfig(LOG_LEVEL ll) {

}