2016-12-24 01:03:10 +00:00
|
|
|
#include <pthread.h>
|
2019-02-10 17:12:22 +00:00
|
|
|
|
2022-06-10 08:22:31 +00:00
|
|
|
#if defined(OS_DARWIN) || defined(OS_SUNOS)
|
|
|
|
#elif defined(OS_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
|
|
|
|
2021-11-13 23:39:19 +00:00
|
|
|
#define THREAD_NAME_SIZE 16
|
|
|
|
|
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
|
2021-11-13 23:39:19 +00:00
|
|
|
static thread_local char thread_name[THREAD_NAME_SIZE]{};
|
2021-09-02 18:02:35 +00:00
|
|
|
|
|
|
|
|
2015-09-24 18:54:21 +00:00
|
|
|
void setThreadName(const char * name)
|
|
|
|
{
|
2019-02-10 17:12:22 +00:00
|
|
|
#ifndef NDEBUG
|
2021-11-13 23:39:19 +00:00
|
|
|
if (strlen(name) > THREAD_NAME_SIZE - 1)
|
2019-02-10 17:12:22 +00:00
|
|
|
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)
|
2016-12-24 01:03:10 +00:00
|
|
|
pthread_set_name_np(pthread_self(), name);
|
2020-04-07 08:33:49 +00:00
|
|
|
if ((false))
|
|
|
|
#elif defined(OS_DARWIN)
|
2016-10-26 22:27:38 +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
|
|
|
|
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
|
|
|
|
2021-11-14 01:57:59 +00:00
|
|
|
memcpy(thread_name, name, 1 + strlen(name));
|
2018-06-01 11:58:17 +00:00
|
|
|
}
|
|
|
|
|
2021-11-13 23:39:19 +00:00
|
|
|
const char * getThreadName()
|
2018-06-01 11:58:17 +00:00
|
|
|
{
|
2021-11-13 23:39:19 +00:00
|
|
|
if (thread_name[0])
|
2021-09-02 18:02:35 +00:00
|
|
|
return thread_name;
|
|
|
|
|
2022-06-10 08:22:31 +00:00
|
|
|
#if defined(OS_DARWIN) || defined(OS_SUNOS)
|
2021-11-13 23:39:19 +00:00
|
|
|
if (pthread_getname_np(pthread_self(), thread_name, 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);
|
2022-06-10 08:22:31 +00:00
|
|
|
#elif defined(OS_FREEBSD)
|
2018-08-21 15:56:50 +00:00
|
|
|
// TODO: make test. freebsd will have this function soon https://freshbsd.org/commit/freebsd/r337983
|
2021-11-13 23:39:19 +00:00
|
|
|
// if (pthread_get_name_np(pthread_self(), thread_name, 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-11-13 23:39:19 +00:00
|
|
|
if (0 != prctl(PR_GET_NAME, thread_name, 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
|
|
|
return thread_name;
|
2015-09-24 18:54:21 +00:00
|
|
|
}
|