From d01d754b83a03beb372046e5db24644289341516 Mon Sep 17 00:00:00 2001 From: Jakub Berkop Date: Sat, 26 Feb 2022 22:59:26 +0100 Subject: [PATCH] Kernel: Fixed behavior of repeated calls to register_string Previously register_string would return incorrect values when called multiple times with the same input. This patch makes this function return the same index, identical strings. This change was required, as this functionality is now being used with read syscall profiling, (#12465), which uses 'register_string' to registers file path on every read syscall. --- Kernel/PerformanceEventBuffer.cpp | 24 +++++++++++++++++++----- Kernel/PerformanceEventBuffer.h | 2 +- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/Kernel/PerformanceEventBuffer.cpp b/Kernel/PerformanceEventBuffer.cpp index 8ef21aad18..98c155fd58 100644 --- a/Kernel/PerformanceEventBuffer.cpp +++ b/Kernel/PerformanceEventBuffer.cpp @@ -199,8 +199,17 @@ ErrorOr PerformanceEventBuffer::to_json_impl(Serializer& object) const { { auto strings = TRY(object.add_array("strings")); - for (auto const& it : m_strings) - TRY(strings.add(it->view())); + Vector strings_sorted_by_index; + TRY(strings_sorted_by_index.try_resize(m_strings.size())); + + for (auto& entry : m_strings) { + strings_sorted_by_index[entry.value] = const_cast(entry.key.ptr()); + } + + for (size_t i = 0; i < m_strings.size(); i++) { + TRY(strings.add(strings_sorted_by_index[i]->view())); + } + TRY(strings.finish()); } @@ -361,9 +370,14 @@ ErrorOr PerformanceEventBuffer::add_process(const Process& process, Proces ErrorOr PerformanceEventBuffer::register_string(NonnullOwnPtr string) { - FlatPtr string_id = m_strings.size(); - TRY(m_strings.try_set(move(string))); - return string_id; + auto it = m_strings.find(string); + if (it != m_strings.end()) { + return it->value; + } + + auto new_index = m_strings.size(); + TRY(m_strings.try_set(move(string), move(new_index))); + return new_index; } } diff --git a/Kernel/PerformanceEventBuffer.h b/Kernel/PerformanceEventBuffer.h index 41d36a16cf..7570dd136b 100644 --- a/Kernel/PerformanceEventBuffer.h +++ b/Kernel/PerformanceEventBuffer.h @@ -145,7 +145,7 @@ private: size_t m_count { 0 }; NonnullOwnPtr m_buffer; - HashTable> m_strings; + HashMap, size_t> m_strings; }; extern bool g_profiling_all_threads;