2016-12-24 01:03:10 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
#include <pthread.h>
|
|
|
|
#elif defined(__FreeBSD__)
|
2016-10-26 22:27:38 +00:00
|
|
|
#include <pthread.h>
|
2016-12-24 01:03:10 +00:00
|
|
|
#include <pthread_np.h>
|
|
|
|
#else
|
|
|
|
#include <sys/prctl.h>
|
2016-10-26 22:27:38 +00:00
|
|
|
#endif
|
2018-06-01 11:58:17 +00:00
|
|
|
#include <pthread.h>
|
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
|
|
|
|
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
|
|
|
|
|
|
|
void setThreadName(const char * name)
|
|
|
|
{
|
2016-12-24 01:03:10 +00:00
|
|
|
#if defined(__FreeBSD__)
|
2017-04-01 07:20:54 +00:00
|
|
|
pthread_set_name_np(pthread_self(), name);
|
|
|
|
return;
|
2016-12-24 01:03:10 +00:00
|
|
|
|
|
|
|
#elif defined(__APPLE__)
|
2017-04-01 07:20:54 +00:00
|
|
|
if (0 != pthread_setname_np(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-06-01 11:58:17 +00:00
|
|
|
DB::throwFromErrno("Cannot set thread name with prctl(PR_SET_NAME, ...)");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getThreadName()
|
|
|
|
{
|
|
|
|
std::string name(16, '\0');
|
|
|
|
|
2018-08-21 15:56:50 +00:00
|
|
|
#if defined(__APPLE__)
|
2018-08-23 18:39:21 +00:00
|
|
|
if (pthread_getname_np(pthread_self(), name.data(), 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
|
2018-08-23 18:39:21 +00:00
|
|
|
// if (pthread_get_name_np(pthread_self(), name.data(), 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
|
|
|
|
if (0 != prctl(PR_GET_NAME, name.data(), 0, 0, 0))
|
|
|
|
DB::throwFromErrno("Cannot get thread name with prctl(PR_GET_NAME)");
|
2018-08-21 15:56:50 +00:00
|
|
|
#endif
|
2018-06-01 11:58:17 +00:00
|
|
|
|
2018-06-13 19:01:07 +00:00
|
|
|
name.resize(std::strlen(name.data()));
|
2018-06-01 11:58:17 +00:00
|
|
|
return name;
|
2015-09-24 18:54:21 +00:00
|
|
|
}
|