linux-trace-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: linux-trace-devel@vger.kernel.org
Cc: Vincent Donnefort <vdonnefort@google.com>,
	"Steven Rostedt (Google)" <rostedt@goodmis.org>
Subject: [PATCH 3/7] libtracefs: Have nonblock tracefs_cpu reads set errno EAGAIN
Date: Tue,  9 Jan 2024 21:51:47 -0500	[thread overview]
Message-ID: <20240110030116.81837-4-rostedt@goodmis.org> (raw)
In-Reply-To: <20240110030116.81837-1-rostedt@goodmis.org>

From: "Steven Rostedt (Google)" <rostedt@goodmis.org>

If ring buffer memory mapping is used, and nothing is available to read on
the buffer, and nonblock is set, still update errno to EAGAIN.

Also always return subbuf_size when any data was read, and zero out the
rest of the buffer just like the kernel would do.

Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 src/tracefs-record.c | 25 ++++++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)

diff --git a/src/tracefs-record.c b/src/tracefs-record.c
index 881f49256dc2..59260da0e32c 100644
--- a/src/tracefs-record.c
+++ b/src/tracefs-record.c
@@ -402,6 +402,25 @@ static int wait_on_input(struct tracefs_cpu *tcpu, bool nonblock)
 	return FD_ISSET(tcpu->fd, &rfds);
 }
 
+/* If nonblock is set, set errno to EAGAIN on no data */
+static int mmap_read(struct tracefs_cpu *tcpu, void *buffer, bool nonblock)
+{
+	void *mapping = tcpu->mapping;
+	int ret;
+
+	ret = trace_mmap_read(mapping, buffer);
+	if (ret <= 0) {
+		if (!ret && nonblock)
+			errno = EAGAIN;
+		return ret;
+	}
+
+	/* Write full sub-buffer size, but zero out empty space */
+	if (ret < tcpu->subbuf_size)
+		memset(buffer + ret, 0, tcpu->subbuf_size - ret);
+	return tcpu->subbuf_size;
+}
+
 /**
  * tracefs_cpu_read - read from the raw trace file
  * @tcpu: The descriptor representing the raw trace file
@@ -433,7 +452,7 @@ int tracefs_cpu_read(struct tracefs_cpu *tcpu, void *buffer, bool nonblock)
 		return ret;
 
 	if (tcpu->mapping)
-		return trace_mmap_read(tcpu->mapping, buffer);
+		return mmap_read(tcpu, buffer, nonblock);
 
 	ret = read(tcpu->fd, buffer, tcpu->subbuf_size);
 
@@ -572,7 +591,7 @@ int tracefs_cpu_buffered_read(struct tracefs_cpu *tcpu, void *buffer, bool nonbl
 		return ret;
 
 	if (tcpu->mapping)
-		return trace_mmap_read(tcpu->mapping, buffer);
+		return mmap_read(tcpu, buffer, nonblock);
 
 	if (tcpu->flags & TC_NONBLOCK)
 		mode |= SPLICE_F_NONBLOCK;
@@ -704,7 +723,7 @@ int tracefs_cpu_flush(struct tracefs_cpu *tcpu, void *buffer)
 		tcpu->buffered = 0;
 
 	if (tcpu->mapping)
-		return trace_mmap_read(tcpu->mapping, buffer);
+		return mmap_read(tcpu, buffer, false);
 
 	if (tcpu->buffered) {
 		ret = read(tcpu->splice_pipe[0], buffer, tcpu->subbuf_size);
-- 
2.43.0


  parent reply	other threads:[~2024-01-10  3:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-10  2:51 [PATCH 0/7] libtracefs: More fixes for memory mapping ring buffer API Steven Rostedt
2024-01-10  2:51 ` [PATCH 1/7] libtracefs: Have mapping work with the other tracefs_cpu* functions Steven Rostedt
2024-01-10  2:51 ` [PATCH 2/7] libtracefs: Have tracefs_mmap_read() include subbuf meta data Steven Rostedt
2024-01-10  2:51 ` Steven Rostedt [this message]
2024-01-10  2:51 ` [PATCH 4/7] libtracefs: Fix tracefs_mmap() kbuf usage Steven Rostedt
2024-01-10  2:51 ` [PATCH 5/7] libtracefs: Call mmap ioctl if a refresh happens Steven Rostedt
2024-01-10  2:51 ` [PATCH 6/7] libtracefs: Add tracefs_mapped_is_supported() API Steven Rostedt
2024-01-10  2:51 ` [PATCH 7/7] libtracefs utest: Add tests to use mapping if supported Steven Rostedt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240110030116.81837-4-rostedt@goodmis.org \
    --to=rostedt@goodmis.org \
    --cc=linux-trace-devel@vger.kernel.org \
    --cc=vdonnefort@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).