Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions runtime/src/indirect_draw_stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct Counts {
std::atomic<unsigned long long> values[4]{};
std::atomic<unsigned long long> by_reason[reason_count]{};
void report(std::FILE* out = stderr) const {
if (!out) return;
char line[1280];
int pos = std::snprintf(
line, sizeof(line),
Expand Down Expand Up @@ -88,13 +89,15 @@ inline unsigned long long reason_skips(Reason reason) {
}

inline void note(Event event) {
const unsigned index = static_cast<unsigned>(event);
if (index >= 4u) return;
auto& c = counts();
const auto n = 1 + c.values[static_cast<unsigned>(event)].fetch_add(1, std::memory_order_relaxed);
const auto n = 1 + c.values[index].fetch_add(1, std::memory_order_relaxed);
if ((n & (n - 1)) == 0) c.report();
}

inline void skip(Event event, Reason reason) {
if (static_cast<unsigned>(reason) >= reason_count) return;
if (static_cast<unsigned>(event) >= 4u || static_cast<unsigned>(reason) >= reason_count) return;
static std::atomic_flag shown[reason_count]{};
if (!shown[reason].test_and_set(std::memory_order_relaxed))
std::fprintf(stderr, "[rt] indirect graphics skipped: %s\n", reason_name(reason));
Expand Down
53 changes: 53 additions & 0 deletions runtime/test/indirect_draw_stats_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "../src/indirect_draw_stats.h"

#include <cstdio>
#include <cstring>

#define EXPECT(cond) do { \
if (!(cond)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #cond); return 1; } \
} while (0)

int main() {
using namespace leonida::indirect_stats;

EXPECT(topology_supported(1));
EXPECT(topology_supported(5));
EXPECT(topology_supported(10));
EXPECT(topology_supported(17));
EXPECT(!topology_supported(0));
EXPECT(!topology_supported(6));
EXPECT(!topology_supported(19));

EXPECT(std::strcmp(reason_name(Reason::compound), "compound signature") == 0);
EXPECT(std::strcmp(reason_tag(Reason::stateless), "stateless") == 0);
EXPECT(std::strcmp(reason_name(Reason::reason_count), "unknown") == 0);
EXPECT(std::strcmp(reason_tag(static_cast<Reason>(999)), "unknown") == 0);
EXPECT(reason_skips(Reason::reason_count) == 0);

// Regression: report(nullptr) must be a safe no-op rather than dereferencing null FILE*.
Counts local{};
local.report(nullptr);

// Regression: out-of-range Event must not index past values[4].
note(static_cast<Event>(4));
note(static_cast<Event>(99));
skip(static_cast<Event>(4), Reason::topology);
EXPECT(reason_skips(Reason::topology) == 0);

// Valid skip updates counts and writes formatted report cleanly.
skip(Event::record_skip, Reason::topology);
EXPECT(reason_skips(Reason::topology) == 1);
std::FILE* tmp = std::tmpfile();
EXPECT(tmp != nullptr);
counts().report(tmp);
std::rewind(tmp);
char buf[512]{};
const std::size_t n = std::fread(buf, 1, sizeof(buf) - 1, tmp);
std::fclose(tmp);
EXPECT(n > 0);
EXPECT(std::strstr(buf, "record_skips=1") != nullptr);
EXPECT(std::strstr(buf, "[topology=1]") != nullptr);

std::puts("indirect_draw_stats_test: ok");
return 0;
}
Loading