Buffer.cpp 9.98 KB
Newer Older
Abhijith PA's avatar
Abhijith PA committed
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 218 219 220 221 222 223 224 225 226 227 228 229 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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
/**
 * Copyright (C) ARM Limited 2013-2014. 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 (mSize - 1)

enum {
	CODE_PEA         = 1,
	CODE_KEYS        = 2,
	CODE_FORMAT      = 3,
	CODE_MAPS        = 4,
	CODE_COMM        = 5,
	CODE_KEYS_OLD    = 6,
	CODE_ONLINE_CPU  = 7,
	CODE_OFFLINE_CPU = 8,
	CODE_KALLSYMS    = 9,
};

// Summary Frame Messages
enum {
	MESSAGE_SUMMARY = 1,
	MESSAGE_CORE_NAME = 3,
};

// From gator_marshaling.c
#define NEWLINE_CANARY \
	/* Unix */ \
	"1\n" \
	/* Windows */ \
	"2\r\n" \
	/* Mac OS */ \
	"3\r" \
	/* RISC OS */ \
	"4\n\r" \
	/* Add another character so the length isn't 0x0a bytes */ \
	"5"

Buffer::Buffer(const int32_t core, const int32_t buftype, const int size, sem_t *const readerSem) : mBuf(new char[size]), mReaderSem(readerSem), mCommitTime(gSessionData->mLiveRate), mSize(size), mReadPos(0), mWritePos(0), mCommitPos(0), mAvailable(true), mIsDone(false), mCore(core), mBufType(buftype) {
	if ((mSize & mask) != 0) {
		logg->logError(__FILE__, __LINE__, "Buffer size is not a power of 2");
		handleException();
	}
	sem_init(&mWriterSem, 0, 0);
	frame();
}

Buffer::~Buffer() {
	delete [] mBuf;
	sem_destroy(&mWriterSem);
}

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

	// commit and read are updated by the writer, only read them once
	int commitPos = mCommitPos;
	int readPos = mReadPos;

	// determine the size of two halves
	int length1 = commitPos - readPos;
	char *buffer1 = mBuf + readPos;
	int length2 = 0;
	char *buffer2 = mBuf;
	if (length1 < 0) {
		length1 = mSize - 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);
	}

	mReadPos = commitPos;

	// send a notification that space is available
	sem_post(&mWriterSem);
}

bool Buffer::commitReady() const {
	return mCommitPos != mReadPos;
}

int Buffer::bytesAvailable() const {
	int filled = mWritePos - mReadPos;
	if (filled < 0) {
		filled += mSize;
	}

	int remaining = mSize - filled;

	if (mAvailable) {
		// 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) {
		mAvailable = false;
	} else {
		mAvailable = true;
	}

	return mAvailable;
}

int Buffer::contiguousSpaceAvailable() const {
	int remaining = bytesAvailable();
	int contiguous = mSize - mWritePos;
	if (remaining < contiguous) {
		return remaining;
	} else {
		return contiguous;
	}
}

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 = mWritePos - mCommitPos;
	if (length < 0) {
		length += mSize;
	}
	length = length - typeLength - sizeof(int32_t);
	for (size_t byte = 0; byte < sizeof(int32_t); byte++) {
		mBuf[(mCommitPos + typeLength + byte) & mask] = (length >> byte * 8) & 0xFF;
	}

	logg->logMessage("Committing data mReadPos: %i mWritePos: %i mCommitPos: %i", mReadPos, mWritePos, mCommitPos);
	mCommitPos = mWritePos;

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

	if (!mIsDone) {
		frame();
	}

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

void Buffer::check(const uint64_t time) {
	int filled = mWritePos - mCommitPos;
	if (filled < 0) {
		filled += mSize;
	}
	if (filled >= ((mSize * 3) / 4) || (gSessionData->mLiveRate > 0 && time >= mCommitTime)) {
		commit(time);
	}
}

void Buffer::packInt(char *const buf, const int size, int &writePos, 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*/(size - 1)] = b;
		packedBytes++;
	}

	writePos = (writePos + packedBytes) & /*mask*/(size - 1);
}

void Buffer::packInt(int32_t x) {
	packInt(mBuf, mSize, mWritePos, x);
}

void Buffer::packInt64(char *const buf, const int size, int &writePos, 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*/(size - 1)] = b;
		packedBytes++;
	}

	writePos = (writePos + packedBytes) & /*mask*/(size - 1);
}

void Buffer::packInt64(int64_t x) {
	packInt64(mBuf, mSize, mWritePos, x);
}

void Buffer::writeBytes(const void *const data, size_t count) {
	size_t i;
	for (i = 0; i < count; ++i) {
		mBuf[(mWritePos + i) & mask] = static_cast<const char *>(data)[i];
	}

	mWritePos = (mWritePos + i) & mask;
}

void Buffer::writeString(const char *const str) {
	const int len = strlen(str);
	packInt(len);
	writeBytes(str, len);
}

void Buffer::frame() {
	if (!gSessionData->mLocalCapture) {
		packInt(RESPONSE_APC_DATA);
	}
	// Reserve space for the length
	mWritePos += sizeof(int32_t);
	packInt(mBufType);
	if ((mBufType == FRAME_BLOCK_COUNTER) || (mBufType == FRAME_PERF_ATTRS) || (mBufType == FRAME_PERF)) {
		packInt(mCore);
	}
}

void Buffer::summary(const uint64_t currTime, const int64_t timestamp, const int64_t uptime, const int64_t monotonicDelta, const char *const uname) {
	packInt(MESSAGE_SUMMARY);
	writeString(NEWLINE_CANARY);
	packInt64(timestamp);
	packInt64(uptime);
	packInt64(monotonicDelta);
	writeString("uname");
	writeString(uname);
	writeString("");
	check(currTime);
}

void Buffer::coreName(const uint64_t currTime, const int core, const int cpuid, const char *const name) {
	if (checkSpace(3 * MAXSIZE_PACK32 + 0x100)) {
		packInt(MESSAGE_CORE_NAME);
		packInt(core);
		packInt(cpuid);
		writeString(name);
	}
	check(currTime);
}

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

	return retval;
}

bool Buffer::eventTid(const int tid) {
	bool retval = false;
	if (checkSpace(2 * MAXSIZE_PACK32)) {
		// key of 1 indicates a tid
		packInt(1);
		packInt(tid);
		retval = true;
	}

	return retval;
}

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

void Buffer::event64(const int key, const int64_t value) {
	if (checkSpace(MAXSIZE_PACK64 + MAXSIZE_PACK32)) {
		packInt(key);
		packInt64(value);
	}
}

void Buffer::pea(const uint64_t currTime, const struct perf_event_attr *const pea, int key) {
	while (!checkSpace(2 * MAXSIZE_PACK32 + pea->size)) {
		sem_wait(&mWriterSem);
	}
	packInt(CODE_PEA);
	writeBytes(pea, pea->size);
	packInt(key);
	check(currTime);
}

void Buffer::keys(const uint64_t currTime, const int count, const __u64 *const ids, const int *const keys) {
	while (!checkSpace(2 * MAXSIZE_PACK32 + count * (MAXSIZE_PACK32 + MAXSIZE_PACK64))) {
		sem_wait(&mWriterSem);
	}
	packInt(CODE_KEYS);
	packInt(count);
	for (int i = 0; i < count; ++i) {
		packInt64(ids[i]);
		packInt(keys[i]);
	}
	check(currTime);
}

void Buffer::keysOld(const uint64_t currTime, const int keyCount, const int *const keys, const int bytes, const char *const buf) {
	while (!checkSpace((2 + keyCount) * MAXSIZE_PACK32 + bytes)) {
		sem_wait(&mWriterSem);
	}
	packInt(CODE_KEYS_OLD);
	packInt(keyCount);
	for (int i = 0; i < keyCount; ++i) {
		packInt(keys[i]);
	}
	writeBytes(buf, bytes);
	check(currTime);
}

void Buffer::format(const uint64_t currTime, const int length, const char *const format) {
	while (!checkSpace(MAXSIZE_PACK32 + length + 1)) {
		sem_wait(&mWriterSem);
	}
	packInt(CODE_FORMAT);
	writeBytes(format, length + 1);
	check(currTime);
}

void Buffer::maps(const uint64_t currTime, const int pid, const int tid, const char *const maps) {
	const int mapsLen = strlen(maps) + 1;
	while (!checkSpace(3 * MAXSIZE_PACK32 + mapsLen)) {
		sem_wait(&mWriterSem);
	}
	packInt(CODE_MAPS);
	packInt(pid);
	packInt(tid);
	writeBytes(maps, mapsLen);
	check(currTime);
}

void Buffer::comm(const uint64_t currTime, const int pid, const int tid, const char *const image, const char *const comm) {
	const int imageLen = strlen(image) + 1;
	const int commLen = strlen(comm) + 1;
	while (!checkSpace(3 * MAXSIZE_PACK32 + imageLen + commLen)) {
		sem_wait(&mWriterSem);
	}
	packInt(CODE_COMM);
	packInt(pid);
	packInt(tid);
	writeBytes(image, imageLen);
	writeBytes(comm, commLen);
	check(currTime);
}

void Buffer::onlineCPU(const uint64_t currTime, const uint64_t time, const int cpu) {
	while (!checkSpace(MAXSIZE_PACK32 + MAXSIZE_PACK64)) {
		sem_wait(&mWriterSem);
	}
	packInt(CODE_ONLINE_CPU);
	packInt64(time);
	packInt(cpu);
	check(currTime);
}

void Buffer::offlineCPU(const uint64_t currTime, const uint64_t time, const int cpu) {
	while (!checkSpace(MAXSIZE_PACK32 + MAXSIZE_PACK64)) {
		sem_wait(&mWriterSem);
	}
	packInt(CODE_OFFLINE_CPU);
	packInt64(time);
	packInt(cpu);
	check(currTime);
}

void Buffer::kallsyms(const uint64_t currTime, const char *const kallsyms) {
	const int kallsymsLen = strlen(kallsyms) + 1;
	while (!checkSpace(3 * MAXSIZE_PACK32 + kallsymsLen)) {
		sem_wait(&mWriterSem);
	}
	packInt(CODE_KALLSYMS);
	writeBytes(kallsyms, kallsymsLen);
	check(currTime);
}

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

bool Buffer::isDone() const {
	return mIsDone && mReadPos == mCommitPos && mCommitPos == mWritePos;
}