blob: ec2c0859e9c05026aae85e0448af783807fb3624 [file] [edit]
/*
* Copyright (C) 2013-2026 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
* Copyright (C) 2007-2013 Sourcefire, Inc.
*
* Authors: Trog, Török Edvin
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <errno.h>
#include <stdarg.h>
#include <stdint.h>
#include <string.h>
// libclamav
#include "clamav.h"
#include "others.h"
#include "mpool.h"
// common
#include "output.h"
#include "thrmgr.h"
#include "clamd_others.h"
#include "server.h"
#ifdef HAVE_MALLINFO
#include <malloc.h>
#endif
/* BSD and HP-UX need a bigger stacksize than the system default */
#if defined(C_BSD) || defined(C_HPUX) || defined(C_AIX) || (defined(C_LINUX) && !defined(__GLIBC__))
#define C_BIGSTACK 1
#endif
static work_queue_t *work_queue_new(void)
{
work_queue_t *work_q;
work_q = (work_queue_t *)malloc(sizeof(work_queue_t));
if (!work_q) {
return NULL;
}
work_q->head = work_q->tail = NULL;
work_q->item_count = 0;
work_q->popped = 0;
return work_q;
}
static int work_queue_add(work_queue_t *work_q, void *data)
{
work_item_t *work_item;
if (!work_q) {
return FALSE;
}
work_item = (work_item_t *)malloc(sizeof(work_item_t));
if (!work_item) {
return FALSE;
}
work_item->next = NULL;
work_item->data = data;
gettimeofday(&(work_item->time_queued), NULL);
if (work_q->head == NULL) {
work_q->head = work_q->tail = work_item;
work_q->item_count = 1;
} else {
work_q->tail->next = work_item;
work_q->tail = work_item;
work_q->item_count++;
}
return TRUE;
}
static void *work_queue_pop(work_queue_t *work_q)
{
work_item_t *work_item;
void *data;
if (!work_q || !work_q->head) {
return NULL;
}
work_item = work_q->head;
data = work_item->data;
work_q->head = work_item->next;
if (work_q->head == NULL) {
work_q->tail = NULL;
}
free(work_item);
work_q->item_count--;
return data;
}
static struct threadpool_list {
threadpool_t *pool;
struct threadpool_list *nxt;
} *pools = NULL;
static pthread_mutex_t pools_lock = PTHREAD_MUTEX_INITIALIZER;
struct stats_buffer {
char *data;
size_t length;
size_t capacity;
};
struct queue_stats {
long min_wait;
long max_wait;
long total_wait;
unsigned valid;
unsigned invalid;
unsigned item_count;
};
/**
* @brief Free a buffered STATS response.
*
* @param buffer Response buffer to free.
*/
static void stats_buffer_cleanup(struct stats_buffer *buffer)
{
if (!buffer)
return;
free(buffer->data);
memset(buffer, 0, sizeof(*buffer));
}
/**
* @brief Append formatted text to a buffered STATS response.
*
* @param buffer Response buffer to extend.
* @param format printf-style format string.
* @return CL_SUCCESS on success, or an error status.
*/
static cl_error_t stats_buffer_append(struct stats_buffer *buffer, const char *format, ...)
{
cl_error_t status = CL_SUCCESS;
size_t required;
va_list args;
int needed;
int written;
va_start(args, format);
needed = vsnprintf(NULL, 0, format, args);
va_end(args);
if (needed < 0)
return CL_EFORMAT;
if ((size_t)needed >= SIZE_MAX - buffer->length)
return CL_EMEM;
required = buffer->length + (size_t)needed + 1;
if (required > buffer->capacity) {
size_t capacity = buffer->capacity ? buffer->capacity : 1024;
char *data;
while (capacity < required) {
if (capacity > SIZE_MAX / 2) {
capacity = required;
break;
}
capacity *= 2;
}
data = realloc(buffer->data, capacity);
if (!data)
return CL_EMEM;
buffer->data = data;
buffer->capacity = capacity;
}
va_start(args, format);
written = vsnprintf(buffer->data + buffer->length,
buffer->capacity - buffer->length, format, args);
va_end(args);
if (written != needed) {
status = CL_EFORMAT;
goto done;
}
buffer->length += (size_t)written;
done:
return status;
}
/**
* @brief Free a task descriptor and its owned resources.
*
* The caller must ensure that the descriptor is no longer visible to STATS
* and that its worker thread has stopped using it.
*
* @param desc Task descriptor to free.
*/
static void task_desc_free(struct task_desc *desc)
{
if (!desc)
return;
free(desc->filename);
pthread_mutex_destroy(&desc->mutex);
free(desc);
}
static void add_topools(threadpool_t *t)
{
struct threadpool_list *new = malloc(sizeof(*new));
if (!new) {
logg(LOGG_ERROR, "Unable to add threadpool to list\n");
return;
}
new->pool = t;
pthread_mutex_lock(&pools_lock);
new->nxt = pools;
pools = new;
pthread_mutex_unlock(&pools_lock);
}
static void remove_frompools(threadpool_t *t)
{
struct threadpool_list *l, *prev;
struct task_desc *desc;
pthread_mutex_lock(&pools_lock);
prev = NULL;
l = pools;
while (l && l->pool != t) {
prev = l;
l = l->nxt;
}
if (!l) {
pthread_mutex_unlock(&pools_lock);
return;
}
if (prev)
prev->nxt = l->nxt;
if (l == pools)
pools = l->nxt;
free(l);
desc = t->tasks;
while (desc) {
struct task_desc *q = desc;
desc = desc->nxt;
task_desc_free(q);
}
t->tasks = NULL;
pthread_mutex_unlock(&pools_lock);
}
/**
* @brief Copy timing statistics from a work queue.
*
* The caller must hold the owning thread pool's mutex.
*
* @param queue Queue to summarize.
* @param tv_now Time used to calculate queue wait durations.
* @param stats Queue statistics to populate.
*/
static void stats_snapshot_queue(const work_queue_t *queue, const struct timeval *tv_now, struct queue_stats *stats)
{
const work_item_t *q;
memset(stats, 0, sizeof(*stats));
stats->min_wait = LONG_MAX;
stats->item_count = (unsigned)queue->item_count;
for (q = queue->head; q; q = q->next) {
long delta;
delta = tv_now->tv_usec - q->time_queued.tv_usec;
delta += (tv_now->tv_sec - q->time_queued.tv_sec) * 1000000;
if (delta < 0) {
stats->invalid++;
continue;
}
if (delta > stats->max_wait)
stats->max_wait = delta;
if (delta < stats->min_wait)
stats->min_wait = delta;
stats->total_wait += delta;
stats->valid++;
}
}
/**
* @brief Append a work queue snapshot to a buffered STATS response.
*
* @param buffer Response buffer to extend.
* @param stats Queue statistics to append.
* @return CL_SUCCESS on success, or an error status.
*/
static cl_error_t stats_buffer_append_queue(struct stats_buffer *buffer, const struct queue_stats *stats)
{
cl_error_t status;
if (!stats->item_count)
return CL_SUCCESS;
status = stats_buffer_append(buffer, " min_wait: %.6f max_wait: %.6f avg_wait: %.6f",
stats->valid ? stats->min_wait / 1e6 : 0.0,
stats->valid ? stats->max_wait / 1e6 : 0.0,
stats->valid ? stats->total_wait / (1e6 * stats->valid) : 0.0);
if (CL_SUCCESS != status)
return status;
if (stats->invalid) {
status = stats_buffer_append(buffer, " (INVALID timestamps: %u)", stats->invalid);
if (CL_SUCCESS != status)
return status;
}
if (stats->valid + stats->invalid != stats->item_count) {
status = stats_buffer_append(buffer, " (ERROR: %u != %u)", stats->valid + stats->invalid,
stats->item_count);
if (CL_SUCCESS != status)
return status;
}
return CL_SUCCESS;
}
/**
* @brief Append a synchronized thread pool snapshot.
*
* The caller must hold pools_lock, which pins the pool while its mutex is
* acquired. No code may acquire pools_lock while holding a pool mutex.
*
* @param buffer Response buffer to extend.
* @param pool Thread pool to summarize.
* @param primary Whether this is the primary pool.
* @param tv_now Time captured for the subsequent task snapshot.
* @return CL_SUCCESS on success, or an error status.
*/
static cl_error_t stats_buffer_append_pool(struct stats_buffer *buffer, threadpool_t *pool, int primary, struct timeval *tv_now)
{
struct queue_stats bulk_stats;
struct queue_stats single_stats;
cl_error_t status = CL_SUCCESS;
pool_state_t pool_state;
unsigned thr_alive;
unsigned thr_idle;
unsigned thr_max;
unsigned idle_timeout;
unsigned queue_items;
const char *state;
if (pthread_mutex_lock(&pool->pool_mutex) != 0) {
logg(LOGG_ERROR, "Unable to lock thread pool mutex for statistics\n");
return CL_ELOCK;
}
pool_state = pool->state;
thr_alive = (unsigned)pool->thr_alive;
thr_idle = (unsigned)pool->thr_idle;
thr_max = (unsigned)pool->thr_max;
idle_timeout = (unsigned)pool->idle_timeout;
queue_items = (unsigned)pool->single_queue->item_count + (unsigned)pool->bulk_queue->item_count;
gettimeofday(tv_now, NULL);
stats_snapshot_queue(pool->bulk_queue, tv_now, &bulk_stats);
stats_snapshot_queue(pool->single_queue, tv_now, &single_stats);
if (pthread_mutex_unlock(&pool->pool_mutex) != 0) {
logg(LOGG_ERROR, "Unable to unlock thread pool mutex for statistics\n");
return CL_ELOCK;
}
switch (pool_state) {
case POOL_INVALID:
state = "INVALID";
break;
case POOL_VALID:
state = "VALID";
break;
case POOL_EXIT:
state = "EXIT";
break;
default:
state = "??";
break;
}
status = stats_buffer_append(buffer, "STATE: %s %s\n", state, primary ? "PRIMARY" : "");
if (CL_SUCCESS != status)
goto done;
status = stats_buffer_append(buffer, "THREADS: live %u idle %u max %u idle-timeout %u\n",
thr_alive, thr_idle, thr_max, idle_timeout);
if (CL_SUCCESS != status)
goto done;
/* TODO: show both queues */
status = stats_buffer_append(buffer, "QUEUE: %u items", queue_items);
if (CL_SUCCESS != status)
goto done;
status = stats_buffer_append_queue(buffer, &bulk_stats);
if (CL_SUCCESS != status)
goto done;
status = stats_buffer_append_queue(buffer, &single_stats);
if (CL_SUCCESS != status)
goto done;
status = stats_buffer_append(buffer, "\n");
done:
return status;
}
/**
* @brief Create an immutable snapshot of the STATS response.
*
* Pool and task state is formatted into an owned memory buffer while the
* corresponding descriptors are protected. The global pool lock is released
* before any response bytes are written to the client, so a slow or
* non-reading client cannot block scan-worker bookkeeping.
*
* @param response Response snapshot to populate.
* @return CL_SUCCESS on success, or an error status.
*/
static cl_error_t stats_response_create(struct stats_buffer *response)
{
struct threadpool_list *l;
cl_error_t status = CL_SUCCESS;
unsigned cnt, pool_cnt = 0;
size_t pool_used = 0, pool_total = 0, seen_cnt = 0;
float mem_heap = 0, mem_mmap = 0, mem_used = 0, mem_free = 0, mem_releasable = 0;
struct cl_engine **seen = NULL;
int has_libc_memstats = 0;
memset(response, 0, sizeof(*response));
if (pthread_mutex_lock(&pools_lock) != 0) {
logg(LOGG_ERROR, "Unable to lock thread pool statistics mutex\n");
return CL_ELOCK;
}
for (cnt = 0, l = pools; l; l = l->nxt) cnt++;
status = stats_buffer_append(response, "POOLS: %u\n\n", cnt);
if (CL_SUCCESS != status)
goto unlock;
for (l = pools; l; l = l->nxt) {
threadpool_t *pool = l->pool;
struct timeval tv_now;
struct task_desc *task;
if (!pool) {
status = stats_buffer_append(response, "NULL\n\n");
if (CL_SUCCESS != status)
goto unlock;
continue;
}
status = stats_buffer_append_pool(response, pool, !l->nxt, &tv_now);
if (CL_SUCCESS != status)
goto unlock;
for (task = pool->tasks; task; task = task->nxt) {
double delta;
if (pthread_mutex_lock(&task->mutex) != 0) {
logg(LOGG_ERROR, "Unable to lock task statistics mutex\n");
status = CL_ELOCK;
goto unlock;
}
delta = tv_now.tv_usec - task->tv.tv_usec;
delta += (tv_now.tv_sec - task->tv.tv_sec) * 1000000.0;
status = stats_buffer_append(response, "\t%s %f %s\n",
task->command ? task->command : "N/A",
delta / 1e6,
task->filename ? task->filename : "");
if (CL_SUCCESS == status && task->engine) {
/* we usually have at most 2 engines so a linear
* search is good enough */
size_t i;
for (i = 0; i < seen_cnt; i++) {
if (seen[i] == task->engine)
break;
}
/* we need to count the memusage from the same
* engine only once */
if (i == seen_cnt) {
struct cl_engine **s;
/* new engine */
s = realloc(seen, (seen_cnt + 1) * sizeof(*seen));
if (!s) {
status = CL_EMEM;
} else {
seen = s;
status = cl_engine_addref((struct cl_engine *)task->engine);
if (CL_SUCCESS == status)
seen[seen_cnt++] = (struct cl_engine *)task->engine;
}
}
}
if (pthread_mutex_unlock(&task->mutex) != 0) {
logg(LOGG_ERROR, "Unable to unlock task statistics mutex\n");
status = CL_ELOCK;
}
if (CL_SUCCESS != status)
goto unlock;
}
status = stats_buffer_append(response, "\n");
if (CL_SUCCESS != status)
goto unlock;
}
unlock:
if (pthread_mutex_unlock(&pools_lock) != 0) {
logg(LOGG_ERROR, "Unable to unlock thread pool statistics mutex\n");
status = CL_ELOCK;
}
if (CL_SUCCESS == status) {
for (cnt = 0; cnt < seen_cnt; cnt++) {
size_t used, total;
if (MPOOL_GETSTATS(seen[cnt], &used, &total) != -1) {
pool_used += used;
pool_total += total;
pool_cnt++;
}
}
}
for (cnt = 0; cnt < seen_cnt; cnt++)
cl_engine_free(seen[cnt]);
free(seen);
if (CL_SUCCESS != status)
goto done;
#ifdef HAVE_MALLINFO
{
struct mallinfo inf = mallinfo();
mem_heap = inf.arena / (1024 * 1024.0);
mem_mmap = inf.hblkhd / (1024 * 1024.0);
mem_used = (inf.usmblks + inf.uordblks) / (1024 * 1024.0);
mem_free = (inf.fsmblks + inf.fordblks) / (1024 * 1024.0);
mem_releasable = inf.keepcost / (1024 * 1024.0);
has_libc_memstats = 1;
}
#endif
if (has_libc_memstats)
status = stats_buffer_append(response,
"MEMSTATS: heap %.3fM mmap %.3fM used %.3fM free %.3fM releasable %.3fM pools %u pools_used %.3fM pools_total %.3fM\n",
mem_heap, mem_mmap, mem_used, mem_free, mem_releasable, pool_cnt,
pool_used / (1024 * 1024.0), pool_total / (1024 * 1024.0));
else
status = stats_buffer_append(response,
"MEMSTATS: heap N/A mmap N/A used N/A free N/A releasable N/A pools %u pools_used %.3fM pools_total %.3fM\n",
pool_cnt, pool_used / (1024 * 1024.0), pool_total / (1024 * 1024.0));
done:
if (CL_SUCCESS != status)
stats_buffer_cleanup(response);
return status;
}
int thrmgr_printstats(int f, char term)
{
struct stats_buffer response;
if (CL_SUCCESS == stats_response_create(&response)) {
mdprintf(f, "%sEND%c", response.data, term);
stats_buffer_cleanup(&response);
} else {
mdprintf(f, "ERROR: error encountered while formatting statistics\nEND%c", term);
}
return 0;
}
void thrmgr_destroy(threadpool_t *threadpool)
{
if (!threadpool) {
return;
}
if (pthread_mutex_lock(&threadpool->pool_mutex) != 0) {
logg(LOGG_ERROR, "Mutex lock failed\n");
exit(-1);
}
if (threadpool->state != POOL_VALID) {
if (pthread_mutex_unlock(&threadpool->pool_mutex) != 0) {
logg(LOGG_ERROR, "Mutex unlock failed\n");
exit(-1);
}
return;
}
threadpool->state = POOL_EXIT;
/* wait for threads to exit */
if (threadpool->thr_alive > 0) {
if (pthread_cond_broadcast(&(threadpool->pool_cond)) != 0) {
pthread_mutex_unlock(&threadpool->pool_mutex);
return;
}
}
while (threadpool->thr_alive > 0) {
if (pthread_cond_wait(&threadpool->pool_cond, &threadpool->pool_mutex) != 0) {
pthread_mutex_unlock(&threadpool->pool_mutex);
return;
}
}
if (pthread_mutex_unlock(&threadpool->pool_mutex) != 0) {
logg(LOGG_ERROR, "Mutex unlock failed\n");
exit(-1);
}
remove_frompools(threadpool);
pthread_mutex_destroy(&(threadpool->pool_mutex));
pthread_cond_destroy(&(threadpool->idle_cond));
pthread_cond_destroy(&(threadpool->queueable_single_cond));
pthread_cond_destroy(&(threadpool->queueable_bulk_cond));
pthread_cond_destroy(&(threadpool->pool_cond));
pthread_attr_destroy(&(threadpool->pool_attr));
free(threadpool->single_queue);
free(threadpool->bulk_queue);
free(threadpool);
return;
}
void thrmgr_wait_for_threads(threadpool_t *threadpool)
{
if (!threadpool) {
return;
}
if (pthread_mutex_lock(&threadpool->pool_mutex) != 0) {
logg(LOGG_ERROR, "Mutex lock failed\n");
exit(-1);
}
if (threadpool->state != POOL_VALID) {
if (pthread_mutex_unlock(&threadpool->pool_mutex) != 0) {
logg(LOGG_ERROR, "Mutex unlock failed\n");
exit(-1);
}
return;
}
/* wait for threads to exit */
if (threadpool->thr_alive > 0) {
if (pthread_cond_broadcast(&(threadpool->pool_cond)) != 0) {
pthread_mutex_unlock(&threadpool->pool_mutex);
return;
}
}
while (threadpool->thr_alive > 0) {
if (pthread_cond_wait(&threadpool->pool_cond, &threadpool->pool_mutex) != 0) {
pthread_mutex_unlock(&threadpool->pool_mutex);
return;
}
}
/* Ok threads all exited, we can release the lock */
if (pthread_mutex_unlock(&threadpool->pool_mutex) != 0) {
logg(LOGG_ERROR, "Mutex unlock failed\n");
exit(-1);
}
return;
}
threadpool_t *thrmgr_new(int max_threads, int idle_timeout, int max_queue, void (*handler)(void *))
{
threadpool_t *threadpool;
#if defined(C_BIGSTACK)
size_t stacksize;
#endif
if (max_threads <= 0) {
return NULL;
}
threadpool = (threadpool_t *)malloc(sizeof(threadpool_t));
if (!threadpool) {
return NULL;
}
threadpool->single_queue = work_queue_new();
if (!threadpool->single_queue) {
free(threadpool);
return NULL;
}
threadpool->bulk_queue = work_queue_new();
if (!threadpool->bulk_queue) {
free(threadpool->single_queue);
free(threadpool);
return NULL;
}
threadpool->queue_max = max_queue;
threadpool->thr_max = max_threads;
threadpool->thr_alive = 0;
threadpool->thr_idle = 0;
threadpool->thr_multiscan = 0;
threadpool->idle_timeout = idle_timeout;
threadpool->handler = handler;
threadpool->tasks = NULL;
if (pthread_mutex_init(&(threadpool->pool_mutex), NULL)) {
free(threadpool->single_queue);
free(threadpool->bulk_queue);
free(threadpool);
return NULL;
}
if (pthread_cond_init(&(threadpool->pool_cond), NULL) != 0) {
pthread_mutex_destroy(&(threadpool->pool_mutex));
free(threadpool->single_queue);
free(threadpool->bulk_queue);
free(threadpool);
return NULL;
}
if (pthread_cond_init(&(threadpool->queueable_single_cond), NULL) != 0) {
pthread_cond_destroy(&(threadpool->pool_cond));
pthread_mutex_destroy(&(threadpool->pool_mutex));
free(threadpool->single_queue);
free(threadpool->bulk_queue);
free(threadpool);
return NULL;
}
if (pthread_cond_init(&(threadpool->queueable_bulk_cond), NULL) != 0) {
pthread_cond_destroy(&(threadpool->queueable_single_cond));
pthread_cond_destroy(&(threadpool->pool_cond));
pthread_mutex_destroy(&(threadpool->pool_mutex));
free(threadpool->single_queue);
free(threadpool->bulk_queue);
free(threadpool);
return NULL;
}
if (pthread_cond_init(&(threadpool->idle_cond), NULL) != 0) {
pthread_cond_destroy(&(threadpool->queueable_single_cond));
pthread_cond_destroy(&(threadpool->queueable_bulk_cond));
pthread_cond_destroy(&(threadpool->pool_cond));
pthread_mutex_destroy(&(threadpool->pool_mutex));
free(threadpool->single_queue);
free(threadpool->bulk_queue);
free(threadpool);
return NULL;
}
if (pthread_attr_init(&(threadpool->pool_attr)) != 0) {
pthread_cond_destroy(&(threadpool->queueable_single_cond));
pthread_cond_destroy(&(threadpool->queueable_bulk_cond));
pthread_cond_destroy(&(threadpool->idle_cond));
pthread_cond_destroy(&(threadpool->pool_cond));
pthread_mutex_destroy(&(threadpool->pool_mutex));
free(threadpool->single_queue);
free(threadpool->bulk_queue);
free(threadpool);
return NULL;
}
if (pthread_attr_setdetachstate(&(threadpool->pool_attr), PTHREAD_CREATE_DETACHED) != 0) {
pthread_cond_destroy(&(threadpool->queueable_single_cond));
pthread_cond_destroy(&(threadpool->queueable_bulk_cond));
pthread_attr_destroy(&(threadpool->pool_attr));
pthread_cond_destroy(&(threadpool->idle_cond));
pthread_cond_destroy(&(threadpool->pool_cond));
pthread_mutex_destroy(&(threadpool->pool_mutex));
free(threadpool->single_queue);
free(threadpool->bulk_queue);
free(threadpool);
return NULL;
}
#if defined(C_BIGSTACK)
pthread_attr_getstacksize(&(threadpool->pool_attr), &stacksize);
stacksize = stacksize + 64 * 1024;
if (stacksize < 1048576) /* at least 1MB please */
#if defined(C_HPUX) && defined(USE_MPOOL)
/* Set aside one cli_pagesize() for the stack's pthread header,
* giving a 1M region to fit a 1M large-page */
if (cli_getpagesize() < 1048576)
stacksize = 1048576 - cli_getpagesize();
else
#endif
stacksize = 1048576;
logg(LOGG_INFO, "Set stacksize to %lu\n", (unsigned long int)stacksize);
pthread_attr_setstacksize(&(threadpool->pool_attr), stacksize);
#endif
threadpool->state = POOL_VALID;
add_topools(threadpool);
return threadpool;
}
static pthread_key_t stats_tls_key;
static pthread_once_t stats_tls_key_once = PTHREAD_ONCE_INIT;
static void stats_tls_key_alloc(void)
{
pthread_key_create(&stats_tls_key, NULL);
}
static const char *IDLE_TASK = "IDLE";
void thrmgr_setactivetask(const char *filename, const char *cmd)
{
struct task_desc *desc;
char *filename_copy = NULL;
char *old_filename;
pthread_once(&stats_tls_key_once, stats_tls_key_alloc);
desc = pthread_getspecific(stats_tls_key);
if (!desc)
return;
if (filename) {
filename_copy = strdup(filename);
if (!filename_copy)
logg(LOGG_ERROR, "Unable to copy active task filename\n");
}
if (pthread_mutex_lock(&desc->mutex) != 0) {
logg(LOGG_ERROR, "Unable to lock task statistics mutex\n");
free(filename_copy);
return;
}
old_filename = desc->filename;
desc->filename = filename_copy;
if (cmd) {
if (!(cmd == IDLE_TASK && desc->command == cmd)) {
desc->command = cmd;
gettimeofday(&desc->tv, NULL);
}
}
if (pthread_mutex_unlock(&desc->mutex) != 0)
logg(LOGG_ERROR, "Unable to unlock task statistics mutex\n");
free(old_filename);
}
void thrmgr_setactiveengine(const struct cl_engine *engine)
{
struct task_desc *desc;
pthread_once(&stats_tls_key_once, stats_tls_key_alloc);
desc = pthread_getspecific(stats_tls_key);
if (!desc)
return;
if (pthread_mutex_lock(&desc->mutex) != 0) {
logg(LOGG_ERROR, "Unable to lock task statistics mutex\n");
return;
}
desc->engine = engine;
if (pthread_mutex_unlock(&desc->mutex) != 0)
logg(LOGG_ERROR, "Unable to unlock task statistics mutex\n");
}
/* Must be called without pool_mutex to preserve the pools_lock -> pool_mutex
* lock order used when collecting statistics. */
static void stats_init(threadpool_t *pool)
{
struct task_desc *desc = calloc(1, sizeof(*desc));
if (!desc)
return;
if (pthread_mutex_init(&desc->mutex, NULL) != 0) {
logg(LOGG_ERROR, "Unable to initialize task statistics mutex\n");
free(desc);
return;
}
pthread_once(&stats_tls_key_once, stats_tls_key_alloc);
if (pthread_setspecific(stats_tls_key, desc) != 0) {
logg(LOGG_ERROR, "Unable to initialize task statistics state\n");
task_desc_free(desc);
return;
}
pthread_mutex_lock(&pools_lock);
if (!pool->tasks)
pool->tasks = desc;
else {
desc->nxt = pool->tasks;
pool->tasks->prv = desc;
pool->tasks = desc;
}
pthread_mutex_unlock(&pools_lock);
}
/**
* @brief Unlink the calling worker's task descriptor.
*
* The caller must hold pools_lock. The returned descriptor is no longer
* visible to STATS and may be freed after releasing pools_lock.
*
* @param pool Thread pool owning the calling worker.
* @return The unlinked descriptor, or NULL if statistics were not initialized.
*/
static struct task_desc *stats_unlink_locked(threadpool_t *pool)
{
struct task_desc *desc = pthread_getspecific(stats_tls_key);
if (!desc)
return NULL;
if (desc->prv)
desc->prv->nxt = desc->nxt;
if (desc->nxt)
desc->nxt->prv = desc->prv;
if (pool->tasks == desc)
pool->tasks = desc->nxt;
pthread_setspecific(stats_tls_key, NULL);
return desc;
}
static inline int thrmgr_contended(threadpool_t *pool, int bulk)
{
/* don't allow bulk items to exceed 50% of queue, so that
* non-bulk items get a chance to be in the queue */
if (bulk && pool->bulk_queue->item_count >= pool->queue_max / 2)
return 1;
return pool->bulk_queue->item_count + pool->single_queue->item_count + pool->thr_alive - pool->thr_idle >= pool->queue_max;
}
/* when both queues have tasks, it will pick 4 items from the single queue,
* and 1 from the bulk */
#define SINGLE_BULK_RATIO 4
#define SINGLE_BULK_SUM (SINGLE_BULK_RATIO + 1)
/* must be called with pool_mutex held */
static void *thrmgr_pop(threadpool_t *pool)
{
void *task;
work_queue_t *first, *second;
int ratio;
if (pool->single_queue->popped < SINGLE_BULK_RATIO) {
first = pool->single_queue;
second = pool->bulk_queue;
ratio = SINGLE_BULK_RATIO;
} else {
second = pool->single_queue;
first = pool->bulk_queue;
ratio = SINGLE_BULK_SUM - SINGLE_BULK_RATIO;
}
task = work_queue_pop(first);
if (task) {
if (++first->popped == ratio)
second->popped = 0;
} else {
task = work_queue_pop(second);
if (task) {
if (++second->popped == ratio)
first->popped = 0;
}
}
if (!thrmgr_contended(pool, 0)) {
logg(LOGG_DEBUG_NV, "THRMGR: queue (single) crossed low threshold -> signaling\n");
pthread_cond_signal(&pool->queueable_single_cond);
}
if (!thrmgr_contended(pool, 1)) {
logg(LOGG_DEBUG_NV, "THRMGR: queue (bulk) crossed low threshold -> signaling\n");
pthread_cond_signal(&pool->queueable_bulk_cond);
}
return task;
}
static void *thrmgr_worker(void *arg)
{
threadpool_t *threadpool = (threadpool_t *)arg;
struct task_desc *desc;
void *job_data;
int retval, must_exit;
struct timespec timeout;
stats_init(threadpool);
/* loop looking for work */
for (;;) {
must_exit = FALSE;
if (pthread_mutex_lock(&(threadpool->pool_mutex)) != 0) {
logg(LOGG_ERROR, "Fatal: mutex lock failed\n");
exit(-2);
}
thrmgr_setactiveengine(NULL);
thrmgr_setactivetask(NULL, IDLE_TASK);
timeout.tv_sec = time(NULL) + threadpool->idle_timeout;
timeout.tv_nsec = 0;
threadpool->thr_idle++;
while (((job_data = thrmgr_pop(threadpool)) == NULL) && (threadpool->state != POOL_EXIT)) {
/* Sleep, awaiting wakeup */
pthread_cond_signal(&threadpool->idle_cond);
retval = pthread_cond_timedwait(&(threadpool->pool_cond),
&(threadpool->pool_mutex), &timeout);
if (retval == ETIMEDOUT) {
must_exit = TRUE;
break;
}
}
threadpool->thr_idle--;
if (threadpool->state == POOL_EXIT) {
must_exit = TRUE;
}
if (pthread_mutex_unlock(&(threadpool->pool_mutex)) != 0) {
logg(LOGG_ERROR, "Fatal: mutex unlock failed\n");
exit(-2);
}
if (job_data) {
threadpool->handler(job_data);
} else if (must_exit) {
/* A dispatcher can add work after the timed wait releases the
* pool mutex but before this worker retires. Make the final
* decision while holding both locks in the same order used by
* STATS so dispatch either observes this worker alive or starts
* a replacement after it has retired. */
if (pthread_mutex_lock(&pools_lock) != 0) {
logg(LOGG_ERROR, "Fatal: pools mutex lock failed\n");
exit(-2);
}
if (pthread_mutex_lock(&(threadpool->pool_mutex)) != 0) {
logg(LOGG_ERROR, "Fatal: mutex lock failed\n");
exit(-2);
}
if (threadpool->state == POOL_VALID &&
(threadpool->single_queue->item_count != 0 ||
threadpool->bulk_queue->item_count != 0)) {
if (pthread_mutex_unlock(&(threadpool->pool_mutex)) != 0) {
logg(LOGG_ERROR, "Fatal: mutex unlock failed\n");
exit(-2);
}
if (pthread_mutex_unlock(&pools_lock) != 0) {
logg(LOGG_ERROR, "Fatal: pools mutex unlock failed\n");
exit(-2);
}
continue;
}
desc = stats_unlink_locked(threadpool);
threadpool->thr_alive--;
if (threadpool->thr_alive == 0) {
/* signal that all threads are finished */
pthread_cond_broadcast(&threadpool->pool_cond);
}
if (pthread_mutex_unlock(&(threadpool->pool_mutex)) != 0) {
logg(LOGG_ERROR, "Fatal: mutex unlock failed\n");
exit(-2);
}
if (pthread_mutex_unlock(&pools_lock) != 0) {
logg(LOGG_ERROR, "Fatal: pools mutex unlock failed\n");
exit(-2);
}
task_desc_free(desc);
return NULL;
}
}
}
static int thrmgr_dispatch_internal(threadpool_t *threadpool, void *user_data, int bulk)
{
int ret = TRUE;
pthread_t thr_id;
if (!threadpool) {
return FALSE;
}
/* Lock the threadpool */
if (pthread_mutex_lock(&(threadpool->pool_mutex)) != 0) {
logg(LOGG_ERROR, "Mutex lock failed\n");
return FALSE;
}
do {
work_queue_t *queue;
pthread_cond_t *queueable_cond;
int items;
if (threadpool->state != POOL_VALID) {
ret = FALSE;
break;
}
if (bulk) {
queue = threadpool->bulk_queue;
queueable_cond = &threadpool->queueable_bulk_cond;
} else {
queue = threadpool->single_queue;
queueable_cond = &threadpool->queueable_single_cond;
}
while (thrmgr_contended(threadpool, bulk)) {
logg(LOGG_DEBUG_NV, "THRMGR: contended, sleeping\n");
pthread_cond_wait(queueable_cond, &threadpool->pool_mutex);
logg(LOGG_DEBUG_NV, "THRMGR: contended, woken\n");
}
if (!work_queue_add(queue, user_data)) {
ret = FALSE;
break;
}
items = threadpool->single_queue->item_count + threadpool->bulk_queue->item_count;
if ((threadpool->thr_idle < items) &&
(threadpool->thr_alive < threadpool->thr_max)) {
/* Start a new thread */
if (pthread_create(&thr_id, &(threadpool->pool_attr),
thrmgr_worker, threadpool) != 0) {
logg(LOGG_ERROR, "pthread_create failed\n");
} else {
threadpool->thr_alive++;
}
}
pthread_cond_signal(&(threadpool->pool_cond));
} while (0);
if (pthread_mutex_unlock(&(threadpool->pool_mutex)) != 0) {
logg(LOGG_ERROR, "Mutex unlock failed\n");
return FALSE;
}
return ret;
}
int thrmgr_dispatch(threadpool_t *threadpool, void *user_data)
{
return thrmgr_dispatch_internal(threadpool, user_data, 0);
}
int thrmgr_group_dispatch(threadpool_t *threadpool, jobgroup_t *group, void *user_data, int bulk)
{
int ret;
if (group) {
pthread_mutex_lock(&group->mutex);
group->jobs++;
logg(LOGG_DEBUG_NV, "THRMGR: active jobs for %p: %d\n", group, group->jobs);
pthread_mutex_unlock(&group->mutex);
}
if (!(ret = thrmgr_dispatch_internal(threadpool, user_data, bulk)) && group) {
pthread_mutex_lock(&group->mutex);
group->jobs--;
logg(LOGG_DEBUG_NV, "THRMGR: active jobs for %p: %d\n", group, group->jobs);
pthread_mutex_unlock(&group->mutex);
}
return ret;
}
/* returns
* 0 - this was not the last thread in the group
* 1 - this was last thread in group, group freed
*/
int thrmgr_group_finished(jobgroup_t *group, enum thrmgr_exit exitc)
{
int ret = 0;
if (!group) {
/* there is no group, we are obviously the last one */
return 1;
}
pthread_mutex_lock(&group->mutex);
logg(LOGG_DEBUG_NV, "THRMGR: group_finished: %p, %d\n", group, group->jobs);
group->exit_total++;
switch (exitc) {
case EXIT_OK:
group->exit_ok++;
break;
case EXIT_ERROR:
group->exit_error++;
break;
default:
break;
}
if (group->jobs) {
if (!--group->jobs) {
ret = 1;
} else
logg(LOGG_DEBUG_NV, "THRMGR: active jobs for %p: %d\n", group, group->jobs);
if (group->jobs == 1)
pthread_cond_signal(&group->only);
}
pthread_mutex_unlock(&group->mutex);
if (ret) {
logg(LOGG_DEBUG_NV, "THRMGR: group_finished: freeing %p\n", group);
pthread_mutex_destroy(&group->mutex);
pthread_cond_destroy(&group->only);
free(group);
}
return ret;
}
void thrmgr_group_waitforall(jobgroup_t *group, unsigned *ok, unsigned *error, unsigned *total)
{
int needexit = 0, needfree = 0;
struct timespec timeout;
pthread_mutex_lock(&group->mutex);
while (group->jobs > 1) {
pthread_mutex_lock(&exit_mutex);
needexit = progexit;
pthread_mutex_unlock(&exit_mutex);
if (needexit)
break;
/* wake to check progexit */
timeout.tv_sec = time(NULL) + 5;
timeout.tv_nsec = 0;
pthread_cond_timedwait(&group->only, &group->mutex, &timeout);
}
*ok = group->exit_ok;
*error = group->exit_error + needexit;
*total = group->exit_total;
if (!--group->jobs)
needfree = 1;
else
logg(LOGG_DEBUG_NV, "THRMGR: active jobs for %p: %d\n", group, group->jobs);
pthread_mutex_unlock(&group->mutex);
if (needfree) {
logg(LOGG_DEBUG_NV, "THRMGR: group finished freeing %p\n", group);
free(group);
}
}
jobgroup_t *thrmgr_group_new(void)
{
jobgroup_t *group;
group = malloc(sizeof(*group));
if (!group)
return NULL;
group->jobs = 1;
group->exit_ok = group->exit_error = group->exit_total = group->force_exit = 0;
if (pthread_mutex_init(&group->mutex, NULL)) {
logg(LOGG_WARNING, "Failed to initialize group mutex");
free(group);
return NULL;
}
if (pthread_cond_init(&group->only, NULL)) {
logg(LOGG_WARNING, "Failed to initialize group cond");
pthread_mutex_destroy(&group->mutex);
free(group);
return NULL;
}
logg(LOGG_DEBUG_NV, "THRMGR: new group: %p\n", group);
return group;
}
int thrmgr_group_need_terminate(jobgroup_t *group)
{
int ret;
if (group) {
pthread_mutex_lock(&group->mutex);
ret = group->force_exit;
pthread_mutex_unlock(&group->mutex);
} else
ret = 0;
pthread_mutex_lock(&exit_mutex);
ret |= progexit;
pthread_mutex_unlock(&exit_mutex);
return ret;
}
void thrmgr_group_terminate(jobgroup_t *group)
{
if (group) {
/* we may not be the last active job, now
* the last active job will free resources */
pthread_mutex_lock(&group->mutex);
group->force_exit = 1;
pthread_mutex_unlock(&group->mutex);
}
}