Skip to contentSkip to conversation

trace: Add option for polling ring buffers

RFC In review Nicolas Saenz Julienne 4 participants 6 messages May 19, 2021 – Jun 2, 2021

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

RFC 1/1 trace: Add option for polling ring buffers

To minimize trace's effect on isolated CPUs. That is, CPUs were only a handful or a single, process are allowed to run. Introduce a new trace
option: 'poll-rb'.

This option changes the heuristic used to wait for data on trace buffers. The default one, based on wait queues, will trigger an IPI[1] on the CPU responsible for new data, which will take care of waking up the trace gathering process (generally trace-cmd). Whereas with 'poll-rb' we will poll (as in busy-wait) the ring buffers from the trace gathering process, releasing the CPUs writing trace data from doing any wakeup work.

This wakeup work, although negligible in the vast majority of workloads, may cause unwarranted latencies on systems running trace on isolated CPUs. This is made worse on PREEMPT_RT kernels, as they defer the IPI handling into a kernel thread, forcing unwarranted context switches on otherwise extremely busy CPUs.

To illustrate this, tracing with PREEMPT_RT=y on an isolated CPU with a single process pinned to it (NO_HZ_FULL=y, and plenty more isolation options enabled). I see:
- 50-100us latency spikes with the default trace-cmd options
- 14-10us latency spikes with 'poll-rb'
- 11-8us latency spikes with no tracing at all

The obvious drawback of 'poll-rb' is putting more pressure on the housekeeping CPUs. Wasting cycles. Hence the notice in the documentation discouraging its use in general.

[1] The IPI, in this case, an irq_work, is needed since trace might run in NMI context. Which is not suitable for wake-ups.

---
 Documentation/trace/ftrace.rst | 10 +++++
 include/linux/ring_buffer.h    |  2 +-
 kernel/trace/ring_buffer.c     | 76 +++++++++++++++++++---------------
 kernel/trace/trace.c           |  2 +-
 kernel/trace/trace.h           |  1 +
 5 files changed, 55 insertions(+), 36 deletions(-)
Documentation/trace/ftrace.rst +10 -0
index 62c98e9bbdd9..224ed8652101 100644
@@ -1248,6 +1248,16 @@ Here are the available options:
 	with the "nop" tracer is the same as just enabling the
 	"branch" tracer.
 
+  poll-rb
+        Use polling instead of waiting for new data in ringbuffers. The wait
+        heuristic will trigger IPIs on the CPU resposible for new data, which
+        can generate false positives when tracing low latency workloads (or the
+        IPIs themselves). This is made worse on PREEMPT_RT kernels, as they
+        defer the IPI handling into a kernel thread, forcing unwarranted
+        context switches on otherwise extremely busy processes.
+
+        When in doubt don't enable this, it'll just waste cpu cycles.
+
 .. tip:: Some tracers have their own options. They only appear in this
        file when the tracer is active. They always appear in the
        options directory.
include/linux/ring_buffer.h +1 -1
index dac53fd3afea..b492061d369b 100644
@@ -98,7 +98,7 @@ __ring_buffer_alloc(unsigned long size, unsigned flags, struct lock_class_key *k
 	__ring_buffer_alloc((size), (flags), &__key);	\
 })
 
-int ring_buffer_wait(struct trace_buffer *buffer, int cpu, int full);
+int ring_buffer_wait(struct trace_buffer *buffer, int cpu, int full, bool poll);
 __poll_t ring_buffer_poll_wait(struct trace_buffer *buffer, int cpu,
 			  struct file *filp, poll_table *poll_table);
 
kernel/trace/ring_buffer.c +42 -34

106 lines not on this page · raw message

kernel/trace/trace.c +1 -1
index 1c4e702133e8..8408b0f50519 100644
@@ -1887,7 +1887,7 @@ static int wait_on_pipe(struct trace_iterator *iter, int full)
 		return 0;
 
 	return ring_buffer_wait(iter->array_buffer->buffer, iter->cpu_file,
-				full);
+				full, iter->tr->trace_flags & TRACE_ITER_POLL_RB);
 }
 
 #ifdef CONFIG_FTRACE_STARTUP_TEST
kernel/trace/trace.h +1 -0
index cd80d046c7a5..045bef803549 100644
@@ -1198,6 +1198,7 @@ extern int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
 		C(EVENT_FORK,		"event-fork"),		\
 		C(PAUSE_ON_TRACE,	"pause-on-trace"),	\
 		C(HASH_PTR,		"hash-ptr"),	/* Print hashed pointer */ \
+		C(POLL_RB,		"poll-rb"),		\
 		FUNCTION_FLAGS					\
 		FGRAPH_FLAGS					\
 		STACK_FLAGS					\
Signature · 1 line
2.31.1
#1 lore raw
Quoted 3 lines · Nicolas Saenz Julienne

To minimize trace's effect on isolated CPUs. That is, CPUs were only a handful or a single, process are allowed to run. Introduce a new trace
option: 'poll-rb'.

maybe this should take a parameter in ms (us?) saying how frequently to poll? it seems like a reasonable assumption that somebody running in this kind of RT environment would be able to judge how often their monitoring task needs to collect data.

Quoted 2 lines

[1] The IPI, in this case, an irq_work, is needed since trace might run in NMI context. Which is not suitable for wake-ups.

could we also consider a try-wakeup which would not succeed if in NMI context? or are there situations where we only gather data in NMI context, and so would never succeed in waking up? if so, maybe schedule the irq_work every 1000 failures to wake up.

#2 lore raw

Hi Willy,

Quoted 9 lines · Matthew Wilcox
Quoted 3 lines · Nicolas Saenz Julienne

To minimize trace's effect on isolated CPUs. That is, CPUs were only a handful or a single, process are allowed to run. Introduce a new trace
option: 'poll-rb'.

maybe this should take a parameter in ms (us?) saying how frequently to poll? it seems like a reasonable assumption that somebody running in this kind of RT environment would be able to judge how often their monitoring task needs to collect data.

+1 (yes please).

Quoted 7 lines
Quoted 2 lines

[1] The IPI, in this case, an irq_work, is needed since trace might run in NMI context. Which is not suitable for wake-ups.

could we also consider a try-wakeup which would not succeed if in NMI context? or are there situations where we only gather data in NMI context, and so would never succeed in waking up? if so, maybe schedule the irq_work every 1000 failures to wake up.

We'd like to reduce overhead on the isolated (as in isolcpus=) CPUs as much as possible (but yes this option was suggested).

#3 lore raw
In reply to Matthew Wilcox

Hi Matthew, thanks for your comments.

Quoted 9 lines · Matthew Wilcox
Quoted 3 lines · Nicolas Saenz Julienne

To minimize trace's effect on isolated CPUs. That is, CPUs were only a handful or a single, process are allowed to run. Introduce a new trace
option: 'poll-rb'.

maybe this should take a parameter in ms (us?) saying how frequently to poll? it seems like a reasonable assumption that somebody running in this kind of RT environment would be able to judge how often their monitoring task needs to collect data.

I'll look into it.

Quoted 5 lines
Quoted 2 lines

[1] The IPI, in this case, an irq_work, is needed since trace might run in NMI context. Which is not suitable for wake-ups.

could we also consider a try-wakeup which would not succeed if in NMI context?

Yes, in a similar vein, my original idea was to defer the wakeup process into a non-isolated CPU using irq_work_on(). But that irq_work flavor is not NMI safe (nor any other IPI mechanisms targeting other CPUs).

Quoted 2 lines

or are there situations where we only gather data in NMI context, and so would never succeed in waking up?

Yes, that's a use-case. For ex. 'trace-cmd record -e nmi'.

Quoted 1 line

if so, maybe schedule the irq_work every 1000 failures to wake up.

You'd be generating latency spikes nonetheless. Which might eventually break the isolated application latency requirements.

As Marcelo said, the least code we run on the isolated CPU the better.

Signature · 1 line
Nicolás Sáenz
maintainer
#4 lore raw
In reply to Nicolas Saenz Julienne
Quoted 25 lines · On Wed, 19 May 2021 19:57:55 +0200 Nicolas Saenz Julienne…

To minimize trace's effect on isolated CPUs. That is, CPUs were only a handful or a single, process are allowed to run. Introduce a new trace
option: 'poll-rb'.

This option changes the heuristic used to wait for data on trace buffers. The default one, based on wait queues, will trigger an IPI[1] on the CPU responsible for new data, which will take care of waking up the trace gathering process (generally trace-cmd). Whereas with 'poll-rb' we will poll (as in busy-wait) the ring buffers from the trace gathering process, releasing the CPUs writing trace data from doing any wakeup work.

This wakeup work, although negligible in the vast majority of workloads, may cause unwarranted latencies on systems running trace on isolated CPUs. This is made worse on PREEMPT_RT kernels, as they defer the IPI handling into a kernel thread, forcing unwarranted context switches on otherwise extremely busy CPUs.

To illustrate this, tracing with PREEMPT_RT=y on an isolated CPU with a single process pinned to it (NO_HZ_FULL=y, and plenty more isolation options enabled). I see:
- 50-100us latency spikes with the default trace-cmd options
- 14-10us latency spikes with 'poll-rb'
- 11-8us latency spikes with no tracing at all

Quoted 6 lines

The obvious drawback of 'poll-rb' is putting more pressure on the housekeeping CPUs. Wasting cycles. Hence the notice in the documentation discouraging its use in general.

[1] The IPI, in this case, an irq_work, is needed since trace might run in NMI context. Which is not suitable for wake-ups.

Can't this simply be done in user-space?

Set the reading of the trace buffers to O_NONBLOCK and it wont wait for buffering to happen, and should prevent it from causing the IPI wake ups.

If you need this for trace-cmd, we can add a --poll option that would do this.

-- Steve

#5 lore raw

Hi Steven, thanks for having a look at this.

Quoted 32 lines · Steven Rostedt
Quoted 29 lines · On Wed, 19 May 2021 19:57:55 +0200 Nicolas Saenz Julienne…

To minimize trace's effect on isolated CPUs. That is, CPUs were only a handful or a single, process are allowed to run. Introduce a new trace
option: 'poll-rb'.

This option changes the heuristic used to wait for data on trace buffers. The default one, based on wait queues, will trigger an IPI[1] on the CPU responsible for new data, which will take care of waking up the trace gathering process (generally trace-cmd). Whereas with 'poll-rb' we will poll (as in busy-wait) the ring buffers from the trace gathering process, releasing the CPUs writing trace data from doing any wakeup work.

This wakeup work, although negligible in the vast majority of workloads, may cause unwarranted latencies on systems running trace on isolated CPUs. This is made worse on PREEMPT_RT kernels, as they defer the IPI handling into a kernel thread, forcing unwarranted context switches on otherwise extremely busy CPUs.

To illustrate this, tracing with PREEMPT_RT=y on an isolated CPU with a single process pinned to it (NO_HZ_FULL=y, and plenty more isolation options enabled). I see:
  - 50-100us latency spikes with the default trace-cmd options
  - 14-10us latency spikes with 'poll-rb'
  - 11-8us latency spikes with no tracing at all

The obvious drawback of 'poll-rb' is putting more pressure on the housekeeping CPUs. Wasting cycles. Hence the notice in the documentation discouraging its use in general.

Quoted 7 lines
Quoted 2 lines

[1] The IPI, in this case, an irq_work, is needed since trace might run in NMI context. Which is not suitable for wake-ups.

Can't this simply be done in user-space?

Set the reading of the trace buffers to O_NONBLOCK and it wont wait for buffering to happen, and should prevent it from causing the IPI wake ups.

Yes, I hadn't thought of O_NONBLOCK...

Quoted 2 lines

If you need this for trace-cmd, we can add a --poll option that would do this.

I just sent a patch with my attempt at implementing --poll.

Signature · 1 line
Nicolás Sáenz