From 53664787fbd2757ca904183f0c0d75a33847e3af Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Fri, 14 May 2021 00:43:08 +0200 Subject: [PATCH] Kernel: Correctly set the lost_samples field for the first sample This ensures that the lost_samples field is set to zero for the first sample. We didn't lose any samples before the first sample so this is the correct value. Without this Profiler gets confused and draws the graph for the process which contains the first CPU sample incorrectly (all zeroes usually). --- Kernel/PerformanceEventBuffer.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Kernel/PerformanceEventBuffer.cpp b/Kernel/PerformanceEventBuffer.cpp index 343f3e6754..730c19304a 100644 --- a/Kernel/PerformanceEventBuffer.cpp +++ b/Kernel/PerformanceEventBuffer.cpp @@ -133,6 +133,7 @@ template bool PerformanceEventBuffer::to_json_impl(Serializer& object) const { auto array = object.add_array("events"); + bool seen_first_sample = false; for (size_t i = 0; i < m_count; ++i) { auto& event = at(i); auto event_object = array.add_object(); @@ -183,7 +184,9 @@ bool PerformanceEventBuffer::to_json_impl(Serializer& object) const event_object.add("pid", event.pid); event_object.add("tid", event.tid); event_object.add("timestamp", event.timestamp); - event_object.add("lost_samples", i != 0 ? event.lost_samples : 0); + event_object.add("lost_samples", seen_first_sample ? event.lost_samples : 0); + if (event.type == PERF_EVENT_SAMPLE) + seen_first_sample = true; auto stack_array = event_object.add_array("stack"); for (size_t j = 0; j < event.stack_size; ++j) { stack_array.add(event.stack[j]);