2016-12-24 01:03:10 +00:00
|
|
|
#include <pthread.h>
|
2019-02-10 17:12:22 +00:00
|
|
|
|
2021-04-28 23:32:41 +00:00
|
|
|
#if defined(__APPLE__) || defined(OS_SUNOS)
|
2016-12-24 01:03:10 +00:00
|
|
|
#elif defined(__FreeBSD__)
|
2019-02-10 17:12:22 +00:00
|
|
|
#include <pthread_np.h>
|
2016-12-24 01:03:10 +00:00
|
|
|
#else
|
2019-02-10 17:12:22 +00:00
|
|
|
#include <sys/prctl.h>
|
2016-10-26 22:27:38 +00:00
|
|
|
#endif
|
2019-02-10 17:12:22 +00:00
|
|
|
|
2018-06-13 19:01:07 +00:00
|
|
|
#include <cstring>
|
2016-12-24 01:03:10 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <Common/setThreadName.h>
|
2015-09-24 18:54:21 +00:00
|
|
|
|
2019-02-10 17:12:22 +00:00
|
|
|
|
2018-06-01 11:58:17 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int PTHREAD_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-24 18:54:21 +00:00
|
|
|
|
2021-09-02 18:02:35 +00:00
|
|
|
/// Cache thread_name to avoid prctl(PR_GET_NAME) for query_log/text_log
|
|
|
|
static thread_local std::string thread_name;
|
|
|
|
|
|
|
|
|
2015-09-24 18:54:21 +00:00
|
|
|
void setThreadName(const char * name)
|
|
|
|
{
|
2019-02-10 17:12:22 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
if (strlen(name) > 15)
|
|
|
|
throw DB::Exception("Thread name cannot be longer than 15 bytes", DB::ErrorCodes::PTHREAD_ERROR);
|
|
|
|
#endif
|
|
|
|
|
2020-04-07 08:33:49 +00:00
|
|
|
#if defined(OS_FREEBSD)
|
2017-04-01 07:20:54 +00:00
|
|
|
pthread_set_name_np(pthread_self(), name);
|
2020-04-07 08:33:49 +00:00
|
|
|
if ((false))
|
|
|
|
#elif defined(OS_DARWIN)
|
2017-04-01 07:20:54 +00:00
|
|
|
if (0 != pthread_setname_np(name))
|
2021-04-28 23:32:41 +00:00
|
|
|
#elif defined(OS_SUNOS)
|
|
|
|
if (0 != pthread_setname_np(pthread_self(), name))
|
2016-12-24 01:03:10 +00:00
|
|
|
#else
|
2017-04-01 07:20:54 +00:00
|
|
|
if (0 != prctl(PR_SET_NAME, name, 0, 0, 0))
|
2016-10-26 22:27:38 +00:00
|
|
|
#endif
|
2018-11-21 20:56:37 +00:00
|
|
|
DB::throwFromErrno("Cannot set thread name with prctl(PR_SET_NAME, ...)", DB::ErrorCodes::PTHREAD_ERROR);
|
2021-09-02 18:02:35 +00:00
|
|
|
|
|
|
|
thread_name = name;
|
2018-06-01 11:58:17 +00:00
|
|
|
}
|
|
|
|
|
2021-09-02 18:02:35 +00:00
|
|
|
const std::string & getThreadName()
|
2018-06-01 11:58:17 +00:00
|
|
|
{
|
2021-09-02 18:02:35 +00:00
|
|
|
if (!thread_name.empty())
|
|
|
|
return thread_name;
|
|
|
|
|
|
|
|
thread_name.resize(16);
|
2018-06-01 11:58:17 +00:00
|
|
|
|
2021-04-28 23:32:41 +00:00
|
|
|
#if defined(__APPLE__) || defined(OS_SUNOS)
|
2021-09-02 18:02:35 +00:00
|
|
|
if (pthread_getname_np(pthread_self(), thread_name.data(), thread_name.size()))
|
2018-08-21 15:56:50 +00:00
|
|
|
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
|
2021-09-02 18:02:35 +00:00
|
|
|
// if (pthread_get_name_np(pthread_self(), thread_name.data(), thread_name.size()))
|
2018-08-21 15:56:50 +00:00
|
|
|
// throw DB::Exception("Cannot get thread name with pthread_get_name_np()", DB::ErrorCodes::PTHREAD_ERROR);
|
2018-06-01 11:58:17 +00:00
|
|
|
#else
|
2021-09-02 18:02:35 +00:00
|
|
|
if (0 != prctl(PR_GET_NAME, thread_name.data(), 0, 0, 0))
|
2018-11-21 20:56:37 +00:00
|
|
|
DB::throwFromErrno("Cannot get thread name with prctl(PR_GET_NAME)", DB::ErrorCodes::PTHREAD_ERROR);
|
2018-08-21 15:56:50 +00:00
|
|
|
#endif
|
2018-06-01 11:58:17 +00:00
|
|
|
|
2021-09-02 18:02:35 +00:00
|
|
|
thread_name.resize(std::strlen(thread_name.data()));
|
|
|
|
return thread_name;
|
2015-09-24 18:54:21 +00:00
|
|
|
}
|