Tor 0.4.9.0-alpha-dev
Data Structures | Macros | Functions | Variables
cpuworker.c File Reference

Uses the workqueue/threadpool code to farm CPU-intensive activities out to subprocesses. More...

#include "core/or/or.h"
#include "core/or/channel.h"
#include "core/or/circuitlist.h"
#include "core/or/connection_or.h"
#include "core/or/congestion_control_common.h"
#include "core/or/congestion_control_flow.h"
#include "app/config/config.h"
#include "core/mainloop/cpuworker.h"
#include "lib/crypt_ops/crypto_rand.h"
#include "lib/crypt_ops/crypto_util.h"
#include "core/or/onion.h"
#include "feature/relay/circuitbuild_relay.h"
#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"
#include "core/or/or_circuit_st.h"

Go to the source code of this file.

Data Structures

struct  worker_state_t
 
struct  cpuworker_request_t
 
struct  cpuworker_reply_t
 
struct  cpuworker_job_t
 

Macros

#define worker_state_free(ws)    FREE_AND_NULL(worker_state_t, worker_state_free_, (ws))
 
#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
 
#define CPUWORKER_REQUEST_MAGIC   0xda4afeed
 
#define CPUWORKER_REPLY_MAGIC   0x5eedf00d
 
#define MAX_BELIEVABLE_ONIONSKIN_DELAY   (2*1000*1000)
 

Functions

static void queue_pending_tasks (void)
 
static void * worker_state_new (void *arg)
 
static void worker_state_free_ (worker_state_t *ws)
 
static void worker_state_free_void (void *arg)
 
static uint32_t get_max_pending_tasks_per_cpu (const networkstatus_t *ns)
 
static void set_max_pending_tasks (const networkstatus_t *ns)
 
void cpuworker_consensus_has_changed (const networkstatus_t *ns)
 
void cpuworker_init (void)
 
unsigned int cpuworker_get_n_threads (void)
 
static workqueue_reply_t update_state_threadfn (void *state_, void *work_)
 
void cpuworkers_rotate_keyinfo (void)
 
static int should_time_request (uint16_t onionskin_type)
 
uint64_t estimated_usec_for_onionskins (uint32_t n_requests, uint16_t onionskin_type)
 
static int get_overhead_for_onionskins (uint32_t *usec_out, double *frac_out, uint16_t onionskin_type)
 
void cpuworker_log_onionskin_overhead (int severity, int onionskin_type, const char *onionskin_type_name)
 
static void cpuworker_onion_handshake_replyfn (void *work_)
 
static workqueue_reply_t cpuworker_onion_handshake_threadfn (void *state_, void *work_)
 
workqueue_entry_tcpuworker_queue_work (workqueue_priority_t priority, workqueue_reply_t(*fn)(void *, void *), void(*reply_fn)(void *), void *arg)
 
int assign_onionskin_to_cpuworker (or_circuit_t *circ, create_cell_t *onionskin)
 
void cpuworker_cancel_circ_handshake (or_circuit_t *circ)
 

Variables

static replyqueue_treplyqueue = NULL
 
static threadpool_tthreadpool = NULL
 
static uint32_t total_pending_tasks = 0
 
static uint32_t max_pending_tasks = 128
 
static uint64_t onionskins_n_processed [MAX_ONION_HANDSHAKE_TYPE+1]
 
static uint64_t onionskins_usec_internal [MAX_ONION_HANDSHAKE_TYPE+1]
 
static uint64_t onionskins_usec_roundtrip [MAX_ONION_HANDSHAKE_TYPE+1]
 

Detailed Description

Uses the workqueue/threadpool code to farm CPU-intensive activities out to subprocesses.

The multithreading backend for this module is in workqueue.c; this module specializes workqueue.c.

Right now, we use this infrastructure

Definition in file cpuworker.c.

Macro Definition Documentation

◆ CPUWORKER_REPLY_MAGIC

#define CPUWORKER_REPLY_MAGIC   0x5eedf00d

Definition at line 161 of file cpuworker.c.

◆ CPUWORKER_REQUEST_MAGIC

#define CPUWORKER_REQUEST_MAGIC   0xda4afeed

Magic numbers to make sure our cpuworker_requests don't grow any mis-framing bugs.

Definition at line 160 of file cpuworker.c.

◆ MAX_BELIEVABLE_ONIONSKIN_DELAY

#define MAX_BELIEVABLE_ONIONSKIN_DELAY   (2*1000*1000)

If any onionskin takes longer than this, we clip them to this time. (microseconds)

Definition at line 274 of file cpuworker.c.

◆ worker_state_free

#define worker_state_free (   ws)     FREE_AND_NULL(worker_state_t, worker_state_free_, (ws))

Definition at line 59 of file cpuworker.c.

Function Documentation

◆ assign_onionskin_to_cpuworker()

int assign_onionskin_to_cpuworker ( or_circuit_t circ,
create_cell_t onionskin 
)

Try to tell a cpuworker to perform the public key operations necessary to respond to onionskin for the circuit circ.

Return 0 if we successfully assign the task, or -1 on failure.

Definition at line 584 of file cpuworker.c.

◆ cpuworker_cancel_circ_handshake()

void cpuworker_cancel_circ_handshake ( or_circuit_t circ)

If circ has a pending handshake that hasn't been processed yet, remove it from the worker queue.

Definition at line 657 of file cpuworker.c.

Referenced by onion_pending_remove().

◆ cpuworker_consensus_has_changed()

void cpuworker_consensus_has_changed ( const networkstatus_t ns)

Called when the consensus has changed.

Definition at line 111 of file cpuworker.c.

◆ cpuworker_get_n_threads()

unsigned int cpuworker_get_n_threads ( void  )

Return the number of threads configured for our CPU worker.

Definition at line 150 of file cpuworker.c.

◆ cpuworker_init()

void cpuworker_init ( void  )

Initialize the cpuworker subsystem. It is OK to call this more than once during Tor's lifetime.

Definition at line 121 of file cpuworker.c.

◆ cpuworker_log_onionskin_overhead()

void cpuworker_log_onionskin_overhead ( int  severity,
int  onionskin_type,
const char *  onionskin_type_name 
)

If we've measured overhead for onionskins of type onionskin_type, log it.

Definition at line 346 of file cpuworker.c.

◆ cpuworker_onion_handshake_replyfn()

static void cpuworker_onion_handshake_replyfn ( void *  work_)
static

Handle a reply from the worker threads.

Definition at line 366 of file cpuworker.c.

◆ cpuworker_onion_handshake_threadfn()

static workqueue_reply_t cpuworker_onion_handshake_threadfn ( void *  state_,
void *  work_ 
)
static

Implementation function for onion handshake requests.

Definition at line 468 of file cpuworker.c.

◆ cpuworker_queue_work()

workqueue_entry_t * cpuworker_queue_work ( workqueue_priority_t  priority,
workqueue_reply_t(*)(void *, void *)  fn,
void(*)(void *)  reply_fn,
void *  arg 
)

DOCDOC

Definition at line 567 of file cpuworker.c.

◆ cpuworkers_rotate_keyinfo()

void cpuworkers_rotate_keyinfo ( void  )

Called when the onion key has changed so update all CPU worker(s) with new function pointers with which a new state will be generated.

Definition at line 242 of file cpuworker.c.

◆ estimated_usec_for_onionskins()

uint64_t estimated_usec_for_onionskins ( uint32_t  n_requests,
uint16_t  onionskin_type 
)

Return an estimate of how many microseconds we will need for a single cpuworker to process n_requests onionskins of type onionskin_type.

Definition at line 298 of file cpuworker.c.

◆ get_max_pending_tasks_per_cpu()

static uint32_t get_max_pending_tasks_per_cpu ( const networkstatus_t ns)
static

Return the consensus parameter max pending tasks per CPU.

Definition at line 85 of file cpuworker.c.

◆ get_overhead_for_onionskins()

static int get_overhead_for_onionskins ( uint32_t *  usec_out,
double *  frac_out,
uint16_t  onionskin_type 
)
static

Compute the absolute and relative overhead of using the cpuworker framework for onionskins of type onionskin_type.

Definition at line 319 of file cpuworker.c.

Referenced by cpuworker_log_onionskin_overhead().

◆ queue_pending_tasks()

static void queue_pending_tasks ( void  )
static

Take pending tasks from the queue and assign them to cpuworkers.

Definition at line 546 of file cpuworker.c.

◆ set_max_pending_tasks()

static void set_max_pending_tasks ( const networkstatus_t ns)
static

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.

Definition at line 103 of file cpuworker.c.

Referenced by cpuworker_consensus_has_changed().

◆ should_time_request()

static int should_time_request ( uint16_t  onionskin_type)
static

Return true iff we'd like to measure a handshake of type onionskin_type. Call only from the main thread.

Otherwise, measure with P=1/128. We avoid doing this for every handshake, since the measurement itself can take a little time.

Definition at line 279 of file cpuworker.c.

◆ update_state_threadfn()

static workqueue_reply_t update_state_threadfn ( void *  state_,
void *  work_ 
)
static

Definition at line 226 of file cpuworker.c.

◆ worker_state_free_()

static void worker_state_free_ ( worker_state_t ws)
static

Definition at line 63 of file cpuworker.c.

◆ worker_state_free_void()

static void worker_state_free_void ( void *  arg)
static

Definition at line 72 of file cpuworker.c.

◆ worker_state_new()

static void * worker_state_new ( void *  arg)
static

Definition at line 50 of file cpuworker.c.

Variable Documentation

◆ max_pending_tasks

uint32_t max_pending_tasks = 128
static

Definition at line 81 of file cpuworker.c.

◆ onionskins_n_processed

uint64_t onionskins_n_processed[MAX_ONION_HANDSHAKE_TYPE+1]
static

Indexed by handshake type: how many onionskins have we processed and counted of that type?

Definition at line 261 of file cpuworker.c.

◆ onionskins_usec_internal

uint64_t onionskins_usec_internal[MAX_ONION_HANDSHAKE_TYPE+1]
static

Indexed by handshake type, corresponding to the onionskins counted in onionskins_n_processed: how many microseconds have we spent in cpuworkers processing that kind of onionskin?

Definition at line 265 of file cpuworker.c.

◆ onionskins_usec_roundtrip

uint64_t onionskins_usec_roundtrip[MAX_ONION_HANDSHAKE_TYPE+1]
static

Indexed by handshake type, corresponding to onionskins counted in onionskins_n_processed: how many microseconds have we spent waiting for cpuworkers to give us answers for that kind of onionskin?

Definition at line 270 of file cpuworker.c.

◆ replyqueue

replyqueue_t* replyqueue = NULL
static

Definition at line 77 of file cpuworker.c.

◆ threadpool

threadpool_t* threadpool = NULL
static

Definition at line 78 of file cpuworker.c.

◆ total_pending_tasks

uint32_t total_pending_tasks = 0
static

Definition at line 80 of file cpuworker.c.