synth/arm/trace.cc

72 lines
1.6 KiB
C++
Raw Normal View History

2022-05-17 03:56:25 +00:00
#include "trace.h"
#include <algorithm>
#include <chrono>
2022-05-17 03:56:25 +00:00
#include "itoa.h"
#include "lock.h"
#include "uart.h"
namespace tracing {
namespace {
struct Event {
uint32_t timestamp;
TraceEvent event;
};
2022-05-17 03:56:25 +00:00
constexpr size_t kTraceBufferSize = 256;
std::array<Event, kTraceBufferSize> buffer;
2022-05-17 03:56:25 +00:00
size_t write_ptr = 0;
size_t size = 0;
2022-05-17 03:56:25 +00:00
} // namespace
void trace(int raw_event) { trace(static_cast<TraceEvent>(raw_event)); }
void trace(TraceEvent event) {
const std::chrono::system_clock::time_point now =
std::chrono::system_clock::now();
const uint32_t uptime_ticks = now.time_since_epoch().count();
{
InterruptLock lock;
2022-05-17 03:56:25 +00:00
buffer[write_ptr] = {.timestamp = uptime_ticks, .event = event};
2022-05-17 03:56:25 +00:00
write_ptr = (write_ptr + 1) % buffer.size();
size = std::min(size + 1, kTraceBufferSize);
#if TRACE_DUMP_WHEN_FULL
if (size == kTraceBufferSize) {
dump();
}
#endif // TRACE_DUMP_WHEN_FULL
}
2022-05-17 03:56:25 +00:00
}
void dump() {
InterruptLock lock;
if (size == kTraceBufferSize) {
std::rotate(buffer.begin(), buffer.begin() + write_ptr, buffer.end());
}
2022-05-17 03:56:25 +00:00
char number[] = "00000000";
2022-05-17 03:56:25 +00:00
UartWriteCrash("----\r\n");
for (Event event : std::span{buffer}.subspan(0, size)) {
itoa(static_cast<int>(event.timestamp), number);
2022-05-17 17:17:56 +00:00
UartWriteCrash(number);
UartWriteCrash(" ");
itoa(static_cast<int>(event.event), number);
UartWriteCrash(number);
UartWriteCrash("\r\n");
2022-05-17 03:56:25 +00:00
}
UartWriteCrash("----\r\n");
size = 0;
write_ptr = 0;
2022-05-17 03:56:25 +00:00
}
} // namespace tracing