From 1e4e36ff2d05c859a4f8b756ffa8735dcffd1a36 Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Sun, 14 Nov 2021 02:39:19 +0300 Subject: [PATCH 1/3] Remove thread_local std::string --- src/Common/setThreadName.cpp | 21 ++++++++++----------- src/Common/setThreadName.h | 2 +- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/Common/setThreadName.cpp b/src/Common/setThreadName.cpp index 958404b9ad1..c115a9ff993 100644 --- a/src/Common/setThreadName.cpp +++ b/src/Common/setThreadName.cpp @@ -12,6 +12,8 @@ #include #include +#define THREAD_NAME_SIZE 16 + namespace DB { @@ -23,13 +25,13 @@ namespace ErrorCodes /// Cache thread_name to avoid prctl(PR_GET_NAME) for query_log/text_log -static thread_local std::string thread_name; +static thread_local char thread_name[THREAD_NAME_SIZE]{}; void setThreadName(const char * name) { #ifndef NDEBUG - if (strlen(name) > 15) + if (strlen(name) > THREAD_NAME_SIZE - 1) throw DB::Exception("Thread name cannot be longer than 15 bytes", DB::ErrorCodes::PTHREAD_ERROR); #endif @@ -45,28 +47,25 @@ void setThreadName(const char * name) #endif DB::throwFromErrno("Cannot set thread name with prctl(PR_SET_NAME, ...)", DB::ErrorCodes::PTHREAD_ERROR); - thread_name = name; + strcpy(thread_name, name); } -const std::string & getThreadName() +const char * getThreadName() { - if (!thread_name.empty()) + if (thread_name[0]) return thread_name; - thread_name.resize(16); - #if defined(__APPLE__) || defined(OS_SUNOS) - if (pthread_getname_np(pthread_self(), thread_name.data(), thread_name.size())) + if (pthread_getname_np(pthread_self(), thread_name, THREAD_NAME_SIZE)) throw DB::Exception("Cannot get thread name with pthread_getname_np()", DB::ErrorCodes::PTHREAD_ERROR); #elif defined(__FreeBSD__) // TODO: make test. freebsd will have this function soon https://freshbsd.org/commit/freebsd/r337983 -// if (pthread_get_name_np(pthread_self(), thread_name.data(), thread_name.size())) +// if (pthread_get_name_np(pthread_self(), thread_name, THREAD_NAME_SIZE)) // throw DB::Exception("Cannot get thread name with pthread_get_name_np()", DB::ErrorCodes::PTHREAD_ERROR); #else - if (0 != prctl(PR_GET_NAME, thread_name.data(), 0, 0, 0)) + if (0 != prctl(PR_GET_NAME, thread_name, 0, 0, 0)) DB::throwFromErrno("Cannot get thread name with prctl(PR_GET_NAME)", DB::ErrorCodes::PTHREAD_ERROR); #endif - thread_name.resize(std::strlen(thread_name.data())); return thread_name; } diff --git a/src/Common/setThreadName.h b/src/Common/setThreadName.h index ea988885db2..1834ea9696f 100644 --- a/src/Common/setThreadName.h +++ b/src/Common/setThreadName.h @@ -7,4 +7,4 @@ */ void setThreadName(const char * name); -const std::string & getThreadName(); +const char * getThreadName(); From bd684f02f2deda977338b00c85a7cac51b7168ff Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Sun, 14 Nov 2021 04:57:59 +0300 Subject: [PATCH 2/3] Fix clang-tidy --- src/Common/setThreadName.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Common/setThreadName.cpp b/src/Common/setThreadName.cpp index c115a9ff993..727bf23b891 100644 --- a/src/Common/setThreadName.cpp +++ b/src/Common/setThreadName.cpp @@ -47,7 +47,7 @@ void setThreadName(const char * name) #endif DB::throwFromErrno("Cannot set thread name with prctl(PR_SET_NAME, ...)", DB::ErrorCodes::PTHREAD_ERROR); - strcpy(thread_name, name); + memcpy(thread_name, name, 1 + strlen(name)); } const char * getThreadName() From fe3ee85d802ed6604fbcbfd56e7818bbb910d801 Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Sun, 14 Nov 2021 10:04:45 +0300 Subject: [PATCH 3/3] Fix MaterializedMySQL --- .../MySQL/MaterializedMySQLSyncThread.cpp | 43 ++++++++++--------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/src/Databases/MySQL/MaterializedMySQLSyncThread.cpp b/src/Databases/MySQL/MaterializedMySQLSyncThread.cpp index 8b6161c9bd0..058708afb80 100644 --- a/src/Databases/MySQL/MaterializedMySQLSyncThread.cpp +++ b/src/Databases/MySQL/MaterializedMySQLSyncThread.cpp @@ -3,26 +3,27 @@ #if USE_MYSQL #include -# include -# include -# include -# include -# include -# include -# include -# include -# include -# include -# include -# include -# include -# include -# include -# include -# include -# include -# include -# include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include namespace DB { @@ -765,7 +766,7 @@ void MaterializedMySQLSyncThread::executeDDLAtomic(const QueryEvent & query_even bool MaterializedMySQLSyncThread::isMySQLSyncThread() { - return getThreadName() == MYSQL_BACKGROUND_THREAD_NAME; + return getThreadName() == std::string_view(MYSQL_BACKGROUND_THREAD_NAME); } void MaterializedMySQLSyncThread::setSynchronizationThreadException(const std::exception_ptr & exception)