2 * Copyright (C) ARM Limited 2013. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
13 #include "SessionData.h"
15 #define mask (size - 1)
17 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) {
18 if ((size & mask) != 0) {
19 logg->logError(__FILE__, __LINE__, "Buffer size is not a power of 2");
29 void Buffer::write (Sender * const sender) {
34 // determine the size of two halves
35 int length1 = commitPos - readPos;
36 char * buffer1 = buf + readPos;
40 length1 = size - readPos;
44 logg->logMessage("Sending data length1: %i length2: %i", length1, length2);
46 // start, middle or end
48 sender->writeData(buffer1, length1, RESPONSE_APC_DATA);
51 // possible wrap around
53 sender->writeData(buffer2, length2, RESPONSE_APC_DATA);
59 bool Buffer::commitReady () const {
60 return commitPos != readPos;
63 int Buffer::bytesAvailable () const {
64 int filled = writePos - readPos;
69 int remaining = size - filled;
72 // Give some extra room; also allows space to insert the overflow error packet
75 // Hysteresis, prevents multiple overflow messages
82 bool Buffer::checkSpace (const int bytes) {
83 const int remaining = bytesAvailable();
85 if (remaining < bytes) {
94 void Buffer::commit (const uint64_t time) {
95 // post-populate the length, which does not include the response type length nor the length itself, i.e. only the length of the payload
96 const int typeLength = gSessionData->mLocalCapture ? 0 : 1;
97 int length = writePos - commitPos;
101 length = length - typeLength - sizeof(int32_t);
102 for (size_t byte = 0; byte < sizeof(int32_t); byte++) {
103 buf[(commitPos + typeLength + byte) & mask] = (length >> byte * 8) & 0xFF;
106 logg->logMessage("Committing data readPos: %i writePos: %i commitPos: %i", readPos, writePos, commitPos);
107 commitPos = writePos;
109 if (gSessionData->mLiveRate > 0) {
110 while (time > commitTime) {
111 commitTime += gSessionData->mLiveRate;
119 // send a notification that data is ready
123 void Buffer::check (const uint64_t time) {
124 int filled = writePos - commitPos;
128 if (filled >= ((size * 3) / 4) || (gSessionData->mLiveRate > 0 && time >= commitTime)) {
133 void Buffer::packInt (int32_t x) {
137 // low order 7 bits of x
141 if ((x == 0 && (b & 0x40) == 0) || (x == -1 && (b & 0x40) != 0)) {
147 buf[(writePos + packedBytes) & mask] = b;
151 writePos = (writePos + packedBytes) & mask;
154 void Buffer::packInt64 (int64_t x) {
158 // low order 7 bits of x
162 if ((x == 0 && (b & 0x40) == 0) || (x == -1 && (b & 0x40) != 0)) {
168 buf[(writePos + packedBytes) & mask] = b;
172 writePos = (writePos + packedBytes) & mask;
175 void Buffer::frame () {
176 if (!gSessionData->mLocalCapture) {
177 packInt(RESPONSE_APC_DATA);
179 // Reserve space for the length
180 writePos += sizeof(int32_t);
185 bool Buffer::eventHeader (const uint64_t curr_time) {
187 if (checkSpace(MAXSIZE_PACK32 + MAXSIZE_PACK64)) {
188 packInt(0); // key of zero indicates a timestamp
189 packInt64(curr_time);
196 bool Buffer::eventTid (const int tid) {
198 if (checkSpace(2*MAXSIZE_PACK32)) {
199 packInt(1); // key of 1 indicates a tid
207 void Buffer::event (const int32_t key, const int32_t value) {
208 if (checkSpace(2 * MAXSIZE_PACK32)) {
214 void Buffer::event64 (const int64_t key, const int64_t value) {
215 if (checkSpace(2 * MAXSIZE_PACK64)) {
221 void Buffer::setDone () {
226 bool Buffer::isDone () const {
227 return done && readPos == commitPos && commitPos == writePos;