rtla/timerlat: Lock the memory of the user-space threads
PATCH v2 1/1 rtla/timerlat: Lock the memory of the user-space threads
With -u, the latency timerlat reports runs until the measuring thread is back in user space. Nothing keeps that thread's memory resident, so under memory pressure the kernel reclaims its code, stack and libc pages, and the page faults on the way back to user space count as latency.
On a Raspberry Pi 3 (PREEMPT_RT 6.12) under stress-ng --class memory, timerlat reported up to 40 ms for the user-space threads, which took 283 major faults, while the kernel threads' latency stayed below 1 ms. cyclictest under the same load: 133 ms without -m (mlockall), 0.9 ms with it.
With this change timerlat reports 0.9 ms for the user-space threads under the same load, and they take no major faults.
Lock the user-space threads' memory before they start measuring, as cyclictest -m does.
---
Changes in v2:
- Shorted comment.
- Moved the mlock up before the sched switch as suggested by John
- Link to v1: https://patch.msgid.link/[email protected]
--- tools/tracing/rtla/src/timerlat_u.c | 8 ++++++++ 1 file changed, 8 insertions(+)
tools/tracing/rtla/src/timerlat_u.c +8 -0
@@ -15,6 +15,7 @@#include <pthread.h> #include <sys/wait.h> #include <sys/prctl.h> +#include <sys/mman.h> #include "common.h" #include "timerlat_u.h"
@@ -37,6 +38,13 @@ static int timerlat_u_main(int cpu, struct timerlat_u_params *params)cpu_set_t set;int retval; +/* +* Fault in and lock all memory before switching to a real-time +* priority, so that page faults don't show up as latency. +*/ +if (mlockall (MCL_CURRENT |MCL_FUTURE )) +err_msg ("Could not lock timerlat u memory: %s\n" ,strerror (errno)); +/* * This all is only setting up the tool. */
---
base-commit: ee83899302a0457a3ba47464a13e8682bd7ba27d
change-id: 20261112-rtla-mlock-2a572f10533e
Best regards,
Signature · 1 line
Daniel Wagner <[email protected]>
Hi Daniel,
tools/tracing/rtla/src/timerlat_u.c static int timerlat_u_main(int cpu, struct timerlat_u_params *params) · line 44
Quoted 12 lines · Daniel Wagner
tools/tracing/rtla/src/timerlat_u.c +4
-0@@ -37,6 +38,13 @@ static int timerlat_u_main(int cpu, struct timerlat_u_params *params)cpu_set_t set;int retval; +/* +* Fault in and lock all memory before switching to a real-time +* priority, so that page faults don't show up as latency. +*/
It isn't because the page faults show up as latencies. It is because doing non-real-time work with a real-time priority is stealing the CPU away from other real-time tasks that presumably have actual real-time work to do.
This program peforms severeal non-real-time activities with the real-time priority, which is not setting a good example. But I am only reviewing your patch, not the full source.
John
Tomas Glozar
maintainerQuoted 6 lines · John Ogness
It isn't because the page faults show up as latencies. It is because doing non-real-time work with a real-time priority is stealing the CPU away from other real-time tasks that presumably have actual real-time work to do.
Quoted 6 lines
This program peforms severeal non-real-time activities with the real-time priority, which is not setting a good example. But I am only reviewing your patch, not the full source.
John
The only other activities the user thread performs with RT priority on before entering the timerlat interface read loop are setting the cgroup and opening the timerlat interface file descriptor. I agree that ideally, setting the priority should be done just before the loop.
However, I don't think either activity takes too long, so the difference is probably negligible in practice. Unlike mlockall, which (if I understand your point correctly) might pre-fault from disk
Tomas
Quoted 5 lines · Tomas Glozar
The only other activities the user thread performs with RT priority on before entering the timerlat interface read loop are setting the cgroup and opening the timerlat interface file descriptor. I agree that ideally, setting the priority should be done just before the loop.
If you say it is ideal to do X, then why not do X? Real-time tasks (especially running at rtprio 95!) need to respect other real-time tasks on the system. Developers new to RT might see this code and copy it. The code even has a comment:
"If you want to use as base for your own tool... go ahead."
For me there are two issues here:
1. RT latency measurement tooling needs to be a little intrusive to a system as practical. Otherwise it may not be possible for developers to use the tooling on their real environment.
2. Official RT tooling should set the example of how to program for RT Linux correctly.
John