aboutsummaryrefslogtreecommitdiff
path: root/tools/gator/daemon/Buffer.cpp
blob: c7abbf3a18208034a1d47c4191edccac2fd0501f (plain)
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
32
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
 * Copyright (C) ARM Limited 2013. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include "Buffer.h"

#include "Logging.h"
#include "Sender.h"
#include "SessionData.h"

#define mask (size - 1)

Buffer::Buffer (const int32_t core, const int32_t buftype, const int size, sem_t *const readerSem) : core(core), buftype(buftype), size(size), readPos(0), writePos(0), commitPos(0), available(true), done(false), buf(new char[size]), commitTime(gSessionData->mLiveRate), readerSem(readerSem) {
	if ((size & mask) != 0) {
		logg->logError(__FILE__, __LINE__, "Buffer size is not a power of 2");
		handleException();
	}
	frame();
}

Buffer::~Buffer () {
	delete [] buf;
}

void Buffer::write (Sender * const sender) {
	if (!commitReady()) {
		return;
	}

	// determine the size of two halves
	int length1 = commitPos - readPos;
	char * buffer1 = buf + readPos;
	int length2 = 0;
	char * buffer2 = buf;
	if (length1 < 0) {
		length1 = size - readPos;
		length2 = commitPos;
	}

	logg->logMessage("Sending data length1: %i length2: %i", length1, length2);

	// start, middle or end
	if (length1 > 0) {
		sender->writeData(buffer1, length1, RESPONSE_APC_DATA);
	}

	// possible wrap around
	if (length2 > 0) {
		sender->writeData(buffer2, length2, RESPONSE_APC_DATA);
	}

	readPos = commitPos;
}

bool Buffer::commitReady () const {
	return commitPos != readPos;
}

int Buffer::bytesAvailable () const {
	int filled = writePos - readPos;
	if (filled < 0) {
		filled += size;
	}

	int remaining = size - filled;

	if (available) {
		// Give some extra room; also allows space to insert the overflow error packet
		remaining -= 200;
	} else {
		// Hysteresis, prevents multiple overflow messages
		remaining -= 2000;
	}

	return remaining;
}

bool Buffer::checkSpace (const int bytes) {
	const int remaining = bytesAvailable();

	if (remaining < bytes) {
		available = false;
	} else {
		available = true;
	}

	return available;
}

void Buffer::commit (const uint64_t time) {
	// post-populate the length, which does not include the response type length nor the length itself, i.e. only the length of the payload
	const int typeLength = gSessionData->mLocalCapture ? 0 : 1;
	int length = writePos - commitPos;
	if (length < 0) {
		length += size;
	}
	length = length - typeLength - sizeof(int32_t);
	for (size_t byte = 0; byte < sizeof(int32_t); byte++) {
		buf[(commitPos + typeLength + byte) & mask] = (length >> byte * 8) & 0xFF;
	}

	logg->logMessage("Committing data readPos: %i writePos: %i commitPos: %i", readPos, writePos, commitPos);
	commitPos = writePos;

	if (gSessionData->mLiveRate > 0) {
		while (time > commitTime) {
			commitTime += gSessionData->mLiveRate;
		}
	}

	if (!done) {
		frame();
	}

	// send a notification that data is ready
	sem_post(readerSem);
}

void Buffer::check (const uint64_t time) {
	int filled = writePos - commitPos;
	if (filled < 0) {
		filled += size;
	}
	if (filled >= ((size * 3) / 4) || (gSessionData->mLiveRate > 0 && time >= commitTime)) {
		commit(time);
	}
}

void Buffer::packInt (int32_t x) {
	int packedBytes = 0;
	int more = true;
	while (more) {
		// low order 7 bits of x
		char b = x & 0x7f;
		x >>= 7;

		if ((x == 0 && (b & 0x40) == 0) || (x == -1 && (b & 0x40) != 0)) {
			more = false;
		} else {
			b |= 0x80;
		}

		buf[(writePos + packedBytes) & mask] = b;
		packedBytes++;
	}

	writePos = (writePos + packedBytes) & mask;
}

void Buffer::packInt64 (int64_t x) {
	int packedBytes = 0;
	int more = true;
	while (more) {
		// low order 7 bits of x
		char b = x & 0x7f;
		x >>= 7;

		if ((x == 0 && (b & 0x40) == 0) || (x == -1 && (b & 0x40) != 0)) {
			more = false;
		} else {
			b |= 0x80;
		}

		buf[(writePos + packedBytes) & mask] = b;
		packedBytes++;
	}

	writePos = (writePos + packedBytes) & mask;
}

void Buffer::frame () {
	if (!gSessionData->mLocalCapture) {
		packInt(RESPONSE_APC_DATA);
	}
	// Reserve space for the length
	writePos += sizeof(int32_t);
	packInt(buftype);
	packInt(core);
}

bool Buffer::eventHeader (const uint64_t curr_time) {
	bool retval = false;
	if (checkSpace(MAXSIZE_PACK32 + MAXSIZE_PACK64)) {
		packInt(0);	// key of zero indicates a timestamp
		packInt64(curr_time);
		retval = true;
	}

	return retval;
}

void Buffer::event (const int32_t key, const int32_t value) {
	if (checkSpace(2 * MAXSIZE_PACK32)) {
		packInt(key);
		packInt(value);
	}
}

void Buffer::event64 (const int64_t key, const int64_t value) {
	if (checkSpace(2 * MAXSIZE_PACK64)) {
		packInt64(key);
		packInt64(value);
	}
}

void Buffer::setDone () {
	done = true;
	commit(0);
}

bool Buffer::isDone () const {
	return done && readPos == commitPos && commitPos == writePos;
}