From patchwork Tue Jul 23 22:07:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 13740393 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0950B1448D8 for ; Tue, 23 Jul 2024 22:08:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721772518; cv=none; b=spROo9+tuFspNkXDX0CxWqdYxUqfOKHxnRMMgFkAMoLQG9MSQNNhdn/3JHkK/Rm8iY8UbI9iJux7yKYzsmF0Os5NXSXK8CQgFggGfbYzJ8bA4+Z8vPsBhAwpg+PiupFkJTELlPGslZFYyTNUbQ51DlMjU7NpWHA0Octaaof4RNo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721772518; c=relaxed/simple; bh=ICudxA5cKjcIndP05XQI+uWbSyR2pzGGZzY57Q61E2Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rNah3vksgRzKAzq6RkxLBUsXPB7nHvPRZKa/cD4oWye93MugtxG6KUZ/r/pNF7i06RHzfj0Z3CMDvViZkkb57uaRPYqJvzXK61UBNFzVRaQZ3stM92Sq5le2V8AVnJ/PaipQldXjIOr5W4WAtAEqmYbGLuCiUxSo64ezcU66Hpw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id C3B12C4AF0A; Tue, 23 Jul 2024 22:08:37 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.98) (envelope-from ) id 1sWNgk-000000023Fy-2x1t; Tue, 23 Jul 2024 18:08:54 -0400 From: Steven Rostedt To: linux-trace-devel@vger.kernel.org Cc: "Steven Rostedt (Google)" Subject: [PATCH 1/3] libtracefs: Enable mmapped ring buffer Date: Tue, 23 Jul 2024 18:07:23 -0400 Message-ID: <20240723220853.489058-2-rostedt@goodmis.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240723220853.489058-1-rostedt@goodmis.org> References: <20240723220853.489058-1-rostedt@goodmis.org> Precedence: bulk X-Mailing-List: linux-trace-devel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: "Steven Rostedt (Google)" 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. Signed-off-by: Steven Rostedt (Google) --- src/tracefs-mmap.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/tracefs-mmap.c b/src/tracefs-mmap.c index a0130bbc1cbe..44d1597f1ba5 100644 --- a/src/tracefs-mmap.c +++ b/src/tracefs-mmap.c @@ -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) From patchwork Tue Jul 23 22:07:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 13740392 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 094C914430D for ; Tue, 23 Jul 2024 22:08:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721772518; cv=none; b=RMsYzu9mf5JE+VMIJ5xPVg/+2ra+vvzPUp7oNwk7AKDTBCb11BlR5z7otopWTqTQY97yumapghd+G3vNUzwWrSplv2jCKPTIha+Ush3zVpACZlDLVPBY8rT6ywFSiQndYJbvtxOFECBsocaN30GVpWRPG0+KlOQXs87jR4qm3YQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721772518; c=relaxed/simple; bh=Fca6sW65qSo/PW/fmQkM8UOqrKriRvFkOT5nMXiIwB0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BmZAEKzsG2fYFDiLtG7L7hXNz9H05pBWhDo4rCd/NdIlCH4/O1xCTHc/opJCFzGSdbAXQ38vpUA8TRYZGHqYzFjH67TE3SHp6e7ejDYDBzIWQN+RCbpCPL0fdPbZ868fdlvyh0v3YWEaHsVPgA5Vvy+xUpJhBmFpYdhP9GUtK4w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id C5BCCC4AF10; Tue, 23 Jul 2024 22:08:37 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.98) (envelope-from ) id 1sWNgk-000000023G1-34VA; Tue, 23 Jul 2024 18:08:54 -0400 From: Steven Rostedt To: linux-trace-devel@vger.kernel.org Cc: "Steven Rostedt (Google)" Subject: [PATCH 2/3] libtracefs: Add cpu-map sample to trace mapped buffer Date: Tue, 23 Jul 2024 18:07:24 -0400 Message-ID: <20240723220853.489058-3-rostedt@goodmis.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240723220853.489058-1-rostedt@goodmis.org> References: <20240723220853.489058-1-rostedt@goodmis.org> Precedence: bulk X-Mailing-List: linux-trace-devel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: "Steven Rostedt (Google)" Add a sample program that reads the trace buffer with the mapped open. Signed-off-by: Steven Rostedt (Google) --- samples/Makefile | 2 ++ samples/cpu-map.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 samples/cpu-map.c diff --git a/samples/Makefile b/samples/Makefile index 81c8006f823e..7b68ae7ad34c 100644 --- a/samples/Makefile +++ b/samples/Makefile @@ -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) diff --git a/samples/cpu-map.c b/samples/cpu-map.c new file mode 100644 index 000000000000..b42742d8d073 --- /dev/null +++ b/samples/cpu-map.c @@ -0,0 +1,90 @@ +#include +#include +#include + +static void read_subbuf(struct tep_handle *tep, struct kbuffer *kbuf) +{ + static struct trace_seq seq; + struct tep_record record; + int missed_events; + + if (seq.buffer) + trace_seq_reset(&seq); + else + trace_seq_init(&seq); + + while ((record.data = kbuffer_read_event(kbuf, &record.ts))) { + record.size = kbuffer_event_size(kbuf); + missed_events = kbuffer_missed_events(kbuf); + if (missed_events) { + printf("[MISSED EVENTS"); + if (missed_events > 0) + printf(": %d]\n", missed_events); + else + printf("]\n"); + } + kbuffer_next_event(kbuf, NULL); + tep_print_event(tep, &seq, &record, + "%s-%d %6.1000d\t%s: %s\n", + TEP_PRINT_COMM, + TEP_PRINT_PID, + TEP_PRINT_TIME, + TEP_PRINT_NAME, + TEP_PRINT_INFO); + trace_seq_do_printf(&seq); + trace_seq_reset(&seq); + } +} + +int main (int argc, char **argv) +{ + struct tracefs_cpu *tcpu; + struct tep_handle *tep; + struct kbuffer *kbuf; + bool mapped; + int cpu; + + if (argc < 2 || !isdigit(argv[1][0])) { + printf("usage: %s cpu\n\n", argv[0]); + exit(-1); + } + + cpu = atoi(argv[1]); + + tep = tracefs_local_events(NULL); + if (!tep) { + perror("Reading trace event formats"); + exit(-1); + } + + tcpu = tracefs_cpu_open_mapped(NULL, cpu, 0); + if (!tcpu) { + perror("Open CPU 0 file"); + exit(-1); + } + + /* + * If this kernel supports mapping, use normal read, + * otherwise use the piped buffer read, although if + * the mapping succeeded, tracefs_cpu_buffered_read_buf() + * acts the same as tracefs_cpu_read_buf(). But this is just + * an example on how to use tracefs_cpu_is_mapped(). + */ + mapped = tracefs_cpu_is_mapped(tcpu); + if (!mapped) + printf("Was not able to map, falling back to buffered read\n"); + while ((kbuf = mapped ? tracefs_cpu_read_buf(tcpu, true) : + tracefs_cpu_buffered_read_buf(tcpu, true))) { + read_subbuf(tep, kbuf); + } + + kbuf = tracefs_cpu_flush_buf(tcpu); + if (kbuf) + read_subbuf(tep, kbuf); + + tracefs_cpu_close(tcpu); + tep_free(tep); + + return 0; +} + From patchwork Tue Jul 23 22:07:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 13740394 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0954C145A0F for ; Tue, 23 Jul 2024 22:08:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721772518; cv=none; b=js1jY3hV1ZMYne/shBxDIRoOdSLJFf8Dh5bGptxdcD8uspxPHzaHLGx0wPPF1DFAQqBsZ8rHNA1MyFQ5tq4ceSPTSxPf0PXnNxf2rsAs1S8BANiMLLc9ZlN1g9ZVGM7XUAJwNfm7QrAOcGH/YUHCpIRlMwJt87MMpIrVCKHq2P8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721772518; c=relaxed/simple; bh=dtBLdVSzJpLi/+M+5PbcpG0LVHsfLgEiy1w6nfQOc6A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mQtpXrLxo6crzlRD31mpopYvyWZYiXsnfSoEJToG0yK5XCZhnhRw1/P3bStPT5nffpQbIhIRuZBO+f3ypJqgYF4m/TR/uuoGWDQLUTZim/hmYYbAElFnZbo6cD+RW/hLuRwjL4DIl+2DHNmUQ0rBg1JgCSLK7yxl9OGFAreFzUM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id BE033C4AF09; Tue, 23 Jul 2024 22:08:37 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.98) (envelope-from ) id 1sWNgk-000000023G4-3BNq; Tue, 23 Jul 2024 18:08:54 -0400 From: Steven Rostedt To: linux-trace-devel@vger.kernel.org Cc: "Steven Rostedt (Google)" Subject: [PATCH 3/3] libtracefs utest: Add better logic to cause missed events Date: Tue, 23 Jul 2024 18:07:25 -0400 Message-ID: <20240723220853.489058-4-rostedt@goodmis.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240723220853.489058-1-rostedt@goodmis.org> References: <20240723220853.489058-1-rostedt@goodmis.org> Precedence: bulk X-Mailing-List: linux-trace-devel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: "Steven Rostedt (Google)" 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(). Signed-off-by: Steven Rostedt (Google) --- utest/tracefs-utest.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/utest/tracefs-utest.c b/utest/tracefs-utest.c index 737e54a4f7fe..b29525335116 100644 --- a/utest/tracefs-utest.c +++ b/utest/tracefs-utest.c @@ -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);