Compare commits

...

32 Commits

Author SHA1 Message Date
Witold Kręcicki
24eddb7679 win32/socket should use common isc_socket 2018-10-24 10:52:56 +00:00
Witold Kręcicki
3d280f238e Move socket type to common isc_socket structure; add isc_sockettype_tls 2018-10-24 10:52:56 +00:00
Witold Kręcicki
43e566937e Windows fixes 2018-10-24 10:52:37 +00:00
Witold Kręcicki
d55304ea29 Fix fetchlimit test to allow softquota to be overruled; Remove 'verifying that named checks for conflicting listeners' test from runtime 2018-10-24 07:10:20 +00:00
Witold Kręcicki
dceabb547b Cookies with non-crypto-secure random 2018-10-24 07:10:20 +00:00
Witold Kręcicki
258d8912d4 Use multiple worker queues in unix/socket.c 2018-10-24 07:10:20 +00:00
Witold Kręcicki
9b191a9a6b use REUSEPORT only for NOLISTEN dispatchers 2018-10-24 07:10:20 +00:00
Witold Kręcicki
1e152250c1 Fixes 2018-10-24 07:10:20 +00:00
Witold Kręcicki
243da5c407 Fix for TCP 2018-10-24 07:10:20 +00:00
Witold Kręcicki
732c7eaf1f Use isc_refcount_t in socket 2018-10-24 07:10:20 +00:00
Witold Krecicki
edb6f5c2a8 Fixes for Solaris 2018-10-24 07:10:20 +00:00
Witold Kręcicki
ba46797f42 Mangle fdlocks a bit 2018-10-24 07:10:20 +00:00
Witold Kręcicki
6d46fbb550 Fix a race in socket code 2018-10-24 07:10:20 +00:00
Witold Kręcicki
1c06e32a93 Set network threads affintiy 2018-10-24 07:10:20 +00:00
Witold Kręcicki
7b5abac4e9 Add runtime detection of SO_REUSEPORT, use it if available (instead of dup) 2018-10-24 07:10:20 +00:00
Witold Kręcicki
30c6d4befd More udp dispatchers 2018-10-24 07:10:20 +00:00
Witold Kręcicki
48ea0a317c Set quantum to 50 2018-10-24 07:10:20 +00:00
Witold Krecicki
bc4a4f7186 Multiple network threads with separate data structures (WiP) 2018-10-24 07:10:20 +00:00
Witold Kręcicki
5841bd1783 Do IO after event directly in the network thread, don't schedule it for worker 2018-10-24 07:10:20 +00:00
Witold Kręcicki
4b55fd0d8a Remove fdwatch sockets 2018-10-24 07:10:20 +00:00
Witold Kręcicki
32ca49c437 Comment about taskmgr exclusive mode, fix a REQUIRE. 2018-10-24 07:06:57 +00:00
Witold Kręcicki
c85a6a533f Allow slight over-quota in'checking lame server clients are dropped at the soft limit' test 2018-10-24 07:06:57 +00:00
Witold Kręcicki
d61bb36061 Saner exclusive task handling in taskmgr 2018-10-24 07:06:57 +00:00
Witold Kręcicki
56d48aa8a2 Formatting 2018-10-24 07:06:57 +00:00
Witold Kręcicki
32b9631d1a Get rid of isc_taskmgr_setmode, we only use it to set privileged mode 2018-10-24 07:06:57 +00:00
Witold Kręcicki
0e1e295ddb Make sure all priority tasks are done before entering normal execution 2018-10-24 07:06:57 +00:00
Witold Kręcicki
8fffa3760d Post shutting down tasks always to manager 0 2018-10-24 07:06:57 +00:00
Witold Kręcicki
49eab869ba Separate structure for each thread/queue; 2-phase-locking for exclusive tasks 2018-10-24 07:06:57 +00:00
Witold Kręcicki
18b5f9dd92 Always restart dispatchers on empty readyq 2018-10-24 07:06:57 +00:00
Witold Kręcicki
cfb23c73f7 Taskmgr shutdown fixes 2018-10-24 07:06:57 +00:00
Witold Kręcicki
87e4e442d7 Multiple worker queues 2018-10-24 07:06:57 +00:00
Witold Krecicki
8447d2e80e isc_thread_setaffinity() 2018-10-24 07:06:57 +00:00
24 changed files with 1465 additions and 1664 deletions

View File

@@ -793,10 +793,7 @@ create_managers(void) {
named_g_udpdisp = 1;
#else
if (named_g_udpdisp == 0) {
if (named_g_cpus_detected == 1)
named_g_udpdisp = 1;
else
named_g_udpdisp = named_g_cpus_detected - 1;
named_g_udpdisp = named_g_cpus_detected;
}
if (named_g_udpdisp > named_g_cpus)
named_g_udpdisp = named_g_cpus;
@@ -824,7 +821,7 @@ create_managers(void) {
}
result = isc_socketmgr_create2(named_g_mctx, &named_g_socketmgr,
maxsocks);
maxsocks, named_g_udpdisp);
if (result != ISC_R_SUCCESS) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_socketmgr_create() failed: %s",

View File

@@ -9319,8 +9319,7 @@ load_zones(named_server_t *server, bool init, bool reconfig) {
* the initial server setup; it isn't necessary during
* a reload.)
*/
isc_taskmgr_setmode(named_g_taskmgr,
isc_taskmgrmode_privileged);
isc_taskmgr_setprivilegedmode(named_g_taskmgr);
}
isc_task_endexclusive(server->task);

View File

@@ -157,7 +157,7 @@ status=`expr $status + $ret`
copy_setports ns3/named3.conf.in ns3/named.conf
$RNDCCMD reconfig 2>&1 | sed 's/^/ns3 /' | cat_i
echo_i "checking lame server clients are dropped at the soft limit"
echo_i "checking lame server clients are dropped near the soft limit"
ret=0
fail=0
exceeded=0
@@ -166,7 +166,7 @@ touch ans4/norespond
for try in 1 2 3 4 5; do
burst b $try 400
$DIG @10.53.0.3 -p ${PORT} a ${try}.example > dig.out.ns3.$try
stat 360 || exceeded=`expr $exceeded + 1`
stat 370 || exceeded=`expr $exceeded + 1`
grep "status: NOERROR" dig.out.ns3.$try > /dev/null 2>&1 && \
success=`expr $success + 1`
grep "status: SERVFAIL" dig.out.ns3.$try > /dev/null 2>&1 && \
@@ -177,7 +177,7 @@ echo_i "$success successful valid queries (expected 5)"
[ "$success" -eq 5 ] || { echo_i "failed"; ret=1; }
echo_i "$fail SERVFAIL responses (expected 0)"
[ "$fail" -eq 0 ] || { echo_i "failed"; ret=1; }
echo_i "clients count exceeded 360 on $exceeded trials (expected 0)"
echo_i "clients count exceeded 370 on $exceeded trials (expected 0)"
[ "$exceeded" -eq 0 ] || { echo_i "failed"; ret=1; }
if [ $ret != 0 ]; then echo_i "failed"; fi
status=`expr $status + $ret`

View File

@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/sh -x
#
# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
#

View File

@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/sh -x
#
# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
#

View File

@@ -24,28 +24,6 @@ grep "another named process" ns2/named.run > /dev/null && ret=1
if [ $ret != 0 ]; then echo_i "failed"; fi
status=`expr $status + $ret`
if [ ! "$CYGWIN" ]; then
n=`expr $n + 1`
echo_i "verifying that named checks for conflicting listeners ($n)"
ret=0
(cd ns2; $NAMED -c named-alt1.conf -D ns2-extra-1 -X other.lock -m record,size,mctx -d 99 -g -U 4 >> named2.run 2>&1 & )
for i in 1 2 3 4 5 6 7 8 9
do
grep "unable to listen on any configured interface" ns2/named2.run > /dev/null && break
sleep 1
done
grep "unable to listen on any configured interface" ns2/named2.run > /dev/null || ret=1
for i in 1 2 3 4 5 6 7 8 9
do
grep "exiting (due to fatal error)" ns2/named2.run > /dev/null && break
sleep 1
done
pid=`cat ns2/named2.pid 2>/dev/null`
test "${pid:+set}" = set && $KILL -15 ${pid} >/dev/null 2>&1
if [ $ret != 0 ]; then echo_i "failed"; fi
status=`expr $status + $ret`
fi
n=`expr $n + 1`
echo_i "verifying that named checks for conflicting named processes ($n)"
ret=0

View File

@@ -72,6 +72,9 @@
/* Define to 1 if you have the <cmocka.h> header file. */
#undef HAVE_CMOCKA_H
/* Define to 1 if you have the `cpuset_setaffinty' function. */
#undef HAVE_CPUSET_SETAFFINITY
/* Define to 1 if you have the <devpoll.h> header file. */
#undef HAVE_DEVPOLL_H
@@ -255,6 +258,9 @@
/* define if OpenSSL supports Ed25519 */
#undef HAVE_OPENSSL_ED25519
/* Define to 1 if you have the `processor_bind' function. */
#undef HAVE_PROCESSOR_BIND
/* Define if you have POSIX threads libraries and header files. */
#undef HAVE_PTHREAD
@@ -273,6 +279,9 @@
/* Have PTHREAD_PRIO_INHERIT. */
#undef HAVE_PTHREAD_PRIO_INHERIT
/* Define to 1 if you have the `pthread_setaffinity_np' function. */
#undef HAVE_PTHREAD_SETAFFINITY_NP
/* Define to 1 if you have the `pthread_setname_np' function. */
#undef HAVE_PTHREAD_SETNAME_NP
@@ -303,6 +312,9 @@
/* Define to 1 if you have the <sched.h> header file. */
#undef HAVE_SCHED_H
/* Define to 1 if you have the `sched_setaffinity' function. */
#undef HAVE_SCHED_SETAFFINITY
/* Define to 1 if you have the `sched_yield' function. */
#undef HAVE_SCHED_YIELD
@@ -357,6 +369,9 @@
/* Define to 1 if you have the <sys/capability.h> header file. */
#undef HAVE_SYS_CAPABILITY_H
/* Define to 1 if you have the <sys/cpuset.h> header file. */
#undef HAVE_SYS_CPUSET_H
/* Define to 1 if you have the <sys/devpoll.h> header file. */
#undef HAVE_SYS_DEVPOLL_H
@@ -366,6 +381,12 @@
/* Define to 1 if you have the <sys/param.h> header file. */
#undef HAVE_SYS_PARAM_H
/* Define to 1 if you have the <sys/prctl.h> header file. */
#undef HAVE_SYS_PRCTL_H
/* Define to 1 if you have the <sys/procset.h> header file. */
#undef HAVE_SYS_PROCSET_H
/* Define to 1 if you have the <sys/select.h> header file. */
#undef HAVE_SYS_SELECT_H

108
configure vendored
View File

@@ -15249,6 +15249,114 @@ fi
done
for ac_header in sys/cpuset.h
do :
ac_fn_c_check_header_mongrel "$LINENO" "sys/cpuset.h" "ac_cv_header_sys_cpuset_h" "$ac_includes_default"
if test "x$ac_cv_header_sys_cpuset_h" = xyes; then :
cat >>confdefs.h <<_ACEOF
#define HAVE_SYS_CPUSET_H 1
_ACEOF
fi
done
for ac_header in sys/procset.h
do :
ac_fn_c_check_header_mongrel "$LINENO" "sys/procset.h" "ac_cv_header_sys_procset_h" "$ac_includes_default"
if test "x$ac_cv_header_sys_procset_h" = xyes; then :
cat >>confdefs.h <<_ACEOF
#define HAVE_SYS_PROCSET_H 1
_ACEOF
fi
done
for ac_func in pthread_setaffinity_np cpuset_setaffinity processor_bind sched_setaffinity
do :
as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
if eval test \"x\$"$as_ac_var"\" = x"yes"; then :
cat >>confdefs.h <<_ACEOF
#define `$as_echo "HAVE_$ac_func" | $as_tr_cpp` 1
_ACEOF
fi
done
#
# Additional OS-specific issues related to pthreads and sigwait.
#
case "$host" in
#
# One more place to look for sigwait.
#
*-freebsd*)
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for sigwait in -lc_r" >&5
$as_echo_n "checking for sigwait in -lc_r... " >&6; }
if ${ac_cv_lib_c_r_sigwait+:} false; then :
$as_echo_n "(cached) " >&6
else
ac_check_lib_save_LIBS=$LIBS
LIBS="-lc_r $LIBS"
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
/* Override any GCC internal prototype to avoid an error.
Use char because int might match the return type of a GCC
builtin and then its argument prototype would still apply. */
#ifdef __cplusplus
extern "C"
#endif
char sigwait ();
int
main ()
{
return sigwait ();
;
return 0;
}
_ACEOF
if ac_fn_c_try_link "$LINENO"; then :
ac_cv_lib_c_r_sigwait=yes
else
ac_cv_lib_c_r_sigwait=no
fi
rm -f core conftest.err conftest.$ac_objext \
conftest$ac_exeext conftest.$ac_ext
LIBS=$ac_check_lib_save_LIBS
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_c_r_sigwait" >&5
$as_echo "$ac_cv_lib_c_r_sigwait" >&6; }
if test "x$ac_cv_lib_c_r_sigwait" = xyes; then :
$as_echo "#define HAVE_SIGWAIT 1" >>confdefs.h
fi
case $host in
*-freebsd5.[012]|*-freebsd5.[012].*);;
*-freebsd5.[3456789]|*-freebsd5.[3456789].*)
$as_echo "#define NEED_PTHREAD_SCOPE_SYSTEM 1" >>confdefs.h
;;
*-freebsd6.*)
$as_echo "#define NEED_PTHREAD_SCOPE_SYSTEM 1" >>confdefs.h
;;
esac
;;
#
# LinuxThreads requires some changes to the way we
# deal with signals.
#
*-linux*)
$as_echo "#define HAVE_LINUXTHREADS 1" >>confdefs.h
;;
esac
# Look for functions relating to thread naming
for ac_func in pthread_setname_np pthread_set_name_np
do :

View File

@@ -715,6 +715,38 @@ AC_CHECK_HEADERS([sched.h])
AC_SEARCH_LIBS([sched_yield],[rt])
AC_CHECK_FUNCS([sched_yield pthread_yield pthread_yield_np])
AC_CHECK_HEADERS([sys/cpuset.h])
AC_CHECK_HEADERS([sys/procset.h])
AC_CHECK_FUNCS([pthread_setaffinity_np cpuset_setaffinity processor_bind sched_setaffinity])
#
# Additional OS-specific issues related to pthreads and sigwait.
#
case "$host" in
#
# One more place to look for sigwait.
#
*-freebsd*)
AC_CHECK_LIB(c_r, sigwait, AC_DEFINE(HAVE_SIGWAIT),)
case $host in
*-freebsd5.[[012]]|*-freebsd5.[[012]].*);;
*-freebsd5.[[3456789]]|*-freebsd5.[[3456789]].*)
AC_DEFINE(NEED_PTHREAD_SCOPE_SYSTEM)
;;
*-freebsd6.*)
AC_DEFINE(NEED_PTHREAD_SCOPE_SYSTEM)
;;
esac
;;
#
# LinuxThreads requires some changes to the way we
# deal with signals.
#
*-linux*)
AC_DEFINE(HAVE_LINUXTHREADS)
;;
esac
# Look for functions relating to thread naming
AC_CHECK_FUNCS([pthread_setname_np pthread_set_name_np])
AC_CHECK_HEADERS([pthread_np.h], [], [], [#include <pthread.h>])

View File

@@ -299,7 +299,8 @@ static isc_result_t get_udpsocket(dns_dispatchmgr_t *mgr,
isc_socketmgr_t *sockmgr,
const isc_sockaddr_t *localaddr,
isc_socket_t **sockp,
isc_socket_t *dup_socket);
isc_socket_t *dup_socket,
bool duponly);
static isc_result_t dispatch_createudp(dns_dispatchmgr_t *mgr,
isc_socketmgr_t *sockmgr,
isc_taskmgr_t *taskmgr,
@@ -317,7 +318,7 @@ static void qid_destroy(isc_mem_t *mctx, dns_qid_t **qidp);
static isc_result_t open_socket(isc_socketmgr_t *mgr,
const isc_sockaddr_t *local,
unsigned int options, isc_socket_t **sockp,
isc_socket_t *dup_socket);
isc_socket_t *dup_socket, bool duponly);
static bool portavailable(dns_dispatchmgr_t *mgr, isc_socket_t *sock,
isc_sockaddr_t *sockaddrp);
@@ -728,7 +729,7 @@ get_dispsocket(dns_dispatch_t *disp, const isc_sockaddr_t *dest,
if (portentry != NULL)
bindoptions |= ISC_SOCKET_REUSEADDRESS;
result = open_socket(sockmgr, &localaddr, bindoptions, &sock,
NULL);
NULL, false);
if (result == ISC_R_SUCCESS) {
if (portentry == NULL) {
portentry = new_portentry(disp, port);
@@ -1668,7 +1669,7 @@ destroy_mgr(dns_dispatchmgr_t **mgrp) {
static isc_result_t
open_socket(isc_socketmgr_t *mgr, const isc_sockaddr_t *local,
unsigned int options, isc_socket_t **sockp,
isc_socket_t *dup_socket)
isc_socket_t *dup_socket, bool duponly)
{
isc_socket_t *sock;
isc_result_t result;
@@ -1678,7 +1679,7 @@ open_socket(isc_socketmgr_t *mgr, const isc_sockaddr_t *local,
result = isc_socket_open(sock);
if (result != ISC_R_SUCCESS)
return (result);
} else if (dup_socket != NULL) {
} else if (dup_socket != NULL && (!isc_socket_hasreuseport() || duponly)) {
result = isc_socket_dup(dup_socket, &sock);
if (result != ISC_R_SUCCESS)
return (result);
@@ -2758,7 +2759,7 @@ dns_dispatch_getudp(dns_dispatchmgr_t *mgr, isc_socketmgr_t *sockmgr,
static isc_result_t
get_udpsocket(dns_dispatchmgr_t *mgr, dns_dispatch_t *disp,
isc_socketmgr_t *sockmgr, const isc_sockaddr_t *localaddr,
isc_socket_t **sockp, isc_socket_t *dup_socket)
isc_socket_t **sockp, isc_socket_t *dup_socket, bool duponly)
{
unsigned int i, j;
isc_socket_t *held[DNS_DISPATCH_HELD];
@@ -2796,7 +2797,7 @@ get_udpsocket(dns_dispatchmgr_t *mgr, dns_dispatch_t *disp,
prt = ports[isc_random_uniform(nports)];
isc_sockaddr_setport(&localaddr_bound, prt);
result = open_socket(sockmgr, &localaddr_bound,
0, &sock, NULL);
0, &sock, NULL, false);
/*
* Continue if the port choosen is already in use
* or the OS has reserved it.
@@ -2817,7 +2818,7 @@ get_udpsocket(dns_dispatchmgr_t *mgr, dns_dispatch_t *disp,
/* Allow to reuse address for non-random ports. */
result = open_socket(sockmgr, localaddr,
ISC_SOCKET_REUSEADDRESS, &sock,
dup_socket);
dup_socket, duponly);
if (result == ISC_R_SUCCESS)
*sockp = sock;
@@ -2829,7 +2830,7 @@ get_udpsocket(dns_dispatchmgr_t *mgr, dns_dispatch_t *disp,
i = 0;
for (j = 0; j < 0xffffU; j++) {
result = open_socket(sockmgr, localaddr, 0, &sock, NULL);
result = open_socket(sockmgr, localaddr, 0, &sock, NULL, false);
if (result != ISC_R_SUCCESS)
goto end;
else if (portavailable(mgr, sock, NULL))
@@ -2874,6 +2875,9 @@ dispatch_createudp(dns_dispatchmgr_t *mgr, isc_socketmgr_t *sockmgr,
isc_socket_t *sock = NULL;
int i = 0;
bool duponly = ((attributes & DNS_DISPATCHATTR_CANREUSE) == 0);
/* This is an attribute needed only at creation time */
attributes &= ~DNS_DISPATCHATTR_CANREUSE;
/*
* dispatch_allocate() checks mgr for us.
*/
@@ -2886,7 +2890,7 @@ dispatch_createudp(dns_dispatchmgr_t *mgr, isc_socketmgr_t *sockmgr,
if ((attributes & DNS_DISPATCHATTR_EXCLUSIVE) == 0) {
result = get_udpsocket(mgr, disp, sockmgr, localaddr, &sock,
dup_socket);
dup_socket, duponly);
if (result != ISC_R_SUCCESS)
goto deallocate_dispatch;
@@ -2911,7 +2915,7 @@ dispatch_createudp(dns_dispatchmgr_t *mgr, isc_socketmgr_t *sockmgr,
*/
isc_sockaddr_anyofpf(&sa_any, isc_sockaddr_pf(localaddr));
if (!isc_sockaddr_eqaddr(&sa_any, localaddr)) {
result = open_socket(sockmgr, localaddr, 0, &sock, NULL);
result = open_socket(sockmgr, localaddr, 0, &sock, NULL, false);
if (sock != NULL)
isc_socket_detach(&sock);
if (result != ISC_R_SUCCESS)

View File

@@ -140,6 +140,7 @@ struct dns_dispatchset {
#define DNS_DISPATCHATTR_CONNECTED 0x00000080U
#define DNS_DISPATCHATTR_FIXEDID 0x00000100U
#define DNS_DISPATCHATTR_EXCLUSIVE 0x00000200U
#define DNS_DISPATCHATTR_CANREUSE 0x00000400U
/*@}*/
/*

View File

@@ -244,8 +244,8 @@ typedef enum {
isc_sockettype_udp = 1,
isc_sockettype_tcp = 2,
isc_sockettype_unix = 3,
isc_sockettype_fdwatch = 4,
isc_sockettype_raw = 5
isc_sockettype_raw = 4,
isc_sockettype_tls = 5
} isc_sockettype_t;
/*@{*/
@@ -276,14 +276,6 @@ typedef enum {
#define ISC_SOCKFLAG_NORETRY 0x00000002 /*%< drop failed UDP sends */
/*@}*/
/*@{*/
/*!
* Flags for fdwatchcreate.
*/
#define ISC_SOCKFDWATCH_READ 0x00000001 /*%< watch for readable */
#define ISC_SOCKFDWATCH_WRITE 0x00000002 /*%< watch for writable */
/*@}*/
/*%
* This structure is actually just the common prefix of a socket manager
* object implementation's version of an isc_socketmgr_t.
@@ -299,7 +291,6 @@ typedef enum {
*/
#ifndef WIN32
struct isc_socketmgr {
unsigned int impmagic;
unsigned int magic;
};
#endif
@@ -309,15 +300,14 @@ struct isc_socketmgr {
(m)->magic == ISCAPI_SOCKETMGR_MAGIC)
/*%
* This is the common prefix of a socket object. The same note as
* that for the socketmgr structure applies.
* This is the common prefix of a socket object.
* The type determines how we handle it - either directly or
* via TLS layer.
*/
#ifndef WIN32
struct isc_socket {
unsigned int impmagic;
unsigned int magic;
isc_sockettype_t type;
};
#endif
#define ISCAPI_SOCKET_MAGIC ISC_MAGIC('A','s','c','t')
#define ISCAPI_SOCKET_VALID(s) ((s) != NULL && \
@@ -330,76 +320,6 @@ struct isc_socket {
*** those functions which return an isc_result.
***/
isc_result_t
isc_socket_fdwatchcreate(isc_socketmgr_t *manager,
int fd,
int flags,
isc_sockfdwatch_t callback,
void *cbarg,
isc_task_t *task,
isc_socket_t **socketp);
/*%<
* Create a new file descriptor watch socket managed by 'manager'.
*
* Note:
*
*\li 'fd' is the already-opened file descriptor (must be less
* than maxsockets).
*\li This function is not available on Windows.
*\li The callback function is called "in-line" - this means the function
* needs to return as fast as possible, as all other I/O will be suspended
* until the callback completes.
*
* Requires:
*
*\li 'manager' is a valid manager
*
*\li 'socketp' is a valid pointer, and *socketp == NULL
*
*\li 'fd' be opened.
*
* Ensures:
*
* '*socketp' is attached to the newly created fdwatch socket
*
* Returns:
*
*\li #ISC_R_SUCCESS
*\li #ISC_R_NOMEMORY
*\li #ISC_R_NORESOURCES
*\li #ISC_R_UNEXPECTED
*\li #ISC_R_RANGE
*/
isc_result_t
isc_socket_fdwatchpoke(isc_socket_t *sock,
int flags);
/*%<
* Poke a file descriptor watch socket informing the manager that it
* should restart watching the socket
*
* Note:
*
*\li 'sock' is the socket returned by isc_socket_fdwatchcreate
*
*\li 'flags' indicates what the manager should watch for on the socket
* in addition to what it may already be watching. It can be one or
* both of ISC_SOCKFDWATCH_READ and ISC_SOCKFDWATCH_WRITE. To
* temporarily disable watching on a socket the value indicating
* no more data should be returned from the call back routine.
*
*\li This function is not available on Windows.
*
* Requires:
*
*\li 'sock' is a valid isc socket
*
*
* Returns:
*
*\li #ISC_R_SUCCESS
*/
isc_result_t
isc_socket_create(isc_socketmgr_t *manager,
int pf,
@@ -408,9 +328,6 @@ isc_socket_create(isc_socketmgr_t *manager,
/*%<
* Create a new 'type' socket managed by 'manager'.
*
* For isc_sockettype_fdwatch sockets you should use isc_socket_fdwatchcreate()
* rather than isc_socket_create().
*
* Note:
*
*\li 'pf' is the desired protocol family, e.g. PF_INET or PF_INET6.
@@ -421,8 +338,6 @@ isc_socket_create(isc_socketmgr_t *manager,
*
*\li 'socketp' is a valid pointer, and *socketp == NULL
*
*\li 'type' is not isc_sockettype_fdwatch
*
* Ensures:
*
* '*socketp' is attached to the newly created socket
@@ -552,17 +467,12 @@ isc_socket_open(isc_socket_t *sock);
* one. This optimization may not be available for some systems, in which
* case this function will return ISC_R_NOTIMPLEMENTED and must not be used.
*
* isc_socket_open() should not be called on sockets created by
* isc_socket_fdwatchcreate().
*
* Requires:
*
* \li there must be no other reference to this socket.
*
* \li 'socket' is a valid and previously closed by isc_socket_close()
*
* \li 'sock->type' is not isc_sockettype_fdwatch
*
* Returns:
* Same as isc_socket_create().
* \li ISC_R_NOTIMPLEMENTED
@@ -578,9 +488,6 @@ isc_socket_close(isc_socket_t *sock);
* systems, in which case this function will return ISC_R_NOTIMPLEMENTED and
* must not be used.
*
* isc_socket_close() should not be called on sockets created by
* isc_socket_fdwatchcreate().
*
* Requires:
*
* \li The socket must have a valid descriptor.
@@ -589,8 +496,6 @@ isc_socket_close(isc_socket_t *sock);
*
* \li There must be no pending I/O requests.
*
* \li 'sock->type' is not isc_sockettype_fdwatch
*
* Returns:
* \li #ISC_R_NOTIMPLEMENTED
*/
@@ -934,7 +839,7 @@ isc_socketmgr_create(isc_mem_t *mctx, isc_socketmgr_t **managerp);
isc_result_t
isc_socketmgr_create2(isc_mem_t *mctx, isc_socketmgr_t **managerp,
unsigned int maxsocks);
unsigned int maxsocks, int nthreads);
/*%<
* Create a socket manager. If "maxsocks" is non-zero, it specifies the
* maximum number of sockets that the created manager should handle.
@@ -1131,6 +1036,12 @@ isc_socketmgr_maxudp(isc_socketmgr_t *mgr, int maxudp);
* Test interface. Drop UDP packet > 'maxudp'.
*/
bool
isc_socket_hasreuseport(void);
/*%<
* Return true if there is SO_REUSEPORT support
*/
#ifdef HAVE_LIBXML2
int
isc_socketmgr_renderxml(isc_socketmgr_t *mgr, xmlTextWriterPtr writer);

View File

@@ -208,6 +208,9 @@ isc_task_detach(isc_task_t **taskp);
void
isc_task_send(isc_task_t *task, isc_event_t **eventp);
void
isc_task_sendto(isc_task_t *task, isc_event_t **eventp, int c);
/*%<
* Send '*event' to 'task'.
*
@@ -221,6 +224,9 @@ isc_task_send(isc_task_t *task, isc_event_t **eventp);
*\li *eventp == NULL.
*/
void
isc_task_sendtoanddetach(isc_task_t **taskp, isc_event_t **eventp, int c);
void
isc_task_sendanddetach(isc_task_t **taskp, isc_event_t **eventp);
/*%<
@@ -666,7 +672,7 @@ isc_taskmgr_create(isc_mem_t *mctx, unsigned int workers,
*/
void
isc_taskmgr_setmode(isc_taskmgr_t *manager, isc_taskmgrmode_t mode);
isc_taskmgr_setprivilegedmode(isc_taskmgr_t *manager);
isc_taskmgrmode_t
isc_taskmgr_mode(isc_taskmgr_t *manager);

View File

@@ -44,6 +44,9 @@ isc_thread_yield(void);
void
isc_thread_setname(isc_thread_t thread, const char *name);
isc_result_t
isc_thread_setaffinity(int cpu);
/* XXX We could do fancier error handling... */
#define isc_thread_join(t, rp) \

View File

@@ -18,6 +18,17 @@
#include <sched.h>
#endif
#if defined(HAVE_CPUSET_H)
#include <sys/param.h>
#include <sys/cpuset.h>
#endif
#if defined(HAVE_SYS_PROCSET_H)
#include <sys/types.h>
#include <sys/processor.h>
#include <sys/procset.h>
#endif
#include <isc/thread.h>
#include <isc/util.h>
@@ -91,3 +102,31 @@ isc_thread_yield(void) {
pthread_yield_np();
#endif
}
isc_result_t
isc_thread_setaffinity(int cpu) {
#if defined(HAVE_CPUSET_SETAFFINITY)
cpuset_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(cpu, &cpuset);
if (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1,
&cpuset, sizeof(cpuset)) != 0) {
return (ISC_R_FAILURE);
}
#elif defined(HAVE_PTHREAD_SETAFFINITY_NP)
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(cpu, &set);
if (pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t),
&set) != 0) {
return (ISC_R_FAILURE);
}
#elif defined(HAVE_PROCESSOR_BIND)
if (processor_bind(P_LWPID, P_MYID, cpu, NULL) != 0) {
return (ISC_R_FAILURE);
}
#else
UNUSED(cpu);
#endif
return (ISC_R_SUCCESS);
}

View File

@@ -21,6 +21,7 @@
#include <stdbool.h>
#include <isc/app.h>
#include <isc/atomic.h>
#include <isc/condition.h>
#include <isc/event.h>
#include <isc/json.h>
@@ -31,6 +32,7 @@
#include <isc/platform.h>
#include <isc/print.h>
#include <isc/string.h>
#include <isc/random.h>
#include <isc/task.h>
#include <isc/thread.h>
#include <isc/time.h>
@@ -41,18 +43,14 @@
#include <openssl/err.h>
#endif
/*%
* For BIND9 internal applications:
* when built with threads we use multiple worker threads shared by the whole
* application.
* when built without threads we share a single global task manager and use
* an integrated event loop for socket, timer, and other generic task events.
* For generic library:
* we don't use either of them: an application can have multiple task managers
* whether or not it's threaded, and if the application is threaded each thread
* is expected to have a separate manager; no "worker threads" are shared by
* the application threads.
/*
* Task manager is built around 'as little locking as possible' concept.
* Each thread has his own queue of tasks to be run, if a task is in running
* state it will stay on the runner it's currently on, if a task is in idle
* state it can be woken up on a specific runner with isc_task_sendto - that
* helps with data locality on CPU.
*/
#ifdef ISC_TASK_TRACE
#define XTRACE(m) fprintf(stderr, "task %p thread %lu: %s\n", \
task, isc_thread_self(), (m))
@@ -86,6 +84,7 @@ static const char *statenames[] = {
typedef struct isc__task isc__task_t;
typedef struct isc__taskmgr isc__taskmgr_t;
typedef struct isc__taskqueue isc__taskqueue_t;
struct isc__task {
/* Not locked. */
@@ -104,6 +103,7 @@ struct isc__task {
isc_time_t tnow;
char name[16];
void * tag;
unsigned int threadid;
/* Locked by task manager lock. */
LINK(isc__task_t) link;
LINK(isc__task_t) ready_link;
@@ -121,27 +121,40 @@ struct isc__task {
typedef ISC_LIST(isc__task_t) isc__tasklist_t;
struct isc__taskqueue {
/* Everything locked by lock */
isc_mutex_t lock;
isc__tasklist_t ready_tasks;
isc__tasklist_t ready_priority_tasks;
isc_condition_t work_available;
isc_thread_t thread;
unsigned int threadid;
isc__taskmgr_t *manager;
};
struct isc__taskmgr {
/* Not locked. */
isc_taskmgr_t common;
isc_mem_t * mctx;
isc_mutex_t lock;
isc_mutex_t halt_lock;
isc_condition_t halt_cond;
unsigned int workers;
isc_thread_t * threads;
atomic_uint_fast32_t tasks_running;
atomic_uint_fast32_t tasks_ready;
atomic_uint_fast32_t curq;
isc__taskqueue_t *queues;
/* Locked by task manager lock. */
unsigned int default_quantum;
LIST(isc__task_t) tasks;
isc__tasklist_t ready_tasks;
isc__tasklist_t ready_priority_tasks;
isc_taskmgrmode_t mode;
isc_condition_t work_available;
isc_condition_t exclusive_granted;
isc_condition_t paused;
unsigned int tasks_running;
unsigned int tasks_ready;
bool pause_requested;
bool exclusive_requested;
bool exiting;
bool pause_requested;
bool exclusive_requested;
bool exiting;
/* Locked by halt_lock */
unsigned int halted;
/*
* Multiple threads can read/write 'excl' at the same time, so we need
@@ -159,7 +172,7 @@ isc__taskmgr_resume(isc_taskmgr_t *manager0);
#define DEFAULT_TASKMGR_QUANTUM 10
#define DEFAULT_DEFAULT_QUANTUM 5
#define DEFAULT_DEFAULT_QUANTUM 50
#define FINISHED(m) ((m)->exiting && EMPTY((m)->tasks))
/*%
@@ -175,13 +188,13 @@ isc_taskmgr_setexcltask(isc_taskmgr_t *mgr0, isc_task_t *task0);
isc_result_t
isc_taskmgr_excltask(isc_taskmgr_t *mgr0, isc_task_t **taskp);
static inline bool
empty_readyq(isc__taskmgr_t *manager);
empty_readyq(isc__taskmgr_t *manager, int c);
static inline isc__task_t *
pop_readyq(isc__taskmgr_t *manager);
pop_readyq(isc__taskmgr_t *manager, int c);
static inline void
push_readyq(isc__taskmgr_t *manager, isc__task_t *task);
push_readyq(isc__taskmgr_t *manager, isc__task_t *task, int c);
/***
*** Tasks.
@@ -190,7 +203,6 @@ push_readyq(isc__taskmgr_t *manager, isc__task_t *task);
static void
task_finished(isc__task_t *task) {
isc__taskmgr_t *manager = task->manager;
REQUIRE(EMPTY(task->events));
REQUIRE(task->nevents == 0);
REQUIRE(EMPTY(task->on_shutdown));
@@ -201,6 +213,7 @@ task_finished(isc__task_t *task) {
LOCK(&manager->lock);
UNLINK(manager->tasks, task, link);
UNLOCK(&manager->lock);
if (FINISHED(manager)) {
/*
* All tasks have completed and the
@@ -208,10 +221,10 @@ task_finished(isc__task_t *task) {
* any idle worker threads so they
* can exit.
*/
BROADCAST(&manager->work_available);
for (unsigned int i=0; i<manager->workers; i++) {
BROADCAST(&manager->queues[i].work_available);
}
}
UNLOCK(&manager->lock);
DESTROYLOCK(&task->lock);
task->common.impmagic = 0;
task->common.magic = 0;
@@ -235,6 +248,9 @@ isc_task_create(isc_taskmgr_t *manager0, unsigned int quantum,
return (ISC_R_NOMEMORY);
XTRACE("isc_task_create");
task->manager = manager;
task->threadid = atomic_fetch_add_explicit(&manager->curq, 1,
memory_order_relaxed)
% manager->workers;
result = isc_mutex_init(&task->lock);
if (result != ISC_R_SUCCESS) {
isc_mem_put(manager->mctx, task, sizeof(*task));
@@ -351,12 +367,11 @@ task_ready(isc__task_t *task) {
REQUIRE(task->state == task_state_ready);
XTRACE("task_ready");
LOCK(&manager->lock);
push_readyq(manager, task);
LOCK(&manager->queues[task->threadid].lock);
push_readyq(manager, task, task->threadid);
if (manager->mode == isc_taskmgrmode_normal || has_privilege)
SIGNAL(&manager->work_available);
UNLOCK(&manager->lock);
SIGNAL(&manager->queues[task->threadid].work_available);
UNLOCK(&manager->queues[task->threadid].lock);
}
static inline bool
@@ -414,7 +429,7 @@ isc_task_detach(isc_task_t **taskp) {
}
static inline bool
task_send(isc__task_t *task, isc_event_t **eventp) {
task_send(isc__task_t *task, isc_event_t **eventp, int c) {
bool was_idle = false;
isc_event_t *event;
@@ -433,6 +448,7 @@ task_send(isc__task_t *task, isc_event_t **eventp) {
if (task->state == task_state_idle) {
was_idle = true;
task->threadid = c;
INSIST(EMPTY(task->events));
task->state = task_state_ready;
}
@@ -447,6 +463,16 @@ task_send(isc__task_t *task, isc_event_t **eventp) {
void
isc_task_send(isc_task_t *task0, isc_event_t **eventp) {
isc_task_sendto(task0, eventp, -1);
}
void
isc_task_sendanddetach(isc_task_t **taskp, isc_event_t **eventp) {
isc_task_sendtoanddetach(taskp, eventp, -1);
}
void
isc_task_sendto(isc_task_t *task0, isc_event_t **eventp, int c) {
isc__task_t *task = (isc__task_t *)task0;
bool was_idle;
@@ -456,6 +482,12 @@ isc_task_send(isc_task_t *task0, isc_event_t **eventp) {
REQUIRE(VALID_TASK(task));
if (c == -1) {
c = atomic_fetch_add_explicit(&task->manager->curq, 1,
memory_order_relaxed)
% task->manager->workers;
}
XTRACE("isc_task_send");
/*
@@ -464,7 +496,7 @@ isc_task_send(isc_task_t *task0, isc_event_t **eventp) {
* some processing is deferred until after the lock is released.
*/
LOCK(&task->lock);
was_idle = task_send(task, eventp);
was_idle = task_send(task, eventp, c);
UNLOCK(&task->lock);
if (was_idle) {
@@ -488,7 +520,7 @@ isc_task_send(isc_task_t *task0, isc_event_t **eventp) {
}
void
isc_task_sendanddetach(isc_task_t **taskp, isc_event_t **eventp) {
isc_task_sendtoanddetach(isc_task_t **taskp, isc_event_t **eventp, int c) {
bool idle1, idle2;
isc__task_t *task;
@@ -500,11 +532,15 @@ isc_task_sendanddetach(isc_task_t **taskp, isc_event_t **eventp) {
REQUIRE(taskp != NULL);
task = (isc__task_t *)*taskp;
REQUIRE(VALID_TASK(task));
if (c == -1) {
c = atomic_fetch_add_explicit(&task->manager->curq, 1,
memory_order_relaxed)
% task->manager->workers;
}
XTRACE("isc_task_sendanddetach");
LOCK(&task->lock);
idle1 = task_send(task, eventp);
idle1 = task_send(task, eventp, c);
idle2 = task_detach(task);
UNLOCK(&task->lock);
@@ -829,13 +865,13 @@ isc_task_getcurrenttimex(isc_task_t *task0, isc_time_t *t) {
* Caller must hold the task manager lock.
*/
static inline bool
empty_readyq(isc__taskmgr_t *manager) {
empty_readyq(isc__taskmgr_t *manager, int c) {
isc__tasklist_t queue;
if (manager->mode == isc_taskmgrmode_normal)
queue = manager->ready_tasks;
queue = manager->queues[c].ready_tasks;
else
queue = manager->ready_priority_tasks;
queue = manager->queues[c].ready_priority_tasks;
return (EMPTY(queue));
}
@@ -849,18 +885,18 @@ empty_readyq(isc__taskmgr_t *manager) {
* Caller must hold the task manager lock.
*/
static inline isc__task_t *
pop_readyq(isc__taskmgr_t *manager) {
pop_readyq(isc__taskmgr_t *manager, int c) {
isc__task_t *task;
if (manager->mode == isc_taskmgrmode_normal)
task = HEAD(manager->ready_tasks);
task = HEAD(manager->queues[c].ready_tasks);
else
task = HEAD(manager->ready_priority_tasks);
task = HEAD(manager->queues[c].ready_priority_tasks);
if (task != NULL) {
DEQUEUE(manager->ready_tasks, task, ready_link);
DEQUEUE(manager->queues[c].ready_tasks, task, ready_link);
if (ISC_LINK_LINKED(task, ready_priority_link))
DEQUEUE(manager->ready_priority_tasks, task,
DEQUEUE(manager->queues[c].ready_priority_tasks, task,
ready_priority_link);
}
@@ -874,20 +910,24 @@ pop_readyq(isc__taskmgr_t *manager) {
* Caller must hold the task manager lock.
*/
static inline void
push_readyq(isc__taskmgr_t *manager, isc__task_t *task) {
ENQUEUE(manager->ready_tasks, task, ready_link);
push_readyq(isc__taskmgr_t *manager, isc__task_t *task, int c) {
ENQUEUE(manager->queues[c].ready_tasks, task, ready_link);
if ((task->flags & TASK_F_PRIVILEGED) != 0)
ENQUEUE(manager->ready_priority_tasks, task,
ENQUEUE(manager->queues[c].ready_priority_tasks, task,
ready_priority_link);
manager->tasks_ready++;
atomic_fetch_add_explicit(&manager->tasks_ready, 1, memory_order_relaxed);
}
static void
dispatch(isc__taskmgr_t *manager) {
dispatch(isc__taskmgr_t *manager, unsigned int threadid) {
isc__task_t *task;
REQUIRE(VALID_MANAGER(manager));
/* Wait for everything to initialize */
LOCK(&manager->lock);
UNLOCK(&manager->lock);
/*
* Again we're trying to hold the lock for as short a time as possible
* and to do as little locking and unlocking as possible.
@@ -937,8 +977,7 @@ dispatch(isc__taskmgr_t *manager) {
* For N iterations of the loop, this code does N+1 locks and N+1
* unlocks. The while expression is always protected by the lock.
*/
LOCK(&manager->lock);
LOCK(&manager->queues[threadid].lock);
while (!FINISHED(manager)) {
/*
@@ -951,13 +990,19 @@ dispatch(isc__taskmgr_t *manager) {
* If a pause has been requested, don't do any work
* until it's been released.
*/
while ((empty_readyq(manager) || manager->pause_requested ||
manager->exclusive_requested) && !FINISHED(manager))
while ((empty_readyq(manager, threadid) && !manager->pause_requested &&
!manager->exclusive_requested) && !FINISHED(manager))
{
XTHREADTRACE(isc_msgcat_get(isc_msgcat,
ISC_MSGSET_GENERAL,
ISC_MSG_WAIT, "wait"));
WAIT(&manager->work_available, &manager->lock);
XTHREADTRACE(isc_msgcat_get(isc_msgcat,
ISC_MSGSET_GENERAL,
ISC_MSG_WAIT, manager->pause_requested ? "paused" : "notpaused"));
XTHREADTRACE(isc_msgcat_get(isc_msgcat,
ISC_MSGSET_GENERAL,
ISC_MSG_WAIT, manager->exclusive_requested ? "excreq" : "notexcreq"));
WAIT(&manager->queues[threadid].work_available, &manager->queues[threadid].lock);
XTHREADTRACE(isc_msgcat_get(isc_msgcat,
ISC_MSGSET_TASK,
ISC_MSG_AWAKE, "awake"));
@@ -965,7 +1010,37 @@ dispatch(isc__taskmgr_t *manager) {
XTHREADTRACE(isc_msgcat_get(isc_msgcat, ISC_MSGSET_TASK,
ISC_MSG_WORKING, "working"));
task = pop_readyq(manager);
if (manager->pause_requested || manager->exclusive_requested) {
UNLOCK(&manager->queues[threadid].lock);
XTHREADTRACE(isc_msgcat_get(isc_msgcat, ISC_MSGSET_TASK,
ISC_MSG_WORKING, "halting"));
/*
* Switching to exclusive mode is done as a 2-phase-lock,
* checking if we have to switch is done without any locks
* on pause_requested and exclusive_requested to save time -
* the worst thing that can happen is that we'll launch one task
* more and exclusive task will be postponed a bit.
*
* Broadcasting on halt_cond seems suboptimal, but exclusive tasks
* are rare enought that we don't care.
*/
LOCK(&manager->halt_lock);
manager->halted++;
BROADCAST(&manager->halt_cond);
while (manager->pause_requested || manager->exclusive_requested) {
WAIT(&manager->halt_cond, &manager->halt_lock);
}
manager->halted--;
SIGNAL(&manager->halt_cond);
UNLOCK(&manager->halt_lock);
LOCK(&manager->queues[threadid].lock);
/* Restart the loop after */
continue;
}
task = pop_readyq(manager, threadid);
if (task != NULL) {
unsigned int dispatch_count = 0;
bool done = false;
@@ -980,15 +1055,16 @@ dispatch(isc__taskmgr_t *manager) {
* have a task to do. We must reacquire the manager
* lock before exiting the 'if (task != NULL)' block.
*/
manager->tasks_ready--;
manager->tasks_running++;
UNLOCK(&manager->lock);
UNLOCK(&manager->queues[threadid].lock);
atomic_fetch_add_explicit(&manager->tasks_ready, -1, memory_order_relaxed);
atomic_fetch_add_explicit(&manager->tasks_running, 1, memory_order_relaxed);
LOCK(&task->lock);
INSIST(task->state == task_state_ready);
task->state = task_state_running;
XTRACE(isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
ISC_MSG_RUNNING, "running"));
XTRACE(task->name);
TIME_NOW(&task->tnow);
task->now = isc_time_seconds(&task->tnow);
do {
@@ -1004,6 +1080,7 @@ dispatch(isc__taskmgr_t *manager) {
ISC_MSGSET_TASK,
ISC_MSG_EXECUTE,
"execute action"));
XTRACE(task->name);
if (event->ev_action != NULL) {
UNLOCK(&task->lock);
(event->ev_action)(
@@ -1094,15 +1171,8 @@ dispatch(isc__taskmgr_t *manager) {
if (finished)
task_finished(task);
LOCK(&manager->lock);
manager->tasks_running--;
if (manager->exclusive_requested &&
manager->tasks_running == 1) {
SIGNAL(&manager->exclusive_granted);
} else if (manager->pause_requested &&
manager->tasks_running == 0) {
SIGNAL(&manager->paused);
}
atomic_fetch_add_explicit(&manager->tasks_running, -1, memory_order_relaxed);
LOCK(&manager->queues[threadid].lock);
if (requeue) {
/*
* We know we're awake, so we don't have
@@ -1123,7 +1193,7 @@ dispatch(isc__taskmgr_t *manager) {
* were usually nonempty, the 'optimization'
* might even hurt rather than help.
*/
push_readyq(manager, task);
push_readyq(manager, task, threadid);
}
}
@@ -1133,27 +1203,51 @@ dispatch(isc__taskmgr_t *manager) {
* we're stuck. Automatically drop privileges at that
* point and continue with the regular ready queue.
*/
if (manager->tasks_running == 0 && empty_readyq(manager)) {
manager->mode = isc_taskmgrmode_normal;
if (!empty_readyq(manager))
BROADCAST(&manager->work_available);
if (atomic_load(&manager->tasks_running) == 0 && manager->mode != isc_taskmgrmode_normal) {
bool empty = true;
for (unsigned i=0; i<manager->workers && empty; i++) {
if (i != threadid) {
LOCK(&manager->queues[i].lock);
}
empty &= empty_readyq(manager, i);
if (i != threadid) {
UNLOCK(&manager->queues[i].lock);
}
}
if (empty) {
manager->mode = isc_taskmgrmode_normal;
for (unsigned i=0; i < manager->workers; i++) {
BROADCAST(&manager->queues[i].work_available);
}
}
}
}
UNLOCK(&manager->lock);
UNLOCK(&manager->queues[threadid].lock);
/*
* There might be other dispatchers waiting on empty tasks,
* wake them up.
*/
for (unsigned i=0; i < manager->workers; i++) {
LOCK(&manager->queues[i].lock);
BROADCAST(&manager->queues[i].work_available);
UNLOCK(&manager->queues[i].lock);
}
}
static isc_threadresult_t
#ifdef _WIN32
WINAPI
#endif
run(void *uap) {
isc__taskmgr_t *manager = uap;
run(void *queuep) {
isc__taskqueue_t *tq = queuep;
isc__taskmgr_t *manager = tq->manager;
int threadid = tq->threadid;
isc_thread_setaffinity(threadid);
XTHREADTRACE(isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
ISC_MSG_STARTING, "starting"));
dispatch(manager);
dispatch(manager, threadid);
XTHREADTRACE(isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
ISC_MSG_EXITING, "exiting"));
@@ -1167,27 +1261,23 @@ run(void *uap) {
static void
manager_free(isc__taskmgr_t *manager) {
isc_mem_t *mctx;
(void)isc_condition_destroy(&manager->exclusive_granted);
(void)isc_condition_destroy(&manager->work_available);
(void)isc_condition_destroy(&manager->paused);
isc_mem_free(manager->mctx, manager->threads);
for (unsigned int i=0; i < manager->workers; i++) {
DESTROYLOCK(&manager->queues[i].lock);
}
DESTROYLOCK(&manager->lock);
DESTROYLOCK(&manager->excl_lock);
DESTROYLOCK(&manager->halt_lock);
isc_mem_put(manager->mctx, manager->queues,
manager->workers * sizeof(isc__taskqueue_t));
manager->common.impmagic = 0;
manager->common.magic = 0;
mctx = manager->mctx;
isc_mem_put(mctx, manager, sizeof(*manager));
isc_mem_detach(&mctx);
isc_mem_putanddetach(&manager->mctx, manager, sizeof(*manager));
}
isc_result_t
isc_taskmgr_create(isc_mem_t *mctx, unsigned int workers,
unsigned int default_quantum, isc_taskmgr_t **managerp)
{
isc_result_t result;
unsigned int i, started = 0;
unsigned int i;
isc__taskmgr_t *manager;
/*
@@ -1198,64 +1288,37 @@ isc_taskmgr_create(isc_mem_t *mctx, unsigned int workers,
REQUIRE(managerp != NULL && *managerp == NULL);
manager = isc_mem_get(mctx, sizeof(*manager));
if (manager == NULL)
return (ISC_R_NOMEMORY);
RUNTIME_CHECK(manager != NULL);
manager->common.impmagic = TASK_MANAGER_MAGIC;
manager->common.magic = ISCAPI_TASKMGR_MAGIC;
manager->mode = isc_taskmgrmode_normal;
manager->mctx = NULL;
result = isc_mutex_init(&manager->lock);
if (result != ISC_R_SUCCESS)
goto cleanup_mgr;
result = isc_mutex_init(&manager->excl_lock);
if (result != ISC_R_SUCCESS) {
DESTROYLOCK(&manager->lock);
goto cleanup_mgr;
}
RUNTIME_CHECK(isc_mutex_init(&manager->lock) == ISC_R_SUCCESS);
RUNTIME_CHECK(isc_mutex_init(&manager->excl_lock) == ISC_R_SUCCESS);
RUNTIME_CHECK(isc_mutex_init(&manager->halt_lock)
== ISC_R_SUCCESS);
RUNTIME_CHECK(isc_condition_init(&manager->halt_cond)
== ISC_R_SUCCESS);
manager->workers = workers;
manager->workers = 0;
manager->threads = isc_mem_allocate(mctx,
workers * sizeof(isc_thread_t));
if (manager->threads == NULL) {
result = ISC_R_NOMEMORY;
goto cleanup_lock;
}
if (isc_condition_init(&manager->work_available) != ISC_R_SUCCESS) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_condition_init() %s",
isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
ISC_MSG_FAILED, "failed"));
result = ISC_R_UNEXPECTED;
goto cleanup_threads;
}
if (isc_condition_init(&manager->exclusive_granted) != ISC_R_SUCCESS) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_condition_init() %s",
isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
ISC_MSG_FAILED, "failed"));
result = ISC_R_UNEXPECTED;
goto cleanup_workavailable;
}
if (isc_condition_init(&manager->paused) != ISC_R_SUCCESS) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_condition_init() %s",
isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
ISC_MSG_FAILED, "failed"));
result = ISC_R_UNEXPECTED;
goto cleanup_exclusivegranted;
}
if (default_quantum == 0)
default_quantum = DEFAULT_DEFAULT_QUANTUM;
manager->default_quantum = default_quantum;
INIT_LIST(manager->tasks);
INIT_LIST(manager->ready_tasks);
INIT_LIST(manager->ready_priority_tasks);
manager->queues = isc_mem_get(mctx, workers *
sizeof(isc__taskqueue_t));
RUNTIME_CHECK(manager->queues != NULL);
manager->tasks_running = 0;
manager->tasks_ready = 0;
manager->exclusive_requested = false;
manager->pause_requested = false;
manager->curq = 0;
manager->exiting = false;
manager->excl = NULL;
manager->halted = 0;
manager->exclusive_requested = false;
manager->pause_requested = false;
isc_mem_attach(mctx, &manager->mctx);
@@ -1264,40 +1327,29 @@ isc_taskmgr_create(isc_mem_t *mctx, unsigned int workers,
* Start workers.
*/
for (i = 0; i < workers; i++) {
if (isc_thread_create(run, manager,
&manager->threads[manager->workers]) ==
ISC_R_SUCCESS) {
char name[16]; /* thread name limit on Linux */
snprintf(name, sizeof(name), "isc-worker%04u", i);
isc_thread_setname(manager->threads[manager->workers],
name);
manager->workers++;
started++;
}
INIT_LIST(manager->queues[i].ready_tasks);
INIT_LIST(manager->queues[i].ready_priority_tasks);
RUNTIME_CHECK(isc_mutex_init(&manager->queues[i].lock)
== ISC_R_SUCCESS);
RUNTIME_CHECK(isc_condition_init(
&manager->queues[i].work_available)
== ISC_R_SUCCESS);
manager->queues[i].manager = manager;
manager->queues[i].threadid = i;
RUNTIME_CHECK(isc_thread_create(run, &manager->queues[i],
&manager->queues[i].thread)
== ISC_R_SUCCESS);
char name[16];
snprintf(name, sizeof(name), "isc-worker%04u", i);
isc_thread_setname(manager->queues[i].thread, name);
}
UNLOCK(&manager->lock);
if (started == 0) {
manager_free(manager);
return (ISC_R_NOTHREADS);
}
isc_thread_setconcurrency(workers);
*managerp = (isc_taskmgr_t *)manager;
return (ISC_R_SUCCESS);
cleanup_exclusivegranted:
(void)isc_condition_destroy(&manager->exclusive_granted);
cleanup_workavailable:
(void)isc_condition_destroy(&manager->work_available);
cleanup_threads:
isc_mem_free(mctx, manager->threads);
cleanup_lock:
DESTROYLOCK(&manager->lock);
cleanup_mgr:
isc_mem_put(mctx, manager, sizeof(*manager));
return (result);
}
void
@@ -1355,29 +1407,38 @@ isc_taskmgr_destroy(isc_taskmgr_t **managerp) {
/*
* Post shutdown event(s) to every task (if they haven't already been
* posted).
* posted). To make things easier post idle tasks to worker 0.
*/
LOCK(&manager->queues[0].lock);
for (task = HEAD(manager->tasks);
task != NULL;
task = NEXT(task, link)) {
LOCK(&task->lock);
if (task_shutdown(task))
push_readyq(manager, task);
if (task_shutdown(task)) {
task->threadid = 0;
push_readyq(manager, task, 0);
}
UNLOCK(&task->lock);
}
UNLOCK(&manager->queues[0].lock);
/*
* Wake up any sleeping workers. This ensures we get work done if
* there's work left to do, and if there are already no tasks left
* it will cause the workers to see manager->exiting.
*/
BROADCAST(&manager->work_available);
for (i = 0; i < manager->workers; i++) {
LOCK(&manager->queues[i].lock);
BROADCAST(&manager->queues[i].work_available);
UNLOCK(&manager->queues[i].lock);
}
UNLOCK(&manager->lock);
/*
* Wait for all the worker threads to exit.
*/
for (i = 0; i < manager->workers; i++)
(void)isc_thread_join(manager->threads[i], NULL);
(void)isc_thread_join(manager->queues[i].thread, NULL);
manager_free(manager);
@@ -1385,11 +1446,11 @@ isc_taskmgr_destroy(isc_taskmgr_t **managerp) {
}
void
isc_taskmgr_setmode(isc_taskmgr_t *manager0, isc_taskmgrmode_t mode) {
isc_taskmgr_setprivilegedmode(isc_taskmgr_t *manager0) {
isc__taskmgr_t *manager = (isc__taskmgr_t *)manager0;
LOCK(&manager->lock);
manager->mode = mode;
manager->mode = isc_taskmgrmode_privileged;
UNLOCK(&manager->lock);
}
@@ -1406,24 +1467,38 @@ isc_taskmgr_mode(isc_taskmgr_t *manager0) {
void
isc__taskmgr_pause(isc_taskmgr_t *manager0) {
isc__taskmgr_t *manager = (isc__taskmgr_t *)manager0;
manager->pause_requested = true;
LOCK(&manager->lock);
while (manager->tasks_running > 0) {
WAIT(&manager->paused, &manager->lock);
unsigned int i;
LOCK(&manager->halt_lock);
while (manager->exclusive_requested || manager->pause_requested) {
UNLOCK(&manager->halt_lock);
/* This is ugly but pause is used EXCLUSIVELY in tests */
isc_thread_yield();
LOCK(&manager->halt_lock);
}
UNLOCK(&manager->lock);
manager->pause_requested = true;
while (manager->halted < manager->workers) {
for (i = 0; i < manager->workers; i++) {
BROADCAST(&manager->queues[i].work_available);
}
WAIT(&manager->halt_cond, &manager->halt_lock);
}
UNLOCK(&manager->halt_lock);
}
void
isc__taskmgr_resume(isc_taskmgr_t *manager0) {
isc__taskmgr_t *manager = (isc__taskmgr_t *)manager0;
LOCK(&manager->lock);
LOCK(&manager->halt_lock);
if (manager->pause_requested) {
manager->pause_requested = false;
BROADCAST(&manager->work_available);
while (manager->halted > 0) {
BROADCAST(&manager->halt_cond);
WAIT(&manager->halt_cond, &manager->halt_lock);
}
}
UNLOCK(&manager->lock);
UNLOCK(&manager->halt_lock);
}
void
@@ -1462,24 +1537,28 @@ isc_result_t
isc_task_beginexclusive(isc_task_t *task0) {
isc__task_t *task = (isc__task_t *)task0;
isc__taskmgr_t *manager = task->manager;
unsigned int i;
REQUIRE(VALID_TASK(task));
REQUIRE(task->state == task_state_running);
/*
* TODO REQUIRE(task == task->manager->excl);
* it should be here, it fails on shutdown server->task
*/
REQUIRE(task == task->manager->excl ||
(task->manager->exiting && task->manager->excl == NULL));
LOCK(&manager->lock);
if (manager->exclusive_requested) {
UNLOCK(&manager->lock);
if (manager->exclusive_requested || manager->pause_requested) {
return (ISC_R_LOCKBUSY);
}
LOCK(&manager->halt_lock);
INSIST(!manager->exclusive_requested && !manager->pause_requested);
manager->exclusive_requested = true;
while (manager->tasks_running > 1) {
WAIT(&manager->exclusive_granted, &manager->lock);
while (manager->halted + 1 < manager->workers) {
for (i = 0; i < manager->workers; i++) {
BROADCAST(&manager->queues[i].work_available);
}
WAIT(&manager->halt_cond, &manager->halt_lock);
}
UNLOCK(&manager->lock);
UNLOCK(&manager->halt_lock);
return (ISC_R_SUCCESS);
}
@@ -1490,11 +1569,14 @@ isc_task_endexclusive(isc_task_t *task0) {
REQUIRE(VALID_TASK(task));
REQUIRE(task->state == task_state_running);
LOCK(&manager->lock);
LOCK(&manager->halt_lock);
REQUIRE(manager->exclusive_requested);
manager->exclusive_requested = false;
BROADCAST(&manager->work_available);
UNLOCK(&manager->lock);
while (manager->halted > 0) {
BROADCAST(&manager->halt_cond);
WAIT(&manager->halt_cond, &manager->halt_lock);
}
UNLOCK(&manager->halt_lock);
}
void
@@ -1515,14 +1597,14 @@ isc_task_setprivilege(isc_task_t *task0, bool priv) {
if (priv == oldpriv)
return;
LOCK(&manager->lock);
LOCK(&manager->queues[task->threadid].lock);
if (priv && ISC_LINK_LINKED(task, ready_link))
ENQUEUE(manager->ready_priority_tasks, task,
ready_priority_link);
ENQUEUE(manager->queues[task->threadid].ready_priority_tasks,
task, ready_priority_link);
else if (!priv && ISC_LINK_LINKED(task, ready_priority_link))
DEQUEUE(manager->ready_priority_tasks, task,
ready_priority_link);
UNLOCK(&manager->lock);
DEQUEUE(manager->queues[task->threadid].ready_priority_tasks,
task, ready_priority_link);
UNLOCK(&manager->queues[task->threadid].lock);
}
bool
@@ -1575,11 +1657,13 @@ isc_taskmgr_renderxml(isc_taskmgr_t *mgr0, xmlTextWriterPtr writer) {
TRY0(xmlTextWriterEndElement(writer)); /* default-quantum */
TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "tasks-running"));
TRY0(xmlTextWriterWriteFormatString(writer, "%d", mgr->tasks_running));
TRY0(xmlTextWriterWriteFormatString(writer, "%d",
(int) mgr->tasks_running));
TRY0(xmlTextWriterEndElement(writer)); /* tasks-running */
TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "tasks-ready"));
TRY0(xmlTextWriterWriteFormatString(writer, "%d", mgr->tasks_ready));
TRY0(xmlTextWriterWriteFormatString(writer, "%d",
(int) mgr->tasks_ready));
TRY0(xmlTextWriterEndElement(writer)); /* tasks-ready */
TRY0(xmlTextWriterEndElement(writer)); /* thread-model */

View File

@@ -67,7 +67,6 @@ set_and_drop(isc_task_t *task, isc_event_t *event) {
*value = (int) isc_taskmgr_mode(taskmgr);
counter++;
UNLOCK(&lock);
isc_taskmgr_setmode(taskmgr, isc_taskmgrmode_normal);
}
/*
@@ -230,7 +229,7 @@ ATF_TC_BODY(privileged_events, tc) {
isc_task_send(task2, &event);
ATF_CHECK_EQ(isc_taskmgr_mode(taskmgr), isc_taskmgrmode_normal);
isc_taskmgr_setmode(taskmgr, isc_taskmgrmode_privileged);
isc_taskmgr_setprivilegedmode(taskmgr);
ATF_CHECK_EQ(isc_taskmgr_mode(taskmgr), isc_taskmgrmode_privileged);
isc__taskmgr_resume(taskmgr);
@@ -351,7 +350,7 @@ ATF_TC_BODY(privilege_drop, tc) {
isc_task_send(task2, &event);
ATF_CHECK_EQ(isc_taskmgr_mode(taskmgr), isc_taskmgrmode_normal);
isc_taskmgr_setmode(taskmgr, isc_taskmgrmode_privileged);
isc_taskmgr_setprivilegedmode(taskmgr);
ATF_CHECK_EQ(isc_taskmgr_mode(taskmgr), isc_taskmgrmode_privileged);
isc__taskmgr_resume(taskmgr);
@@ -363,14 +362,12 @@ ATF_TC_BODY(privilege_drop, tc) {
}
/*
* We can't guarantee what order the events fire, but
* we do know *exactly one* of the privileged tasks will
* have run in privileged mode...
* We need to check that all privilege mode events were fired
* in privileged mode, and non privileged in non-privileged.
*/
ATF_CHECK(a == isc_taskmgrmode_privileged ||
c == isc_taskmgrmode_privileged ||
ATF_CHECK(a == isc_taskmgrmode_privileged &&
c == isc_taskmgrmode_privileged &&
d == isc_taskmgrmode_privileged);
ATF_CHECK(a + c + d == isc_taskmgrmode_privileged);
/* ...and neither of the non-privileged tasks did... */
ATF_CHECK(b == isc_taskmgrmode_normal || e == isc_taskmgrmode_normal);

File diff suppressed because it is too large Load Diff

View File

@@ -79,6 +79,9 @@ isc_thread_setconcurrency(unsigned int level);
void
isc_thread_setname(isc_thread_t, const char *);
isc_result_t
isc_thread_setaffinity(int cpu);
int
isc_thread_key_create(isc_thread_key_t *key, void (*func)(void *));

View File

@@ -78,6 +78,7 @@ isc_socket_getpeername
isc_socket_getsockname
isc_socket_gettag
isc_socket_gettype
isc_socket_hasreuseport
isc_socket_ipv6only
isc_socket_listen
isc_socket_open
@@ -629,6 +630,8 @@ isc_task_purgeevent
isc_task_purgerange
isc_task_send
isc_task_sendanddetach
isc_task_sendto
isc_task_sendtoanddetach
isc_task_setname
isc_task_setprivilege
isc_task_shutdown
@@ -645,7 +648,7 @@ isc_taskmgr_renderjson
isc_taskmgr_renderxml
@END LIBXML2
isc_taskmgr_setexcltask
isc_taskmgr_setmode
isc_taskmgr_setprivilegedmode
isc_taskpool_create
isc_taskpool_destroy
isc_taskpool_expand
@@ -658,6 +661,7 @@ isc_thread_key_create
isc_thread_key_delete
isc_thread_key_getspecific
isc_thread_key_setspecific
isc_thread_setaffinity
isc_thread_setconcurrency
isc_thread_setname
isc_time_add

File diff suppressed because it is too large Load Diff

View File

@@ -66,6 +66,12 @@ isc_thread_setname(isc_thread_t thread, const char *name) {
UNUSED(name);
}
isc_result_t
isc_thread_setaffinity(int cpu) {
/* no-op on Windows for now */
return (ISC_R_SUCCESS);
}
void *
isc_thread_key_getspecific(isc_thread_key_t key) {
return(TlsGetValue(key));

View File

@@ -1656,7 +1656,7 @@ ns_client_addopt(ns_client_t *client, dns_message_t *message,
isc_buffer_init(&buf, cookie, sizeof(cookie));
isc_stdtime_get(&now);
isc_nonce_buf(&nonce, sizeof(nonce));
isc_random_buf(&nonce, sizeof(nonce));
compute_cookie(client, now, nonce, client->sctx->secret, &buf);

View File

@@ -470,6 +470,7 @@ ns_interface_listenudp(ns_interface_t *ifp) {
else
attrs |= DNS_DISPATCHATTR_IPV6;
attrs |= DNS_DISPATCHATTR_NOLISTEN;
attrs |= DNS_DISPATCHATTR_CANREUSE;
attrmask = 0;
attrmask |= DNS_DISPATCHATTR_UDP | DNS_DISPATCHATTR_TCP;
attrmask |= DNS_DISPATCHATTR_IPV4 | DNS_DISPATCHATTR_IPV6;