Skip to contentSkip to conversation

ipvs: Use kthreads for stats

RFC PATCH Superseded Julian Anastasov 2 participants 8 messages Sep 8, 2022 – Sep 9, 2022

Subsystems (5)

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Julian Anastasov

author maintainer
#0 lore raw

v2 cover ipvs: Use kthreads for stats

Hello,

This patchset implements stats estimation in
kthread context. Simple tests do not show any problem.

As it is late here, I'm sending v2 for
review. It is interesting to know what value for IPVS_EST_TICK_CHAINS to use, it is used for the IPVS_EST_MAX_COUNT calculation. We should determine it from tests once the loops are in final form. Now the limit increased a little bit to 38400. Tomorrow I'll check again the patches for possible problems.

Overview of the basic concepts. More in the
commit messages...

RCU Locking:

- As stats are now RCU-locked, tot_stats, svc and dest which hold estimator structures are now always freed from RCU callback. This ensures RCU grace period after the ip_vs_stop_estimator() call.

Kthread data:

- every kthread works over its own data structure and all such structures are attached to array

- even while there can be a kthread structure, its task may not be running, eg. before first service is added or while the sysctl var is set to an empty cpulist or when run_estimation is 0.

- a task and its structure may be released if all estimators are unlinked from its chains, leaving the slot in the array empty

- every kthread data structure allows limited number of estimators

- to add new estimators we use the last added kthread context (est_add_ktid). The new estimators are linked to the chains just before the estimated one, based on add_row. This ensures their estimation will start after 2 seconds. If estimators are added in bursts, common case if all services and dests are initially configured, we may spread the estimators to more chains. This will reduce the chain imbalance.

Not done yet:
* limit for kthreads (sysctl var)

Changes in v2:
Patch 2:
* kd->mutex is gone, cond_resched rate determined by IPVS_EST_CHAIN_DEPTH
* IPVS_EST_MAX_COUNT is a hard limit now
* kthread data is now 1-50 allocated tick structures, each containing heads for limited chains. Bitmaps should allow faster access. We avoid large allocations for structs.
* as the td->present bitmap is shared, use atomic bitops
* ip_vs_start_estimator now returns error code
* _bh locking removed from stats->lock
* bump arg is gone from ip_vs_est_reload_start
* prepare for upcoming changes that remove _irq from u64_stats_fetch_begin_irq/u64_stats_fetch_retry_irq
* est_add_ktid is now always valid
Patch 3:
* use .. in est_nice docs

Julian Anastasov (4):
ipvs: add rcu protection to stats
ipvs: use kthreads for stats estimation
ipvs: add est_cpulist and est_nice sysctl vars
ipvs: run_estimation should control the kthread tasks

 Documentation/networking/ipvs-sysctl.rst |  24 +-
 include/net/ip_vs.h                      | 122 +++++-
 net/netfilter/ipvs/ip_vs_core.c          |  10 +-
 net/netfilter/ipvs/ip_vs_ctl.c           | 367 +++++++++++++++---
 net/netfilter/ipvs/ip_vs_est.c           | 450 +++++++++++++++++++----
 5 files changed, 843 insertions(+), 130 deletions(-)
Signature · 1 line
2.37.3
author maintainer
#1 lore raw

RFC PATCH v2 1/4 ipvs: add rcu protection to stats

In preparation to using RCU locking for the list with estimators, make sure the struct ip_vs_stats are released after RCU grace period by using RCU callbacks. This affects ipvs->tot_stats where we can not use RCU callbacks for ipvs, so we use allocated struct ip_vs_stats_rcu. For services and dests we force RCU callbacks for all cases.

---
 include/net/ip_vs.h             |  8 ++++-
 net/netfilter/ipvs/ip_vs_core.c | 10 ++++--
 net/netfilter/ipvs/ip_vs_ctl.c  | 64 ++++++++++++++++++++++-----------
 3 files changed, 57 insertions(+), 25 deletions(-)
include/net/ip_vs.h +7 -1
index ff1804a0c469..bd8ae137e43b 100644
@@ -405,6 +405,11 @@ struct ip_vs_stats {
 	struct ip_vs_kstats	kstats0;	/* reset values */
 };
 
+struct ip_vs_stats_rcu {
+	struct ip_vs_stats	s;
+	struct rcu_head		rcu_head;
+};
+
 struct dst_entry;
 struct iphdr;
 struct ip_vs_conn;
@@ -688,6 +693,7 @@ struct ip_vs_dest {
 	union nf_inet_addr	vaddr;		/* virtual IP address */
 	__u32			vfwmark;	/* firewall mark of service */
 
+	struct rcu_head		rcu_head;
 	struct list_head	t_list;		/* in dest_trash */
 	unsigned int		in_rs_table:1;	/* we are in rs_table */
 };
@@ -869,7 +875,7 @@ struct netns_ipvs {
 	atomic_t		conn_count;      /* connection counter */
 
 	/* ip_vs_ctl */
-	struct ip_vs_stats		tot_stats;  /* Statistics & est. */
+	struct ip_vs_stats_rcu	*tot_stats;      /* Statistics & est. */
 
 	int			num_services;    /* no of virtual services */
 	int			num_services6;   /* IPv6 virtual services */
net/netfilter/ipvs/ip_vs_core.c +7 -3
index 51ad557a525b..fcdaef1fcccf 100644
@@ -143,7 +143,7 @@ ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
 		s->cnt.inbytes += skb->len;
 		u64_stats_update_end(&s->syncp);
 
-		s = this_cpu_ptr(ipvs->tot_stats.cpustats);
+		s = this_cpu_ptr(ipvs->tot_stats->s.cpustats);
 		u64_stats_update_begin(&s->syncp);
 		s->cnt.inpkts++;
 		s->cnt.inbytes += skb->len;
@@ -179,7 +179,7 @@ ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
 		s->cnt.outbytes += skb->len;
 		u64_stats_update_end(&s->syncp);
 
-		s = this_cpu_ptr(ipvs->tot_stats.cpustats);
+		s = this_cpu_ptr(ipvs->tot_stats->s.cpustats);
 		u64_stats_update_begin(&s->syncp);
 		s->cnt.outpkts++;
 		s->cnt.outbytes += skb->len;
@@ -208,7 +208,7 @@ ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
 	s->cnt.conns++;
 	u64_stats_update_end(&s->syncp);
 
-	s = this_cpu_ptr(ipvs->tot_stats.cpustats);
+	s = this_cpu_ptr(ipvs->tot_stats->s.cpustats);
 	u64_stats_update_begin(&s->syncp);
 	s->cnt.conns++;
 	u64_stats_update_end(&s->syncp);
@@ -2448,6 +2448,10 @@ static void __exit ip_vs_cleanup(void)
 	ip_vs_conn_cleanup();
 	ip_vs_protocol_cleanup();
 	ip_vs_control_cleanup();
+	/* common rcu_barrier() used by:
+	 * - ip_vs_control_cleanup()
+	 */
+	rcu_barrier();
 	pr_info("ipvs unloaded.\n");
 }
 
net/netfilter/ipvs/ip_vs_ctl.c +43 -21

171 lines not on this page · raw message

Signature · 1 line
2.37.3
author maintainer
#2 lore raw

RFC PATCH v2 2/4 ipvs: use kthreads for stats estimation

In reply to Julian Anastasov

Estimating all entries in single list in timer context causes large latency with multiple rules.

Spread the estimator structures in multiple chains and use kthread(s) for the estimation. Every chain is processed under RCU lock. The kthreads are tuned for maximum number of estimators to process.

We also add delayed work est_reload_work that will make sure the kthread tasks are properly started/stopped.

ip_vs_start_estimator() is changed to report errors which allows to safely store the estimators in allocated structures.

---
 include/net/ip_vs.h            |  62 ++++-
 net/netfilter/ipvs/ip_vs_ctl.c | 137 +++++++---
 net/netfilter/ipvs/ip_vs_est.c | 444 ++++++++++++++++++++++++++++-----
 3 files changed, 540 insertions(+), 103 deletions(-)
include/net/ip_vs.h +57 -5
index bd8ae137e43b..cffb63d3020c 100644
@@ -365,7 +365,7 @@ struct ip_vs_cpu_stats {
 
 /* IPVS statistics objects */
 struct ip_vs_estimator {
-	struct list_head	list;
+	struct hlist_node	list;
 
 	u64			last_inbytes;
 	u64			last_outbytes;
@@ -378,6 +378,50 @@ struct ip_vs_estimator {
 	u64			outpps;
 	u64			inbps;
 	u64			outbps;
+
+	u32			ktid:16,	/* kthread ID */
+				ktrow:8,	/* row ID for kthread */
+				ktcid:8;	/* chain ID for kthread */
+};
+
+/* Process estimators in multiple timer ticks */
+#define IPVS_EST_NTICKS		50
+/* Estimation uses a 2-second period */
+#define IPVS_EST_TICK		((2 * HZ) / IPVS_EST_NTICKS)
+
+/* The defines should match cond_resched_rcu */
+#if defined(CONFIG_DEBUG_ATOMIC_SLEEP) || !defined(CONFIG_PREEMPT_RCU)
+#define IPVS_EST_TICK_CHAINS	48
+#define IPVS_EST_CHAIN_DEPTH	16
+#else
+#define IPVS_EST_TICK_CHAINS	1
+#define IPVS_EST_CHAIN_DEPTH	(48 * 16)
+#endif
+
+/* Hard limit of estimators per kthread data */
+#define IPVS_EST_MAX_COUNT	(IPVS_EST_NTICKS * IPVS_EST_TICK_CHAINS * \
+				 IPVS_EST_CHAIN_DEPTH)
+
+/* Multiple chains processed in same tick */
+struct ip_vs_est_tick_data {
+	struct hlist_head	chains[IPVS_EST_TICK_CHAINS];
+	DECLARE_BITMAP(present, IPVS_EST_TICK_CHAINS);
+	DECLARE_BITMAP(full, IPVS_EST_TICK_CHAINS);
+	int			chain_len[IPVS_EST_TICK_CHAINS];
+};
+
+/* Context for estimation kthread */
+struct ip_vs_est_kt_data {
+	struct netns_ipvs	*ipvs;
+	struct task_struct	*task;		/* task if running */
+	struct ip_vs_est_tick_data __rcu *ticks[IPVS_EST_NTICKS];
+	DECLARE_BITMAP(avail, IPVS_EST_NTICKS);	/* tick has space for ests */
+	unsigned long		est_timer;	/* estimation timer (jiffies) */
+	int			tick_len[IPVS_EST_NTICKS];	/* est count */
+	int			id;		/* ktid per netns */
+	int			est_count;	/* attached ests to kthread */
+	int			add_row;	/* row for new ests */
+	int			est_row;	/* estimated row */
 };
 
 /*
@@ -948,9 +992,13 @@ struct netns_ipvs {
 	struct ctl_table_header	*lblcr_ctl_header;
 	struct ctl_table	*lblcr_ctl_table;
 	/* ip_vs_est */
-	struct list_head	est_list;	/* estimator list */
-	spinlock_t		est_lock;
-	struct timer_list	est_timer;	/* Estimation timer */
+	struct delayed_work	est_reload_work;/* Reload kthread tasks */
+	struct mutex		est_mutex;	/* protect kthread tasks */
+	struct ip_vs_est_kt_data **est_kt_arr;	/* Array of kthread data ptrs */
+	int			est_kt_count;	/* Allocated ptrs */
+	int			est_add_ktid;	/* ktid where to add ests */
+	atomic_t		est_genid;	/* kthreads reload genid */
+	atomic_t		est_genid_done;	/* applied genid */
 	/* ip_vs_sync */
 	spinlock_t		sync_lock;
 	struct ipvs_master_sync_state *ms;
@@ -1481,10 +1529,14 @@ int stop_sync_thread(struct netns_ipvs *ipvs, int state);
 void ip_vs_sync_conn(struct netns_ipvs *ipvs, struct ip_vs_conn *cp, int pkts);
 
 /* IPVS rate estimator prototypes (from ip_vs_est.c) */
-void ip_vs_start_estimator(struct netns_ipvs *ipvs, struct ip_vs_stats *stats);
+int ip_vs_start_estimator(struct netns_ipvs *ipvs, struct ip_vs_stats *stats);
 void ip_vs_stop_estimator(struct netns_ipvs *ipvs, struct ip_vs_stats *stats);
 void ip_vs_zero_estimator(struct ip_vs_stats *stats);
 void ip_vs_read_estimator(struct ip_vs_kstats *dst, struct ip_vs_stats *stats);
+void ip_vs_est_reload_start(struct netns_ipvs *ipvs);
+int ip_vs_est_kthread_start(struct netns_ipvs *ipvs,
+			    struct ip_vs_est_kt_data *kd);
+void ip_vs_est_kthread_stop(struct ip_vs_est_kt_data *kd);
 
 /* Various IPVS packet transmitters (from ip_vs_xmit.c) */
 int ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
net/netfilter/ipvs/ip_vs_ctl.c +107 -30

306 lines not on this page · raw message

net/netfilter/ipvs/ip_vs_est.c +376 -68

497 lines not on this page · raw message

Signature · 1 line
2.37.3
author maintainer
#3 lore raw

Re: [RFC PATCHv2 2/4] ipvs: use kthreads for stats estimation

Hello,

net/netfilter/ipvs/ip_vs_est.c static void estimation_timer(struct timer_list *t) · line 357

Quoted 8 lines · Julian Anastasov
net/netfilter/ipvs/ip_vs_est.c +8 -0
+add_est:
+	ktid = kd->id;
+	/* add_row points after the row we should use */
+	crow = READ_ONCE(kd->add_row) - 1;
+	if (crow < 0)
+		crow = IPVS_EST_NTICKS - 1;
+	row = crow;
+	if (crow) {
	OK, not fatal but this should be

	if (crow < IPVS_EST_NTICKS - 1) {

net/netfilter/ipvs/ip_vs_est.c static void estimation_timer(struct timer_list *t) · line 363

Quoted 6 lines
net/netfilter/ipvs/ip_vs_est.c +6 -0
+		crow++;
+		row = find_last_bit(kd->avail, crow);
+	}
+	if (row >= crow)
+		row = find_last_bit(kd->avail, IPVS_EST_NTICKS);
+

Regards

--
Julian Anastasov <[email protected]>

author maintainer
#4 lore raw

RFC PATCH v2 3/4 ipvs: add est_cpulist and est_nice sysctl vars

In reply to Julian Anastasov

Allow the kthreads for stats to be configured for specific cpulist (isolation) and niceness (scheduling priority).

---
 Documentation/networking/ipvs-sysctl.rst |  20 ++++
 include/net/ip_vs.h                      |  50 ++++++++
 net/netfilter/ipvs/ip_vs_ctl.c           | 141 ++++++++++++++++++++++-
 net/netfilter/ipvs/ip_vs_est.c           |   9 +-
 4 files changed, 218 insertions(+), 2 deletions(-)
Documentation/networking/ipvs-sysctl.rst +20 -0
index 387fda80f05f..1b778705d706 100644
@@ -129,6 +129,26 @@ drop_packet - INTEGER
 	threshold. When the mode 3 is set, the always mode drop rate
 	is controlled by the /proc/sys/net/ipv4/vs/am_droprate.
 
+est_cpulist - CPULIST
+	Allowed	CPUs for estimation kthreads
+
+	Syntax: standard cpulist format
+	empty list - stop kthread tasks and estimation
+	default - the system's housekeeping CPUs for kthreads
+
+	Example:
+	"all": all possible CPUs
+	"0-N": all possible CPUs, N denotes last CPU number
+	"0,1-N:1/2": first and all CPUs with odd number
+	"": empty list
+
+est_nice - INTEGER
+	default 0
+	Valid range: -20 (more favorable) .. 19 (less favorable)
+
+	Niceness value to use for the estimation kthreads (scheduling
+	priority)
+
 expire_nodest_conn - BOOLEAN
 	- 0 - disabled (default)
 	- not 0 - enabled
include/net/ip_vs.h +50 -0

86 lines not on this page · raw message

net/netfilter/ipvs/ip_vs_ctl.c +140 -1

177 lines not on this page · raw message

net/netfilter/ipvs/ip_vs_est.c +8 -1
index eb973a664c26..6024ea8075e4 100644
@@ -55,6 +55,9 @@
   - kthread contexts are created and attached to array
   - the kthread tasks are created when first service is added, before that
     the total stats are not estimated
+  - when configuration (cpulist/nice) is changed, the tasks are restarted
+    by work (est_reload_work)
+  - kthread tasks are stopped while the cpulist is empty
   - the kthread context holds lists with estimators (chains) which are
     processed every 2 seconds
   - as estimators can be added dynamically and in bursts, we try to spread
@@ -207,6 +210,7 @@ void ip_vs_est_reload_start(struct netns_ipvs *ipvs)
 	/* Ignore reloads before first service is added */
 	if (!ipvs->enable)
 		return;
+	ip_vs_est_stopped_recalc(ipvs);
 	/* Bump the kthread configuration genid */
 	atomic_inc(&ipvs->est_genid);
 	queue_delayed_work(system_long_wq, &ipvs->est_reload_work, 0);
@@ -237,6 +241,9 @@ int ip_vs_est_kthread_start(struct netns_ipvs *ipvs,
 		goto out;
 	}
 
+	set_user_nice(kd->task, sysctl_est_nice(ipvs));
+	set_cpus_allowed_ptr(kd->task, sysctl_est_cpulist(ipvs));
+
 	pr_info("starting estimator thread %d...\n", kd->id);
 	wake_up_process(kd->task);
 
@@ -286,7 +293,7 @@ static int ip_vs_est_add_kthread(struct netns_ipvs *ipvs)
 	kd->est_timer = jiffies;
 	kd->id = id;
 	/* Start kthread tasks only when services are present */
-	if (ipvs->enable) {
+	if (ipvs->enable && !ip_vs_est_stopped(ipvs)) {
 		ret = ip_vs_est_kthread_start(ipvs, kd);
 		if (ret < 0)
 			goto out;
Signature · 1 line
2.37.3
author maintainer
#5 lore raw

RFC PATCH v2 4/4 ipvs: run_estimation should control the kthread tasks

In reply to Julian Anastasov

Change the run_estimation flag to start/stop the kthread tasks.

---
 Documentation/networking/ipvs-sysctl.rst |  4 ++--
 include/net/ip_vs.h                      |  6 +++--
 net/netfilter/ipvs/ip_vs_ctl.c           | 29 +++++++++++++++++++++++-
 net/netfilter/ipvs/ip_vs_est.c           |  3 +--
 4 files changed, 35 insertions(+), 7 deletions(-)
Documentation/networking/ipvs-sysctl.rst +2 -2
index 1b778705d706..3fb5fa142eef 100644
@@ -324,8 +324,8 @@ run_estimation - BOOLEAN
 	0 - disabled
 	not 0 - enabled (default)
 
-	If disabled, the estimation will be stop, and you can't see
-	any update on speed estimation data.
+	If disabled, the estimation will be suspended and kthread tasks
+	stopped.
 
 	You can always re-enable estimation by setting this value to 1.
 	But be careful, the first estimation after re-enable is not
include/net/ip_vs.h +4 -2
index 4486324afc84..8bc1160ce574 100644
@@ -1574,8 +1574,10 @@ void ip_vs_est_kthread_stop(struct ip_vs_est_kt_data *kd);
 static inline void ip_vs_est_stopped_recalc(struct netns_ipvs *ipvs)
 {
 #ifdef CONFIG_SYSCTL
-	ipvs->est_stopped = ipvs->est_cpulist_valid &&
-			    cpumask_empty(sysctl_est_cpulist(ipvs));
+	/* Stop tasks while cpulist is empty or if disabled with flag */
+	ipvs->est_stopped = !sysctl_run_estimation(ipvs) ||
+			    (ipvs->est_cpulist_valid &&
+			     cpumask_empty(sysctl_est_cpulist(ipvs)));
 #endif
 }
 
net/netfilter/ipvs/ip_vs_ctl.c +28 -1
index 5c3bd701cb19..ea4df891145f 100644
@@ -2036,6 +2036,32 @@ static int ipvs_proc_est_nice(struct ctl_table *table, int write,
 	return ret;
 }
 
+static int ipvs_proc_run_estimation(struct ctl_table *table, int write,
+				    void *buffer, size_t *lenp, loff_t *ppos)
+{
+	struct netns_ipvs *ipvs = table->extra2;
+	int *valp = table->data;
+	int val = *valp;
+	int ret;
+
+	struct ctl_table tmp_table = {
+		.data = &val,
+		.maxlen = sizeof(int),
+		.mode = table->mode,
+	};
+
+	ret = proc_dointvec(&tmp_table, write, buffer, lenp, ppos);
+	if (write && ret >= 0) {
+		mutex_lock(&ipvs->est_mutex);
+		if (*valp != val) {
+			*valp = val;
+			ip_vs_est_reload_start(ipvs);
+		}
+		mutex_unlock(&ipvs->est_mutex);
+	}
+	return ret;
+}
+
 /*
  *	IPVS sysctl table (under the /proc/sys/net/ipv4/vs/)
  *	Do not change order or insert new entries without
@@ -2210,7 +2236,7 @@ static struct ctl_table vs_vars[] = {
 		.procname	= "run_estimation",
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
-		.proc_handler	= proc_dointvec,
+		.proc_handler	= ipvs_proc_run_estimation,
 	},
 	{
 		.procname	= "est_cpulist",
@@ -4302,6 +4328,7 @@ static int __net_init ip_vs_control_net_init_sysctl(struct netns_ipvs *ipvs)
 	tbl[idx++].data = &ipvs->sysctl_schedule_icmp;
 	tbl[idx++].data = &ipvs->sysctl_ignore_tunneled;
 	ipvs->sysctl_run_estimation = 1;
+	tbl[idx].extra2 = ipvs;
 	tbl[idx++].data = &ipvs->sysctl_run_estimation;
 
 	ipvs->est_cpulist_valid = 0;
net/netfilter/ipvs/ip_vs_est.c +1 -2
index 6024ea8075e4..a6a40fc4b14a 100644
@@ -163,7 +163,6 @@ static void ip_vs_tick_estimation(struct ip_vs_est_kt_data *kd, int row)
 static int ip_vs_estimation_kthread(void *data)
 {
 	struct ip_vs_est_kt_data *kd = data;
-	struct netns_ipvs *ipvs = kd->ipvs;
 	int row = kd->est_row;
 	unsigned long now;
 	long gap;
@@ -188,7 +187,7 @@ static int ip_vs_estimation_kthread(void *data)
 				kd->est_timer = now;
 		}
 
-		if (sysctl_run_estimation(ipvs) && kd->tick_len[row])
+		if (kd->tick_len[row])
 			ip_vs_tick_estimation(kd, row);
 
 		row++;
Signature · 1 line
2.37.3
#6 lore raw
In reply to Julian Anastasov
Quoted 7 lines · Julian Anastasov

It is interesting to know what value for IPVS_EST_TICK_CHAINS to use, it is used for the IPVS_EST_MAX_COUNT calculation. We should determine it from tests once the loops are in final form. Now the limit increased a little bit to 38400. Tomorrow I'll check again the patches for possible problems.

I couldn't wait so I have run tests on various machines and used the sched_switch tracepoint to measure the time needed to process one chain. The table contains a median time for processing one chain, the maximum time measured, the median divided by the number of CPUs and the time needed to process one chain if there were 1024 CPUs of that type in a machine:

Quoted 7 lines

NR CPU Time(ms) Max(ms) Time/CPU(ms) 1024 CPUs(ms)

48 Intel Xeon CPU E5-2670 v3, 2 nodes   1.220    1.343         0.025     26.027
64 Intel Xeon Gold 6326, 2 nodes        0.920    1.494         0.014     14.720
192 Intel Xeon Gold 6330H, 4 nodes      3.957    4.153         0.021     21.104
256 AMD EPYC 7713, 2 NUMA nodes         3.927    5.464         0.015     15.708
 80 ARM Neoverse-N1, 1 NUMA node        1.833    2.502         0.023     23.462
128 ARM Kunpeng 920, 4 NUMA nodes       3.822    4.635         0.030     30.576

I have to admit I was hoping the current IPVS_EST_CHAIN_DEPTH would work on machines with more than 1024 CPUs. If the max time values are used the time needed to process one chain on a 1024 CPU machine gets even closer to 40 ms, which it must not reach lest the estimates become inaccurate. I also have profiling data so I intend to look at the disassembly of ip_vs_estimation_kthread() to see which instructions take the most time. I will take a look at the v2 of the code on Monday.

Signature · 2 lines
Jiri Wiesner
SUSE Labs
author maintainer
#7 lore raw

Hello,

Quoted 10 lines · Jiri Wiesner
Quoted 7 lines · Julian Anastasov

It is interesting to know what value for IPVS_EST_TICK_CHAINS to use, it is used for the IPVS_EST_MAX_COUNT calculation. We should determine it from tests once the loops are in final form. Now the limit increased a little bit to 38400. Tomorrow I'll check again the patches for possible problems.

I couldn't wait so I have run tests on various machines and used the sched_switch tracepoint to measure the time needed to process one chain. The table contains a median time for processing one chain, the maximum time measured, the median divided by the number of CPUs and the time needed to process one chain if there were 1024 CPUs of that type in a machine:

Quoted 8 lines
Quoted 7 lines

NR CPU Time(ms) Max(ms) Time/CPU(ms) 1024 CPUs(ms)

48 Intel Xeon CPU E5-2670 v3, 2 nodes   1.220    1.343         0.025     26.027
64 Intel Xeon Gold 6326, 2 nodes        0.920    1.494         0.014     14.720
192 Intel Xeon Gold 6330H, 4 nodes      3.957    4.153         0.021     21.104
256 AMD EPYC 7713, 2 NUMA nodes         3.927    5.464         0.015     15.708
 80 ARM Neoverse-N1, 1 NUMA node        1.833    2.502         0.023     23.462
128 ARM Kunpeng 920, 4 NUMA nodes       3.822    4.635         0.030     30.576

I have to admit I was hoping the current IPVS_EST_CHAIN_DEPTH would work on machines with more than 1024 CPUs. If the max time values are used the time needed to process one chain on a 1024 CPU machine gets even closer to 40 ms, which it must not reach lest the estimates become inaccurate. I also have profiling data so I intend to look at the disassembly of ip_vs_estimation_kthread() to see which instructions take the most time. I will take a look at the v2 of the code on Monday.

v2 uses find_next_bit in for_each_set_bit which has
cost. But we should not be surprised, if 268ms are for 50000 estimators on 104 CPUs (I guess this is also the number of possible CPUs we actually use), one estimator reads from 104 CPUs for 5.36 microsecs, we can conclude for 1024 CPUs the following:

Num Est 104 CPU 1024 CPU
========================================
1 5.36us 53us
4 21us 211us
16 86us 845us

The v2 algorithm allows IPVS_EST_CHAIN_DEPTH to
be changed to var which we can determine based on the CPU count, more CPUs will need more threads and we have CPUs for them:

kd->chain_depth = max(1800 / num_possible_cpus(), 2);

Goals:
- chain time: sub-100 usec cond_resched rate
- tick time: 10% of max 40ms

CPUs Depth est_max_count Chain Time Tick Time
=========================================================
4 450 1080000 93us 4453us
16 112 268800 92us 4433us
104 17 40800 91us 4374us
1024 2 4800 106us 5066us
4096 2 4800 422us 20265us

Summary:

- For 4096 CPUs we can start 208 kthreads for 1000000 ests, crazy :)

- 4096 CPUs need to be fast to go below these 20ms or we should use chain with 1 estimator for 2048+ CPUs

If we track somehow when a stats is updated,
may be we can skip estimators that are idle for some time, this can save CPU cycles for estimating unused dests.

Also, I'm investigating the idea to use
task_rlimit(current, RLIMIT_NPROC) as kthread limit when first service is added and to save it into ipvs->est_max_threads.

Another idea is ip_vs_estimation_kthread not to
change add_row but ip_vs_start_estimator to consider instead est_row for the same purpose but only when kd->est_count becomes large, say 2 * IPVS_EST_TICK_CHAINS * kd->chain_depth. The idea is to fill 2 ticks completely when small number of estimators are added and to prefer est_row when we exceed this threshold and prefer to spread the estimators to more ticks by honouring the 2-second initial timer.

	For example:

	if (kd->est_count >= 2 * IPVS_EST_TICK_CHAINS *
	    kd->chain_depth)
		crow = READ_ONCE(kd->est_row);
	else
		crow = READ_ONCE(kd->add_row);
	crow--;
	...

Regards

--
Julian Anastasov <[email protected]>