From nobody Sun Feb 8 12:37:06 2026 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 898467FBD2 for ; Wed, 21 Feb 2024 14:06:41 +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=1708524401; cv=none; b=iZOxUubK57ZWb1X8bAtv3JDTqvWQ0Ftrh8nbLLGJ4l021TGLlb1zsEiK006THr1Q4ame4xBGwDkV+A4KUG6eeIYD1CLAE72IxYjQG6SxeFneXXiVMkjOjFU7TRHHYssx992SHCAFTvHJbojWJS/0HqHRWIPv5ELFxJhPXxnk/RI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708524401; c=relaxed/simple; bh=tDXX+89cw4rUZmzOH4jcwXQ5+OeFPNYHS0TeN35uyoE=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=c9gYXQ/MG860oGpjx0uh7UU78oHKfRfcQYnL5K3neXE/+H1ErADSuL9tbDdpTI0P1yjxRlmdlU5yqGdoeybxC60XXAK5ryyEvBITZYMM6pO1X5ni1HR/gYiSjaNvCjmOI2VHantEHtUNiV5Y/d2pp+2Rz20hM715xgR+g7nz2lY= 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 526DEC433C7; Wed, 21 Feb 2024 14:06:41 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rcnGv-00000002iBp-2VHd; Wed, 21 Feb 2024 09:08:29 -0500 Message-ID: <20240221140829.460739161@goodmis.org> User-Agent: quilt/0.67 Date: Wed, 21 Feb 2024 09:08:06 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Vincent Donnefort Subject: [for-next][PATCH 10/11] Documentation: tracing: Add ring-buffer mapping References: <20240221140756.797572998@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Vincent Donnefort It is now possible to mmap() a ring-buffer to stream its content. Add some documentation and a code example. Link: https://lore.kernel.org/linux-trace-kernel/20240220202310.2489614-6-v= donnefort@google.com Signed-off-by: Vincent Donnefort Signed-off-by: Steven Rostedt (Google) --- Documentation/trace/index.rst | 1 + Documentation/trace/ring-buffer-map.rst | 106 ++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 Documentation/trace/ring-buffer-map.rst diff --git a/Documentation/trace/index.rst b/Documentation/trace/index.rst index 5092d6c13af5..0b300901fd75 100644 --- a/Documentation/trace/index.rst +++ b/Documentation/trace/index.rst @@ -29,6 +29,7 @@ Linux Tracing Technologies timerlat-tracer intel_th ring-buffer-design + ring-buffer-map stm sys-t coresight/index diff --git a/Documentation/trace/ring-buffer-map.rst b/Documentation/trace/= ring-buffer-map.rst new file mode 100644 index 000000000000..0426ab4bcf3d --- /dev/null +++ b/Documentation/trace/ring-buffer-map.rst @@ -0,0 +1,106 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +Tracefs ring-buffer memory mapping +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +:Author: Vincent Donnefort + +Overview +=3D=3D=3D=3D=3D=3D=3D=3D +Tracefs ring-buffer memory map provides an efficient method to stream data +as no memory copy is necessary. The application mapping the ring-buffer be= comes +then a consumer for that ring-buffer, in a similar fashion to trace_pipe. + +Memory mapping setup +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +The mapping works with a mmap() of the trace_pipe_raw interface. + +The first system page of the mapping contains ring-buffer statistics and +description. It is referred as the meta-page. One of the most important fi= eld of +the meta-page is the reader. It contains the sub-buffer ID which can be sa= fely +read by the mapper (see ring-buffer-design.rst). + +The meta-page is followed by all the sub-buffers, ordered by ascendant ID.= It is +therefore effortless to know where the reader starts in the mapping: + +.. code-block:: c + + reader_id =3D meta->reader->id; + reader_offset =3D meta->meta_page_size + reader_id * meta->subbuf_= size; + +When the application is done with the current reader, it can get a new one= using +the trace_pipe_raw ioctl() TRACE_MMAP_IOCTL_GET_READER. This ioctl also up= dates +the meta-page fields. + +Limitations +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +When a mapping is in place on a Tracefs ring-buffer, it is not possible to +either resize it (either by increasing the entire size of the ring-buffer = or +each subbuf). It is also not possible to use snapshot and causes splice to= copy +the ring buffer data instead of using the copyless swap from the ring buff= er. + +Concurrent readers (either another application mapping that ring-buffer or= the +kernel with trace_pipe) are allowed but not recommended. They will compete= for +the ring-buffer and the output is unpredictable, just like concurrent read= ers on +trace_pipe would be. + +Example +=3D=3D=3D=3D=3D=3D=3D + +.. code-block:: c + + #include + #include + #include + #include + + #include + + #include + #include + + #define TRACE_PIPE_RAW "/sys/kernel/tracing/per_cpu/cpu0/trace_pip= e_raw" + + int main(void) + { + int page_size =3D getpagesize(), fd, reader_id; + unsigned long meta_len, data_len; + struct trace_buffer_meta *meta; + void *map, *reader, *data; + + fd =3D open(TRACE_PIPE_RAW, O_RDONLY | O_NONBLOCK); + if (fd < 0) + exit(EXIT_FAILURE); + + map =3D mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0= ); + if (map =3D=3D MAP_FAILED) + exit(EXIT_FAILURE); + + meta =3D (struct trace_buffer_meta *)map; + meta_len =3D meta->meta_page_size; + + printf("entries: %llu\n", meta->entries); + printf("overrun: %llu\n", meta->overrun); + printf("read: %llu\n", meta->read); + printf("nr_subbufs: %u\n", meta->nr_subbufs); + + data_len =3D meta->subbuf_size * meta->nr_subbufs; + data =3D mmap(NULL, data_len, PROT_READ, MAP_SHARED, fd, m= eta_len); + if (data =3D=3D MAP_FAILED) + exit(EXIT_FAILURE); + + if (ioctl(fd, TRACE_MMAP_IOCTL_GET_READER) < 0) + exit(EXIT_FAILURE); + + reader_id =3D meta->reader.id; + reader =3D data + meta->subbuf_size * reader_id; + + printf("Current reader address: %p\n", reader); + + munmap(data, data_len); + munmap(meta, meta_len); + close (fd); + + return 0; + } --=20 2.43.0