core: make gf_thread_create() easier to use

This patch creates a specific function to set the thread name using a
string format and a variable argument list, like printf().

This function is used to set the thread name from gf_thread_create(),
which now accepts a variable argument list to create the full name. It's
not necessary anymore to use a local array to build the name of the
thread. This is done automatically.

Change-Id: Idd8d01fd462c227359b96e98699f8c6d962dc17c
Updates: bz#1193929
Signed-off-by: Xavi Hernandez <xhernandez@redhat.com>
This commit is contained in:
Xavi Hernandez 2019-01-24 18:31:10 +01:00
parent 6b98735956
commit 4674678951
9 changed files with 110 additions and 82 deletions

View File

@ -3949,19 +3949,64 @@ error_return:
return ret;
}
void
gf_thread_set_vname(pthread_t thread, const char *name, va_list args)
{
char thread_name[GF_THREAD_NAME_LIMIT];
int ret;
/* Initialize the thread name with the prefix (not NULL terminated). */
memcpy(thread_name, GF_THREAD_NAME_PREFIX,
sizeof(GF_THREAD_NAME_PREFIX) - 1);
ret = vsnprintf(thread_name + sizeof(GF_THREAD_NAME_PREFIX) - 1,
sizeof(thread_name) - sizeof(GF_THREAD_NAME_PREFIX) + 1,
name, args);
if (ret < 0) {
gf_msg(THIS->name, GF_LOG_WARNING, 0, LG_MSG_PTHREAD_NAMING_FAILED,
"Failed to compose thread name ('%s')", name);
return;
}
if (ret >= sizeof(thread_name)) {
gf_msg(THIS->name, GF_LOG_WARNING, 0, LG_MSG_PTHREAD_NAMING_FAILED,
"Thread name is too long. It has been truncated ('%s')",
thread_name);
}
#ifdef GF_LINUX_HOST_OS
ret = pthread_setname_np(thread, thread_name);
#elif defined(__NetBSD__)
ret = pthread_setname_np(thread, thread_name, NULL);
#elif defined(__FreeBSD__)
pthread_set_name_np(thread, thread_name);
ret = 0;
#else
ret = ENOSYS;
#endif
if (ret != 0) {
gf_msg(THIS->name, GF_LOG_WARNING, ret, LG_MSG_PTHREAD_NAMING_FAILED,
"Could not set thread name: %s", thread_name);
}
}
void
gf_thread_set_name(pthread_t thread, const char *name, ...)
{
va_list args;
va_start(args, name);
gf_thread_set_vname(thread, name, args);
va_end(args);
}
int
gf_thread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg, const char *name)
gf_thread_vcreate(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg, const char *name,
va_list args)
{
sigset_t set, old;
int ret;
char thread_name[GF_THREAD_NAMEMAX + GF_THREAD_NAME_PREFIX_LEN] = {
0,
};
/* Max name on Linux is 16 and on NetBSD is 32
* All Gluster threads have a set prefix of gluster and hence the limit
* of 9 on GF_THREAD_NAMEMAX including the null character.
*/
sigemptyset(&old);
sigfillset(&set);
@ -3975,20 +4020,12 @@ gf_thread_create(pthread_t *thread, const pthread_attr_t *attr,
pthread_sigmask(SIG_BLOCK, &set, &old);
ret = pthread_create(thread, attr, start_routine, arg);
snprintf(thread_name, sizeof(thread_name), "%s%s", GF_THREAD_NAME_PREFIX,
name);
if (0 == ret && name) {
#ifdef GF_LINUX_HOST_OS
pthread_setname_np(*thread, thread_name);
#elif defined(__NetBSD__)
pthread_setname_np(*thread, thread_name, NULL);
#elif defined(__FreeBSD__)
pthread_set_name_np(*thread, thread_name);
#else
gf_msg(THIS->name, GF_LOG_WARNING, 0, LG_MSG_PTHREAD_NAMING_FAILED,
"Could not set thread name: %s", thread_name);
#endif
if (ret != 0) {
gf_msg(THIS->name, GF_LOG_ERROR, ret, LG_MSG_PTHREAD_FAILED,
"Thread creation failed");
ret = -1;
} else if (name != NULL) {
gf_thread_set_vname(*thread, name, args);
}
pthread_sigmask(SIG_SETMASK, &old, NULL);
@ -3996,11 +4033,27 @@ gf_thread_create(pthread_t *thread, const pthread_attr_t *attr,
return ret;
}
int
gf_thread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg, const char *name,
...)
{
va_list args;
int ret;
va_start(args, name);
ret = gf_thread_vcreate(thread, attr, start_routine, arg, name, args);
va_end(args);
return ret;
}
int
gf_thread_create_detached(pthread_t *thread, void *(*start_routine)(void *),
void *arg, const char *name)
void *arg, const char *name, ...)
{
pthread_attr_t attr;
va_list args;
int ret = -1;
ret = pthread_attr_init(&attr);
@ -4012,12 +4065,9 @@ gf_thread_create_detached(pthread_t *thread, void *(*start_routine)(void *),
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
ret = gf_thread_create(thread, &attr, start_routine, arg, name);
if (ret) {
gf_msg(THIS->name, GF_LOG_ERROR, ret, LG_MSG_PTHREAD_FAILED,
"Thread creation failed");
ret = -1;
}
va_start(args, name);
ret = gf_thread_vcreate(thread, &attr, start_routine, arg, name, args);
va_end(args);
pthread_attr_destroy(&attr);

View File

@ -781,9 +781,6 @@ event_dispatch_epoll(struct event_pool *event_pool)
int pollercount = 0;
int ret = -1;
struct event_thread_data *ev_data = NULL;
char thread_name[GF_THREAD_NAMEMAX] = {
0,
};
/* Start the configured number of pollers */
pthread_mutex_lock(&event_pool->mutex);
@ -817,10 +814,8 @@ event_dispatch_epoll(struct event_pool *event_pool)
ev_data->event_pool = event_pool;
ev_data->event_index = i + 1;
snprintf(thread_name, sizeof(thread_name), "epoll%03hx",
(i & 0x3ff));
ret = gf_thread_create(&t_id, NULL, event_dispatch_epoll_worker,
ev_data, thread_name);
ev_data, "epoll%03hx", i & 0x3ff);
if (!ret) {
event_pool->pollers[i] = t_id;
@ -883,9 +878,6 @@ event_reconfigure_threads_epoll(struct event_pool *event_pool, int value)
pthread_t t_id;
int oldthreadcount;
struct event_thread_data *ev_data = NULL;
char thread_name[GF_THREAD_NAMEMAX] = {
0,
};
pthread_mutex_lock(&event_pool->mutex);
{
@ -925,11 +917,9 @@ event_reconfigure_threads_epoll(struct event_pool *event_pool, int value)
ev_data->event_pool = event_pool;
ev_data->event_index = i + 1;
snprintf(thread_name, sizeof(thread_name), "epoll%03hx",
(i & 0x3ff));
ret = gf_thread_create(&t_id, NULL,
event_dispatch_epoll_worker, ev_data,
thread_name);
"epoll%03hx", i & 0x3ff);
if (ret) {
gf_msg("epoll", GF_LOG_WARNING, 0,
LG_MSG_START_EPOLL_THREAD_FAILED,

View File

@ -147,9 +147,8 @@ trap(void);
/* pthread related */
/* as per the man page, thread-name should be at max 16 bytes */
/* with prefix of 'glfs_' (5), we are left with 11 more bytes */
#define GF_THREAD_NAMEMAX 11
#define GF_THREAD_NAME_LIMIT 16
#define GF_THREAD_NAME_PREFIX "glfs_"
#define GF_THREAD_NAME_PREFIX_LEN 5
/*
* we could have initialized these as +ve values and treated
@ -950,10 +949,24 @@ gf_set_timestamp(const char *src, const char *dest);
int
gf_thread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg, const char *name);
void *(*start_routine)(void *), void *arg, const char *name,
...) __attribute__((__format__(__printf__, 5, 6)));
int
gf_thread_vcreate(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg, const char *name,
va_list args);
int
gf_thread_create_detached(pthread_t *thread, void *(*start_routine)(void *),
void *arg, const char *name);
void *arg, const char *name, ...)
__attribute__((__format__(__printf__, 4, 5)));
void
gf_thread_set_name(pthread_t thread, const char *name, ...)
__attribute__((__format__(__printf__, 2, 3)));
void
gf_thread_set_vname(pthread_t thread, const char *name, va_list args);
gf_boolean_t
gf_is_pid_running(int pid);
gf_boolean_t

View File

@ -710,7 +710,10 @@ gf_strTrim
gf_strstr
gf_thread_cleanup_xint
gf_thread_create
gf_thread_vcreate
gf_thread_create_detached
gf_thread_set_name
gf_thread_set_vname
gf_timer_call_after
gf_timer_call_cancel
gf_timer_registry_destroy

View File

@ -695,9 +695,6 @@ syncenv_scale(struct syncenv *env)
int scale = 0;
int i = 0;
int ret = 0;
char thread_name[GF_THREAD_NAMEMAX] = {
0,
};
pthread_mutex_lock(&env->mutex);
{
@ -717,11 +714,9 @@ syncenv_scale(struct syncenv *env)
}
env->proc[i].env = env;
snprintf(thread_name, sizeof(thread_name), "sproc%03hx",
(env->procs & 0x3ff));
ret = gf_thread_create(&env->proc[i].processor, NULL,
syncenv_processor, &env->proc[i],
thread_name);
"sproc%03hx", env->procs & 0x3ff);
if (ret)
break;
env->procs++;
@ -783,9 +778,6 @@ syncenv_new(size_t stacksize, int procmin, int procmax)
struct syncenv *newenv = NULL;
int ret = 0;
int i = 0;
char thread_name[GF_THREAD_NAMEMAX] = {
0,
};
if (!procmin || procmin < 0)
procmin = SYNCENV_PROC_MIN;
@ -814,11 +806,9 @@ syncenv_new(size_t stacksize, int procmin, int procmax)
for (i = 0; i < newenv->procmin; i++) {
newenv->proc[i].env = newenv;
snprintf(thread_name, sizeof(thread_name), "%s%d", "sproc",
(newenv->procs));
ret = gf_thread_create(&newenv->proc[i].processor, NULL,
syncenv_processor, &newenv->proc[i],
thread_name);
syncenv_processor, &newenv->proc[i], "sproc%d",
newenv->procs);
if (ret)
break;
newenv->procs++;

View File

@ -4436,9 +4436,6 @@ gf_defrag_parallel_migration_init(xlator_t *this, gf_defrag_info_t *defrag,
int thread_spawn_count = 0;
int index = 0;
pthread_t *tid = NULL;
char thread_name[GF_THREAD_NAMEMAX] = {
0,
};
if (!defrag)
goto out;
@ -4472,10 +4469,8 @@ gf_defrag_parallel_migration_init(xlator_t *this, gf_defrag_info_t *defrag,
/*Spawn Threads Here*/
while (index < thread_spawn_count) {
snprintf(thread_name, sizeof(thread_name), "dhtmig%d",
((index + 1) & 0x3ff));
ret = gf_thread_create(&(tid[index]), NULL, &gf_defrag_task,
(void *)defrag, thread_name);
(void *)defrag, "dhtmig%d", (index + 1) & 0x3ff);
if (ret != 0) {
gf_msg("DHT", GF_LOG_ERROR, ret, 0, "Thread[%d] creation failed. ",
index);

View File

@ -564,9 +564,6 @@ gf_history_consume(void *data)
{0},
};
gf_changelog_consume_data_t *curr = NULL;
char thread_name[GF_THREAD_NAMEMAX] = {
0,
};
hist_data = (gf_changelog_history_data_t *)data;
if (hist_data == NULL) {
@ -612,12 +609,10 @@ gf_history_consume(void *data)
curr->retval = 0;
memset(curr->changelog, '\0', PATH_MAX);
snprintf(thread_name, sizeof(thread_name), "clogc%03hx",
((iter + 1) & 0x3ff));
ret = gf_thread_create(&th_id[iter], NULL,
gf_changelog_consume_wrap, curr,
thread_name);
"clogc%03hx", (iter + 1) & 0x3ff);
if (ret) {
gf_msg(this->name, GF_LOG_ERROR, ret,
CHANGELOG_LIB_MSG_THREAD_CREATION_FAILED,

View File

@ -69,9 +69,6 @@ changelog_init_rpc_threads(xlator_t *this, changelog_priv_t *priv, rbuf_t *rbuf,
int j = 0;
int ret = 0;
changelog_clnt_t *conn = NULL;
char thread_name[GF_THREAD_NAMEMAX] = {
0,
};
conn = &priv->connections;
@ -111,9 +108,9 @@ changelog_init_rpc_threads(xlator_t *this, changelog_priv_t *priv, rbuf_t *rbuf,
/* spawn dispatcher threads */
for (; j < nr_dispatchers; j++) {
snprintf(thread_name, sizeof(thread_name), "clogd%03hx", (j & 0x3ff));
ret = gf_thread_create(&priv->ev_dispatcher[j], NULL,
changelog_ev_dispatch, conn, thread_name);
changelog_ev_dispatch, conn, "clogd%03hx",
j & 0x3ff);
if (ret != 0) {
changelog_cleanup_dispatchers(this, priv, j);
break;

View File

@ -813,9 +813,6 @@ __iot_workers_scale(iot_conf_t *conf)
pthread_t thread;
int ret = 0;
int i = 0;
char thread_name[GF_THREAD_NAMEMAX] = {
0,
};
for (i = 0; i < GF_FOP_PRI_MAX; i++)
scale += min(conf->queue_sizes[i], conf->ac_iot_limit[i]);
@ -833,10 +830,8 @@ __iot_workers_scale(iot_conf_t *conf)
while (diff) {
diff--;
snprintf(thread_name, sizeof(thread_name), "iotwr%03hx",
(conf->curr_count & 0x3ff));
ret = gf_thread_create(&thread, &conf->w_attr, iot_worker, conf,
thread_name);
"iotwr%03hx", conf->curr_count & 0x3ff);
if (ret == 0) {
conf->curr_count++;
gf_msg_debug(conf->this->name, 0,