relay: Make the max pending tasks per CPU a consensus parameter

Until now, there was this magic number (64) used as the maximum number
of tasks a CPU worker can take at once.

This commit makes it a consensus parameter so our future selves can
think of a better value depending on network conditions.

Part of #40704

Signed-off-by: David Goulet <dgoulet@torproject.org>
This commit is contained in:
David Goulet
2022-11-09 12:49:23 -05:00
parent 9c8c7804d5
commit e3f6908984
5 changed files with 51 additions and 8 deletions
+38 -4
View File
@@ -32,6 +32,7 @@
#include "feature/relay/onion_queue.h"
#include "feature/stats/rephist.h"
#include "feature/relay/router.h"
#include "feature/nodelist/networkstatus.h"
#include "lib/evloop/workqueue.h"
#include "core/crypto/onion_crypto.h"
@@ -75,8 +76,42 @@ worker_state_free_void(void *arg)
static replyqueue_t *replyqueue = NULL;
static threadpool_t *threadpool = NULL;
static int total_pending_tasks = 0;
static int max_pending_tasks = 128;
static uint32_t total_pending_tasks = 0;
static uint32_t max_pending_tasks = 128;
/** Return the consensus parameter max pending tasks per CPU. */
static uint32_t
get_max_pending_tasks_per_cpu(const networkstatus_t *ns)
{
/* Total voodoo. Can we make this more sensible? Maybe, that is why we made it
* a consensus parameter so our future self can figure out this magic. */
#define MAX_PENDING_TASKS_PER_CPU_DEFAULT 64
#define MAX_PENDING_TASKS_PER_CPU_MIN 1
#define MAX_PENDING_TASKS_PER_CPU_MAX INT32_MAX
return networkstatus_get_param(ns, "max_pending_tasks_per_cpu",
MAX_PENDING_TASKS_PER_CPU_DEFAULT,
MAX_PENDING_TASKS_PER_CPU_MIN,
MAX_PENDING_TASKS_PER_CPU_MAX);
}
/** Set the max pending tasks per CPU worker. This uses the consensus to check
* for the allowed number per CPU. The ns parameter can be NULL as in that no
* consensus is available at the time of setting this value. */
static void
set_max_pending_tasks(const networkstatus_t *ns)
{
max_pending_tasks =
get_num_cpus(get_options()) * get_max_pending_tasks_per_cpu(ns);
}
/** Called when the consensus has changed. */
void
cpuworker_consensus_has_changed(const networkstatus_t *ns)
{
tor_assert(ns);
set_max_pending_tasks(ns);
}
/** Initialize the cpuworker subsystem. It is OK to call this more than once
* during Tor's lifetime.
@@ -106,8 +141,7 @@ cpu_init(void)
tor_assert(r == 0);
}
/* Total voodoo. Can we make this more sensible? */
max_pending_tasks = get_num_cpus(get_options()) * 64;
set_max_pending_tasks(NULL);
}
/** Magic numbers to make sure our cpuworker_requests don't grow any
+5
View File
@@ -12,8 +12,13 @@
#ifndef TOR_CPUWORKER_H
#define TOR_CPUWORKER_H
#include "feature/nodelist/networkstatus_st.h"
void cpu_init(void);
void cpuworkers_rotate_keyinfo(void);
void cpuworker_consensus_has_changed(const networkstatus_t *ns);
struct workqueue_entry_t;
enum workqueue_reply_t;
enum workqueue_priority_t;