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
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ class TraceThread {
std::span<const Slice> slices;
std::vector<Total> totals;
bool dropped = false;
std::uint64_t eventsLost = 0;
std::optional<Clock::time_point> eventsFullAt;
bool totalsFull = false;
};

inline static const std::atomic<bool> AlwaysBuffering{true};
Expand Down Expand Up @@ -195,7 +198,10 @@ class TraceThread {
if (scope == nullptr) continue;
snapshot.totals.push_back({scope, entry.stage.load(std::memory_order_relaxed), Clock::duration(entry.length.load(std::memory_order_relaxed)), entry.count.load(std::memory_order_relaxed)});
}
snapshot.dropped = dropped.load(std::memory_order_relaxed);
snapshot.eventsLost = lost.load(std::memory_order_acquire);
if (snapshot.eventsLost != 0) snapshot.eventsFullAt = Clock::time_point(Clock::duration(firstLost.load(std::memory_order_relaxed)));
snapshot.totalsFull = totalsFull.load(std::memory_order_relaxed);
snapshot.dropped = snapshot.eventsLost != 0 || snapshot.totalsFull;
return snapshot;
}

Expand All @@ -210,7 +216,9 @@ class TraceThread {
void append(const Slice& slice) {
const auto index = used.load(std::memory_order_relaxed);
if (index == capacity) {
dropped.store(true, std::memory_order_relaxed);
const auto count = lost.load(std::memory_order_relaxed);
if (count == 0) firstLost.store(slice.begin.time_since_epoch().count(), std::memory_order_relaxed);
lost.store(count + 1, std::memory_order_release);
return;
}
slices[index] = slice;
Expand All @@ -233,7 +241,7 @@ class TraceThread {
entry.count.store(entry.count.load(std::memory_order_relaxed) + 1, std::memory_order_relaxed);
return;
}
dropped.store(true, std::memory_order_relaxed);
totalsFull.store(true, std::memory_order_relaxed);
}

const std::string name;
Expand All @@ -242,7 +250,9 @@ class TraceThread {
const std::unique_ptr<Slice[]> slices;
const std::size_t capacity;
std::atomic<std::size_t> used{0};
std::atomic<bool> dropped{false};
std::atomic<std::uint64_t> lost{0};
std::atomic<Clock::rep> firstLost{0};
std::atomic<bool> totalsFull{false};
std::array<Entry, 512> entries;
bool sampled = false;
std::uint64_t sampledPresent = 0;
Expand Down Expand Up @@ -344,6 +354,10 @@ class TraceFormat {
}
output += text;
}
if (thread.eventsFullAt) {
event("trace buffer full", "i", *thread.eventsFullAt, tid);
output += ",\"s\":\"t\",\"args\":{\"events_lost\":" + std::to_string(thread.eventsLost) + "}}";
}
}
for (std::size_t index = 0; index + 1 < presents.size(); ++index) {
event("present", "i", presents[index], presenter + 1);
Expand Down Expand Up @@ -423,8 +437,22 @@ class TraceFormat {
bool firstWorker = true;
std::map<std::string, Clock::duration> phases;
bool dropped = false;
struct DroppedThread {
std::string name;
std::uint64_t eventsLost = 0;
std::optional<Clock::time_point> eventsFullAt;
bool totalsFull = false;
};
std::vector<DroppedThread> droppedList;
for (const auto& thread : threads) {
dropped = dropped || thread.dropped;
if (thread.eventsLost != 0 || thread.totalsFull) {
auto entry = std::find_if(droppedList.begin(), droppedList.end(), [&](const DroppedThread& candidate) { return candidate.name == thread.name; });
if (entry == droppedList.end()) entry = droppedList.insert(droppedList.end(), DroppedThread{thread.name});
entry->eventsLost += thread.eventsLost;
if (thread.eventsLost != 0 && thread.eventsFullAt && (!entry->eventsFullAt || *thread.eventsFullAt < *entry->eventsFullAt)) entry->eventsFullAt = thread.eventsFullAt;
entry->totalsFull = entry->totalsFull || thread.totalsFull;
}
Clock::duration busy{};
for (const auto& total : thread.totals) {
const std::string_view scope = total.scope;
Expand All @@ -447,6 +475,20 @@ class TraceFormat {
}
output += "},\"trace_dropped\":";
output += dropped ? "true" : "false";
if (!droppedList.empty()) {
std::string droppedThreads;
for (const auto& entry : droppedList) {
if (!droppedThreads.empty()) droppedThreads += ',';
AppendString(droppedThreads, entry.name);
droppedThreads += ":{";
if (entry.eventsLost != 0) {
if (entry.eventsFullAt && !presents.empty() && *entry.eventsFullAt >= presents.front() && *entry.eventsFullAt <= presents.back()) droppedThreads += "\"events_full_after_s\":" + number(std::chrono::duration<double>(*entry.eventsFullAt - presents.front()).count()) + ",";
droppedThreads += "\"events_lost\":" + std::to_string(entry.eventsLost) + ",";
}
droppedThreads += std::string("\"totals_full\":") + (entry.totalsFull ? "true" : "false") + "}";
}
output += ",\"trace_dropped_threads\":{" + droppedThreads + "}";
}
output += ",\"trace\":";
AppendString(output, summary.trace);
output += "}\n";
Expand Down Expand Up @@ -483,7 +525,7 @@ class PerformanceTrace {
if (!buffering.load(std::memory_order_relaxed)) return;
std::lock_guard lock(gpuMutex);
if (gpuRanges.size() == gpuRanges.capacity()) {
gpuDropped = true;
++gpuLost;
return;
}
gpuRanges.push_back({name, beginNs, endNs, batch});
Expand All @@ -509,7 +551,7 @@ class PerformanceTrace {
inline static thread_local TraceThread* local = nullptr;
inline static std::mutex gpuMutex;
inline static std::vector<TraceGpuRange> gpuRanges;
inline static bool gpuDropped = false;
inline static std::uint64_t gpuLost = 0;
};

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ void PerformanceTrace::Present(std::uint32_t width, std::uint32_t height, const
{
std::lock_guard gpu(gpuMutex);
gpuRanges.clear();
gpuLost = 0;
gpuRanges.reserve(GpuRangeCapacity);
}
state.presents.push_back(now);
Expand Down Expand Up @@ -256,7 +257,7 @@ void PerformanceTrace::Present(std::uint32_t width, std::uint32_t height, const
if (state.gpuClock && device != nullptr && device->CalibrateClock(clock.gpuNs, clock.cpu)) {
std::lock_guard gpu(gpuMutex);
gpuSlices = TraceFormat::GpuSlices(gpuRanges, *state.gpuClock, clock, state.presents.front());
snapshots.push_back({"gpu", gpuSlices, {}, gpuDropped});
snapshots.push_back({"gpu", gpuSlices, {}, gpuLost != 0, gpuLost});
}
const auto process = processId();
auto summary = identity(width, height, device);
Expand Down
79 changes: 79 additions & 0 deletions core/libs/prx/libSceAgcDriver/tests/PerformanceTrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,31 @@ void BufferCap() {
for (int index = 0; index < 3; ++index) thread.Add("Driver.Packet", nullptr, origin + milliseconds(index), origin + milliseconds(index) + microseconds(30));
const auto snapshot = thread.Collect();
Check(snapshot.slices.size() == 2 && snapshot.dropped, "a full buffer did not stop and set the dropped flag");
Check(snapshot.eventsLost == 1 && snapshot.eventsFullAt == origin + milliseconds(2) && !snapshot.totalsFull, "a full buffer did not report the lost slice and its time apart from the totals");
Check(snapshot.totals.size() == 1 && snapshot.totals[0].count == 3 && snapshot.totals[0].length == microseconds(90), "totals missed a slice past the cap");
}

void LostEventsCount() {
std::atomic<std::uint64_t> presents{1};
TraceThread thread("queue worker 0x0", presents, 2);
const auto origin = Clock::now();
Check(!thread.Collect().eventsFullAt && thread.Collect().eventsLost == 0, "a thread that lost nothing reported a loss");
for (int index = 0; index < 10; ++index) thread.Add("Driver.Packet", nullptr, origin + milliseconds(index), origin + milliseconds(index) + microseconds(30));
const auto snapshot = thread.Collect();
Check(snapshot.eventsLost == 8 && snapshot.eventsFullAt == origin + milliseconds(2), "the lost slices were not counted from the first one");
}

void TotalsOverflow() {
static char scopes[600];
std::atomic<std::uint64_t> presents{1};
TraceThread thread("presenter", presents, 4096);
const auto origin = Clock::now();
for (const auto& scope : scopes) thread.Add(&scope, nullptr, origin, origin + microseconds(30));
const auto snapshot = thread.Collect();
Check(snapshot.totalsFull && snapshot.dropped && snapshot.eventsLost == 0 && !snapshot.eventsFullAt, "a full totals table was not reported apart from the event buffer");
Check(snapshot.totals.size() == 512 && snapshot.slices.size() == 600, "a full totals table stopped the event buffer or kept more than its entries");
}

void CapacitySettings() {
Check(TraceThread::ParseCapacity(nullptr) == TraceThread::DefaultCapacity, "the default event capacity changed");
Check(TraceThread::ParseCapacity("1") == 1, "the smallest event capacity was rejected");
Expand Down Expand Up @@ -152,6 +174,22 @@ void BatchIds() {
Check(TraceFormat::GpuSlices(ranges, first, last, origin)[0].batch == 3, "a GPU range lost its batch id");
}

void FullBufferEvents() {
const auto origin = Clock::now();
const std::vector<TraceThread::Slice> slices{{"Driver.Packet", nullptr, origin + milliseconds(1), microseconds(30)}};
TraceThread::Snapshot snapshot{"queue worker 0x0", slices, {}, true, 7, origin + milliseconds(3), false};
const std::vector<TraceThread::Snapshot> threads{snapshot};
const std::vector<Clock::time_point> presents{origin, origin + milliseconds(16)};
const auto json = TraceFormat::Events(7, origin, threads, 0, presents, 5);
const std::string expected =
"{\"name\":\"trace buffer full\",\"ph\":\"i\",\"ts\":3000.000,\"pid\":7,\"tid\":1,\"s\":\"t\",\"args\":{\"events_lost\":7}},\n"
"{\"name\":\"present\"";
Check(json.find(expected) != std::string::npos, "the cut-off instant event is missing or has the wrong shape");
snapshot.eventsLost = 0;
snapshot.eventsFullAt.reset();
Check(TraceFormat::Events(7, origin, std::vector<TraceThread::Snapshot>{snapshot}, 0, presents, 5).find("trace buffer full") == std::string::npos, "a thread that lost nothing got a cut-off event");
}

void SummaryFields() {
const auto origin = Clock::now();
AgcDriver::TraceSummary summary;
Expand Down Expand Up @@ -184,6 +222,24 @@ void SummaryFields() {
auto dropped = threads;
dropped[1].dropped = true;
Check(TraceFormat::Summary(summary, dropped, presents).find("\"trace_dropped\":true") != std::string::npos, "a dropped thread did not mark the summary");
Check(line.find("trace_dropped_threads") == std::string::npos, "a summary without drops listed threads");
dropped[0].dropped = true;
dropped[0].eventsLost = 2240000;
dropped[0].eventsFullAt = presents.front() + milliseconds(32);
dropped[1].totalsFull = true;
const auto detailed = TraceFormat::Summary(summary, dropped, presents);
Check(detailed.find("\"trace_dropped\":true,\"trace_dropped_threads\":{\"queue worker 0x0\":{\"events_full_after_s\":0.032,\"events_lost\":2240000,\"totals_full\":false},\"presenter\":{\"totals_full\":true}},\"trace\":") != std::string::npos, "the dropped threads were not listed with their reasons");
auto shared = threads;
shared.push_back({"shader prepare", {}, {}, true});
shared.push_back({"shader prepare", {}, {}, true});
shared[2].eventsLost = 5;
shared[2].eventsFullAt = presents.front() + milliseconds(30);
shared[3].eventsLost = 7;
shared[3].eventsFullAt = presents.front() + milliseconds(10);
shared[3].totalsFull = true;
const auto merged = TraceFormat::Summary(summary, shared, presents);
Check(merged.find("\"trace_dropped_threads\":{\"shader prepare\":{\"events_full_after_s\":0.010,\"events_lost\":12,\"totals_full\":true}},\"trace\":") != std::string::npos, "threads with one name were not merged into one entry");
Check(merged.find("shader prepare") == merged.rfind("shader prepare"), "threads with one name were listed twice");
}

void GpuClock() {
Expand Down Expand Up @@ -271,6 +327,25 @@ void IntervalSummaryFields() {
Check(line == expected, "interval summary line changed");
}

void IntervalDroppedThreads() {
const auto origin = Clock::now();
AgcDriver::TraceSummary summary;
summary.interval = std::make_pair(30.0, 60.0);
std::vector<TraceThread::Snapshot> threads{{"queue worker 0x0", {}, {}, true, 5}};
const std::vector<Clock::time_point> presents{origin + milliseconds(100), origin + milliseconds(116)};
threads[0].eventsFullAt = origin + milliseconds(40);
auto line = TraceFormat::Summary(summary, threads, presents);
Check(line.find("\"queue worker 0x0\":{\"events_lost\":5,\"totals_full\":false}") != std::string::npos, "a loss before the interval printed its time");
threads[0].eventsFullAt = origin + milliseconds(104);
line = TraceFormat::Summary(summary, threads, presents);
Check(line.find("{\"events_full_after_s\":0.004,\"events_lost\":5,") != std::string::npos, "a loss inside the interval lost its time");
threads[0].eventsFullAt = origin + milliseconds(120);
line = TraceFormat::Summary(summary, threads, presents);
Check(line.find("\"queue worker 0x0\":{\"events_lost\":5,\"totals_full\":false}") != std::string::npos && line.find("events_full_after_s") == std::string::npos, "a loss after the last present printed its time");
line = TraceFormat::Summary(summary, threads, {});
Check(line.find("\"queue worker 0x0\":{\"events_lost\":5,\"totals_full\":false}") != std::string::npos && line.find("events_full_after_s") == std::string::npos, "an interval without presents printed a loss time");
}

void GzipFile() {
std::string text;
for (int index = 0; index < 20000; ++index) text += "{\"name\":\"Driver.Packet\",\"ph\":\"X\",\"ts\":" + std::to_string(index) + "}\n";
Expand Down Expand Up @@ -323,7 +398,10 @@ int main() {
WindowBySeconds();
WindowRejects();
BufferCap();
LostEventsCount();
TotalsOverflow();
CapacitySettings();
FullBufferEvents();
FoldShortSlices();
EscapeStrings();
EventsShape();
Expand All @@ -334,6 +412,7 @@ int main() {
SummaryOnlyThread();
TotalsSince();
IntervalSummaryFields();
IntervalDroppedThreads();
GzipFile();
AppendLines();
std::cout << "performance trace tests passed\n";
Expand Down
Loading
Loading