libtracefs utest: Fix trace cpu pipe and test_mem failures
cover libtracefs utest: Fix trace cpu pipe and test_mem failures
These fix unit test failures reported here:
https://lore.kernel.org/linux-trace-devel/[email protected]/
- Patch 1: on architectures with 64K pages (ppc64el), the large step of "trace cpu pipe" writes more than 13 GB into /tmp with the memory mapped buffer, which fails with ENOSPC on a tmpfs /tmp.
- Patch 2: "make test_mem" hangs in "trace pid events filter", as the spinning threads starve the main thread under valgrind.
- Patch 3: "trace cpu pipe" fails under valgrind when the memory mapped reader can not keep up and the kernel overwrites events.
The patches are independent of each other. I can also send them separately, if that's preferred.
Related fixes for the same report:
- kbuffer: Fix out-of-bounds write in kbuffer_read_buffer()
https://lore.kernel.org/linux-trace-devel/[email protected]/T/#u
- libtracefs: Do not miss sub-buffers when reading a memory mapped buffer
https://lore.kernel.org/linux-trace-devel/[email protected]/T/#u
- libtracefs utest: Fix buffer size in follow events clear test
https://lore.kernel.org/linux-trace-devel/[email protected]/
Tested on ppc64el (POWER9, 64K pages, kernel 7.3) on top of master, together with the related fixes above: "trace cpu pipe" passes in "make test" and "make test_mem", and "make test_mem" no longer hangs.
Frank Heimes (3):
libtracefs utest: Scale trace cpu pipe test to the sub-buffer size libtracefs utest: Run valgrind memory test with fair scheduling libtracefs utest: Account for overruns in the trace cpu pipe test
Makefile | 1 + utest/tracefs-utest.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 2 deletions(-)
Signature · 1 line
2.53.0
PATCH 1/3 libtracefs utest: Scale trace cpu pipe test to the sub-buffer size
The last step of the "trace cpu pipe" test writes events_per_buf * 1000 events, i.e. the equivalent of 1000 sub-buffers, while a reader thread copies the ring buffer with tracefs_cpu_write() into a temporary file in /tmp.
When the ring buffer is memory mapped, tracefs_cpu_write() writes a full sub-buffer, padded with zeros, for every read that returns data. As the reader thread polls continuously, most reads only return a few events, so the amount of data written is roughly the number of reads times the sub-buffer size.
On architectures with a 64K PAGE_SIZE like ppc64el the sub-buffers are 16 times larger than on x86 and events_per_buf is about 3276 instead of about 200, so the test generates 3.3 million events and writes more than 13 GB into the temporary file. With /tmp being a tmpfs (as on several distributions) this fails with ENOSPC:
Test: trace cpu pipe ...FAILED
1. tracefs-utest.c:1966 - ret >= 0
2. tracefs-utest.c:1970 - cnt == expect
3. tracefs-utest.c:1966 - ret >= 0
4. tracefs-utest.c:1970 - cnt == expectScale the number of sub-buffers to the sub-buffer size, using 1000 sub-buffers of 4K as reference. This keeps the test unchanged for 4K sub-buffers (1000) and uses 62 sub-buffers for 64K sub-buffers, which still is a multiple of the ring buffer size.
--- utest/tracefs-utest.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)
utest/tracefs-utest.c +13 -1
@@ -1973,15 +1973,27 @@ static void test_cpu_pipe(struct test_cpu_data *data, int expect)static void test_instance_trace_cpu_pipe (struct tracefs_instance *instance,bool map) {struct test_cpu_data data; +int big_num;if (setup_trace_cpu (instance, &data, true, map))return ; +/* +* Scale the number of sub-buffers to write to the sub-buffer size, +* using 1000 sub-buffers of 4K as reference. With the memory mapped +* buffer every read writes a full (zero padded) sub-buffer, which on +* architectures with large sub-buffers (64K PAGE_SIZE) can easily +* fill the file system holding the temporary file. +*/ + big_num =1000 *4096 / data.bufsize; +if (big_num <1 ) + big_num =1 ; +test_cpu_pipe (&data,1 );test_cpu_pipe (&data, data.events_per_buf /2 );test_cpu_pipe (&data, data.events_per_buf);test_cpu_pipe (&data, data.events_per_buf +1 ); -test_cpu_pipe (&data, data.events_per_buf *1000 ); +test_cpu_pipe (&data, data.events_per_buf * big_num);shutdown_trace_cpu (&data); }
Signature · 1 line
2.53.0
PATCH 2/3 libtracefs utest: Run valgrind memory test with fair scheduling
In reply to Frank Heimesvalgrind serializes the threads of the program under test with a lock that, by default, is not handed out fairly. A thread that never blocks can therefore get the lock again and again, starving the other threads.
The "trace pid events filter" and "trace pid function filter" tests start two threads that spin calling getppid() until the main thread sets data->stop after msleep(100). Under valgrind the spinning threads can starve the main thread, so data->stop is never set and the test hangs until the test runner times out. This is reproducible on ppc64el where make test_mem hangs in "trace pid events filter" for hours.
Run valgrind with --fair-sched=yes, which makes valgrind schedule the threads in a round-robin fashion.
--- Makefile | 1 + 1 file changed, 1 insertion(+)
Makefile +1 -0
@@ -217,6 +217,7 @@ ifneq ($(shell id -u), 0) $(error "The unit test should be run as root, as it reuqires full access to tracefs") endif CK_FORK=no LD_LIBRARY_PATH=$(bdir) $(VALGRIND) \ + --fair-sched=yes \ --show-leak-kinds=all --leak-resolution=high \ --leak-check=full --show-possibly-lost=yes \ --track-origins=yes -s \
Signature · 1 line
2.53.0
PATCH 3/3 libtracefs utest: Account for overruns in the trace cpu pipe test
In reply to Frank HeimesThe "trace cpu pipe" test generates events on CPU 0 while a reader thread copies the ring buffer with tracefs_cpu_write() into a file, and then expects all events in the file. For the large step of the test the number of events is about three times what the ring buffer can hold, so the test only passes if the reader thread keeps up with the writer.
That is not the case when running under valgrind with the memory mapped ring buffer, where the reader walks every event in user space. The ring buffer then overwrites the oldest events, and the test fails in "make test_mem" on all architectures (amd64, ppc64el, ...):
Test: trace cpu pipe ...missed events -1
FAILED
1. tracefs-utest.c:1970 - cnt == expect
2. tracefs-utest.c:1970 - cnt == expect
The kernel accounts for every overwritten event in the overrun counter of the per CPU stats. For example on ppc64el:
map=1 expect=203112 cnt=85176 overrun=117936
Check that every event was either read or accounted for as overrun by the kernel. If no events are overwritten, which is the normal case without valgrind, the test is as strict as before.
--- utest/tracefs-utest.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-)
utest/tracefs-utest.c +27 -1
@@ -1942,9 +1942,24 @@ static void *trace_cpu_thread(void *arg)return (void *)ret; } +static ssize_t get_cpu_overrun (struct tracefs_instance *instance,int cpu) +{ +struct tracefs_buffer_stat *tstat; +ssize_t overrun; + + tstat =tracefs_instance_get_stat (instance, cpu); +if (!tstat) +return -1 ; + overrun =tracefs_buffer_stat_overrun (tstat); +tracefs_instance_put_stat (tstat); + +return overrun; +} +static void test_cpu_pipe (struct test_cpu_data *data,int expect) {pthread_t thread; +ssize_t overrun;void *retval;long ret;int cnt;
@@ -1952,6 +1967,10 @@ static void test_cpu_pipe(struct test_cpu_data *data, int expect)tracefs_instance_file_clear (data->instance,"trace" );ftruncate (data->fd,0 ); +/* The events are generated on CPU 0, see setup_trace_cpu() */ + overrun =get_cpu_overrun (data->instance,0 ); +CU_TEST (overrun >=0 ); + data->done = false;pthread_create (&thread,NULL , trace_cpu_thread, data);
@@ -1967,7 +1986,14 @@ static void test_cpu_pipe(struct test_cpu_data *data, int expect) cnt =read_trace_cpu_file (data); -CU_TEST (cnt == expect); +/* +* If the reader can not keep up with the writer (for example +* when running under valgrind), the ring buffer overwrites the +* oldest events. Those events are lost, but accounted for by +* the kernel in the overrun counter. +*/ + overrun =get_cpu_overrun (data->instance,0 ) - overrun; +CU_TEST (cnt + overrun == expect); }static void test_instance_trace_cpu_pipe (struct tracefs_instance *instance,bool map)
Signature · 1 line
2.53.0