better perf

This commit is contained in:
Martino Ferrari
2026-02-25 16:51:07 +01:00
parent aaf69c0949
commit dfb399bbba
12 changed files with 713 additions and 633 deletions

View File

@@ -4,7 +4,7 @@
#include "CompilerTypes.h"
#include "TypeDescriptor.h"
#include "StreamString.h"
#include <cstring> // For memcpy
#include <cstring>
namespace MARTe {
@@ -29,6 +29,10 @@ struct TraceHeader {
};
#pragma pack(pop)
/**
* @brief Ring buffer for high-frequency signal tracing.
* @details New format per sample: [ID:4][Timestamp:8][Size:4][Data:N]
*/
class TraceRingBuffer {
public:
TraceRingBuffer() {
@@ -55,12 +59,11 @@ public:
return (buffer != NULL_PTR(uint8*));
}
bool Push(uint32 signalID, void* data, uint32 size) {
uint32 packetSize = 4 + 4 + size;
bool Push(uint32 signalID, uint64 timestamp, void* data, uint32 size) {
uint32 packetSize = 4 + 8 + 4 + size; // ID + TS + Size + Data
uint32 read = readIndex;
uint32 write = writeIndex;
// Calculate available space
uint32 available = 0;
if (read <= write) {
available = bufferSize - (write - read) - 1;
@@ -70,35 +73,31 @@ public:
if (available < packetSize) return false;
// Use temporary write index to ensure atomic update
uint32 tempWrite = write;
WriteToBuffer(&tempWrite, &signalID, 4);
WriteToBuffer(&tempWrite, &timestamp, 8);
WriteToBuffer(&tempWrite, &size, 4);
WriteToBuffer(&tempWrite, data, size);
// Memory Barrier to ensure data is visible before index update
// __sync_synchronize();
// Final atomic update
writeIndex = tempWrite;
return true;
}
bool Pop(uint32 &signalID, void* dataBuffer, uint32 &size, uint32 maxSize) {
bool Pop(uint32 &signalID, uint64 &timestamp, void* dataBuffer, uint32 &size, uint32 maxSize) {
uint32 read = readIndex;
uint32 write = writeIndex;
if (read == write) return false;
uint32 tempRead = read;
uint32 tempId = 0;
uint64 tempTs = 0;
uint32 tempSize = 0;
// Peek header
ReadFromBuffer(&tempRead, &tempId, 4);
ReadFromBuffer(&tempRead, &tempTs, 8);
ReadFromBuffer(&tempRead, &tempSize, 4);
if (tempSize > maxSize) {
// Error case: drop data up to writeIndex (resync)
readIndex = write;
return false;
}
@@ -106,11 +105,9 @@ public:
ReadFromBuffer(&tempRead, dataBuffer, tempSize);
signalID = tempId;
timestamp = tempTs;
size = tempSize;
// Memory Barrier
// __sync_synchronize();
readIndex = tempRead;
return true;
}
@@ -126,7 +123,6 @@ private:
void WriteToBuffer(uint32 *idx, void* src, uint32 count) {
uint32 current = *idx;
uint32 spaceToEnd = bufferSize - current;
if (count <= spaceToEnd) {
std::memcpy(&buffer[current], src, count);
*idx = (current + count) % bufferSize;
@@ -141,7 +137,6 @@ private:
void ReadFromBuffer(uint32 *idx, void* dst, uint32 count) {
uint32 current = *idx;
uint32 spaceToEnd = bufferSize - current;
if (count <= spaceToEnd) {
std::memcpy(dst, &buffer[current], count);
*idx = (current + count) % bufferSize;