2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-31 22:45:39 +00:00

Always use the number of CPUS for resolver->ntasks

Since the fctx hash table is now self-resizing, and resolver tasks are
selected to match the thread that created the fetch context, there
shouldn't be any significant advantage to having multiple tasks per CPU;
a single task per thread should be sufficient.

Additionally, the fetch context is always pinned to the calling netmgr
thread to minimize the contention just to coalesced fetches - if two
threads starts the same fetch, it will be pinned to the first one to get
the bucket.
This commit is contained in:
Evan Hunt
2022-05-07 20:58:19 -07:00
committed by Ondřej Surý
parent 857f3bede3
commit 6936db2f59
9 changed files with 57 additions and 62 deletions

View File

@@ -153,12 +153,6 @@
#define SIZE_AS_PERCENT ((size_t)-2) #define SIZE_AS_PERCENT ((size_t)-2)
#endif /* ifndef SIZE_AS_PERCENT */ #endif /* ifndef SIZE_AS_PERCENT */
#ifdef TUNE_LARGE
#define RESOLVER_NTASKS_PERCPU 32
#else
#define RESOLVER_NTASKS_PERCPU 8
#endif /* TUNE_LARGE */
/* RFC7828 defines timeout as 16-bit value specified in units of 100 /* RFC7828 defines timeout as 16-bit value specified in units of 100
* milliseconds, so the maximum and minimum advertised and keepalive * milliseconds, so the maximum and minimum advertised and keepalive
* timeouts are capped by the data type (it's ~109 minutes) * timeouts are capped by the data type (it's ~109 minutes)
@@ -4734,9 +4728,8 @@ configure_view(dns_view_t *view, dns_viewlist_t *viewlist, cfg_obj_t *config,
ndisp = 4 * ISC_MIN(named_g_udpdisp, MAX_UDP_DISPATCH); ndisp = 4 * ISC_MIN(named_g_udpdisp, MAX_UDP_DISPATCH);
CHECK(dns_view_createresolver( CHECK(dns_view_createresolver(
view, named_g_taskmgr, RESOLVER_NTASKS_PERCPU * named_g_cpus, view, named_g_taskmgr, ndisp, named_g_netmgr, named_g_timermgr,
ndisp, named_g_netmgr, named_g_timermgr, resopts, resopts, named_g_dispatchmgr, dispatch4, dispatch6));
named_g_dispatchmgr, dispatch4, dispatch6));
if (resstats == NULL) { if (resstats == NULL) {
CHECK(isc_stats_create(mctx, &resstats, CHECK(isc_stats_create(mctx, &resstats,

View File

@@ -62,12 +62,6 @@
#define MAX_RESTARTS 16 #define MAX_RESTARTS 16
#ifdef TUNE_LARGE
#define RESOLVER_NTASKS 523
#else /* ifdef TUNE_LARGE */
#define RESOLVER_NTASKS 31
#endif /* TUNE_LARGE */
#define CHECK(r) \ #define CHECK(r) \
do { \ do { \
result = (r); \ result = (r); \
@@ -225,7 +219,7 @@ getudpdispatch(int family, dns_dispatchmgr_t *dispatchmgr,
static isc_result_t static isc_result_t
createview(isc_mem_t *mctx, dns_rdataclass_t rdclass, isc_taskmgr_t *taskmgr, createview(isc_mem_t *mctx, dns_rdataclass_t rdclass, isc_taskmgr_t *taskmgr,
unsigned int ntasks, isc_nm_t *nm, isc_timermgr_t *timermgr, isc_nm_t *nm, isc_timermgr_t *timermgr,
dns_dispatchmgr_t *dispatchmgr, dns_dispatch_t *dispatchv4, dns_dispatchmgr_t *dispatchmgr, dns_dispatch_t *dispatchv4,
dns_dispatch_t *dispatchv6, dns_view_t **viewp) { dns_dispatch_t *dispatchv6, dns_view_t **viewp) {
isc_result_t result; isc_result_t result;
@@ -243,9 +237,8 @@ createview(isc_mem_t *mctx, dns_rdataclass_t rdclass, isc_taskmgr_t *taskmgr,
return (result); return (result);
} }
result = dns_view_createresolver(view, taskmgr, ntasks, 1, nm, timermgr, result = dns_view_createresolver(view, taskmgr, 1, nm, timermgr, 0,
0, dispatchmgr, dispatchv4, dispatchmgr, dispatchv4, dispatchv6);
dispatchv6);
if (result != ISC_R_SUCCESS) { if (result != ISC_R_SUCCESS) {
dns_view_detach(&view); dns_view_detach(&view);
return (result); return (result);
@@ -335,9 +328,8 @@ dns_client_create(isc_mem_t *mctx, isc_taskmgr_t *taskmgr, isc_nm_t *nm,
isc_refcount_init(&client->references, 1); isc_refcount_init(&client->references, 1);
/* Create the default view for class IN */ /* Create the default view for class IN */
result = createview(mctx, dns_rdataclass_in, taskmgr, RESOLVER_NTASKS, result = createview(mctx, dns_rdataclass_in, taskmgr, nm, timermgr,
nm, timermgr, client->dispatchmgr, dispatchv4, client->dispatchmgr, dispatchv4, dispatchv6, &view);
dispatchv6, &view);
if (result != ISC_R_SUCCESS) { if (result != ISC_R_SUCCESS) {
goto cleanup_references; goto cleanup_references;
} }

View File

@@ -165,10 +165,10 @@ typedef enum { dns_quotatype_zone = 0, dns_quotatype_server } dns_quotatype_t;
isc_result_t isc_result_t
dns_resolver_create(dns_view_t *view, isc_taskmgr_t *taskmgr, dns_resolver_create(dns_view_t *view, isc_taskmgr_t *taskmgr,
unsigned int ntasks, unsigned int ndisp, isc_nm_t *nm, unsigned int ndisp, isc_nm_t *nm, isc_timermgr_t *timermgr,
isc_timermgr_t *timermgr, unsigned int options, unsigned int options, dns_dispatchmgr_t *dispatchmgr,
dns_dispatchmgr_t *dispatchmgr, dns_dispatch_t *dispatchv4, dns_dispatch_t *dispatchv4, dns_dispatch_t *dispatchv6,
dns_dispatch_t *dispatchv6, dns_resolver_t **resp); dns_resolver_t **resp);
/*%< /*%<
* Create a resolver. * Create a resolver.

View File

@@ -385,7 +385,7 @@ dns_view_createzonetable(dns_view_t *view);
isc_result_t isc_result_t
dns_view_createresolver(dns_view_t *view, isc_taskmgr_t *taskmgr, dns_view_createresolver(dns_view_t *view, isc_taskmgr_t *taskmgr,
unsigned int ntasks, unsigned int ndisp, isc_nm_t *nm, unsigned int ndisp, isc_nm_t *nm,
isc_timermgr_t *timermgr, unsigned int options, isc_timermgr_t *timermgr, unsigned int options,
dns_dispatchmgr_t *dispatchmgr, dns_dispatchmgr_t *dispatchmgr,
dns_dispatch_t *dispatchv4, dns_dispatch_t *dispatchv6); dns_dispatch_t *dispatchv4, dns_dispatch_t *dispatchv6);

View File

@@ -330,7 +330,9 @@ struct fetchctx {
char *info; char *info;
isc_mem_t *mctx; isc_mem_t *mctx;
isc_stdtime_t now; isc_stdtime_t now;
isc_task_t *task;
isc_task_t *restask;
unsigned int tid;
/* Atomic */ /* Atomic */
isc_refcount_t references; isc_refcount_t references;
@@ -3365,10 +3367,10 @@ findname(fetchctx_t *fctx, const dns_name_t *name, in_port_t port,
* See what we know about this address. * See what we know about this address.
*/ */
fctx_addref(fctx); fctx_addref(fctx);
result = dns_adb_createfind(fctx->adb, fctx->task, fctx_finddone, fctx, result = dns_adb_createfind(fctx->adb, fctx->restask, fctx_finddone,
name, fctx->name, fctx->type, options, now, fctx, name, fctx->name, fctx->type, options,
NULL, res->view->dstport, fctx->depth + 1, now, NULL, res->view->dstport,
fctx->qc, &find); fctx->depth + 1, fctx->qc, &find);
isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER, isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
DNS_LOGMODULE_RESOLVER, ISC_LOG_DEBUG(3), DNS_LOGMODULE_RESOLVER, ISC_LOG_DEBUG(3),
@@ -4172,8 +4174,8 @@ fctx_try(fetchctx_t *fctx, bool retrying, bool badcache) {
result = dns_resolver_createfetch( result = dns_resolver_createfetch(
fctx->res, fctx->qminname, fctx->qmintype, fctx->domain, fctx->res, fctx->qminname, fctx->qmintype, fctx->domain,
&fctx->nameservers, NULL, NULL, 0, options, 0, fctx->qc, &fctx->nameservers, NULL, NULL, 0, options, 0, fctx->qc,
fctx->task, resume_qmin, fctx, &fctx->qminrrset, NULL, fctx->restask, resume_qmin, fctx, &fctx->qminrrset,
&fctx->qminfetch); NULL, &fctx->qminfetch);
if (result != ISC_R_SUCCESS) { if (result != ISC_R_SUCCESS) {
fctx_unref(fctx); fctx_unref(fctx);
fctx_done_detach(&fctx, DNS_R_SERVFAIL); fctx_done_detach(&fctx, DNS_R_SERVFAIL);
@@ -4433,7 +4435,7 @@ fctx_shutdown(fetchctx_t *fctx) {
if (fctx->state != fetchstate_init) { if (fctx->state != fetchstate_init) {
FCTXTRACE("posting control event"); FCTXTRACE("posting control event");
cevent = &fctx->control_event; cevent = &fctx->control_event;
isc_task_send(fctx->task, &cevent); isc_task_send(fctx->restask, &cevent);
} }
} }
@@ -4666,22 +4668,26 @@ fctx_create(dns_resolver_t *res, const dns_name_t *name, dns_rdatatype_t type,
isc_interval_t interval; isc_interval_t interval;
unsigned int findoptions = 0; unsigned int findoptions = 0;
char buf[DNS_NAME_FORMATSIZE + DNS_RDATATYPE_FORMATSIZE + 1]; char buf[DNS_NAME_FORMATSIZE + DNS_RDATATYPE_FORMATSIZE + 1];
unsigned int tid;
size_t p; size_t p;
int tid = isc_nm_tid();
if (tid == ISC_NETMGR_TID_UNKNOWN) {
tid = 0;
}
/* /*
* Caller must be holding the lock for 'bucket' * Caller must be holding the lock for 'bucket'
*/ */
REQUIRE(fctxp != NULL && *fctxp == NULL); REQUIRE(fctxp != NULL && *fctxp == NULL);
tid = isc_random_uniform(res->ntasks);
fctx = isc_mem_get(res->mctx, sizeof(*fctx)); fctx = isc_mem_get(res->mctx, sizeof(*fctx));
*fctx = (fetchctx_t){ *fctx = (fetchctx_t){
.type = type, .type = type,
.qmintype = type, .qmintype = type,
.options = options, .options = options,
.bucket = bucket, .bucket = bucket,
.task = res->tasks[tid], .tid = tid,
.restask = res->tasks[tid],
.state = fetchstate_init, .state = fetchstate_init,
.depth = depth, .depth = depth,
.qmin_labels = 1, .qmin_labels = 1,
@@ -4869,7 +4875,7 @@ fctx_create(dns_resolver_t *res, const dns_name_t *name, dns_rdatatype_t type,
* lifetime. It will be made active when the fetch is * lifetime. It will be made active when the fetch is
* started. * started.
*/ */
isc_timer_create(res->timermgr, fctx->task, fctx_expired, fctx, isc_timer_create(res->timermgr, fctx->restask, fctx_expired, fctx,
&fctx->timer); &fctx->timer);
/* /*
@@ -6329,7 +6335,7 @@ cache_name(fetchctx_t *fctx, dns_name_t *name, dns_message_t *message,
fctx, message, addrinfo, name, fctx, message, addrinfo, name,
rdataset->type, rdataset, rdataset->type, rdataset,
sigrdataset, valoptions, sigrdataset, valoptions,
fctx->task); fctx->restask);
} }
} else if (CHAINING(rdataset)) { } else if (CHAINING(rdataset)) {
if (rdataset->type == dns_rdatatype_cname) { if (rdataset->type == dns_rdatatype_cname) {
@@ -6436,7 +6442,7 @@ cache_name(fetchctx_t *fctx, dns_name_t *name, dns_message_t *message,
result = valcreate(fctx, message, addrinfo, name, vtype, result = valcreate(fctx, message, addrinfo, name, vtype,
valrdataset, valsigrdataset, valoptions, valrdataset, valsigrdataset, valoptions,
fctx->task); fctx->restask);
} }
if (result == ISC_R_SUCCESS && have_answer) { if (result == ISC_R_SUCCESS && have_answer) {
@@ -6656,7 +6662,7 @@ ncache_message(fetchctx_t *fctx, dns_message_t *message,
* Do negative response validation. * Do negative response validation.
*/ */
result = valcreate(fctx, message, addrinfo, name, fctx->type, result = valcreate(fctx, message, addrinfo, name, fctx->type,
NULL, NULL, valoptions, fctx->task); NULL, NULL, valoptions, fctx->restask);
/* /*
* If validation is necessary, return now. Otherwise * If validation is necessary, return now. Otherwise
* continue to process the message, letting the * continue to process the message, letting the
@@ -9694,7 +9700,7 @@ rctx_chaseds(respctx_t *rctx, dns_message_t *message,
fctx_addref(fctx); fctx_addref(fctx);
result = dns_resolver_createfetch( result = dns_resolver_createfetch(
fctx->res, fctx->nsname, dns_rdatatype_ns, NULL, NULL, NULL, fctx->res, fctx->nsname, dns_rdatatype_ns, NULL, NULL, NULL,
NULL, 0, fctx->options, 0, NULL, fctx->task, resume_dslookup, NULL, 0, fctx->options, 0, NULL, fctx->restask, resume_dslookup,
fctx, &fctx->nsrrset, NULL, &fctx->nsfetch); fctx, &fctx->nsrrset, NULL, &fctx->nsfetch);
if (result != ISC_R_SUCCESS) { if (result != ISC_R_SUCCESS) {
if (result == DNS_R_DUPLICATE) { if (result == DNS_R_DUPLICATE) {
@@ -10162,10 +10168,10 @@ spillattimer_countdown(isc_task_t *task, isc_event_t *event) {
isc_result_t isc_result_t
dns_resolver_create(dns_view_t *view, isc_taskmgr_t *taskmgr, dns_resolver_create(dns_view_t *view, isc_taskmgr_t *taskmgr,
unsigned int ntasks, unsigned int ndisp, isc_nm_t *nm, unsigned int ndisp, isc_nm_t *nm, isc_timermgr_t *timermgr,
isc_timermgr_t *timermgr, unsigned int options, unsigned int options, dns_dispatchmgr_t *dispatchmgr,
dns_dispatchmgr_t *dispatchmgr, dns_dispatch_t *dispatchv4, dns_dispatch_t *dispatchv4, dns_dispatch_t *dispatchv6,
dns_dispatch_t *dispatchv6, dns_resolver_t **resp) { dns_resolver_t **resp) {
isc_result_t result = ISC_R_SUCCESS; isc_result_t result = ISC_R_SUCCESS;
char name[sizeof("res4294967295")]; char name[sizeof("res4294967295")];
dns_resolver_t *res = NULL; dns_resolver_t *res = NULL;
@@ -10176,7 +10182,6 @@ dns_resolver_create(dns_view_t *view, isc_taskmgr_t *taskmgr,
*/ */
REQUIRE(DNS_VIEW_VALID(view)); REQUIRE(DNS_VIEW_VALID(view));
REQUIRE(ntasks > 0);
REQUIRE(ndisp > 0); REQUIRE(ndisp > 0);
REQUIRE(resp != NULL && *resp == NULL); REQUIRE(resp != NULL && *resp == NULL);
REQUIRE(dispatchmgr != NULL); REQUIRE(dispatchmgr != NULL);
@@ -10190,7 +10195,7 @@ dns_resolver_create(dns_view_t *view, isc_taskmgr_t *taskmgr,
.taskmgr = taskmgr, .taskmgr = taskmgr,
.dispatchmgr = dispatchmgr, .dispatchmgr = dispatchmgr,
.options = options, .options = options,
.ntasks = ntasks, .ntasks = isc_nm_getnworkers(nm),
.udpsize = DEFAULT_EDNS_BUFSIZE, .udpsize = DEFAULT_EDNS_BUFSIZE,
.spillatmin = 10, .spillatmin = 10,
.spillat = 10, .spillat = 10,
@@ -10217,13 +10222,14 @@ dns_resolver_create(dns_view_t *view, isc_taskmgr_t *taskmgr,
goto cleanup_res; goto cleanup_res;
} }
res->tasks = isc_mem_get(view->mctx, ntasks * sizeof(res->tasks[0])); res->tasks = isc_mem_get(view->mctx,
for (uint32_t i = 0; i < ntasks; i++) { res->ntasks * sizeof(res->tasks[0]));
memset(res->tasks, 0, res->ntasks * sizeof(res->tasks[0]));
for (uint32_t i = 0; i < res->ntasks; i++) {
/* /*
* Since we have a pool of tasks we bind them to task * Since we have a pool of tasks we bind them to task
* queues to spread the load evenly * queues to spread the load evenly
*/ */
res->tasks[i] = NULL;
result = isc_task_create_bound(taskmgr, 0, &res->tasks[i], i); result = isc_task_create_bound(taskmgr, 0, &res->tasks[i], i);
if (result != ISC_R_SUCCESS) { if (result != ISC_R_SUCCESS) {
goto cleanup_tasks; goto cleanup_tasks;
@@ -10288,8 +10294,10 @@ cleanup_primelock:
isc_ht_destroy(&res->buckets); isc_ht_destroy(&res->buckets);
cleanup_tasks: cleanup_tasks:
for (size_t i = 0; i < ntasks; i++) { for (size_t i = 0; i < res->ntasks; i++) {
isc_task_detach(&res->tasks[i]); if (res->tasks[i] != NULL) {
isc_task_detach(&res->tasks[i]);
}
} }
isc_mem_put(view->mctx, res->tasks, isc_mem_put(view->mctx, res->tasks,
res->ntasks * sizeof(res->tasks[0])); res->ntasks * sizeof(res->tasks[0]));
@@ -10620,11 +10628,13 @@ get_fctxbucket(dns_resolver_t *res, const dns_name_t *domain) {
result = isc_ht_find(res->buckets, domain->ndata, domain->length, result = isc_ht_find(res->buckets, domain->ndata, domain->length,
(void **)&bucket); (void **)&bucket);
if (result != ISC_R_SUCCESS) { if (result != ISC_R_SUCCESS) {
RWUNLOCK(&res->hash_lock, ltype);
bucket = isc_mem_get(res->mctx, sizeof(*bucket)); bucket = isc_mem_get(res->mctx, sizeof(*bucket));
*bucket = (fctxbucket_t){ .fctxs = { 0 } }; *bucket = (fctxbucket_t){
.fctxs = { 0 },
};
ISC_LIST_INIT(bucket->fctxs); ISC_LIST_INIT(bucket->fctxs);
isc_mutex_init(&bucket->lock); isc_mutex_init(&bucket->lock);
RWUNLOCK(&res->hash_lock, ltype);
ltype = isc_rwlocktype_write; ltype = isc_rwlocktype_write;
RWLOCK(&res->hash_lock, ltype); RWLOCK(&res->hash_lock, ltype);
@@ -10773,7 +10783,7 @@ dns_resolver_createfetch(dns_resolver_t *res, const dns_name_t *name,
ISC_EVENT_INIT(event, sizeof(*event), 0, NULL, ISC_EVENT_INIT(event, sizeof(*event), 0, NULL,
DNS_EVENT_FETCHCONTROL, fctx_start, fctx, DNS_EVENT_FETCHCONTROL, fctx_start, fctx,
NULL, NULL, NULL); NULL, NULL, NULL);
isc_task_send(fctx->task, &event); isc_task_send(fctx->restask, &event);
} else { } else {
dodestroy = true; dodestroy = true;
} }

View File

@@ -81,7 +81,7 @@ static void
mkres(dns_resolver_t **resolverp) { mkres(dns_resolver_t **resolverp) {
isc_result_t result; isc_result_t result;
result = dns_resolver_create(view, taskmgr, 1, 1, netmgr, timermgr, 0, result = dns_resolver_create(view, taskmgr, 1, netmgr, timermgr, 0,
dispatchmgr, dispatch, NULL, resolverp); dispatchmgr, dispatch, NULL, resolverp);
assert_int_equal(result, ISC_R_SUCCESS); assert_int_equal(result, ISC_R_SUCCESS);
} }

View File

@@ -617,7 +617,7 @@ dns_view_createzonetable(dns_view_t *view) {
isc_result_t isc_result_t
dns_view_createresolver(dns_view_t *view, isc_taskmgr_t *taskmgr, dns_view_createresolver(dns_view_t *view, isc_taskmgr_t *taskmgr,
unsigned int ntasks, unsigned int ndisp, isc_nm_t *nm, unsigned int ndisp, isc_nm_t *nm,
isc_timermgr_t *timermgr, unsigned int options, isc_timermgr_t *timermgr, unsigned int options,
dns_dispatchmgr_t *dispatchmgr, dns_dispatchmgr_t *dispatchmgr,
dns_dispatch_t *dispatchv4, dns_dispatch_t *dispatchv4,
@@ -635,7 +635,7 @@ dns_view_createresolver(dns_view_t *view, isc_taskmgr_t *taskmgr,
} }
isc_task_setname(view->task, "view", view); isc_task_setname(view->task, "view", view);
result = dns_resolver_create(view, taskmgr, ntasks, ndisp, nm, timermgr, result = dns_resolver_create(view, taskmgr, ndisp, nm, timermgr,
options, dispatchmgr, dispatchv4, options, dispatchmgr, dispatchv4,
dispatchv6, &view->resolver); dispatchv6, &view->resolver);
if (result != ISC_R_SUCCESS) { if (result != ISC_R_SUCCESS) {

View File

@@ -102,6 +102,8 @@ isc_nm_detach(isc_nm_t **mgr0);
* for all other references to be gone. * for all other references to be gone.
*/ */
#define ISC_NETMGR_TID_UNKNOWN -1
/* Return thread ID of current thread, or ISC_NETMGR_TID_UNKNOWN */ /* Return thread ID of current thread, or ISC_NETMGR_TID_UNKNOWN */
int int
isc_nm_tid(void); isc_nm_tid(void);

View File

@@ -38,8 +38,6 @@
#include <isc/util.h> #include <isc/util.h>
#include <isc/uv.h> #include <isc/uv.h>
#define ISC_NETMGR_TID_UNKNOWN -1
/* Must be different from ISC_NETMGR_TID_UNKNOWN */ /* Must be different from ISC_NETMGR_TID_UNKNOWN */
#define ISC_NETMGR_NON_INTERLOCKED -2 #define ISC_NETMGR_NON_INTERLOCKED -2