Skip to contentSkip to conversation

ipvs: Use kthreads for stats

RFC PATCH Superseded Julian Anastasov 2 participants 24 messages Oct 31, 2022 – Nov 21, 2022

Subsystems (5)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Julian Anastasov

author maintainer
#0 lore raw

v6 cover ipvs: Use kthreads for stats

Hello,

Posting v6 (release candidate 1). New patch
inserted at 3th position. Patch 7 just for debugging, do not apply if not needed.

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

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. For now we limit kthreads depending on the number of CPUs.

- 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.

- the allocated kthread context may grow from 1 to 50 allocated structures for ticks which saves memory for setups with small number of estimators

- 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. Kthread 0 is also used to initially calculate the number of estimators to allow in every chain considering a sub-100 microsecond cond_resched rate. This number can be from 1 to hundreds.

- kthread 0 has an additional job of optimizing the adding of estimators: they are first added in temp list (est_temp_list) and later kthread 0 distributes them to other kthreads. The optimization is based on the fact that newly added estimator should be estimated after 2 seconds, so we have the time to offload the adding to chain from controlling process to kthread 0.

- 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.

There are things that I don't like but for now
I don't have a better idea for them:

- calculation of chain_max can go wrong, depending on the current load, CPU speed, memory speeds, running in VM, whether tested estimators are in CPU cache, even if we are doing it in SCHED_FIFO mode or with BH disabled. I expect such noise to be insignificant but who knows.

- ip_vs_stop_estimator is not a simple unlinking of list node, we spend cycles to account for the removed estimator

- the relinking does not preserve the delay for all estimators. To solve it, we would need to pre-allocate kthread data for all threads and then to enqueue stats in parallel.

- __ip_vs_mutex is global mutex for all netns. But it protects hash tables that are still global ones.

Changes in v6:
Patch 4 (was 3 in v5):
* while ests are in est_temp_list use est->ktrow as delay (offset from est_row) to apply on enqueue, initially or later when estimators are relinked. By this way we try to preserve the current timer delay on relinking. If there is no space in the desired time slot, the initial delay can be reduced, while on relinking the delay can be increased. Still, this is far from perfect and the relinking continues to apply wrong delay for some of the ests while its goal is still to fill kthreads data one by one.
* ip_vs_est_calc_limits() now relinks the entries trying to preserve the delay, ests with lowest delay will get higher chance to preserve it
* add_row is now not updated (it is wrong on relinking) but will continue to be used as starting position for the bit searching, so logic is preserved
* while relinking try to keep tot_stats in kt0, we do not want to keep some kthread 1..N just for it
* ip_vs_est_calc_limits() now performs 12 tests but sleeps for 20ms on every 4 tests to make sure CPU speed is increased
Patch 3:
* new patch that converts per-cpu counters to u64_stats_t

Changes in v5:
Patch 4 (was 3 in v4):
* use ip_vs_est_max_threads() helper
Patch 3 (was 2 in v4):
* kthread 0 now disables BH instead of using SCHED_FIFO mode, this should work because now we perform less number of repeated test over pre-allocated kd->calc_stats structure. Use cache_factor = 4 to approximate the time non-cached (due to large number) per-cpu stats are estimated compared to the cached data we estimate in calc phase. This needs to be tested with different number of CPUs and NUMA nodes.
* limit max threads using the formula 4 * Number of CPUs, not on rlimit
* remove _len suffix from vars like chain_max_len, tick_max_len, est_chain_max_len
Patch 2:
* new patch that adds functions for stats allocations

Changes in v4:
Patch 2:
* kthread 0 can start with calculation phase in SCHED_FIFO mode to determine chain_max_len suitable for 100us cond_resched rate and 12% of 40ms CPU usage in a tick. Current value of IPVS_EST_TICK_CHAINS=48 determines tick time of 4.8ms (i.e. in units of 100us) which is 12% of max tick time of 40ms. The question is how reliable will be such calculation test.
* est_calc_phase indicates a mode where we dequeue estimators from kthreads, apply new chain_max_len and enqueue again all estimators to kthreads, done by kthread 0
* est->ktid now can be -1 to indicate est is in est_temp_list ready to be distributed to kthread by kt 0, done in ip_vs_est_drain_temp_list(). kthread 0 data is now released only after the data for others kthreads
* ip_vs_start_estimator was not setting ret = 0
* READ_ONCE not needed for volatile jiffies
Patch 3:
* restrict cpulist based on the cpus_allowed of process that assigns cpulist, not on cpu_possible_mask
* change of cpulist will trigger calc phase
Patch 5:
* print message every minute, not 2 seconds

Changes in v3:
Patch 2:
* calculate chain_max_len (was IPVS_EST_CHAIN_DEPTH) but it needs further tuning based on real estimation test
* est_max_threads set from rlimit(RLIMIT_NPROC). I don't see analog to get_ucounts_value() to get the max value.
* the atomic bitop for td->present is not needed, remove it
* start filling based on est_row after 2 ticks are fully allocated. As 2/50 is 4% this can be increased more.

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 (7):
ipvs: add rcu protection to stats
ipvs: use common functions for stats allocation
ipvs: use u64_stats_t for the per-cpu counters
ipvs: use kthreads for stats estimation
ipvs: add est_cpulist and est_nice sysctl vars
ipvs: run_estimation should control the kthread tasks
ipvs: debug the tick time

 Documentation/networking/ipvs-sysctl.rst |  24 +-
 include/net/ip_vs.h                      | 154 +++-
 net/netfilter/ipvs/ip_vs_core.c          |  40 +-
 net/netfilter/ipvs/ip_vs_ctl.c           | 448 ++++++++---
 net/netfilter/ipvs/ip_vs_est.c           | 902 +++++++++++++++++++++--
 5 files changed, 1383 insertions(+), 185 deletions(-)
Signature · 1 line
2.38.1
author maintainer
#1 lore raw

RFC PATCH v6 1/7 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.38.1
Reviewed-by on v6 1/7
#2 lore raw

Re: [RFC PATCHv6 1/7] ipvs: add rcu protection to stats

Quoted 10 lines · Julian Anastasov

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.

---

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

RFC PATCH v6 2/7 ipvs: use common functions for stats allocation

In reply to Julian Anastasov

Move alloc_percpu/free_percpu logic in new functions

---
 include/net/ip_vs.h            |  5 ++
 net/netfilter/ipvs/ip_vs_ctl.c | 96 +++++++++++++++++++---------------
 2 files changed, 60 insertions(+), 41 deletions(-)
include/net/ip_vs.h +5 -0
index bd8ae137e43b..e5582c01a4a3 100644
@@ -410,6 +410,11 @@ struct ip_vs_stats_rcu {
 	struct rcu_head		rcu_head;
 };
 
+int ip_vs_stats_init_alloc(struct ip_vs_stats *s);
+struct ip_vs_stats *ip_vs_stats_alloc(void);
+void ip_vs_stats_release(struct ip_vs_stats *stats);
+void ip_vs_stats_free(struct ip_vs_stats *stats);
+
 struct dst_entry;
 struct iphdr;
 struct ip_vs_conn;
net/netfilter/ipvs/ip_vs_ctl.c +54 -39

178 lines not on this page · raw message

author maintainer
#5 lore raw

RFC PATCH v6 3/7 ipvs: use u64_stats_t for the per-cpu counters

In reply to Julian Anastasov

Use the provided u64_stats_t type to avoid
load/store tearing.

Fixes 316580b69d0a ("u64_stats: provide u64_stats_t type")
---
 include/net/ip_vs.h             | 10 +++++-----
 net/netfilter/ipvs/ip_vs_core.c | 30 +++++++++++++++---------------
 net/netfilter/ipvs/ip_vs_ctl.c  | 10 +++++-----
 net/netfilter/ipvs/ip_vs_est.c  | 20 ++++++++++----------
 4 files changed, 35 insertions(+), 35 deletions(-)
include/net/ip_vs.h +5 -5
index e5582c01a4a3..a4d44138c2a8 100644
@@ -351,11 +351,11 @@ struct ip_vs_seq {
 
 /* counters per cpu */
 struct ip_vs_counters {
-	__u64		conns;		/* connections scheduled */
-	__u64		inpkts;		/* incoming packets */
-	__u64		outpkts;	/* outgoing packets */
-	__u64		inbytes;	/* incoming bytes */
-	__u64		outbytes;	/* outgoing bytes */
+	u64_stats_t	conns;		/* connections scheduled */
+	u64_stats_t	inpkts;		/* incoming packets */
+	u64_stats_t	outpkts;	/* outgoing packets */
+	u64_stats_t	inbytes;	/* incoming bytes */
+	u64_stats_t	outbytes;	/* outgoing bytes */
 };
 /* Stats per cpu */
 struct ip_vs_cpu_stats {
net/netfilter/ipvs/ip_vs_core.c +15 -15
index fcdaef1fcccf..2fcc26507d69 100644
@@ -132,21 +132,21 @@ ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
 
 		s = this_cpu_ptr(dest->stats.cpustats);
 		u64_stats_update_begin(&s->syncp);
-		s->cnt.inpkts++;
-		s->cnt.inbytes += skb->len;
+		u64_stats_inc(&s->cnt.inpkts);
+		u64_stats_add(&s->cnt.inbytes, skb->len);
 		u64_stats_update_end(&s->syncp);
 
 		svc = rcu_dereference(dest->svc);
 		s = this_cpu_ptr(svc->stats.cpustats);
 		u64_stats_update_begin(&s->syncp);
-		s->cnt.inpkts++;
-		s->cnt.inbytes += skb->len;
+		u64_stats_inc(&s->cnt.inpkts);
+		u64_stats_add(&s->cnt.inbytes, skb->len);
 		u64_stats_update_end(&s->syncp);
 
 		s = this_cpu_ptr(ipvs->tot_stats->s.cpustats);
 		u64_stats_update_begin(&s->syncp);
-		s->cnt.inpkts++;
-		s->cnt.inbytes += skb->len;
+		u64_stats_inc(&s->cnt.inpkts);
+		u64_stats_add(&s->cnt.inbytes, skb->len);
 		u64_stats_update_end(&s->syncp);
 
 		local_bh_enable();
@@ -168,21 +168,21 @@ ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
 
 		s = this_cpu_ptr(dest->stats.cpustats);
 		u64_stats_update_begin(&s->syncp);
-		s->cnt.outpkts++;
-		s->cnt.outbytes += skb->len;
+		u64_stats_inc(&s->cnt.outpkts);
+		u64_stats_add(&s->cnt.outbytes, skb->len);
 		u64_stats_update_end(&s->syncp);
 
 		svc = rcu_dereference(dest->svc);
 		s = this_cpu_ptr(svc->stats.cpustats);
 		u64_stats_update_begin(&s->syncp);
-		s->cnt.outpkts++;
-		s->cnt.outbytes += skb->len;
+		u64_stats_inc(&s->cnt.outpkts);
+		u64_stats_add(&s->cnt.outbytes, skb->len);
 		u64_stats_update_end(&s->syncp);
 
 		s = this_cpu_ptr(ipvs->tot_stats->s.cpustats);
 		u64_stats_update_begin(&s->syncp);
-		s->cnt.outpkts++;
-		s->cnt.outbytes += skb->len;
+		u64_stats_inc(&s->cnt.outpkts);
+		u64_stats_add(&s->cnt.outbytes, skb->len);
 		u64_stats_update_end(&s->syncp);
 
 		local_bh_enable();
@@ -200,17 +200,17 @@ ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
 
 	s = this_cpu_ptr(cp->dest->stats.cpustats);
 	u64_stats_update_begin(&s->syncp);
-	s->cnt.conns++;
+	u64_stats_inc(&s->cnt.conns);
 	u64_stats_update_end(&s->syncp);
 
 	s = this_cpu_ptr(svc->stats.cpustats);
 	u64_stats_update_begin(&s->syncp);
-	s->cnt.conns++;
+	u64_stats_inc(&s->cnt.conns);
 	u64_stats_update_end(&s->syncp);
 
 	s = this_cpu_ptr(ipvs->tot_stats->s.cpustats);
 	u64_stats_update_begin(&s->syncp);
-	s->cnt.conns++;
+	u64_stats_inc(&s->cnt.conns);
 	u64_stats_update_end(&s->syncp);
 
 	local_bh_enable();
net/netfilter/ipvs/ip_vs_ctl.c +5 -5
index ec6db864ac36..5f9cc2e7ba71 100644
@@ -2335,11 +2335,11 @@ static int ip_vs_stats_percpu_show(struct seq_file *seq, void *v)
 
 		do {
 			start = u64_stats_fetch_begin(&u->syncp);
-			conns = u->cnt.conns;
-			inpkts = u->cnt.inpkts;
-			outpkts = u->cnt.outpkts;
-			inbytes = u->cnt.inbytes;
-			outbytes = u->cnt.outbytes;
+			conns = u64_stats_read(&u->cnt.conns);
+			inpkts = u64_stats_read(&u->cnt.inpkts);
+			outpkts = u64_stats_read(&u->cnt.outpkts);
+			inbytes = u64_stats_read(&u->cnt.inbytes);
+			outbytes = u64_stats_read(&u->cnt.outbytes);
 		} while (u64_stats_fetch_retry(&u->syncp, start));
 
 		seq_printf(seq, "%3X %8LX %8LX %8LX %16LX %16LX\n",
net/netfilter/ipvs/ip_vs_est.c +10 -10
index 9a1a7af6a186..f53150d82a92 100644
@@ -67,11 +67,11 @@ static void ip_vs_read_cpu_stats(struct ip_vs_kstats *sum,
 		if (add) {
 			do {
 				start = u64_stats_fetch_begin(&s->syncp);
-				conns = s->cnt.conns;
-				inpkts = s->cnt.inpkts;
-				outpkts = s->cnt.outpkts;
-				inbytes = s->cnt.inbytes;
-				outbytes = s->cnt.outbytes;
+				conns = u64_stats_read(&s->cnt.conns);
+				inpkts = u64_stats_read(&s->cnt.inpkts);
+				outpkts = u64_stats_read(&s->cnt.outpkts);
+				inbytes = u64_stats_read(&s->cnt.inbytes);
+				outbytes = u64_stats_read(&s->cnt.outbytes);
 			} while (u64_stats_fetch_retry(&s->syncp, start));
 			sum->conns += conns;
 			sum->inpkts += inpkts;
@@ -82,11 +82,11 @@ static void ip_vs_read_cpu_stats(struct ip_vs_kstats *sum,
 			add = true;
 			do {
 				start = u64_stats_fetch_begin(&s->syncp);
-				sum->conns = s->cnt.conns;
-				sum->inpkts = s->cnt.inpkts;
-				sum->outpkts = s->cnt.outpkts;
-				sum->inbytes = s->cnt.inbytes;
-				sum->outbytes = s->cnt.outbytes;
+				sum->conns = u64_stats_read(&s->cnt.conns);
+				sum->inpkts = u64_stats_read(&s->cnt.inpkts);
+				sum->outpkts = u64_stats_read(&s->cnt.outpkts);
+				sum->inbytes = u64_stats_read(&s->cnt.inbytes);
+				sum->outbytes = u64_stats_read(&s->cnt.outbytes);
 			} while (u64_stats_fetch_retry(&s->syncp, start));
 		}
 	}
Signature · 1 line
2.38.1
#6 lore raw

Re: [RFC PATCHv6 3/7] ipvs: use u64_stats_t for the per-cpu counters

Quoted 23 lines · Julian Anastasov

Use the provided u64_stats_t type to avoid
load/store tearing.

Fixes 316580b69d0a ("u64_stats: provide u64_stats_t type")
---
 include/net/ip_vs.h             | 10 +++++-----
 net/netfilter/ipvs/ip_vs_core.c | 30 +++++++++++++++---------------
 net/netfilter/ipvs/ip_vs_ctl.c  | 10 +++++-----
 net/netfilter/ipvs/ip_vs_est.c  | 20 ++++++++++----------
 4 files changed, 35 insertions(+), 35 deletions(-)
include/net/ip_vs.h +0 -3
index e5582c01a4a3..a4d44138c2a8 100644
@@ -351,11 +351,11 @@ struct ip_vs_seq {
 
 /* counters per cpu */
 struct ip_vs_counters {
-	__u64		conns;		/* connections scheduled */
-	__u64		inpkts;		/* incoming packets */
-	__u64		outpkts;	/* outgoing packets */

include/net/ip_vs.h struct ip_vs_seq { · line 361

Quoted 10 lines
include/net/ip_vs.h +5 -2
-	__u64		inbytes;	/* incoming bytes */
-	__u64		outbytes;	/* outgoing bytes */
+	u64_stats_t	conns;		/* connections scheduled */
+	u64_stats_t	inpkts;		/* incoming packets */
+	u64_stats_t	outpkts;	/* outgoing packets */
+	u64_stats_t	inbytes;	/* incoming bytes */
+	u64_stats_t	outbytes;	/* outgoing bytes */
 };
 /* Stats per cpu */
 struct ip_vs_cpu_stats {

Converting the per cpu stat to u64_stats_t means that the compiler cannot optimize the memory access and addition on x86_64. Previously, the summation of per cpu counters in ip_vs_chain_estimation() looked like:

15b65:  add    (%rsi),%r14
15b68:  add    0x8(%rsi),%r15
15b6c:  add    0x10(%rsi),%r13
15b70:  add    0x18(%rsi),%r12
15b74:  add    0x20(%rsi),%rbp
The u64_stats_read() calls in ip_vs_chain_estimation() turned it into:
159d5:  mov    (%rcx),%r11
159d8:  mov    0x8(%rcx),%r10
159dc:  mov    0x10(%rcx),%r9
159e0:  mov    0x18(%rcx),%rdi
159e4:  mov    0x20(%rcx),%rdx
159e8:  add    %r11,%r14
159eb:  add    %r10,%r13
159ee:  add    %r9,%r12
159f1:  add    %rdi,%rbp
159f4:  add    %rdx,%rbx

I guess that is not a big deal because the mov should be the instruction taking the most time on account of accessing per cpu regions of other CPUs. The add will be fast.

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

Re: [RFC PATCHv6 3/7] ipvs: use u64_stats_t for the per-cpu counters

Quoted 3 lines · Jiri Wiesner
Quoted 2 lines · Julian Anastasov

Use the provided u64_stats_t type to avoid
load/store tearing.

I think only 32-bit machines have a problem storing a 64-bit counter atomically. u64_stats_fetch_begin() and u64_stats_fetch_retry() should take care of that.

Quoted 49 lines
Quoted 21 lines
Fixes 316580b69d0a ("u64_stats: provide u64_stats_t type")
---
 include/net/ip_vs.h             | 10 +++++-----
 net/netfilter/ipvs/ip_vs_core.c | 30 +++++++++++++++---------------
 net/netfilter/ipvs/ip_vs_ctl.c  | 10 +++++-----
 net/netfilter/ipvs/ip_vs_est.c  | 20 ++++++++++----------
 4 files changed, 35 insertions(+), 35 deletions(-)
include/net/ip_vs.h +0 -3
index e5582c01a4a3..a4d44138c2a8 100644
@@ -351,11 +351,11 @@ struct ip_vs_seq {
 
 /* counters per cpu */
 struct ip_vs_counters {
-	__u64		conns;		/* connections scheduled */
-	__u64		inpkts;		/* incoming packets */
-	__u64		outpkts;	/* outgoing packets */

include/net/ip_vs.h struct ip_vs_seq { · line 361

Quoted 10 lines
include/net/ip_vs.h +5 -2
-	__u64		inbytes;	/* incoming bytes */
-	__u64		outbytes;	/* outgoing bytes */
+	u64_stats_t	conns;		/* connections scheduled */
+	u64_stats_t	inpkts;		/* incoming packets */
+	u64_stats_t	outpkts;	/* outgoing packets */
+	u64_stats_t	inbytes;	/* incoming bytes */
+	u64_stats_t	outbytes;	/* outgoing bytes */
 };
 /* Stats per cpu */
 struct ip_vs_cpu_stats {

Converting the per cpu stat to u64_stats_t means that the compiler cannot optimize the memory access and addition on x86_64. Previously, the summation of per cpu counters in ip_vs_chain_estimation() looked like:

15b65:  add    (%rsi),%r14
15b68:  add    0x8(%rsi),%r15
15b6c:  add    0x10(%rsi),%r13
15b70:  add    0x18(%rsi),%r12
15b74:  add    0x20(%rsi),%rbp
The u64_stats_read() calls in ip_vs_chain_estimation() turned it into:
159d5:  mov    (%rcx),%r11
159d8:  mov    0x8(%rcx),%r10
159dc:  mov    0x10(%rcx),%r9
159e0:  mov    0x18(%rcx),%rdi
159e4:  mov    0x20(%rcx),%rdx
159e8:  add    %r11,%r14
159eb:  add    %r10,%r13
159ee:  add    %r9,%r12
159f1:  add    %rdi,%rbp
159f4:  add    %rdx,%rbx
Quoted 1 line

I guess that is not a big deal because the mov should be the instruction taking the most time on account of accessing per cpu regions of other CPUs. The add will be fast.

Another concern is the number of registers needed for the summation. Previously, 5 registers were needed. Now, 10 registers are needed. This would matter mostly if the size of the stack frame of ip_vs_chain_estimation() and the number of callee-saved registers stored to the stack changed, but introducing u64_stats_read() in ip_vs_chain_estimation() did not change that.

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

Re: [RFC PATCHv6 3/7] ipvs: use u64_stats_t for the per-cpu counters

Hello,

Quoted 6 lines · Jiri Wiesner
Quoted 3 lines · Jiri Wiesner
Quoted 2 lines · Julian Anastasov

Use the provided u64_stats_t type to avoid
load/store tearing.

I think only 32-bit machines have a problem storing a 64-bit counter atomically. u64_stats_fetch_begin() and u64_stats_fetch_retry() should take care of that.

Some compilers for 64-bit can use two 32-bit
loads (load tearing) while we require atomic 64-bit read in case reader is interrupted by updater. That is why READ_ONCE is used now. I don't know which compilers can do this. That is why I switched to u64_stats_t to correctly use the u64_stats interface. And our data is 8-byte aligned. 32-bit are using seqcount_t, so they are protected for this problem.

Quoted 19 lines
Quoted 17 lines

Converting the per cpu stat to u64_stats_t means that the compiler cannot optimize the memory access and addition on x86_64. Previously, the summation of per cpu counters in ip_vs_chain_estimation() looked like:

15b65:  add    (%rsi),%r14
15b68:  add    0x8(%rsi),%r15
15b6c:  add    0x10(%rsi),%r13
15b70:  add    0x18(%rsi),%r12
15b74:  add    0x20(%rsi),%rbp
The u64_stats_read() calls in ip_vs_chain_estimation() turned it into:
159d5:  mov    (%rcx),%r11
159d8:  mov    0x8(%rcx),%r10
159dc:  mov    0x10(%rcx),%r9
159e0:  mov    0x18(%rcx),%rdi
159e4:  mov    0x20(%rcx),%rdx
159e8:  add    %r11,%r14
159eb:  add    %r10,%r13
159ee:  add    %r9,%r12
159f1:  add    %rdi,%rbp
159f4:  add    %rdx,%rbx
Quoted 1 line

I guess that is not a big deal because the mov should be the instruction taking the most time on account of accessing per cpu regions of other CPUs. The add will be fast.

Quoted 1 line

Another concern is the number of registers needed for the summation. Previously, 5 registers were needed. Now, 10 registers are needed. This would matter mostly if the size of the stack frame of ip_vs_chain_estimation() and the number of callee-saved registers stored to the stack changed, but introducing u64_stats_read() in ip_vs_chain_estimation() did not change that.

It happens in this way probably because compiler should not
reorder two or more successive instances of READ_ONCE (rwonce.h), something that hurts u64_stats_read(). As result, it multiplies up to 5 times the needed temp storage (registers or stack).

Another option would be to use something like
this below, try on top of v6. It is rude to add ifdefs here but we should avoid unneeded code for 64-bit. On 32-bit, code is more but operations are same. On 64-bit it should order read+add one by one which can run in parallel. In my compile tests it uses 3 times movq(READ_ONCE)+addq(to sum) with same temp register which defeats fully parallel operations and for the last 2 counters uses movq,movq,addq,addq with different registers which can run in parallel. It seems there are no free registers to make it for all 5 counters.

With this patch the benefit is that compiler should
not hold all 5 values before adding them but on x86 this patch shows some problem: it does not allocate 5 new temp registers to read and add the values in parallel. Without this patch, as you show it, it somehow allocates 5+5 registers which allows parallel operations.

If you think we are better with such change, we
can include it in patch 4. It will be better in case one day we add more counters and there are more available registers. You can notice the difference probably by comparing the chain_max value in performance mode.

net/netfilter/ipvs/ip_vs_est.c +26 -13
index 4489a3dbad1e..52d77715f7ea 100644
@@ -83,7 +83,9 @@ static void ip_vs_chain_estimation(struct hlist_head *chain)
 	u64 rate;
 
 	hlist_for_each_entry_rcu(e, chain, list) {
+#if BITS_PER_LONG == 32
 		u64 conns, inpkts, outpkts, inbytes, outbytes;
+#endif
 		u64 kconns = 0, kinpkts = 0, koutpkts = 0;
 		u64 kinbytes = 0, koutbytes = 0;
 		unsigned int start;
@@ -95,19 +97,30 @@ static void ip_vs_chain_estimation(struct hlist_head *chain)
 		s = container_of(e, struct ip_vs_stats, est);
 		for_each_possible_cpu(i) {
 			c = per_cpu_ptr(s->cpustats, i);
-			do {
-				start = u64_stats_fetch_begin(&c->syncp);
-				conns = u64_stats_read(&c->cnt.conns);
-				inpkts = u64_stats_read(&c->cnt.inpkts);
-				outpkts = u64_stats_read(&c->cnt.outpkts);
-				inbytes = u64_stats_read(&c->cnt.inbytes);
-				outbytes = u64_stats_read(&c->cnt.outbytes);
-			} while (u64_stats_fetch_retry(&c->syncp, start));
-			kconns += conns;
-			kinpkts += inpkts;
-			koutpkts += outpkts;
-			kinbytes += inbytes;
-			koutbytes += outbytes;
+#if BITS_PER_LONG == 32
+			conns = kconns;
+			inpkts = kinpkts;
+			outpkts = koutpkts;
+			inbytes = kinbytes;
+			outbytes = koutbytes;
+#endif
+			for (;;) {
+				start += u64_stats_fetch_begin(&c->syncp);
+				kconns += u64_stats_read(&c->cnt.conns);
+				kinpkts += u64_stats_read(&c->cnt.inpkts);
+				koutpkts += u64_stats_read(&c->cnt.outpkts);
+				kinbytes += u64_stats_read(&c->cnt.inbytes);
+				koutbytes += u64_stats_read(&c->cnt.outbytes);
+				if (!u64_stats_fetch_retry(&c->syncp, start))
+					break;
+#if BITS_PER_LONG == 32
+				kconns = conns;
+				kinpkts = inpkts;
+				koutpkts = outpkts;
+				kinbytes = inbytes;
+				koutbytes = outbytes;
+#endif
+			}
 		}
 
 		spin_lock(&s->lock);

Regards

--
Julian Anastasov <[email protected]>

author maintainer
#9 lore raw

Re: [RFC PATCHv6 3/7] ipvs: use u64_stats_t for the per-cpu counters

Hello,

net/netfilter/ipvs/ip_vs_est.c static void ip_vs_chain_estimation(struct hlist_head *chain) · line 108

Quoted 2 lines · Julian Anastasov
net/netfilter/ipvs/ip_vs_est.c +2 -0
+			for (;;) {
+				start += u64_stats_fetch_begin(&c->syncp);

Hm, this should be assignment and not addition...
Not a problem for 64-bit tests.

Quoted 4 lines
net/netfilter/ipvs/ip_vs_est.c +4 -0
+				kconns += u64_stats_read(&c->cnt.conns);
+				kinpkts += u64_stats_read(&c->cnt.inpkts);
+				koutpkts += u64_stats_read(&c->cnt.outpkts);
+				kinbytes += u64_stats_read(&c->cnt.inbytes);

net/netfilter/ipvs/ip_vs_est.c static void ip_vs_chain_estimation(struct hlist_head *chain) · line 122

Quoted 10 lines
net/netfilter/ipvs/ip_vs_est.c +10 -0
+				koutbytes += u64_stats_read(&c->cnt.outbytes);
+				if (!u64_stats_fetch_retry(&c->syncp, start))
+					break;
+#if BITS_PER_LONG == 32
+				kconns = conns;
+				kinpkts = inpkts;
+				koutpkts = outpkts;
+				kinbytes = inbytes;
+				koutbytes = outbytes;
+#endif

Regards

--
Julian Anastasov <[email protected]>

#10 lore raw

Re: [RFC PATCHv6 3/7] ipvs: use u64_stats_t for the per-cpu counters

In reply to Julian Anastasov
Quoted 12 lines · Julian Anastasov

Hello,

Quoted 6 lines · Jiri Wiesner
Quoted 3 lines · Jiri Wiesner
Quoted 2 lines · Julian Anastasov

Use the provided u64_stats_t type to avoid
load/store tearing.

I think only 32-bit machines have a problem storing a 64-bit counter atomically. u64_stats_fetch_begin() and u64_stats_fetch_retry() should take care of that.

Quoted 7 lines

Some compilers for 64-bit can use two 32-bit
loads (load tearing) while we require atomic 64-bit read in case reader is interrupted by updater. That is why READ_ONCE is used now. I don't know which compilers can do this. That is why I switched to u64_stats_t to correctly use the u64_stats interface. And our data is 8-byte aligned. 32-bit are using seqcount_t, so they are protected for this problem.

All right. It is counter-intuitive but I guess those compiles have their reasons. I think it would not hurt to expand the description of the patch with the explanation above.

Quoted 44 lines
Quoted 19 lines
Quoted 17 lines

Converting the per cpu stat to u64_stats_t means that the compiler cannot optimize the memory access and addition on x86_64. Previously, the summation of per cpu counters in ip_vs_chain_estimation() looked like:

15b65:  add    (%rsi),%r14
15b68:  add    0x8(%rsi),%r15
15b6c:  add    0x10(%rsi),%r13
15b70:  add    0x18(%rsi),%r12
15b74:  add    0x20(%rsi),%rbp
The u64_stats_read() calls in ip_vs_chain_estimation() turned it into:
159d5:  mov    (%rcx),%r11
159d8:  mov    0x8(%rcx),%r10
159dc:  mov    0x10(%rcx),%r9
159e0:  mov    0x18(%rcx),%rdi
159e4:  mov    0x20(%rcx),%rdx
159e8:  add    %r11,%r14
159eb:  add    %r10,%r13
159ee:  add    %r9,%r12
159f1:  add    %rdi,%rbp
159f4:  add    %rdx,%rbx
Quoted 1 line

I guess that is not a big deal because the mov should be the instruction taking the most time on account of accessing per cpu regions of other CPUs. The add will be fast.

Quoted 1 line

Another concern is the number of registers needed for the summation. Previously, 5 registers were needed. Now, 10 registers are needed. This would matter mostly if the size of the stack frame of ip_vs_chain_estimation() and the number of callee-saved registers stored to the stack changed, but introducing u64_stats_read() in ip_vs_chain_estimation() did not change that.

It happens in this way probably because compiler should not
reorder two or more successive instances of READ_ONCE (rwonce.h), something that hurts u64_stats_read(). As result, it multiplies up to 5 times the needed temp storage (registers or stack).

Another option would be to use something like
this below, try on top of v6. It is rude to add ifdefs here but we should avoid unneeded code for 64-bit. On 32-bit, code is more but operations are same. On 64-bit it should order read+add one by one which can run in parallel. In my compile tests it uses 3 times movq(READ_ONCE)+addq(to sum) with same temp register which defeats fully parallel operations and for the last 2 counters uses movq,movq,addq,addq with different registers which can run in parallel. It seems there are no free registers to make it for all 5 counters.

With this patch the benefit is that compiler should
not hold all 5 values before adding them but on x86 this patch shows some problem: it does not allocate 5 new temp registers to read and add the values in parallel. Without this patch, as you show it, it somehow allocates 5+5 registers which allows parallel operations.

Quoted 5 lines

If you think we are better with such change, we
can include it in patch 4. It will be better in case one day we add more counters and there are more available registers. You can notice the difference probably by comparing the chain_max value in performance mode.

I have tested the change to ip_vs_chain_estimation() and ran profiling with bus-cycles, which are proportional to time spent executing code. The number of estimator per kthread was the same in both tests - 88800. It turns out the change made the code slower:

Quoted 6 lines
#       Util1          Util2                    Diff  Command          Shared Object        Symbol   CPU
1,124,932,796  1,143,867,951     18,935,155 (  1.7%)  ipvs-e:0:0       [ip_vs]              all      all
   16,480,274     16,853,752        373,478 (  2.3%)  ipvs-e:0:1       [ip_vs]              all      all
            0         21,987         21,987 (100.0%)  ipvs-e:0:0       [kvm]                all      all
    4,622,992      4,366,150       -256,842 (  5.6%)  ipvs-e:0:1       [kernel.kallsyms]    all      all
  180,226,857    164,665,271    -15,561,586 (  8.6%)  ipvs-e:0:0       [kernel.kallsyms]    all      all

The most significant component was the time spent in ip_vs_chain_estimation():

Quoted 9 lines
#       Util1          Util2                    Diff  Command          Shared Object        Symbol                     CPU
1,124,414,110  1,143,352,233     18,938,123 (  1.7%)  ipvs-e:0:0       [ip_vs]              ip_vs_chain_estimation     all
   16,291,044     16,574,498        283,454 (  1.7%)  ipvs-e:0:1       [ip_vs]              ip_vs_chain_estimation     all
      189,230        279,254         90,024 ( 47.6%)  ipvs-e:0:1       [ip_vs]              ip_vs_estimation_kthread   all
      518,686        515,718         -2,968 (  0.6%)  ipvs-e:0:0       [ip_vs]              ip_vs_estimation_kthread   all
    1,562,512      1,377,609       -184,903 ( 11.8%)  ipvs-e:0:0       [kernel.kallsyms]    kthread_should_stop        all
    2,435,803      2,138,404       -297,399 ( 12.2%)  ipvs-e:0:1       [kernel.kallsyms]    _find_next_bit             all
   16,304,577     15,786,553       -518,024 (  3.2%)  ipvs-e:0:0       [kernel.kallsyms]    _raw_spin_lock             all
  151,110,969    137,172,160    -13,938,809 (  9.2%)  ipvs-e:0:0       [kernel.kallsyms]    _find_next_bit             all

The disassembly shows it is the mov instructions (there is a skid of 1 instruction) what takes the most time:

Quoted 46 lines

Percent | Source code & Disassembly of kcore for bus-cycles (55354 samples, percent: local period)
: ffffffffc0bad980 <ip_vs_chain_estimation>:
v6 patchset v6 patchset + ip_vs_chain_estimation() change
-------------------------------------------------------------------------------------------------------

  0.00 :       nopl   0x0(%rax,%rax,1)             0.00 :       nopl   0x0(%rax,%rax,1)
  0.00 :       push   %r15                         0.00 :       push   %r15
  0.00 :       push   %r14                         0.00 :       push   %r14
  0.00 :       push   %r13                         0.00 :       push   %r13
  0.00 :       push   %r12                         0.00 :       push   %r12
  0.00 :       push   %rbp                         0.00 :       push   %rbp
  0.00 :       push   %rbx                         0.00 :       push   %rbx
  0.00 :       sub    $0x8,%rsp                    0.00 :       sub    $0x8,%rsp
  0.00 :       mov    (%rdi),%r15                  0.00 :       mov    (%rdi),%r15
  0.00 :       test   %r15,%r15                    0.00 :       test   %r15,%r15
  0.00 :       je     0xffffffffc0badaf1           0.00 :       je     0xffffffffc0b7faf1
  0.00 :       call   0xffffffff8bcd33a0           0.00 :       call   0xffffffffa1ed33a0
  0.00 :       test   %al,%al                      0.00 :       test   %al,%al
  0.00 :       jne    0xffffffffc0badaf1           0.00 :       jne    0xffffffffc0b7faf1
  0.00 :       mov    -0x334ccb22(%rip),%esi       0.00 :       mov    -0x1d29eb22(%rip),%esi
  0.00 :       xor    %eax,%eax                    0.00 :       xor    %eax,%eax
  0.00 :       xor    %ebx,%ebx                    0.00 :       xor    %ebx,%ebx
  0.00 :       xor    %ebp,%ebp                    0.00 :       xor    %ebp,%ebp
  0.00 :       xor    %r12d,%r12d                  0.00 :       xor    %r12d,%r12d
  0.00 :       xor    %r13d,%r13d                  0.00 :       xor    %r13d,%r13d
  0.05 :       xor    %r14d,%r14d                  0.08 :       xor    %r14d,%r14d
  0.00 :       jmp    0xffffffffc0bad9f7           0.00 :       jmp    0xffffffffc0b7f9f7
  0.14 :       movslq %eax,%rdx                    0.08 :       movslq %eax,%rdx
  0.00 :       mov    0x68(%r15),%rcx              0.00 :       mov    0x68(%r15),%rcx
 11.92 :       add    $0x1,%eax                   12.25 :       add    $0x1,%eax
  0.00 :       add    -0x72ead560(,%rdx,8),%rcx    0.00 :       add    -0x5ccad560(,%rdx,8),%rcx
  4.07 :       mov    (%rcx),%r11                  3.84 :       mov    (%rcx),%rdx
 34.28 :       mov    0x8(%rcx),%r10              34.17 :       add    %rdx,%r14
 10.35 :       mov    0x10(%rcx),%r9               2.62 :       mov    0x8(%rcx),%rdx
  7.28 :       mov    0x18(%rcx),%rdi              9.33 :       add    %rdx,%r13
  7.70 :       mov    0x20(%rcx),%rdx              1.82 :       mov    0x10(%rcx),%rdx
  6.80 :       add    %r11,%r14                    6.11 :       add    %rdx,%r12
  0.27 :       add    %r10,%r13                    1.26 :       mov    0x18(%rcx),%rdx
  0.36 :       add    %r9,%r12                     6.00 :       add    %rdx,%rbp
  2.24 :       add    %rdi,%rbp                    2.97 :       mov    0x20(%rcx),%rdx
  1.21 :       add    %rdx,%rbx                    5.78 :       add    %rdx,%rbx
  0.07 :       movslq %eax,%rdx                    1.08 :       movslq %eax,%rdx
  0.35 :       mov    $0xffffffff8d6e0860,%rdi     0.00 :       mov    $0xffffffffa38e0860,%rdi
  2.26 :       call   0xffffffff8c16e270           2.62 :       call   0xffffffffa236e270
  0.17 :       mov    -0x334ccb7c(%rip),%esi       0.11 :       mov    -0x1d29eb7c(%rip),%esi
  0.00 :       cmp    %eax,%esi                    0.00 :       cmp    %eax,%esi
  0.00 :       ja     0xffffffffc0bad9c3           0.00 :       ja     0xffffffffc0b7f9c3
Quoted 8 lines
  0.00 :       lea    0x70(%r15),%rdx              0.00 :       lea    0x70(%r15),%rdx
  0.00 :       mov    %rdx,%rdi                    0.00 :       mov    %rdx,%rdi
  0.04 :       mov    %rdx,(%rsp)                  0.05 :       mov    %rdx,(%rsp)
  0.00 :       call   0xffffffff8c74d800           0.00 :       call   0xffffffffa294d800
  0.06 :       mov    %r14,%rax                    0.07 :       mov    %r14,%rax
  0.00 :       sub    0x20(%r15),%rax              0.00 :       sub    0x20(%r15),%rax
  9.55 :       mov    0x38(%r15),%rcx              8.97 :       mov    0x38(%r15),%rcx
  0.01 :       mov    (%rsp),%rdx                  0.03 :       mov    (%rsp),%rdx

The results indicate that the extra add instructions should not matter - memory accesses (the mov instructions) are the bottleneck.

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

Re: [RFC PATCHv6 3/7] ipvs: use u64_stats_t for the per-cpu counters

Hello,

Quoted 11 lines · Jiri Wiesner
Quoted 8 lines · Julian Anastasov

Some compilers for 64-bit can use two 32-bit
loads (load tearing) while we require atomic 64-bit read in case reader is interrupted by updater. That is why READ_ONCE is used now. I don't know which compilers can do this. That is why I switched to u64_stats_t to correctly use the u64_stats interface. And our data is 8-byte aligned. 32-bit are using seqcount_t, so they are protected for this problem.

All right. It is counter-intuitive but I guess those compiles have their reasons. I think it would not hurt to expand the description of the patch with the explanation above.

OK. I relied on the mentioned commit to explain the problem
as other similar patches do.

Quoted 9 lines
Quoted 5 lines

If you think we are better with such change, we
can include it in patch 4. It will be better in case one day we add more counters and there are more available registers. You can notice the difference probably by comparing the chain_max value in performance mode.

I have tested the change to ip_vs_chain_estimation() and ran profiling with bus-cycles, which are proportional to time spent executing code. The number of estimator per kthread was the same in both tests - 88800. It turns out the change made the code slower:

Quoted 2 lines
#       Util1          Util2                    Diff  Command          Shared Object        Symbol   CPU
1,124,932,796  1,143,867,951     18,935,155 (  1.7%)  ipvs-e:0:0       [ip_vs]              all      all

chain_max is probably 37 here and 1.7% is low
enough (below 1/37) not to be accounted in chain_max.

Quoted 15 lines
Quoted 4 lines
   16,480,274     16,853,752        373,478 (  2.3%)  ipvs-e:0:1       [ip_vs]              all      all
            0         21,987         21,987 (100.0%)  ipvs-e:0:0       [kvm]                all      all
    4,622,992      4,366,150       -256,842 (  5.6%)  ipvs-e:0:1       [kernel.kallsyms]    all      all
  180,226,857    164,665,271    -15,561,586 (  8.6%)  ipvs-e:0:0       [kernel.kallsyms]    all      all

The most significant component was the time spent in ip_vs_chain_estimation():

Quoted 9 lines
#       Util1          Util2                    Diff  Command          Shared Object        Symbol                     CPU
1,124,414,110  1,143,352,233     18,938,123 (  1.7%)  ipvs-e:0:0       [ip_vs]              ip_vs_chain_estimation     all
   16,291,044     16,574,498        283,454 (  1.7%)  ipvs-e:0:1       [ip_vs]              ip_vs_chain_estimation     all
      189,230        279,254         90,024 ( 47.6%)  ipvs-e:0:1       [ip_vs]              ip_vs_estimation_kthread   all
      518,686        515,718         -2,968 (  0.6%)  ipvs-e:0:0       [ip_vs]              ip_vs_estimation_kthread   all
    1,562,512      1,377,609       -184,903 ( 11.8%)  ipvs-e:0:0       [kernel.kallsyms]    kthread_should_stop        all
    2,435,803      2,138,404       -297,399 ( 12.2%)  ipvs-e:0:1       [kernel.kallsyms]    _find_next_bit             all
   16,304,577     15,786,553       -518,024 (  3.2%)  ipvs-e:0:0       [kernel.kallsyms]    _raw_spin_lock             all
  151,110,969    137,172,160    -13,938,809 (  9.2%)  ipvs-e:0:0       [kernel.kallsyms]    _find_next_bit             all
Quoted 16 lines

The disassembly shows it is the mov instructions (there is a skid of 1 instruction) what takes the most time:

Quoted 15 lines

Percent | Source code & Disassembly of kcore for bus-cycles (55354 samples, percent: local period)
: ffffffffc0bad980 <ip_vs_chain_estimation>:
v6 patchset v6 patchset + ip_vs_chain_estimation() change
-------------------------------------------------------------------------------------------------------

  0.00 :       add    -0x72ead560(,%rdx,8),%rcx    0.00 :       add    -0x5ccad560(,%rdx,8),%rcx
  4.07 :       mov    (%rcx),%r11                  3.84 :       mov    (%rcx),%rdx
 34.28 :       mov    0x8(%rcx),%r10              34.17 :       add    %rdx,%r14
 10.35 :       mov    0x10(%rcx),%r9               2.62 :       mov    0x8(%rcx),%rdx
  7.28 :       mov    0x18(%rcx),%rdi              9.33 :       add    %rdx,%r13
  7.70 :       mov    0x20(%rcx),%rdx              1.82 :       mov    0x10(%rcx),%rdx
  6.80 :       add    %r11,%r14                    6.11 :       add    %rdx,%r12
  0.27 :       add    %r10,%r13                    1.26 :       mov    0x18(%rcx),%rdx
  0.36 :       add    %r9,%r12                     6.00 :       add    %rdx,%rbp
  2.24 :       add    %rdi,%rbp                    2.97 :       mov    0x20(%rcx),%rdx

Bad, same register RDX used. I guess, this is
CONFIG_GENERIC_CPU=y kernel which is suitable for different CPUs. My example was tested with CONFIG_MCORE2=y. But the optimizations are compiler's job, there can be a cost to free registers for our operations. Who knows, change can be faster on other platforms with less available registers to hold the 5 counters and their sum in registers.

Probably, we can force it to add mem8 into reg by using
something like this:

static inline void u64_stats_read_add(u64_stats_t *p, u64 *sum)

{
	local64_add_to(&p->v, (long *) sum);
}

static inline void local_add_to(local_t *l, long *sum)

{
	asm(_ASM_ADD "%1,%0"
	    : "+r" (*sum)
	    : "m" (l->a.counter));
}

But it deserves more testing.

Quoted 1 line

The results indicate that the extra add instructions should not matter - memory accesses (the mov instructions) are the bottleneck.

Yep, then lets work without this change for now,
no need to deviate much from the general u64_stats usage if difference is negligible.

Regards

--
Julian Anastasov <[email protected]>

#12 lore raw

Re: [RFC PATCHv6 3/7] ipvs: use u64_stats_t for the per-cpu counters

Quoted 3 lines · Julian Anastasov

Bad, same register RDX used. I guess, this is
CONFIG_GENERIC_CPU=y kernel which is suitable for different CPUs.

Yes, it is. I use SUSE's distro config.

Signature · 2 lines
Jiri Wiesner
SUSE Labs
Tested-by Reviewed-by on v6 3/7
#13 lore raw

Re: [RFC PATCHv6 3/7] ipvs: use u64_stats_t for the per-cpu counters

In reply to Julian Anastasov
Quoted 6 lines · Julian Anastasov

Use the provided u64_stats_t type to avoid
load/store tearing.

Fixes 316580b69d0a ("u64_stats: provide u64_stats_t type")

---

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

RFC PATCH v6 4/7 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. First kthread determines parameters to use, eg. maximum number of estimators to process per kthread based on chain's length, allowing sub-100us cond_resched rate and estimation taking 1/8 of the CPU.

First kthread also plays the role of distributor of added estimators to all kthreads.

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            |  74 ++-
 net/netfilter/ipvs/ip_vs_ctl.c | 126 ++++-
 net/netfilter/ipvs/ip_vs_est.c | 871 ++++++++++++++++++++++++++++++---
 3 files changed, 971 insertions(+), 100 deletions(-)
include/net/ip_vs.h +69 -5
index a4d44138c2a8..9c02840bc7c9 100644
@@ -42,6 +42,8 @@ static inline struct netns_ipvs *net_ipvs(struct net* net)
 /* Connections' size value needed by ip_vs_ctl.c */
 extern int ip_vs_conn_tab_size;
 
+extern struct mutex __ip_vs_mutex;
+
 struct ip_vs_iphdr {
 	int hdr_flags;	/* ipvs flags */
 	__u32 off;	/* Where IP or IPv4 header starts */
@@ -365,7 +367,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 +380,10 @@ struct ip_vs_estimator {
 	u64			outpps;
 	u64			inbps;
 	u64			outbps;
+
+	s32			ktid:16,	/* kthread ID, -1=temp list */
+				ktrow:8,	/* row ID for kthread */
+				ktcid:8;	/* chain ID for kthread */
 };
 
 /*
@@ -415,6 +421,52 @@ struct ip_vs_stats *ip_vs_stats_alloc(void);
 void ip_vs_stats_release(struct ip_vs_stats *stats);
 void ip_vs_stats_free(struct ip_vs_stats *stats);
 
+/* Process estimators in multiple timer ticks */
+#define IPVS_EST_NTICKS		50
+/* Estimation uses a 2-second period containing ticks (in jiffies) */
+#define IPVS_EST_TICK		((2 * HZ) / IPVS_EST_NTICKS)
+
+/* Desired number of chains per tick (chain load factor in 100us units),
+ * 48=4.8ms of 40ms tick (12% CPU usage):
+ * 2 sec * 1000 ms in sec * 10 (100us in ms) / 8 (12.5%) / IPVS_EST_NTICKS
+ */
+#define IPVS_EST_CHAIN_FACTOR	ALIGN_DOWN(2 * 1000 * 10 / 8 / IPVS_EST_NTICKS, 8)
+
+/* Compiled number of chains per tick
+ * The defines should match cond_resched_rcu
+ */
+#if defined(CONFIG_DEBUG_ATOMIC_SLEEP) || !defined(CONFIG_PREEMPT_RCU)
+#define IPVS_EST_TICK_CHAINS	IPVS_EST_CHAIN_FACTOR
+#else
+#define IPVS_EST_TICK_CHAINS	1
+#endif
+
+/* 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) */
+	struct ip_vs_stats	*calc_stats;	/* Used for calculation */
+	int			tick_len[IPVS_EST_NTICKS];	/* est count */
+	int			id;		/* ktid per netns */
+	int			chain_max;	/* max ests per tick chain */
+	int			tick_max;	/* max ests per tick */
+	int			est_count;	/* attached ests to kthread */
+	int			est_max_count;	/* max ests per kthread */
+	int			add_row;	/* row for new ests */
+	int			est_row;	/* estimated row */
+};
+
 struct dst_entry;
 struct iphdr;
 struct ip_vs_conn;
@@ -953,9 +1005,17 @@ 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 hlist_head	est_temp_list;	/* Ests during calc phase */
+	struct ip_vs_est_kt_data **est_kt_arr;	/* Array of kthread data ptrs */
+	unsigned long		est_max_threads;/* rlimit */
+	int			est_calc_phase;	/* Calculation phase */
+	int			est_chain_max;	/* Calculated chain_max */
+	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;
@@ -1486,10 +1546,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 +99 -27

269 lines not on this page · raw message

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

924 lines not on this page · raw message

Signature · 1 line
2.38.1
#15 lore raw

Re: [RFC PATCHv6 4/7] ipvs: use kthreads for stats estimation

Quoted 23 lines · Julian Anastasov
net/netfilter/ipvs/ip_vs_est.c +23 -0
+/* Calculate limits for all kthreads */
+static int ip_vs_est_calc_limits(struct netns_ipvs *ipvs, int *chain_max)
+{
+	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
+	struct ip_vs_est_kt_data *kd;
+	struct hlist_head chain;
+	struct ip_vs_stats *s;
+	int cache_factor = 4;
+	int i, loops, ntest;
+	s32 min_est = 0;
+	ktime_t t1, t2;
+	s64 diff, val;
+	int max = 8;
+	int ret = 1;
+
+	INIT_HLIST_HEAD(&chain);
+	mutex_lock(&__ip_vs_mutex);
+	kd = ipvs->est_kt_arr[0];
+	mutex_unlock(&__ip_vs_mutex);
+	s = kd ? kd->calc_stats : NULL;
+	if (!s)
+		goto out;
+	hlist_add_head(&s->est.list, &chain);

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

Quoted 10 lines
net/netfilter/ipvs/ip_vs_est.c +10 -0
+
+	loops = 1;
+	/* Get best result from many tests */
+	for (ntest = 0; ntest < 12; ntest++) {
+		if (!(ntest & 3)) {
+			wait_event_idle_timeout(wq, kthread_should_stop(),
+						HZ / 50);
+			if (!ipvs->enable || kthread_should_stop())
+				goto stop;
+		}

I was testing the stability of chain_max:
Intel(R) Xeon(R) Gold 6326 CPU @ 2.90GHz
64 CPUs, 2 NUMA nodes

Quoted 4 lines

while :; do ipvsadm -A -t 10.10.10.1:2000; sleep 3; ipvsadm -D -t 10.10.10.1:2000; modprobe -r ip_vs_wlc ip_vs; sleep 10m; done

# dmesg | awk '/calc: chain_max/{m = $(NF - 5); sub("chain_max=", "", m); sub(",", "", m); m = int(m / 10) * 10; hist[m]++} END {PROCINFO["sorted_in"] = "@ind_num_asc"; for (m in hist) printf "%5d %5d\n", m, hist[m]}'
   30    90
   40     2

Chain_max was often 30-something. Chain_max was never below 30, which was observed earlier when only 3 tests were carried out.

AMD EPYC 7601 32-Core Processor
128 CPUs, 8 NUMA nodes
Zen 1 machines such as this one have a large number of NUMA nodes due to restrictions in the CPU architecture. First, tests with different governors:

Quoted 21 lines

cpupower frequency-set -g ondemand

[  653.441325] IPVS: starting estimator thread 0...
[  653.514918] IPVS: calc: chain_max=8, single est=11171ns, diff=11301, loops=1, ntest=12
[  653.523580] IPVS: dequeue: 892ns
[  653.527528] IPVS: using max 384 ests per chain, 19200 per kthread
[  655.349916] IPVS: tick time: 3059313ns for 128 CPUs, 384 ests, 1 chains, chain_max=384
[  685.230016] IPVS: starting estimator thread 1...
[  717.110852] IPVS: starting estimator thread 2...
[  719.349755] IPVS: tick time: 2896668ns for 128 CPUs, 384 ests, 1 chains, chain_max=384
[  750.349974] IPVS: starting estimator thread 3...
[  783.349841] IPVS: tick time: 2942604ns for 128 CPUs, 384 ests, 1 chains, chain_max=384
[  847.349811] IPVS: tick time: 2930872ns for 128 CPUs, 384 ests, 1 chains, chain_max=384
   PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
 74902 root      20   0       0      0      0 I 7.591 0.000   0:13.95 ipvs-e:0:1
 94104 root      20   0       0      0      0 I 7.591 0.000   0:11.59 ipvs-e:0:2
 55669 root      20   0       0      0      0 I 6.931 0.000   0:15.75 ipvs-e:0:0
113311 root      20   0       0      0      0 I 0.990 0.000   0:01.31 ipvs-e:0:3
cpupower frequency-set -g performance
[ 1448.118857] IPVS: starting estimator thread 0...
[ 1448.194882] IPVS: calc: chain_max=22, single est=4138ns, diff=4298, loops=1, ntest=12
[ 1448.203435] IPVS: dequeue: 340ns
Quoted 8 lines
[ 1448.207373] IPVS: using max 1056 ests per chain, 52800 per kthread
[ 1450.029581] IPVS: tick time: 2727370ns for 128 CPUs, 518 ests, 1 chains, chain_max=1056
[ 1510.792734] IPVS: starting estimator thread 1...
[ 1514.032300] IPVS: tick time: 5436826ns for 128 CPUs, 1056 ests, 1 chains, chain_max=1056
[ 1578.032593] IPVS: tick time: 5691875ns for 128 CPUs, 1056 ests, 1 chains, chain_max=1056
   PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
 42514 root      20   0       0      0      0 I 14.24 0.000   0:14.96 ipvs-e:0:0
 95356 root      20   0       0      0      0 I 1.987 0.000   0:01.34 ipvs-e:0:1

While having the services loaded, I switched to the ondemand governor:

Quoted 5 lines
[ 1706.032577] IPVS: tick time: 5666868ns for 128 CPUs, 1056 ests, 1 chains, chain_max=1056
[ 1770.032534] IPVS: tick time: 5638505ns for 128 CPUs, 1056 ests, 1 chains, chain_max=1056
   PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
 42514 root      20   0       0      0      0 I 18.15 0.000   0:35.75 ipvs-e:0:0
 95356 root      20   0       0      0      0 I 2.310 0.000   0:04.05 ipvs-e:0:1

While having the services loaded, I kept the ondemand governor and saturated all logical CPUs on the machine:

Quoted 6 lines
[ 1834.033988] IPVS: tick time: 7129383ns for 128 CPUs, 1056 ests, 1 chains, chain_max=1056
[ 1898.038151] IPVS: tick time: 7281418ns for 128 CPUs, 1056 ests, 1 chains, chain_max=1056
   PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
102581 root      20   0 1047636 262304   1580 R 12791 0.100 161:03.94 a.out
 42514 root      20   0       0      0      0 I 17.76 0.000   1:06.88 ipvs-e:0:0
 95356 root      20   0       0      0      0 I 2.303 0.000   0:08.33 ipvs-e:0:1

As for the stability of chain_max:

Quoted 4 lines

while :; do ipvsadm -A -t 10.10.10.1:2000; sleep 3; ipvsadm -D -t 10.10.10.1:2000; modprobe -r ip_vs_wlc ip_vs; sleep 2m; done
dmesg | awk '/calc: chain_max/{m = $(NF - 5); sub("chain_max=", "", m); sub(",", "", m); m = int(m / 2) * 2; hist[m]++} END {PROCINFO["sorted_in"] = "@ind_num_asc"; for (m in hist) printf "%5d %5d\n", m, hist[m]}'

    8    22

24 1

Basically, chain_max calculation under gonernors than ramp up CPU frequency more slowly (ondemand on AMD or powersave for intel_pstate) is stabler than before on both AMD and Intel. We know from previous results that even ARM with multiple NUMA nodes is not a complete disaster. Switching CPU frequency gonernors, including the unfavourable switches from performance to ondemand, does not saturate CPUs. When it comes to CPU frequency gonernors, people tend to use either ondemand (or powersave for intel_pstate) or performance consistently - switches between gonernors can be expected to be rare in production. I will need to find out to read through the latest version of the patch set.

Quoted 52 lines
net/netfilter/ipvs/ip_vs_est.c +52 -0
+
+		local_bh_disable();
+		rcu_read_lock();
+
+		/* Put stats in cache */
+		ip_vs_chain_estimation(&chain);
+
+		t1 = ktime_get();
+		for (i = loops * cache_factor; i > 0; i--)
+			ip_vs_chain_estimation(&chain);
+		t2 = ktime_get();
+
+		rcu_read_unlock();
+		local_bh_enable();
+
+		if (!ipvs->enable || kthread_should_stop())
+			goto stop;
+		cond_resched();
+
+		diff = ktime_to_ns(ktime_sub(t2, t1));
+		if (diff <= 1 * NSEC_PER_USEC) {
+			/* Do more loops on low resolution */
+			loops *= 2;
+			continue;
+		}
+		if (diff >= NSEC_PER_SEC)
+			continue;
+		val = diff;
+		do_div(val, loops);
+		if (!min_est || val < min_est) {
+			min_est = val;
+			/* goal: 95usec per chain */
+			val = 95 * NSEC_PER_USEC;
+			if (val >= min_est) {
+				do_div(val, min_est);
+				max = (int)val;
+			} else {
+				max = 1;
+			}
+		}
+	}
+
+out:
+	if (s)
+		hlist_del_init(&s->est.list);
+	*chain_max = max;
+	return ret;
+
+stop:
+	ret = 0;
+	goto out;
+}
Signature · 2 lines
Jiri Wiesner
SUSE Labs
author maintainer
#16 lore raw

Re: [RFC PATCHv6 4/7] ipvs: use kthreads for stats estimation

Hello,

Quoted 15 lines · Jiri Wiesner

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

Quoted 10 lines · Julian Anastasov
net/netfilter/ipvs/ip_vs_est.c +10 -0
+
+	loops = 1;
+	/* Get best result from many tests */
+	for (ntest = 0; ntest < 12; ntest++) {
+		if (!(ntest & 3)) {
+			wait_event_idle_timeout(wq, kthread_should_stop(),
+						HZ / 50);
+			if (!ipvs->enable || kthread_should_stop())
+				goto stop;
+		}

I was testing the stability of chain_max:
Intel(R) Xeon(R) Gold 6326 CPU @ 2.90GHz
64 CPUs, 2 NUMA nodes

Quoted 5 lines
Quoted 4 lines

while :; do ipvsadm -A -t 10.10.10.1:2000; sleep 3; ipvsadm -D -t 10.10.10.1:2000; modprobe -r ip_vs_wlc ip_vs; sleep 10m; done

# dmesg | awk '/calc: chain_max/{m = $(NF - 5); sub("chain_max=", "", m); sub(",", "", m); m = int(m / 10) * 10; hist[m]++} END {PROCINFO["sorted_in"] = "@ind_num_asc"; for (m in hist) printf "%5d %5d\n", m, hist[m]}'
   30    90
   40     2

Chain_max was often 30-something. Chain_max was never below 30, which was observed earlier when only 3 tests were carried out.

So, 12 tests and 3 20ms gaps eliminate any cpufreq
issues in most of the cases and we do not see small chain_max value.

Quoted 15 lines

AMD EPYC 7601 32-Core Processor
128 CPUs, 8 NUMA nodes
Zen 1 machines such as this one have a large number of NUMA nodes due to restrictions in the CPU architecture. First, tests with different governors:

Quoted 12 lines

cpupower frequency-set -g ondemand

[  653.441325] IPVS: starting estimator thread 0...
[  653.514918] IPVS: calc: chain_max=8, single est=11171ns, diff=11301, loops=1, ntest=12
[  653.523580] IPVS: dequeue: 892ns
[  653.527528] IPVS: using max 384 ests per chain, 19200 per kthread
[  655.349916] IPVS: tick time: 3059313ns for 128 CPUs, 384 ests, 1 chains, chain_max=384
[  685.230016] IPVS: starting estimator thread 1...
[  717.110852] IPVS: starting estimator thread 2...
[  719.349755] IPVS: tick time: 2896668ns for 128 CPUs, 384 ests, 1 chains, chain_max=384
[  750.349974] IPVS: starting estimator thread 3...
[  783.349841] IPVS: tick time: 2942604ns for 128 CPUs, 384 ests, 1 chains, chain_max=384
[  847.349811] IPVS: tick time: 2930872ns for 128 CPUs, 384 ests, 1 chains, chain_max=384

Looks like cache_factor of 4 is good both to
ondemand which prefers cache_factor 3 (2.9->4ms) and performance which prefers cache_factor 5 (5.6->4.3ms):

gov/cache_factor chain_max tick time (goal 4.8ms)
ondemand/4 8 2.9ms
ondemand/3 11 4ms
performance/4 22 5.6ms
performance/5 17 4.3ms

Quoted 5 lines
Quoted 5 lines
   PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
 74902 root      20   0       0      0      0 I 7.591 0.000   0:13.95 ipvs-e:0:1
 94104 root      20   0       0      0      0 I 7.591 0.000   0:11.59 ipvs-e:0:2
 55669 root      20   0       0      0      0 I 6.931 0.000   0:15.75 ipvs-e:0:0
113311 root      20   0       0      0      0 I 0.990 0.000   0:01.31 ipvs-e:0:3
Quoted 8 lines
Quoted 8 lines

cpupower frequency-set -g performance

[ 1448.118857] IPVS: starting estimator thread 0...
[ 1448.194882] IPVS: calc: chain_max=22, single est=4138ns, diff=4298, loops=1, ntest=12
[ 1448.203435] IPVS: dequeue: 340ns
[ 1448.207373] IPVS: using max 1056 ests per chain, 52800 per kthread
[ 1450.029581] IPVS: tick time: 2727370ns for 128 CPUs, 518 ests, 1 chains, chain_max=1056
[ 1510.792734] IPVS: starting estimator thread 1...
[ 1514.032300] IPVS: tick time: 5436826ns for 128 CPUs, 1056 ests, 1 chains, chain_max=1056

performance takes 5.4-5.6ms with chain_max 22

Quoted 7 lines
Quoted 4 lines
[ 1578.032593] IPVS: tick time: 5691875ns for 128 CPUs, 1056 ests, 1 chains, chain_max=1056
   PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
 42514 root      20   0       0      0      0 I 14.24 0.000   0:14.96 ipvs-e:0:0
 95356 root      20   0       0      0      0 I 1.987 0.000   0:01.34 ipvs-e:0:1

While having the services loaded, I switched to the ondemand governor:

Quoted 2 lines
[ 1706.032577] IPVS: tick time: 5666868ns for 128 CPUs, 1056 ests, 1 chains, chain_max=1056
[ 1770.032534] IPVS: tick time: 5638505ns for 128 CPUs, 1056 ests, 1 chains, chain_max=1056

Hm, ondemand governor takes 5.6ms just like
the above performance result? This is probabllly still performance mode?

Quoted 6 lines
Quoted 3 lines
   PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
 42514 root      20   0       0      0      0 I 18.15 0.000   0:35.75 ipvs-e:0:0
 95356 root      20   0       0      0      0 I 2.310 0.000   0:04.05 ipvs-e:0:1

While having the services loaded, I kept the ondemand governor and saturated all logical CPUs on the machine:

Quoted 2 lines
[ 1834.033988] IPVS: tick time: 7129383ns for 128 CPUs, 1056 ests, 1 chains, chain_max=1056
[ 1898.038151] IPVS: tick time: 7281418ns for 128 CPUs, 1056 ests, 1 chains, chain_max=1056

Or ondemand takes 7.1ms with performance's chain_max=22
which is more real if we do 2.9*22/8=7.9ms.

Quoted 4 lines
Quoted 4 lines
   PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
102581 root      20   0 1047636 262304   1580 R 12791 0.100 161:03.94 a.out
 42514 root      20   0       0      0      0 I 17.76 0.000   1:06.88 ipvs-e:0:0
 95356 root      20   0       0      0      0 I 2.303 0.000   0:08.33 ipvs-e:0:1

If kthreads are isolated at some unused CPUs they
have space to go from desired 12.5% to 100% :) 17% is still not so scary :) The scheduler can probably allow even 100% usage by moving the kthreads between idle CPUs.

Quoted 8 lines

As for the stability of chain_max:

Quoted 4 lines

while :; do ipvsadm -A -t 10.10.10.1:2000; sleep 3; ipvsadm -D -t 10.10.10.1:2000; modprobe -r ip_vs_wlc ip_vs; sleep 2m; done
dmesg | awk '/calc: chain_max/{m = $(NF - 5); sub("chain_max=", "", m); sub(",", "", m); m = int(m / 2) * 2; hist[m]++} END {PROCINFO["sorted_in"] = "@ind_num_asc"; for (m in hist) printf "%5d %5d\n", m, hist[m]}'

    8    22

24 1

Basically, chain_max calculation under gonernors than ramp up CPU frequency more slowly (ondemand on AMD or powersave for intel_pstate) is stabler than before on both AMD and Intel. We know from previous results that even ARM with multiple NUMA nodes is not a complete disaster. Switching CPU frequency gonernors, including the unfavourable switches from performance to ondemand, does not saturate CPUs. When it comes to CPU frequency gonernors, people tend to use either ondemand (or powersave for intel_pstate) or performance consistently - switches between gonernors can be expected to be rare in production. I will need to find out to read through the latest version of the patch set.

OK. Thank you for testing the different cases!
Let me know if any changes are needed before releasing the patchset. We can even include some testing results in the commit messages.

Regards

--
Julian Anastasov <[email protected]>

#17 lore raw

Re: [RFC PATCHv6 4/7] ipvs: use kthreads for stats estimation

Quoted 20 lines · Julian Anastasov
Quoted 15 lines

AMD EPYC 7601 32-Core Processor
128 CPUs, 8 NUMA nodes
Zen 1 machines such as this one have a large number of NUMA nodes due to restrictions in the CPU architecture. First, tests with different governors:

Quoted 12 lines

cpupower frequency-set -g ondemand

[  653.441325] IPVS: starting estimator thread 0...
[  653.514918] IPVS: calc: chain_max=8, single est=11171ns, diff=11301, loops=1, ntest=12
[  653.523580] IPVS: dequeue: 892ns
[  653.527528] IPVS: using max 384 ests per chain, 19200 per kthread
[  655.349916] IPVS: tick time: 3059313ns for 128 CPUs, 384 ests, 1 chains, chain_max=384
[  685.230016] IPVS: starting estimator thread 1...
[  717.110852] IPVS: starting estimator thread 2...
[  719.349755] IPVS: tick time: 2896668ns for 128 CPUs, 384 ests, 1 chains, chain_max=384
[  750.349974] IPVS: starting estimator thread 3...
[  783.349841] IPVS: tick time: 2942604ns for 128 CPUs, 384 ests, 1 chains, chain_max=384
[  847.349811] IPVS: tick time: 2930872ns for 128 CPUs, 384 ests, 1 chains, chain_max=384

Looks like cache_factor of 4 is good both to
ondemand which prefers cache_factor 3 (2.9->4ms) and performance which prefers cache_factor 5 (5.6->4.3ms):

Quoted 5 lines

gov/cache_factor chain_max tick time (goal 4.8ms)
ondemand/4 8 2.9ms
ondemand/3 11 4ms
performance/4 22 5.6ms
performance/5 17 4.3ms

Yes, a cache factor of 4 happens to be a good compromise on this particular Zen 1 machine.

Quoted 11 lines
Quoted 7 lines
Quoted 4 lines
[ 1578.032593] IPVS: tick time: 5691875ns for 128 CPUs, 1056 ests, 1 chains, chain_max=1056
   PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
 42514 root      20   0       0      0      0 I 14.24 0.000   0:14.96 ipvs-e:0:0
 95356 root      20   0       0      0      0 I 1.987 0.000   0:01.34 ipvs-e:0:1

While having the services loaded, I switched to the ondemand governor:

Quoted 2 lines
[ 1706.032577] IPVS: tick time: 5666868ns for 128 CPUs, 1056 ests, 1 chains, chain_max=1056
[ 1770.032534] IPVS: tick time: 5638505ns for 128 CPUs, 1056 ests, 1 chains, chain_max=1056

Hm, ondemand governor takes 5.6ms just like
the above performance result? This is probabllly still performance mode?

I am not sure if I copied the right messages from the log. Probably not.

Quoted 7 lines
Quoted 2 lines

Basically, chain_max calculation under gonernors than ramp up CPU frequency more slowly (ondemand on AMD or powersave for intel_pstate) is stabler than before on both AMD and Intel. We know from previous results that even ARM with multiple NUMA nodes is not a complete disaster. Switching CPU frequency gonernors, including the unfavourable switches from performance to ondemand, does not saturate CPUs. When it comes to CPU frequency gonernors, people tend to use either ondemand (or powersave for intel_pstate) or performance consistently - switches between gonernors can be expected to be rare in production. I will need to find out to read through the latest version of the patch set.

OK. Thank you for testing the different cases!
Let me know if any changes are needed before releasing the patchset. We can even include some testing results in the commit messages.

Absolutely.

Signature · 2 lines
Jiri Wiesner
SUSE Labs
Tested-by Reviewed-by on v6 4/7
#18 lore raw

Re: [RFC PATCHv6 4/7] ipvs: use kthreads for stats estimation

In reply to Julian Anastasov
Quoted 17 lines · 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. First kthread determines parameters to use, eg. maximum number of estimators to process per kthread based on chain's length, allowing sub-100us cond_resched rate and estimation taking 1/8 of the CPU.

First kthread also plays the role of distributor of added estimators to all kthreads.

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

Quoted 6 lines

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

---

include/net/ip_vs.h struct netns_ipvs { · line 1012

Quoted 12 lines
include/net/ip_vs.h +5 -3
@@ -953,9 +1005,17 @@ 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 hlist_head	est_temp_list;	/* Ests during calc phase */
+	struct ip_vs_est_kt_data **est_kt_arr;	/* Array of kthread data ptrs */
+	unsigned long		est_max_threads;/* rlimit */

Not an rlimit anymore.

Quoted 9 lines
(quoted diff) +6 -0
+	int			est_calc_phase;	/* Calculation phase */
+	int			est_chain_max;	/* Calculated chain_max */
+	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;

Review struct netns_ipvs {

Quoted 10 lines
(quoted diff) +8 -1
 
-	INIT_LIST_HEAD(&est->list);
+/* Start estimation for stats */
+int ip_vs_start_estimator(struct netns_ipvs *ipvs, struct ip_vs_stats *stats)
+{
+	struct ip_vs_estimator *est = &stats->est;
+	int ret;
+
+	if (!ipvs->est_max_threads && ipvs->enable)
+		ipvs->est_max_threads = 4 * num_possible_cpus();

To avoid the magic number - 4, a symbolic constant could be used. The 4 is related to the design decision that a fully loaded kthread should take 1/8 of the CPU time of a CPU.

Quoted 17 lines
net/netfilter/ipvs/ip_vs_est.c +17 -0
+
+	est->ktid = -1;
+	est->ktrow = IPVS_EST_NTICKS - 1;	/* Initial delay */
+
+	/* We prefer this code to be short, kthread 0 will requeue the
+	 * estimator to available chain. If tasks are disabled, we
+	 * will not allocate much memory, just for kt 0.
+	 */
+	ret = 0;
+	if (!ipvs->est_kt_count || !ipvs->est_kt_arr[0])
+		ret = ip_vs_est_add_kthread(ipvs);
+	if (ret >= 0)
+		hlist_add_head(&est->list, &ipvs->est_temp_list);
+	else
+		INIT_HLIST_NODE(&est->list);
+	return ret;
+}
Signature · 2 lines
Jiri Wiesner
SUSE Labs
author maintainer
#19 lore raw

RFC PATCH v6 5/7 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                      |  55 +++++++++
 net/netfilter/ipvs/ip_vs_ctl.c           | 143 ++++++++++++++++++++++-
 net/netfilter/ipvs/ip_vs_est.c           |  11 +-
 4 files changed, 226 insertions(+), 3 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 +55 -0

91 lines not on this page · raw message

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

179 lines not on this page · raw message

net/netfilter/ipvs/ip_vs_est.c +9 -2
index c134f9466dc6..91eb0f1f8a00 100644
@@ -57,6 +57,9 @@
   - kthread contexts are created and attached to array
   - the kthread tasks are started 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
@@ -229,6 +232,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);
@@ -259,6 +263,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);
 
@@ -334,7 +341,7 @@ static int ip_vs_est_add_kthread(struct netns_ipvs *ipvs)
 	}
 
 	/* 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;
@@ -475,7 +482,7 @@ int ip_vs_start_estimator(struct netns_ipvs *ipvs, struct ip_vs_stats *stats)
 	int ret;
 
 	if (!ipvs->est_max_threads && ipvs->enable)
-		ipvs->est_max_threads = 4 * num_possible_cpus();
+		ipvs->est_max_threads = ip_vs_est_max_threads(ipvs);
 
 	est->ktid = -1;
 	est->ktrow = IPVS_EST_NTICKS - 1;	/* Initial delay */
Signature · 1 line
2.38.1
author maintainer
#21 lore raw

RFC PATCH v6 6/7 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           |  2 +-
 4 files changed, 35 insertions(+), 6 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 0a745b626cc8..1afb7ec8d537 100644
@@ -1591,8 +1591,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 38df3ee655ed..c9f598505642 100644
@@ -2056,6 +2056,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
@@ -2230,7 +2256,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",
@@ -4323,6 +4349,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 -1
index 91eb0f1f8a00..526100976d59 100644
@@ -212,7 +212,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.38.1
author maintainer
#23 lore raw

RFC PATCH v6 7/7 ipvs: debug the tick time

In reply to Julian Anastasov

Just for testing print the tick time every minute

---
 net/netfilter/ipvs/ip_vs_est.c | 28 ++++++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)
net/netfilter/ipvs/ip_vs_est.c +26 -2
index 526100976d59..4489a3dbad1e 100644
@@ -147,7 +147,14 @@ static void ip_vs_tick_estimation(struct ip_vs_est_kt_data *kd, int row)
 {
 	struct ip_vs_est_tick_data *td;
 	int cid;
-
+	u64 ns = 0;
+	static int used_row = -1;
+	static int pass;
+
+	if (used_row < 0)
+		used_row = row;
+	if (row == used_row && !kd->id && !(pass & 31))
+		ns = ktime_get_ns();
 	rcu_read_lock();
 	td = rcu_dereference(kd->ticks[row]);
 	if (!td)
@@ -164,6 +171,16 @@ static void ip_vs_tick_estimation(struct ip_vs_est_kt_data *kd, int row)
 
 out:
 	rcu_read_unlock();
+	if (row == used_row && !kd->id && !(pass++ & 31)) {
+		static int ncpu;
+
+		ns = ktime_get_ns() - ns;
+		if (!ncpu)
+			ncpu = num_possible_cpus();
+		pr_info("tick time: %lluns for %d CPUs, %d ests, %d chains, chain_max=%d\n",
+			(unsigned long long)ns, ncpu, kd->tick_len[row],
+			IPVS_EST_TICK_CHAINS, kd->chain_max);
+	}
 }
 
 static int ip_vs_estimation_kthread(void *data)
@@ -637,7 +654,7 @@ static int ip_vs_est_calc_limits(struct netns_ipvs *ipvs, int *chain_max)
 	int i, loops, ntest;
 	s32 min_est = 0;
 	ktime_t t1, t2;
-	s64 diff, val;
+	s64 diff = 0, val;
 	int max = 8;
 	int ret = 1;
 
@@ -702,6 +719,8 @@ static int ip_vs_est_calc_limits(struct netns_ipvs *ipvs, int *chain_max)
 	}
 
 out:
+	pr_info("calc: chain_max=%d, single est=%dns, diff=%d, loops=%d, ntest=%d\n",
+		max, min_est, (int)diff, loops, ntest);
 	if (s)
 		hlist_del_init(&s->est.list);
 	*chain_max = max;
@@ -737,6 +756,7 @@ static void ip_vs_est_calc_phase(struct netns_ipvs *ipvs)
 	int id, row, cid, delay;
 	bool last, last_td;
 	int chain_max;
+	u64 ns = 0;
 	int step;
 
 	if (!ip_vs_est_calc_limits(ipvs, &chain_max))
@@ -770,6 +790,8 @@ static void ip_vs_est_calc_phase(struct netns_ipvs *ipvs)
 	 */
 	delay = IPVS_EST_NTICKS;
 
+	ns = ktime_get_ns();
+
 next_delay:
 	delay--;
 	if (delay < 0)
@@ -856,6 +878,8 @@ static void ip_vs_est_calc_phase(struct netns_ipvs *ipvs)
 	goto walk_chain;
 
 end_dequeue:
+	ns = ktime_get_ns() - ns;
+	pr_info("dequeue: %lluns\n", (unsigned long long)ns);
 	/* All estimators removed while calculating ? */
 	if (!ipvs->est_kt_count)
 		goto unlock;
Signature · 1 line
2.38.1