Skip to contentSkip to conversation

libtracefs: Enable ring buffer user space memory mapping

PATCH New Steven Rostedt 1 participant 4 messages Jul 23, 2024

lore
Share & tools
lore
#0 lore raw

cover libtracefs: Enable ring buffer user space memory mapping

From: "Steven Rostedt (Google)" <[email protected]>

Now that the ring buffer memory mapping is officially part of the Linux Kernel as of v6.10, enable it in the library.

Also add sample code to use it and make a fix in the self tests.

Steven Rostedt (Google) (3):
libtracefs: Enable mmapped ring buffer
libtracefs: Add cpu-map sample to trace mapped buffer libtracefs utest: Add better logic to cause missed events

 samples/Makefile      |  2 +
 samples/cpu-map.c     | 90 +++++++++++++++++++++++++++++++++++++++++++
 src/tracefs-mmap.c    | 10 ++---
 utest/tracefs-utest.c | 15 +++++++-
 4 files changed, 108 insertions(+), 9 deletions(-)
 create mode 100644 samples/cpu-map.c
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/3 libtracefs: Enable mmapped ring buffer

From: "Steven Rostedt (Google)" <[email protected]>

As of v6.10, ring buffer user space mapping is now supported by the Linux kernel. This also means that it is an official API. Copy the latest API from that kernel into the trace_mmap.h file and make it accessible to user space programs.

---
 src/tracefs-mmap.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
src/tracefs-mmap.c +3 -7
index a0130bbc1cbe..44d1597f1ba5 100644
@@ -22,8 +22,8 @@
  * @entries:		Number of entries in the ring-buffer.
  * @overrun:		Number of entries lost in the ring-buffer.
  * @read:		Number of entries that have been read.
- * @Reserved1:		Reserved for future use.
- * @Reserved2:		Reserved for future use.
+ * @Reserved1:		Internal use only.
+ * @Reserved2:		Internal use only.
  */
 struct trace_buffer_meta {
 	__u32		meta_page_size;
@@ -48,7 +48,7 @@ struct trace_buffer_meta {
 	__u64	Reserved2;
 };
 
-#define TRACE_MMAP_IOCTL_GET_READER		_IO('T', 0x1)
+#define TRACE_MMAP_IOCTL_GET_READER		_IO('R', 0x20)
 
 struct trace_mmap {
 	struct trace_buffer_meta	*map;
@@ -78,10 +78,6 @@ __hidden void *trace_mmap(int fd, struct kbuffer *kbuf)
 	void *meta;
 	void *data;
 
-#ifndef FORCE_MMAP_ENABLE
-	return NULL;
-#endif
-
 	page_size = getpagesize();
 	meta = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0);
 	if (meta == MAP_FAILED)
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/3 libtracefs: Add cpu-map sample to trace mapped buffer

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

Add a sample program that reads the trace buffer with the mapped open.

---
 samples/Makefile  |  2 ++
 samples/cpu-map.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 92 insertions(+)
 create mode 100644 samples/cpu-map.c
samples/Makefile +2 -0
index 81c8006f823e..7b68ae7ad34c 100644
@@ -67,6 +67,8 @@ $(EXAMPLES): $(patsubst %,$(sdir)/%,$(TARGETS))
 #
 # $(bdir)/XX.o: $(bdir)/XX.c
 #	$(CC) -g -Wall $(CFLAGS) -c -o $@ $^ -I../include/ $(LIBTRACEEVENT_INCLUDES)
+$(bdir)/cpu-map.o: $(bdir)/cpu-map.c
+	$(CC) -g -Wall $(CFLAGS) -c -o $@ $^ -I../include/ $(LIBTRACEEVENT_INCLUDES)
 $(bdir)/kprobes.o: $(bdir)/kprobes.c
 	$(CC) -g -Wall $(CFLAGS) -c -o $@ $^ -I../include/ $(LIBTRACEEVENT_INCLUDES)
 
samples/cpu-map.c new file +90 -0

90 lines not on this page · raw message

Signature · 1 line
2.43.0
#3 lore raw

PATCH 3/3 libtracefs utest: Add better logic to cause missed events

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

When the system is running without much debug, it is possible to run the function tracer without triggering missed events. As the tests are expecting to have missed events to test the missed events handler, it gfails the test because no missed events happen, and the missed events handler is correctly not called. But the tests expect it to be called.

Add more logic to force missed events.

* Shrink the buffer to just 4 pages.
* Run "ls -l /usr/bin > /dev/null" instead of sleep().

---
 utest/tracefs-utest.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
utest/tracefs-utest.c +13 -2
index 737e54a4f7fe..b29525335116 100644
@@ -1576,6 +1576,8 @@ static void test_instance_follow_events_clear(struct tracefs_instance *instance)
 {
 	struct follow_data fdata;
 	struct tep_handle *tep;
+	unsigned long page_size;
+	size_t save_size;
 	char **list;
 	int ret;
 
@@ -1671,7 +1673,7 @@ static void test_instance_follow_events_clear(struct tracefs_instance *instance)
 
 	tracefs_trace_on(instance);
 	call_getppid(100);
-	msleep(100);
+	system("ls -l /usr/bin > /dev/null");
 	tracefs_trace_off(instance);
 
 	ret = tracefs_iterate_raw_events(tep, instance, NULL, 0, NULL, &fdata);
@@ -1695,17 +1697,26 @@ static void test_instance_follow_events_clear(struct tracefs_instance *instance)
 	if (!fdata.function)
 		return;
 
+	/* Shrink the buffer to make sure we have missed events */
+	page_size = getpagesize();
+	save_size = tracefs_instance_get_buffer_size(instance, 0);
+	ret = tracefs_instance_set_buffer_size(instance, page_size * 4, 0);
+	CU_TEST(ret == 0);
+
 	tracefs_trace_on(instance);
 	call_getppid(100);
 	/* Stir the kernel a bit */
 	list = tracefs_event_systems(NULL);
 	tracefs_list_free(list);
-	sleep(1);
+	system("ls -l /usr/bin > /dev/null");
 	tracefs_trace_off(instance);
 
 	ret = tracefs_iterate_raw_events(tep, instance, NULL, 0, NULL, &fdata);
 	CU_TEST(ret == 0);
 
+	ret = tracefs_instance_set_buffer_size(instance, save_size, 0);
+	CU_TEST(ret == 0);
+
 	/* Nothing should have been hit */
 	CU_TEST(fdata.switch_hit == 0);
 	CU_TEST(fdata.waking_hit == 0);
Signature · 1 line
2.43.0