diff mbox series

[v11,5/5] ring-buffer/selftest: Add ring-buffer mapping test

Message ID 20240111161712.1480333-6-vdonnefort@google.com (mailing list archive)
State New
Headers show
Series None | expand

Commit Message

Vincent Donnefort Jan. 11, 2024, 4:17 p.m. UTC
This test maps a ring-buffer and validate the meta-page after reset and
after emitting few events.

Cc: Shuah Khan <shuah@kernel.org>
Cc: Shuah Khan <skhan@linuxfoundation.org>
Cc: linux-kselftest@vger.kernel.org
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>

Comments

Masami Hiramatsu (Google) Jan. 13, 2024, 1:39 p.m. UTC | #1
On Thu, 11 Jan 2024 16:17:12 +0000
Vincent Donnefort <vdonnefort@google.com> wrote:

> This test maps a ring-buffer and validate the meta-page after reset and
> after emitting few events.
> 
> Cc: Shuah Khan <shuah@kernel.org>
> Cc: Shuah Khan <skhan@linuxfoundation.org>
> Cc: linux-kselftest@vger.kernel.org
> Signed-off-by: Vincent Donnefort <vdonnefort@google.com>

Looks good to me and tested.

Reviewed-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Tested-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>

Thank you,

> 
> diff --git a/tools/testing/selftests/ring-buffer/Makefile b/tools/testing/selftests/ring-buffer/Makefile
> new file mode 100644
> index 000000000000..627c5fa6d1ab
> --- /dev/null
> +++ b/tools/testing/selftests/ring-buffer/Makefile
> @@ -0,0 +1,8 @@
> +# SPDX-License-Identifier: GPL-2.0
> +CFLAGS += -Wl,-no-as-needed -Wall
> +CFLAGS += $(KHDR_INCLUDES)
> +CFLAGS += -D_GNU_SOURCE
> +
> +TEST_GEN_PROGS = map_test
> +
> +include ../lib.mk
> diff --git a/tools/testing/selftests/ring-buffer/config b/tools/testing/selftests/ring-buffer/config
> new file mode 100644
> index 000000000000..ef8214661612
> --- /dev/null
> +++ b/tools/testing/selftests/ring-buffer/config
> @@ -0,0 +1 @@
> +CONFIG_FTRACE=y
> diff --git a/tools/testing/selftests/ring-buffer/map_test.c b/tools/testing/selftests/ring-buffer/map_test.c
> new file mode 100644
> index 000000000000..49107e8da5e9
> --- /dev/null
> +++ b/tools/testing/selftests/ring-buffer/map_test.c
> @@ -0,0 +1,188 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Ring-buffer memory mapping tests
> + *
> + * Copyright (c) 2024 Vincent Donnefort <vdonnefort@google.com>
> + */
> +#include <fcntl.h>
> +#include <sched.h>
> +#include <stdbool.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <unistd.h>
> +
> +#include <linux/trace_mmap.h>
> +
> +#include <sys/mman.h>
> +#include <sys/ioctl.h>
> +
> +#include "../user_events/user_events_selftests.h" /* share tracefs setup */
> +#include "../kselftest_harness.h"
> +
> +#define TRACEFS_ROOT "/sys/kernel/tracing"
> +
> +static int __tracefs_write(const char *path, const char *value)
> +{
> +	FILE *file;
> +
> +	file = fopen(path, "w");
> +	if (!file)
> +		return -1;
> +
> +	fputs(value, file);
> +	fclose(file);
> +
> +	return 0;
> +}
> +
> +static int __tracefs_write_int(const char *path, int value)
> +{
> +	char *str;
> +	int ret;
> +
> +	if (asprintf(&str, "%d", value) < 0)
> +		return -1;
> +
> +	ret = __tracefs_write(path, str);
> +
> +	free(str);
> +
> +	return ret;
> +}
> +
> +#define tracefs_write_int(path, value) \
> +	ASSERT_EQ(__tracefs_write_int((path), (value)), 0)
> +
> +static int tracefs_reset(void)
> +{
> +	if (__tracefs_write_int(TRACEFS_ROOT"/tracing_on", 0))
> +		return -1;
> +	if (__tracefs_write_int(TRACEFS_ROOT"/trace", 0))
> +		return -1;
> +	if (__tracefs_write(TRACEFS_ROOT"/set_event", ""))
> +		return -1;
> +
> +	return 0;
> +}
> +
> +FIXTURE(map) {
> +	struct trace_buffer_meta	*meta;
> +	void				*data;
> +	int				cpu_fd;
> +	bool				umount;
> +};
> +
> +FIXTURE_VARIANT(map) {
> +	int	subbuf_size;
> +};
> +
> +FIXTURE_VARIANT_ADD(map, subbuf_size_4k) {
> +	.subbuf_size = 4,
> +};
> +
> +FIXTURE_VARIANT_ADD(map, subbuf_size_8k) {
> +	.subbuf_size = 8,
> +};
> +
> +FIXTURE_SETUP(map)
> +{
> +	int cpu = sched_getcpu(), page_size = getpagesize();
> +	unsigned long meta_len, data_len;
> +	char *cpu_path, *message;
> +	bool fail, umount;
> +	cpu_set_t cpu_mask;
> +	void *map;
> +
> +	if (!tracefs_enabled(&message, &fail, &umount)) {
> +		if (fail) {
> +			TH_LOG("Tracefs setup failed: %s", message);
> +			ASSERT_FALSE(fail);
> +		}
> +		SKIP(return, "Skipping: %s", message);
> +	}
> +
> +	self->umount = umount;
> +
> +	ASSERT_GE(cpu, 0);
> +
> +	ASSERT_EQ(tracefs_reset(), 0);
> +
> +	tracefs_write_int(TRACEFS_ROOT"/buffer_subbuf_size_kb", variant->subbuf_size);
> +
> +	ASSERT_GE(asprintf(&cpu_path,
> +			   TRACEFS_ROOT"/per_cpu/cpu%d/trace_pipe_raw",
> +			   cpu), 0);
> +
> +	self->cpu_fd = open(cpu_path, O_RDONLY | O_NONBLOCK);
> +	ASSERT_GE(self->cpu_fd, 0);
> +	free(cpu_path);
> +
> +	map = mmap(NULL, page_size, PROT_READ, MAP_SHARED, self->cpu_fd, 0);
> +	ASSERT_NE(map, MAP_FAILED);
> +	self->meta = (struct trace_buffer_meta *)map;
> +
> +	meta_len = self->meta->meta_page_size;
> +	data_len = self->meta->subbuf_size * self->meta->nr_subbufs;
> +
> +	map = mmap(NULL, data_len, PROT_READ, MAP_SHARED, self->cpu_fd, meta_len);
> +	ASSERT_NE(map, MAP_FAILED);
> +	self->data = map;
> +
> +	/*
> +	 * Ensure generated events will be found on this very same ring-buffer.
> +	 */
> +	CPU_ZERO(&cpu_mask);
> +	CPU_SET(cpu, &cpu_mask);
> +	ASSERT_EQ(sched_setaffinity(0, sizeof(cpu_mask), &cpu_mask), 0);
> +}
> +
> +FIXTURE_TEARDOWN(map)
> +{
> +	tracefs_reset();
> +
> +	if (self->umount)
> +		tracefs_unmount();
> +
> +	munmap(self->data, self->meta->subbuf_size * self->meta->nr_subbufs);
> +	munmap(self->meta, self->meta->meta_page_size);
> +	close(self->cpu_fd);
> +}
> +
> +TEST_F(map, meta_page_check)
> +{
> +	int cnt = 0;
> +
> +	ASSERT_EQ(self->meta->entries, 0);
> +	ASSERT_EQ(self->meta->overrun, 0);
> +	ASSERT_EQ(self->meta->read, 0);
> +	ASSERT_EQ(self->meta->subbufs_touched, 0);
> +	ASSERT_EQ(self->meta->subbufs_lost, 0);
> +	ASSERT_EQ(self->meta->subbufs_read, 0);
> +
> +	ASSERT_EQ(self->meta->reader.id, 0);
> +	ASSERT_EQ(self->meta->reader.read, 0);
> +
> +	ASSERT_EQ(ioctl(self->cpu_fd, TRACE_MMAP_IOCTL_GET_READER), 0);
> +	ASSERT_EQ(self->meta->reader.id, 0);
> +
> +	tracefs_write_int(TRACEFS_ROOT"/tracing_on", 1);
> +	for (int i = 0; i < 16; i++)
> +		tracefs_write_int(TRACEFS_ROOT"/trace_marker", i);
> +again:
> +	ASSERT_EQ(ioctl(self->cpu_fd, TRACE_MMAP_IOCTL_GET_READER), 0);
> +
> +	ASSERT_EQ(self->meta->entries, 16);
> +	ASSERT_EQ(self->meta->overrun, 0);
> +	ASSERT_EQ(self->meta->read, 16);
> +	/* subbufs_touched doesn't take into account the commit page */
> +	ASSERT_EQ(self->meta->subbufs_touched, 0);
> +	ASSERT_EQ(self->meta->subbufs_lost, 0);
> +	ASSERT_EQ(self->meta->subbufs_read, 1);
> +
> +	ASSERT_EQ(self->meta->reader.id, 1);
> +
> +	if (!(cnt++))
> +		goto again;
> +}
> +
> +TEST_HARNESS_MAIN
> -- 
> 2.43.0.275.g3460e3d667-goog
>
Masami Hiramatsu (Google) Jan. 14, 2024, 2:17 p.m. UTC | #2
Hi Vincent,

On Sat, 13 Jan 2024 22:39:46 +0900
Masami Hiramatsu (Google) <mhiramat@kernel.org> wrote:

> On Thu, 11 Jan 2024 16:17:12 +0000
> Vincent Donnefort <vdonnefort@google.com> wrote:
> 
> > This test maps a ring-buffer and validate the meta-page after reset and
> > after emitting few events.
> > 
> > Cc: Shuah Khan <shuah@kernel.org>
> > Cc: Shuah Khan <skhan@linuxfoundation.org>
> > Cc: linux-kselftest@vger.kernel.org
> > Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
> 
> Looks good to me and tested.
> 
> Reviewed-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
> Tested-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>

Sorry, I should cancel these. I found this test did not pass if I set
the function tracer on.

/ # cd /sys/kernel/tracing/
/sys/kernel/tracing # echo function > current_tracer 
/sys/kernel/tracing # /mnt/map_test 
TAP version 13
1..2
# Starting 2 tests from 2 test cases.
#  RUN           map.subbuf_size_4k.meta_page_check ...
# map_test.c:174:meta_page_check:Expected self->meta->entries (15293) == 16 (16)
# meta_page_check: Test terminated by assertion
#          FAIL  map.subbuf_size_4k.meta_page_check
not ok 1 map.subbuf_size_4k.meta_page_check
#  RUN           map.subbuf_size_8k.meta_page_check ...
# map_test.c:174:meta_page_check:Expected self->meta->entries (15270) == 16 (16)
# meta_page_check: Test terminated by assertion
#          FAIL  map.subbuf_size_8k.meta_page_check
not ok 2 map.subbuf_size_8k.meta_page_check
# FAILED: 0 / 2 tests passed.
# Totals: pass:0 fail:2 xfail:0 xpass:0 skip:0 error:0

Does this depend on "nop" tracer?

Thank you,

> 
> Thank you,
> 
> > 
> > diff --git a/tools/testing/selftests/ring-buffer/Makefile b/tools/testing/selftests/ring-buffer/Makefile
> > new file mode 100644
> > index 000000000000..627c5fa6d1ab
> > --- /dev/null
> > +++ b/tools/testing/selftests/ring-buffer/Makefile
> > @@ -0,0 +1,8 @@
> > +# SPDX-License-Identifier: GPL-2.0
> > +CFLAGS += -Wl,-no-as-needed -Wall
> > +CFLAGS += $(KHDR_INCLUDES)
> > +CFLAGS += -D_GNU_SOURCE
> > +
> > +TEST_GEN_PROGS = map_test
> > +
> > +include ../lib.mk
> > diff --git a/tools/testing/selftests/ring-buffer/config b/tools/testing/selftests/ring-buffer/config
> > new file mode 100644
> > index 000000000000..ef8214661612
> > --- /dev/null
> > +++ b/tools/testing/selftests/ring-buffer/config
> > @@ -0,0 +1 @@
> > +CONFIG_FTRACE=y
> > diff --git a/tools/testing/selftests/ring-buffer/map_test.c b/tools/testing/selftests/ring-buffer/map_test.c
> > new file mode 100644
> > index 000000000000..49107e8da5e9
> > --- /dev/null
> > +++ b/tools/testing/selftests/ring-buffer/map_test.c
> > @@ -0,0 +1,188 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +/*
> > + * Ring-buffer memory mapping tests
> > + *
> > + * Copyright (c) 2024 Vincent Donnefort <vdonnefort@google.com>
> > + */
> > +#include <fcntl.h>
> > +#include <sched.h>
> > +#include <stdbool.h>
> > +#include <stdio.h>
> > +#include <stdlib.h>
> > +#include <unistd.h>
> > +
> > +#include <linux/trace_mmap.h>
> > +
> > +#include <sys/mman.h>
> > +#include <sys/ioctl.h>
> > +
> > +#include "../user_events/user_events_selftests.h" /* share tracefs setup */
> > +#include "../kselftest_harness.h"
> > +
> > +#define TRACEFS_ROOT "/sys/kernel/tracing"
> > +
> > +static int __tracefs_write(const char *path, const char *value)
> > +{
> > +	FILE *file;
> > +
> > +	file = fopen(path, "w");
> > +	if (!file)
> > +		return -1;
> > +
> > +	fputs(value, file);
> > +	fclose(file);
> > +
> > +	return 0;
> > +}
> > +
> > +static int __tracefs_write_int(const char *path, int value)
> > +{
> > +	char *str;
> > +	int ret;
> > +
> > +	if (asprintf(&str, "%d", value) < 0)
> > +		return -1;
> > +
> > +	ret = __tracefs_write(path, str);
> > +
> > +	free(str);
> > +
> > +	return ret;
> > +}
> > +
> > +#define tracefs_write_int(path, value) \
> > +	ASSERT_EQ(__tracefs_write_int((path), (value)), 0)
> > +
> > +static int tracefs_reset(void)
> > +{
> > +	if (__tracefs_write_int(TRACEFS_ROOT"/tracing_on", 0))
> > +		return -1;
> > +	if (__tracefs_write_int(TRACEFS_ROOT"/trace", 0))
> > +		return -1;
> > +	if (__tracefs_write(TRACEFS_ROOT"/set_event", ""))
> > +		return -1;
> > +
> > +	return 0;
> > +}
> > +
> > +FIXTURE(map) {
> > +	struct trace_buffer_meta	*meta;
> > +	void				*data;
> > +	int				cpu_fd;
> > +	bool				umount;
> > +};
> > +
> > +FIXTURE_VARIANT(map) {
> > +	int	subbuf_size;
> > +};
> > +
> > +FIXTURE_VARIANT_ADD(map, subbuf_size_4k) {
> > +	.subbuf_size = 4,
> > +};
> > +
> > +FIXTURE_VARIANT_ADD(map, subbuf_size_8k) {
> > +	.subbuf_size = 8,
> > +};
> > +
> > +FIXTURE_SETUP(map)
> > +{
> > +	int cpu = sched_getcpu(), page_size = getpagesize();
> > +	unsigned long meta_len, data_len;
> > +	char *cpu_path, *message;
> > +	bool fail, umount;
> > +	cpu_set_t cpu_mask;
> > +	void *map;
> > +
> > +	if (!tracefs_enabled(&message, &fail, &umount)) {
> > +		if (fail) {
> > +			TH_LOG("Tracefs setup failed: %s", message);
> > +			ASSERT_FALSE(fail);
> > +		}
> > +		SKIP(return, "Skipping: %s", message);
> > +	}
> > +
> > +	self->umount = umount;
> > +
> > +	ASSERT_GE(cpu, 0);
> > +
> > +	ASSERT_EQ(tracefs_reset(), 0);
> > +
> > +	tracefs_write_int(TRACEFS_ROOT"/buffer_subbuf_size_kb", variant->subbuf_size);
> > +
> > +	ASSERT_GE(asprintf(&cpu_path,
> > +			   TRACEFS_ROOT"/per_cpu/cpu%d/trace_pipe_raw",
> > +			   cpu), 0);
> > +
> > +	self->cpu_fd = open(cpu_path, O_RDONLY | O_NONBLOCK);
> > +	ASSERT_GE(self->cpu_fd, 0);
> > +	free(cpu_path);
> > +
> > +	map = mmap(NULL, page_size, PROT_READ, MAP_SHARED, self->cpu_fd, 0);
> > +	ASSERT_NE(map, MAP_FAILED);
> > +	self->meta = (struct trace_buffer_meta *)map;
> > +
> > +	meta_len = self->meta->meta_page_size;
> > +	data_len = self->meta->subbuf_size * self->meta->nr_subbufs;
> > +
> > +	map = mmap(NULL, data_len, PROT_READ, MAP_SHARED, self->cpu_fd, meta_len);
> > +	ASSERT_NE(map, MAP_FAILED);
> > +	self->data = map;
> > +
> > +	/*
> > +	 * Ensure generated events will be found on this very same ring-buffer.
> > +	 */
> > +	CPU_ZERO(&cpu_mask);
> > +	CPU_SET(cpu, &cpu_mask);
> > +	ASSERT_EQ(sched_setaffinity(0, sizeof(cpu_mask), &cpu_mask), 0);
> > +}
> > +
> > +FIXTURE_TEARDOWN(map)
> > +{
> > +	tracefs_reset();
> > +
> > +	if (self->umount)
> > +		tracefs_unmount();
> > +
> > +	munmap(self->data, self->meta->subbuf_size * self->meta->nr_subbufs);
> > +	munmap(self->meta, self->meta->meta_page_size);
> > +	close(self->cpu_fd);
> > +}
> > +
> > +TEST_F(map, meta_page_check)
> > +{
> > +	int cnt = 0;
> > +
> > +	ASSERT_EQ(self->meta->entries, 0);
> > +	ASSERT_EQ(self->meta->overrun, 0);
> > +	ASSERT_EQ(self->meta->read, 0);
> > +	ASSERT_EQ(self->meta->subbufs_touched, 0);
> > +	ASSERT_EQ(self->meta->subbufs_lost, 0);
> > +	ASSERT_EQ(self->meta->subbufs_read, 0);
> > +
> > +	ASSERT_EQ(self->meta->reader.id, 0);
> > +	ASSERT_EQ(self->meta->reader.read, 0);
> > +
> > +	ASSERT_EQ(ioctl(self->cpu_fd, TRACE_MMAP_IOCTL_GET_READER), 0);
> > +	ASSERT_EQ(self->meta->reader.id, 0);
> > +
> > +	tracefs_write_int(TRACEFS_ROOT"/tracing_on", 1);
> > +	for (int i = 0; i < 16; i++)
> > +		tracefs_write_int(TRACEFS_ROOT"/trace_marker", i);
> > +again:
> > +	ASSERT_EQ(ioctl(self->cpu_fd, TRACE_MMAP_IOCTL_GET_READER), 0);
> > +
> > +	ASSERT_EQ(self->meta->entries, 16);
> > +	ASSERT_EQ(self->meta->overrun, 0);
> > +	ASSERT_EQ(self->meta->read, 16);
> > +	/* subbufs_touched doesn't take into account the commit page */
> > +	ASSERT_EQ(self->meta->subbufs_touched, 0);
> > +	ASSERT_EQ(self->meta->subbufs_lost, 0);
> > +	ASSERT_EQ(self->meta->subbufs_read, 1);
> > +
> > +	ASSERT_EQ(self->meta->reader.id, 1);
> > +
> > +	if (!(cnt++))
> > +		goto again;
> > +}
> > +
> > +TEST_HARNESS_MAIN
> > -- 
> > 2.43.0.275.g3460e3d667-goog
> > 
> 
> 
> -- 
> Masami Hiramatsu (Google) <mhiramat@kernel.org>
Steven Rostedt Jan. 14, 2024, 4:20 p.m. UTC | #3
On Sun, 14 Jan 2024 23:17:11 +0900
Masami Hiramatsu (Google) <mhiramat@kernel.org> wrote:
> > 
> > Looks good to me and tested.
> > 
> > Reviewed-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
> > Tested-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>  
> 
> Sorry, I should cancel these. I found this test did not pass if I set
> the function tracer on.
> 
> / # cd /sys/kernel/tracing/
> /sys/kernel/tracing # echo function > current_tracer 
> /sys/kernel/tracing # /mnt/map_test 
> TAP version 13
> 1..2
> # Starting 2 tests from 2 test cases.
> #  RUN           map.subbuf_size_4k.meta_page_check ...
> # map_test.c:174:meta_page_check:Expected self->meta->entries (15293) == 16 (16)
> # meta_page_check: Test terminated by assertion
> #          FAIL  map.subbuf_size_4k.meta_page_check
> not ok 1 map.subbuf_size_4k.meta_page_check
> #  RUN           map.subbuf_size_8k.meta_page_check ...
> # map_test.c:174:meta_page_check:Expected self->meta->entries (15270) == 16 (16)
> # meta_page_check: Test terminated by assertion
> #          FAIL  map.subbuf_size_8k.meta_page_check
> not ok 2 map.subbuf_size_8k.meta_page_check
> # FAILED: 0 / 2 tests passed.
> # Totals: pass:0 fail:2 xfail:0 xpass:0 skip:0 error:0
> 
> Does this depend on "nop" tracer?

Yes.

> > > +
> > > +static int tracefs_reset(void)
> > > +{
> > > +	if (__tracefs_write_int(TRACEFS_ROOT"/tracing_on", 0))
> > > +		return -1;
> > > +	if (__tracefs_write_int(TRACEFS_ROOT"/trace", 0))
> > > +		return -1;
> > > +	if (__tracefs_write(TRACEFS_ROOT"/set_event", ""))
> > > +		return -1;

Looks like the below is needed:

	if (__traces_write(TRACEFS_ROOT"/current_tracer", "nop"))
		return -1;

> > > +
> > > +	return 0;
> > > +}
> > > +

-- Steve
diff mbox series

Patch

diff --git a/tools/testing/selftests/ring-buffer/Makefile b/tools/testing/selftests/ring-buffer/Makefile
new file mode 100644
index 000000000000..627c5fa6d1ab
--- /dev/null
+++ b/tools/testing/selftests/ring-buffer/Makefile
@@ -0,0 +1,8 @@ 
+# SPDX-License-Identifier: GPL-2.0
+CFLAGS += -Wl,-no-as-needed -Wall
+CFLAGS += $(KHDR_INCLUDES)
+CFLAGS += -D_GNU_SOURCE
+
+TEST_GEN_PROGS = map_test
+
+include ../lib.mk
diff --git a/tools/testing/selftests/ring-buffer/config b/tools/testing/selftests/ring-buffer/config
new file mode 100644
index 000000000000..ef8214661612
--- /dev/null
+++ b/tools/testing/selftests/ring-buffer/config
@@ -0,0 +1 @@ 
+CONFIG_FTRACE=y
diff --git a/tools/testing/selftests/ring-buffer/map_test.c b/tools/testing/selftests/ring-buffer/map_test.c
new file mode 100644
index 000000000000..49107e8da5e9
--- /dev/null
+++ b/tools/testing/selftests/ring-buffer/map_test.c
@@ -0,0 +1,188 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Ring-buffer memory mapping tests
+ *
+ * Copyright (c) 2024 Vincent Donnefort <vdonnefort@google.com>
+ */
+#include <fcntl.h>
+#include <sched.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <linux/trace_mmap.h>
+
+#include <sys/mman.h>
+#include <sys/ioctl.h>
+
+#include "../user_events/user_events_selftests.h" /* share tracefs setup */
+#include "../kselftest_harness.h"
+
+#define TRACEFS_ROOT "/sys/kernel/tracing"
+
+static int __tracefs_write(const char *path, const char *value)
+{
+	FILE *file;
+
+	file = fopen(path, "w");
+	if (!file)
+		return -1;
+
+	fputs(value, file);
+	fclose(file);
+
+	return 0;
+}
+
+static int __tracefs_write_int(const char *path, int value)
+{
+	char *str;
+	int ret;
+
+	if (asprintf(&str, "%d", value) < 0)
+		return -1;
+
+	ret = __tracefs_write(path, str);
+
+	free(str);
+
+	return ret;
+}
+
+#define tracefs_write_int(path, value) \
+	ASSERT_EQ(__tracefs_write_int((path), (value)), 0)
+
+static int tracefs_reset(void)
+{
+	if (__tracefs_write_int(TRACEFS_ROOT"/tracing_on", 0))
+		return -1;
+	if (__tracefs_write_int(TRACEFS_ROOT"/trace", 0))
+		return -1;
+	if (__tracefs_write(TRACEFS_ROOT"/set_event", ""))
+		return -1;
+
+	return 0;
+}
+
+FIXTURE(map) {
+	struct trace_buffer_meta	*meta;
+	void				*data;
+	int				cpu_fd;
+	bool				umount;
+};
+
+FIXTURE_VARIANT(map) {
+	int	subbuf_size;
+};
+
+FIXTURE_VARIANT_ADD(map, subbuf_size_4k) {
+	.subbuf_size = 4,
+};
+
+FIXTURE_VARIANT_ADD(map, subbuf_size_8k) {
+	.subbuf_size = 8,
+};
+
+FIXTURE_SETUP(map)
+{
+	int cpu = sched_getcpu(), page_size = getpagesize();
+	unsigned long meta_len, data_len;
+	char *cpu_path, *message;
+	bool fail, umount;
+	cpu_set_t cpu_mask;
+	void *map;
+
+	if (!tracefs_enabled(&message, &fail, &umount)) {
+		if (fail) {
+			TH_LOG("Tracefs setup failed: %s", message);
+			ASSERT_FALSE(fail);
+		}
+		SKIP(return, "Skipping: %s", message);
+	}
+
+	self->umount = umount;
+
+	ASSERT_GE(cpu, 0);
+
+	ASSERT_EQ(tracefs_reset(), 0);
+
+	tracefs_write_int(TRACEFS_ROOT"/buffer_subbuf_size_kb", variant->subbuf_size);
+
+	ASSERT_GE(asprintf(&cpu_path,
+			   TRACEFS_ROOT"/per_cpu/cpu%d/trace_pipe_raw",
+			   cpu), 0);
+
+	self->cpu_fd = open(cpu_path, O_RDONLY | O_NONBLOCK);
+	ASSERT_GE(self->cpu_fd, 0);
+	free(cpu_path);
+
+	map = mmap(NULL, page_size, PROT_READ, MAP_SHARED, self->cpu_fd, 0);
+	ASSERT_NE(map, MAP_FAILED);
+	self->meta = (struct trace_buffer_meta *)map;
+
+	meta_len = self->meta->meta_page_size;
+	data_len = self->meta->subbuf_size * self->meta->nr_subbufs;
+
+	map = mmap(NULL, data_len, PROT_READ, MAP_SHARED, self->cpu_fd, meta_len);
+	ASSERT_NE(map, MAP_FAILED);
+	self->data = map;
+
+	/*
+	 * Ensure generated events will be found on this very same ring-buffer.
+	 */
+	CPU_ZERO(&cpu_mask);
+	CPU_SET(cpu, &cpu_mask);
+	ASSERT_EQ(sched_setaffinity(0, sizeof(cpu_mask), &cpu_mask), 0);
+}
+
+FIXTURE_TEARDOWN(map)
+{
+	tracefs_reset();
+
+	if (self->umount)
+		tracefs_unmount();
+
+	munmap(self->data, self->meta->subbuf_size * self->meta->nr_subbufs);
+	munmap(self->meta, self->meta->meta_page_size);
+	close(self->cpu_fd);
+}
+
+TEST_F(map, meta_page_check)
+{
+	int cnt = 0;
+
+	ASSERT_EQ(self->meta->entries, 0);
+	ASSERT_EQ(self->meta->overrun, 0);
+	ASSERT_EQ(self->meta->read, 0);
+	ASSERT_EQ(self->meta->subbufs_touched, 0);
+	ASSERT_EQ(self->meta->subbufs_lost, 0);
+	ASSERT_EQ(self->meta->subbufs_read, 0);
+
+	ASSERT_EQ(self->meta->reader.id, 0);
+	ASSERT_EQ(self->meta->reader.read, 0);
+
+	ASSERT_EQ(ioctl(self->cpu_fd, TRACE_MMAP_IOCTL_GET_READER), 0);
+	ASSERT_EQ(self->meta->reader.id, 0);
+
+	tracefs_write_int(TRACEFS_ROOT"/tracing_on", 1);
+	for (int i = 0; i < 16; i++)
+		tracefs_write_int(TRACEFS_ROOT"/trace_marker", i);
+again:
+	ASSERT_EQ(ioctl(self->cpu_fd, TRACE_MMAP_IOCTL_GET_READER), 0);
+
+	ASSERT_EQ(self->meta->entries, 16);
+	ASSERT_EQ(self->meta->overrun, 0);
+	ASSERT_EQ(self->meta->read, 16);
+	/* subbufs_touched doesn't take into account the commit page */
+	ASSERT_EQ(self->meta->subbufs_touched, 0);
+	ASSERT_EQ(self->meta->subbufs_lost, 0);
+	ASSERT_EQ(self->meta->subbufs_read, 1);
+
+	ASSERT_EQ(self->meta->reader.id, 1);
+
+	if (!(cnt++))
+		goto again;
+}
+
+TEST_HARNESS_MAIN