Skip to content

Commit

Permalink
debug mode: only dump queue size stats when exceeding highwater mark
Browse files Browse the repository at this point in the history
In order to make the queue size stats more useful, and not be
overwhelemd with too much output, this change makes it so that queue
sizes are only printed when they exceed the previous highwater mark.

Also, this adds a slight code cleanup by removing the `#if defined`
around the callsite for `dump_queue_size_stats()`, and the function is
defined as a no-op when `QPTPOOL_QUEUE_SIZE` is undefined.
  • Loading branch information
bertschinger committed Nov 21, 2024
1 parent f2390c3 commit f722c43
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/QueuePerThreadPool.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ struct QPTPool {
#if defined(DEBUG) && defined(PER_THREAD_STATS)
struct OutputBuffers *debug_buffers;
#endif

#if defined(DEBUG) && defined(QPTPOOL_QUEUE_SIZE)
size_t queue_size_highwater; /* used when printing out queue size stats */
#endif
};

/* struct to pass into pthread_create */
Expand Down Expand Up @@ -268,29 +272,42 @@ static void claim_work(QPTPoolThreadData_t *tw) {
pthread_mutex_unlock(&tw->claimed_mutex);
}

#if defined(DEBUG) && defined (QPTPOOL_QUEUE_SIZE)
static void dump_queue_size_stats(QPTPool_t *ctx, QPTPoolThreadData_t *tw) {
#if defined(DEBUG) && defined (QPTPOOL_QUEUE_SIZE)
pthread_mutex_lock(&ctx->mutex);
pthread_mutex_lock(&print_mutex);
tw->waiting.size = tw->claimed.size;

struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
fprintf(stderr, "qptpool_size %" PRIu64 " ", since_epoch(&now) - epoch);
char buf[4096];
char *p = buf;

size_t sum = 0;
for(size_t i = 0; i < ctx->nthreads; i++) {
fprintf(stderr, "%zu ", ctx->data[i].waiting.size);
p += snprintf(p, sizeof(buf), "%zu ", ctx->data[i].waiting.size);
sum += ctx->data[i].waiting.size;
fprintf(stderr, "%zu ", ctx->data[i].deferred.size);
p += snprintf(p, sizeof(buf), "%zu ", ctx->data[i].deferred.size);
sum += ctx->data[i].deferred.size;
}
fprintf(stderr, "%zu\n", sum);

if (sum > ctx->queue_size_highwater) {
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
fprintf(stderr, "qptpool_size %" PRIu64 " ", since_epoch(&now) - epoch);

fprintf(stderr, "%s", buf);

ctx->queue_size_highwater = sum;
fprintf(stderr, "total size of all queues: %zu\n", sum);
}

tw->waiting.size = 0;
pthread_mutex_unlock(&print_mutex);
pthread_mutex_unlock(&ctx->mutex);
#else
(void) ctx;
(void) tw;
#endif
}
#endif

/*
* process_work() -
Expand Down Expand Up @@ -378,9 +395,7 @@ static void *worker_function(void *args) {
claim_work(tw);
timestamp_set_end(wf_move_queue);

#if defined(DEBUG) && defined (QPTPOOL_QUEUE_SIZE)
dump_queue_size_stats(ctx, tw);
#endif

pthread_mutex_unlock(&tw->mutex);
/* tw->waiting is now empty and can be pushed to */
Expand Down

0 comments on commit f722c43

Please sign in to comment.