Skip to content

Commit

Permalink
thread names
Browse files Browse the repository at this point in the history
- `os`: Move/rename thread set names function, don't include `config.h` in header files
- `dnsperf`: Set thread name to `dnsperf-...`
  • Loading branch information
jelu committed Jan 8, 2024
1 parent 17c680d commit 8bdd480
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 30 deletions.
10 changes: 5 additions & 5 deletions src/dnsperf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1582,13 +1582,13 @@ threadinfo_init(threadinfo_t* tinfo, const config_t* config,
}
tinfo->current_sock = 0;

char name[16];
char name[32];

This comment has been minimized.

Copy link
@pspacek

pspacek Jan 8, 2024

Contributor

That's a bad idea. glibc is currently limited to 16 bytes.

This comment has been minimized.

Copy link
@jelu

jelu via email Jan 8, 2024

Author Member

This comment has been minimized.

Copy link
@jelu

jelu Jan 9, 2024

Author Member

fixed #257

PERF_THREAD(&tinfo->receiver, do_recv, tinfo);
snprintf(name, sizeof(name), "perf-recv-%04d", idx);
perf_thread_setname(tinfo->receiver, name);
snprintf(name, sizeof(name), "dnsperf-recv-%04d", idx);
perf_os_thread_setname(tinfo->receiver, name);
PERF_THREAD(&tinfo->sender, do_send, tinfo);
snprintf(name, sizeof(name), "perf-send-%04d", idx);
perf_thread_setname(tinfo->sender, name);
snprintf(name, sizeof(name), "dnsperf-send-%04d", idx);
perf_os_thread_setname(tinfo->sender, name);
}

static void
Expand Down
24 changes: 24 additions & 0 deletions src/os.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
#include <string.h>
#include <poll.h>

#if defined(HAVE_PTHREAD_NP_H)
#include <pthread_np.h>
#endif /* if defined(HAVE_PTHREAD_NP_H) */

void perf_os_blocksignal(int sig, bool block)
{
sigset_t sset;
Expand Down Expand Up @@ -149,3 +153,23 @@ perf_os_waituntilanywritable(struct perf_net_socket** socks, unsigned int nfds,
return (PERF_R_SUCCESS);
}
}

void perf_os_thread_setname(pthread_t thread, const char* name)
{
#if defined(HAVE_PTHREAD_SETNAME_NP) && !defined(__APPLE__)
/*
* macOS has pthread_setname_np but only works on the
* current thread so it's not used here
*/
#if defined(__NetBSD__)
(void)pthread_setname_np(thread, name, NULL);
#else /* if defined(__NetBSD__) */
(void)pthread_setname_np(thread, name);
#endif /* if defined(__NetBSD__) */
#elif defined(HAVE_PTHREAD_SET_NAME_NP)
(void)pthread_set_name_np(thread, name);
#else /* if defined(HAVE_PTHREAD_SETNAME_NP) && !defined(__APPLE__) */
(void)(thread);
(void)(name);
#endif /* if defined(HAVE_PTHREAD_SETNAME_NP) && !defined(__APPLE__) */
}
3 changes: 3 additions & 0 deletions src/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include <inttypes.h>
#include <stdbool.h>
#include <pthread.h>

void perf_os_blocksignal(int sig, bool block);

Expand All @@ -41,4 +42,6 @@ perf_result_t
perf_os_waituntilanywritable(struct perf_net_socket** socks, unsigned int nfds, int pipe_fd,
int64_t timeout);

void perf_os_thread_setname(pthread_t thread, const char* name);

#endif
25 changes: 0 additions & 25 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@
#ifndef PERF_UTIL_H
#define PERF_UTIL_H 1

#include "config.h"
#include <pthread.h>
#if defined(HAVE_PTHREAD_NP_H)
#include <pthread_np.h>
#endif /* if defined(HAVE_PTHREAD_NP_H) */

#include <inttypes.h>
#include <stdbool.h>
#include <string.h>
Expand Down Expand Up @@ -162,24 +157,4 @@ static __inline__ uint64_t perf_get_time(void)

#define PERF_SAFE_DIV(n, d) ((d) == 0 ? 0 : (n) / (d))

static void
perf_thread_setname(pthread_t thread, const char* name)
{
#if defined(HAVE_PTHREAD_SETNAME_NP) && !defined(__APPLE__)
/*
* macOS has pthread_setname_np but only works on the
* current thread so it's not used here
*/
#if defined(__NetBSD__)
(void)pthread_setname_np(thread, name, NULL);
#else /* if defined(__NetBSD__) */
(void)pthread_setname_np(thread, name);
#endif /* if defined(__NetBSD__) */
#elif defined(HAVE_PTHREAD_SET_NAME_NP)
(void)pthread_set_name_np(thread, name);
#else /* if defined(HAVE_PTHREAD_SETNAME_NP) && !defined(__APPLE__) */
(void)(thread);
(void)(name);
#endif /* if defined(HAVE_PTHREAD_SETNAME_NP) && !defined(__APPLE__) */
}
#endif

0 comments on commit 8bdd480

Please sign in to comment.