3f2a16
From 140ae14a4d6374530c9ab19a29c246abbf6d60b8 Mon Sep 17 00:00:00 2001
3f2a16
From: =?UTF-8?q?Petr=20Men=C5=A1=C3=ADk?= <pemensik@redhat.com>
3f2a16
Date: Fri, 11 Feb 2022 13:29:03 +0100
3f2a16
Subject: [PATCH] Add extra hp workers parameter
3f2a16
3f2a16
May be needed by dyndb plugins to prepare extra worker threads. If they
3f2a16
use isc_task_send, their threads need to have initialized enough thread
3f2a16
identifiers in isc_hp_init. New parameters allow that.
3f2a16
3f2a16
Allows specification of both relative and absolute number of extra
3f2a16
workers.
3f2a16
3f2a16
Adds new function isc_managers_create2() with extra parameter, call
3f2a16
original function from many tests and tools unchanged.
3f2a16
---
3f2a16
 bin/named/include/named/globals.h |  2 ++
3f2a16
 bin/named/include/named/main.h    |  2 +-
3f2a16
 bin/named/main.c                  | 29 +++++++++++++++++++++++++----
3f2a16
 bin/named/named.rst               |  9 +++++++++
3f2a16
 lib/isc/include/isc/managers.h    |  5 +++++
3f2a16
 lib/isc/managers.c                | 11 ++++++++++-
3f2a16
 6 files changed, 52 insertions(+), 6 deletions(-)
3f2a16
3f2a16
diff --git a/bin/named/include/named/globals.h b/bin/named/include/named/globals.h
3f2a16
index 82b632ef04..d46e62b94d 100644
3f2a16
--- a/bin/named/include/named/globals.h
3f2a16
+++ b/bin/named/include/named/globals.h
3f2a16
@@ -53,6 +53,8 @@ EXTERN unsigned int named_g_udpdisp	      INIT(0);
3f2a16
 EXTERN isc_taskmgr_t *named_g_taskmgr	      INIT(NULL);
3f2a16
 EXTERN dns_dispatchmgr_t *named_g_dispatchmgr INIT(NULL);
3f2a16
 EXTERN unsigned int named_g_cpus_detected     INIT(1);
3f2a16
+EXTERN unsigned int named_g_hp_extra_workers  INIT(0);
3f2a16
+EXTERN unsigned int named_g_hp_extra_percent  INIT(100);
3f2a16
 
3f2a16
 #ifdef ENABLE_AFL
3f2a16
 EXTERN bool named_g_run_done INIT(false);
3f2a16
diff --git a/bin/named/include/named/main.h b/bin/named/include/named/main.h
3f2a16
index 816aeae7a0..9618251f4d 100644
3f2a16
--- a/bin/named/include/named/main.h
3f2a16
+++ b/bin/named/include/named/main.h
3f2a16
@@ -23,7 +23,7 @@
3f2a16
 /*
3f2a16
  * Commandline arguments for named; also referenced in win32/ntservice.c
3f2a16
  */
3f2a16
-#define NAMED_MAIN_ARGS "46A:c:d:D:E:fFgL:M:m:n:N:p:sS:t:T:U:u:vVx:X:"
3f2a16
+#define NAMED_MAIN_ARGS "46A:c:d:D:E:fFgh:H:L:M:m:n:N:p:sS:t:T:U:u:vVx:X:"
3f2a16
 
3f2a16
 ISC_PLATFORM_NORETURN_PRE void
3f2a16
 named_main_earlyfatal(const char *format, ...)
3f2a16
diff --git a/bin/named/main.c b/bin/named/main.c
3f2a16
index 9ad2d0e277..fbe4c8d011 100644
3f2a16
--- a/bin/named/main.c
3f2a16
+++ b/bin/named/main.c
3f2a16
@@ -351,7 +351,8 @@ usage(void) {
3f2a16
 			"username] [-U listeners]\n"
3f2a16
 			"             [-X lockfile] [-m "
3f2a16
 			"{usage|trace|record|size|mctx}]\n"
3f2a16
-			"             [-M fill|nofill]\n"
3f2a16
+			"             [-M fill|nofill] [-h extra_workers] "
3f2a16
+			"[-H extra_percent]\n"
3f2a16
 			"usage: named [-v|-V]\n");
3f2a16
 }
3f2a16
 
3f2a16
@@ -792,6 +793,21 @@ parse_command_line(int argc, char *argv[]) {
3f2a16
 			named_g_foreground = true;
3f2a16
 			named_g_logstderr = true;
3f2a16
 			break;
3f2a16
+		case 'h':
3f2a16
+			named_g_hp_extra_workers =
3f2a16
+				parse_int(isc_commandline_argument,
3f2a16
+					  "number of extra workers");
3f2a16
+			break;
3f2a16
+		case 'H':
3f2a16
+			named_g_hp_extra_percent =
3f2a16
+				parse_int(isc_commandline_argument,
3f2a16
+					  "percent of extra workers");
3f2a16
+			if (named_g_hp_extra_percent < 100) {
3f2a16
+				named_main_earlyfatal(
3f2a16
+					"percent of extra workers "
3f2a16
+					"cannot be less than 100");
3f2a16
+			}
3f2a16
+			break;
3f2a16
 		case 'L':
3f2a16
 			named_g_logfile = isc_commandline_argument;
3f2a16
 			break;
3f2a16
@@ -904,6 +920,10 @@ create_managers(void) {
3f2a16
 	if (named_g_cpus == 0) {
3f2a16
 		named_g_cpus = named_g_cpus_detected;
3f2a16
 	}
3f2a16
+	if (named_g_hp_extra_percent > 100) {
3f2a16
+		named_g_hp_extra_workers +=
3f2a16
+			named_g_cpus * (named_g_hp_extra_percent - 100) / 100;
3f2a16
+	}
3f2a16
 	isc_log_write(
3f2a16
 		named_g_lctx, NAMED_LOGCATEGORY_GENERAL, NAMED_LOGMODULE_SERVER,
3f2a16
 		ISC_LOG_INFO, "found %u CPU%s, using %u worker thread%s",
3f2a16
@@ -924,11 +944,12 @@ create_managers(void) {
3f2a16
 		      "using %u UDP listener%s per interface", named_g_udpdisp,
3f2a16
 		      named_g_udpdisp == 1 ? "" : "s");
3f2a16
 
3f2a16
-	result = isc_managers_create(named_g_mctx, named_g_cpus, 0, &named_g_nm,
3f2a16
-				     &named_g_taskmgr);
3f2a16
+	result = isc_managers_create2(named_g_mctx, named_g_cpus, 0,
3f2a16
+				      named_g_hp_extra_workers, &named_g_nm,
3f2a16
+				      &named_g_taskmgr);
3f2a16
 	if (result != ISC_R_SUCCESS) {
3f2a16
 		UNEXPECTED_ERROR(__FILE__, __LINE__,
3f2a16
-				 "isc_managers_create() failed: %s",
3f2a16
+				 "isc_managers_create2() failed: %s",
3f2a16
 				 isc_result_totext(result));
3f2a16
 		return (ISC_R_UNEXPECTED);
3f2a16
 	}
3f2a16
diff --git a/bin/named/named.rst b/bin/named/named.rst
3f2a16
index 39c445f5f9..b5bf95398c 100644
3f2a16
--- a/bin/named/named.rst
3f2a16
+++ b/bin/named/named.rst
3f2a16
@@ -75,6 +75,15 @@ Options
3f2a16
 ``-g``
3f2a16
    This option runs the server in the foreground and forces all logging to ``stderr``.
3f2a16
 
3f2a16
+``-h #workers``
3f2a16
+   This option requests extra worker threads initialized by hazard pointers. Used only for
3f2a16
+   dyndb plugins.
3f2a16
+
3f2a16
+``-H workers%``
3f2a16
+   This option requests extra worker threads initialized by hazard pointers. Value is
3f2a16
+   computed as percent of number of cpus set by ``-n`` parameter. Minimal value is 100%.
3f2a16
+   Used only for dyndb plugins.
3f2a16
+
3f2a16
 ``-L logfile``
3f2a16
    This option sets the log to the file ``logfile`` by default, instead of the system log.
3f2a16
 
3f2a16
diff --git a/lib/isc/include/isc/managers.h b/lib/isc/include/isc/managers.h
3f2a16
index 0ed17ff31d..712ea3e3ed 100644
3f2a16
--- a/lib/isc/include/isc/managers.h
3f2a16
+++ b/lib/isc/include/isc/managers.h
3f2a16
@@ -25,5 +25,10 @@ isc_result_t
3f2a16
 isc_managers_create(isc_mem_t *mctx, size_t workers, size_t quantum,
3f2a16
 		    isc_nm_t **netmgrp, isc_taskmgr_t **taskmgrp);
3f2a16
 
3f2a16
+isc_result_t
3f2a16
+isc_managers_create2(isc_mem_t *mctx, size_t workers, size_t quantum,
3f2a16
+		     size_t hp_extra, isc_nm_t **netmgrp,
3f2a16
+		     isc_taskmgr_t **taskmgrp);
3f2a16
+
3f2a16
 void
3f2a16
 isc_managers_destroy(isc_nm_t **netmgrp, isc_taskmgr_t **taskmgrp);
3f2a16
diff --git a/lib/isc/managers.c b/lib/isc/managers.c
3f2a16
index a285e80d8a..daf19c5596 100644
3f2a16
--- a/lib/isc/managers.c
3f2a16
+++ b/lib/isc/managers.c
3f2a16
@@ -21,15 +21,24 @@
3f2a16
 isc_result_t
3f2a16
 isc_managers_create(isc_mem_t *mctx, size_t workers, size_t quantum,
3f2a16
 		    isc_nm_t **netmgrp, isc_taskmgr_t **taskmgrp) {
3f2a16
+	return isc_managers_create2(mctx, workers, quantum, 0, netmgrp,
3f2a16
+				    taskmgrp);
3f2a16
+}
3f2a16
+
3f2a16
+isc_result_t
3f2a16
+isc_managers_create2(isc_mem_t *mctx, size_t workers, size_t quantum,
3f2a16
+		     size_t hp_extra, isc_nm_t **netmgrp,
3f2a16
+		     isc_taskmgr_t **taskmgrp) {
3f2a16
 	isc_result_t result;
3f2a16
 	isc_taskmgr_t *taskmgr = NULL;
3f2a16
 	isc_nm_t *netmgr = NULL;
3f2a16
+	size_t hp_workers = 4 * (workers + hp_extra);
3f2a16
 
3f2a16
 	/*
3f2a16
 	 * We have ncpus network threads, ncpus old network threads - make
3f2a16
 	 * it 4x just to be on the safe side.
3f2a16
 	 */
3f2a16
-	isc_hp_init(4 * workers);
3f2a16
+	isc_hp_init(hp_workers);
3f2a16
 
3f2a16
 	REQUIRE(netmgrp != NULL && *netmgrp == NULL);
3f2a16
 	isc__netmgr_create(mctx, workers, &netmgr);
3f2a16
-- 
3f2a16
2.34.1
3f2a16