diff options
author | yuryalekseev <yuryalekseev@yandex-team.com> | 2023-05-20 09:06:48 +0300 |
---|---|---|
committer | yuryalekseev <yuryalekseev@yandex-team.com> | 2023-05-20 09:06:48 +0300 |
commit | dde72ac0b132311e33a3c35abd3d01965e68a9d8 (patch) | |
tree | 4f84c533bc5884bbf99ee5187ff04396b0a18c59 /library/cpp/yt | |
parent | 99cebf961f4e56be130135abfa4ab02169cc4dda (diff) | |
download | ydb-dde72ac0b132311e33a3c35abd3d01965e68a9d8.tar.gz |
YT-17341: Use thread name instead of thread id in error origins.
Diffstat (limited to 'library/cpp/yt')
-rw-r--r-- | library/cpp/yt/misc/CMakeLists.darwin-x86_64.txt | 1 | ||||
-rw-r--r-- | library/cpp/yt/misc/CMakeLists.linux-aarch64.txt | 1 | ||||
-rw-r--r-- | library/cpp/yt/misc/CMakeLists.linux-x86_64.txt | 1 | ||||
-rw-r--r-- | library/cpp/yt/misc/CMakeLists.windows-x86_64.txt | 1 | ||||
-rw-r--r-- | library/cpp/yt/misc/thread_name.cpp | 37 | ||||
-rw-r--r-- | library/cpp/yt/misc/thread_name.h | 24 |
6 files changed, 65 insertions, 0 deletions
diff --git a/library/cpp/yt/misc/CMakeLists.darwin-x86_64.txt b/library/cpp/yt/misc/CMakeLists.darwin-x86_64.txt index 92907b63dc..bb8ae6f368 100644 --- a/library/cpp/yt/misc/CMakeLists.darwin-x86_64.txt +++ b/library/cpp/yt/misc/CMakeLists.darwin-x86_64.txt @@ -19,5 +19,6 @@ target_link_libraries(cpp-yt-misc PUBLIC target_sources(cpp-yt-misc PRIVATE ${CMAKE_SOURCE_DIR}/library/cpp/yt/misc/guid.cpp ${CMAKE_SOURCE_DIR}/library/cpp/yt/misc/source_location.cpp + ${CMAKE_SOURCE_DIR}/library/cpp/yt/misc/thread_name.cpp ${CMAKE_SOURCE_DIR}/library/cpp/yt/misc/variant.cpp ) diff --git a/library/cpp/yt/misc/CMakeLists.linux-aarch64.txt b/library/cpp/yt/misc/CMakeLists.linux-aarch64.txt index 9ed005067b..7715f3febe 100644 --- a/library/cpp/yt/misc/CMakeLists.linux-aarch64.txt +++ b/library/cpp/yt/misc/CMakeLists.linux-aarch64.txt @@ -20,5 +20,6 @@ target_link_libraries(cpp-yt-misc PUBLIC target_sources(cpp-yt-misc PRIVATE ${CMAKE_SOURCE_DIR}/library/cpp/yt/misc/guid.cpp ${CMAKE_SOURCE_DIR}/library/cpp/yt/misc/source_location.cpp + ${CMAKE_SOURCE_DIR}/library/cpp/yt/misc/thread_name.cpp ${CMAKE_SOURCE_DIR}/library/cpp/yt/misc/variant.cpp ) diff --git a/library/cpp/yt/misc/CMakeLists.linux-x86_64.txt b/library/cpp/yt/misc/CMakeLists.linux-x86_64.txt index 9ed005067b..7715f3febe 100644 --- a/library/cpp/yt/misc/CMakeLists.linux-x86_64.txt +++ b/library/cpp/yt/misc/CMakeLists.linux-x86_64.txt @@ -20,5 +20,6 @@ target_link_libraries(cpp-yt-misc PUBLIC target_sources(cpp-yt-misc PRIVATE ${CMAKE_SOURCE_DIR}/library/cpp/yt/misc/guid.cpp ${CMAKE_SOURCE_DIR}/library/cpp/yt/misc/source_location.cpp + ${CMAKE_SOURCE_DIR}/library/cpp/yt/misc/thread_name.cpp ${CMAKE_SOURCE_DIR}/library/cpp/yt/misc/variant.cpp ) diff --git a/library/cpp/yt/misc/CMakeLists.windows-x86_64.txt b/library/cpp/yt/misc/CMakeLists.windows-x86_64.txt index d05719a445..0d302cdb6d 100644 --- a/library/cpp/yt/misc/CMakeLists.windows-x86_64.txt +++ b/library/cpp/yt/misc/CMakeLists.windows-x86_64.txt @@ -16,5 +16,6 @@ target_link_libraries(cpp-yt-misc PUBLIC target_sources(cpp-yt-misc PRIVATE ${CMAKE_SOURCE_DIR}/library/cpp/yt/misc/guid.cpp ${CMAKE_SOURCE_DIR}/library/cpp/yt/misc/source_location.cpp + ${CMAKE_SOURCE_DIR}/library/cpp/yt/misc/thread_name.cpp ${CMAKE_SOURCE_DIR}/library/cpp/yt/misc/variant.cpp ) diff --git a/library/cpp/yt/misc/thread_name.cpp b/library/cpp/yt/misc/thread_name.cpp new file mode 100644 index 0000000000..372e14462e --- /dev/null +++ b/library/cpp/yt/misc/thread_name.cpp @@ -0,0 +1,37 @@ +#include "thread_name.h" + +#include <util/generic/string.h> +#include <util/system/thread.h> + +#include <algorithm> +#include <cstring> + +namespace NYT { + +//////////////////////////////////////////////////////////////////////////////// + +TString TThreadName::ToString() const +{ + // Buffer is zero terminated. + return Buffer.data(); +} + +//////////////////////////////////////////////////////////////////////////////// + +// This function uses cached TThread::CurrentThreadName() result +TThreadName GetCurrentThreadName() +{ + static thread_local TThreadName threadName; + if (threadName.Length == 0) { + if (auto name = TThread::CurrentThreadName()) { + auto length = std::min<int>(TThreadName::BufferCapacity - 1, name.length()); + threadName.Length = length; + ::memcpy(threadName.Buffer.data(), name.data(), length); + } + } + return threadName; +} + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT diff --git a/library/cpp/yt/misc/thread_name.h b/library/cpp/yt/misc/thread_name.h new file mode 100644 index 0000000000..5f87cb1fd8 --- /dev/null +++ b/library/cpp/yt/misc/thread_name.h @@ -0,0 +1,24 @@ +#pragma once + +#include <util/generic/string.h> + +#include <array> + +namespace NYT { + +//////////////////////////////////////////////////////////////////////////////// + +struct TThreadName +{ + static constexpr int BufferCapacity = 16; // including zero terminator + std::array<char, BufferCapacity> Buffer{}; // zero-terminated + int Length; // not including zero terminator + + TString ToString() const; +}; + +TThreadName GetCurrentThreadName(); + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT |