Skip to content

Commit

Permalink
[threads] make it easier to debug thread startup / stop operations wi…
Browse files Browse the repository at this point in the history
…th pretty names

Signed-off-by: Fabio M. Di Nitto <fdinitto@redhat.com>
  • Loading branch information
fabbione committed Sep 6, 2018
1 parent a68ade6 commit f89627f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 9 deletions.
5 changes: 5 additions & 0 deletions libknet/internals.h
Expand Up @@ -337,6 +337,11 @@ typedef struct knet_transport_ops {

socklen_t sockaddr_len(const struct sockaddr_storage *ss);

struct pretty_names {
const char *name;
uint8_t val;
};

/**
* This is a kernel style list implementation.
*
Expand Down
5 changes: 0 additions & 5 deletions libknet/logging.c
Expand Up @@ -20,11 +20,6 @@
#include "logging.h"
#include "threads_common.h"

struct pretty_names {
const char *name;
uint8_t val;
};

static struct pretty_names subsystem_names[] =
{
{ "common", KNET_SUB_COMMON },
Expand Down
59 changes: 55 additions & 4 deletions libknet/threads_common.c
Expand Up @@ -64,6 +64,55 @@ int get_global_wrlock(knet_handle_t knet_h)
return pthread_rwlock_wrlock(&knet_h->global_rwlock);
}

static struct pretty_names thread_names[] =
{
{ "TX", KNET_THREAD_TX },
{ "RX", KNET_THREAD_RX },
{ "HB", KNET_THREAD_HB },
{ "PMTUD", KNET_THREAD_PMTUD },
#ifdef HAVE_NETINET_SCTP_H
{ "SCTP_LISTEN", KNET_THREAD_SCTP_LISTEN },
{ "SCTP_CONN", KNET_THREAD_SCTP_CONN },
#endif
{ "DST_LINK", KNET_THREAD_DST_LINK }
};

static struct pretty_names thread_status[] =
{
{ "stopped", KNET_THREAD_STOPPED },
{ "running", KNET_THREAD_RUNNING }
};

/*
* this seems overloaded at the moment but
* we might want to expand status checks
* to include "starting" and "stopping"
*/

static const char *get_thread_status_name(uint8_t status)
{
unsigned int i;

for (i = 0; i < KNET_THREAD_STATUS_MAX; i++) {
if (thread_status[i].val == status) {
return thread_status[i].name;
}
}
return "unknown";
}

static const char *get_thread_name(uint8_t thread_id)
{
unsigned int i;

for (i = 0; i < KNET_THREAD_MAX; i++) {
if (thread_names[i].val == thread_id) {
return thread_names[i].name;
}
}
return "unknown";
}

int set_thread_status(knet_handle_t knet_h, uint8_t thread_id, uint8_t status)
{
if (pthread_mutex_lock(&knet_h->threads_status_mutex) != 0) {
Expand All @@ -73,8 +122,8 @@ int set_thread_status(knet_handle_t knet_h, uint8_t thread_id, uint8_t status)

knet_h->threads_status[thread_id] = status;

log_debug(knet_h, KNET_SUB_HANDLE, "Updated status for thread %u to %u",
thread_id, status);
log_debug(knet_h, KNET_SUB_HANDLE, "Updated status for thread %s to %s",
get_thread_name(thread_id), get_thread_status_name(status));

pthread_mutex_unlock(&knet_h->threads_status_mutex);
return 0;
Expand All @@ -94,8 +143,10 @@ int wait_all_threads_status(knet_handle_t knet_h, uint8_t status)
found = 1;

for (i = 0; i < KNET_THREAD_MAX; i++) {
log_debug(knet_h, KNET_SUB_HANDLE, "Checking thread: %u status: %u req: %u",
i, knet_h->threads_status[i], status);
log_debug(knet_h, KNET_SUB_HANDLE, "Checking thread: %s status: %s req: %s",
get_thread_name(i),
get_thread_status_name(knet_h->threads_status[i]),
get_thread_status_name(status));
if (knet_h->threads_status[i] != status) {
found = 0;
}
Expand Down
1 change: 1 addition & 0 deletions libknet/threads_common.h
Expand Up @@ -16,6 +16,7 @@

#define KNET_THREAD_STOPPED 0
#define KNET_THREAD_RUNNING 1
#define KNET_THREAD_STATUS_MAX KNET_THREAD_RUNNING + 1

#define KNET_THREAD_TX 0
#define KNET_THREAD_RX 1
Expand Down

0 comments on commit f89627f

Please sign in to comment.