Received: by 2002:a05:7412:8d11:b0:fa:4934:9f with SMTP id bj17csp105989rdb; Sun, 14 Jan 2024 08:23:35 -0800 (PST) X-Google-Smtp-Source: AGHT+IHoxkFG5SOTRqYLv9Yc0c+X0000FHUivgK2vdCaOfkSqiUyhz4LI5AmmV33nrcNoPfh7/oy X-Received: by 2002:a17:906:4956:b0:a28:fc8b:6942 with SMTP id f22-20020a170906495600b00a28fc8b6942mr1922176ejt.9.1705249415617; Sun, 14 Jan 2024 08:23:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1705249415; cv=none; d=google.com; s=arc-20160816; b=dti0wcceyZTIINDzX931jo2+Dht8+UHCIZnMd+l6i3TsNzE23hVU2oU/EjLSrDZJX7 T2oc5PUt0rryP453HofV5eqfj2yyyWOn/rQnFigKdhnIJb581HoDJyi8NW6zcpJO82y4 gjyt3dXKZlbvSXIvS6EUrvM1N9A/0NBlDaneDGVvbmlYCfpZoaWySW0MvMYQPB76RJ3l qMjfQi480R/N346ABW41JClg16pCwB84zA9OtPUL2xV5odMdmvCMAw2rMG4KStUj/iCI SIHHvnultlKlO65Yxukej2T87B3ebmoIpIJKiD+C5J86MvpYExBS8pUfTjciO1FaD9IT o95w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :subject:cc:to:from:date; bh=ltADFIefDaiJKtb5SCWFjpgwsDbuxsalVNhItUVe57A=; fh=RgzLGBoasBFBaYzygqDfqjTbi/jF4k8V6Y1cJQMO4QE=; b=F0AfvCgQapN5ottv06ACSSUj1XPt8V2/CpOJbSe98gcKF0doTunX/gI8oKnM+DuZKv g7IAuz2HLIqenRWXiFn+Eg0l2fNkrH751QV5rkhHIz57ppTouBwG4JG7QQMtthzgzif/ 4KAP6HKbG/9vkiPa7xZ6fJxdzf4hM6qry7Q5P/DkNdkYPtP3PES0fZP32t4WCZ7BlJ/s M4vellWXo5pXp5vMMEdxZLb7Fqx0NBPtCN/BuVDPbkYtrHh++uMfEvBoYBErE+JnTBPU /AaONWPVKCO/1gjEV/yzhEd4zWKPaJJolUIpZ7be/mH5U9OEAVG7iwzo8YF2lyQePLVd 7oQQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-25511-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-25511-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id ka14-20020a170907990e00b00a2a12e01927si3036238ejc.279.2024.01.14.08.23.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Jan 2024 08:23:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-25511-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-25511-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-25511-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 2A1181F2165B for ; Sun, 14 Jan 2024 16:23:35 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B9CC1539C; Sun, 14 Jan 2024 16:23:27 +0000 (UTC) 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 3606A5384; Sun, 14 Jan 2024 16:23:26 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 01716C433F1; Sun, 14 Jan 2024 16:23:25 +0000 (UTC) Date: Sun, 14 Jan 2024 11:23:24 -0500 From: Steven Rostedt To: "Masami Hiramatsu (Google)" Cc: Vincent Donnefort , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, mathieu.desnoyers@efficios.com, kernel-team@android.com Subject: Re: [PATCH v11 4/5] Documentation: tracing: Add ring-buffer mapping Message-ID: <20240114112324.0ddbb457@rorschach.local.home> In-Reply-To: <20240114232643.ed27554959afea426446e9b5@kernel.org> References: <20240111161712.1480333-1-vdonnefort@google.com> <20240111161712.1480333-5-vdonnefort@google.com> <20240114232643.ed27554959afea426446e9b5@kernel.org> X-Mailer: Claws Mail 3.17.8 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Sun, 14 Jan 2024 23:26:43 +0900 Masami Hiramatsu (Google) wrote: > Hi Vincent, > > On Thu, 11 Jan 2024 16:17:11 +0000 > Vincent Donnefort wrote: > > > It is now possible to mmap() a ring-buffer to stream its content. Add > > some documentation and a code example. > > > > Signed-off-by: Vincent Donnefort > > > > 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..2ba7b5339178 > > --- /dev/null > > +++ b/Documentation/trace/ring-buffer-map.rst > > @@ -0,0 +1,105 @@ > > +.. SPDX-License-Identifier: GPL-2.0 > > + > > +================================== > > +Tracefs ring-buffer memory mapping > > +================================== > > + > > +:Author: Vincent Donnefort > > + > > +Overview > > +======== > > +Tracefs ring-buffer memory map provides an efficient method to stream data > > +as no memory copy is necessary. The application mapping the ring-buffer becomes > > +then a consumer for that ring-buffer, in a similar fashion to trace_pipe. > > + > > +Memory mapping setup > > +==================== > > +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 field of > > +the meta-page is the reader. It contains the subbuf ID which can be safely read > > +by the mapper (see ring-buffer-design.rst). > > + > > +The meta-page is followed by all the subbuf, ordered by ascendant ID. It is > > +therefore effortless to know where the reader starts in the mapping: > > + > > +.. code-block:: c > > + > > + reader_id = meta->reader->id; > > + reader_offset = 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 updates > > +the meta-page fields. > > + > > +Limitations > > +=========== > > +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 or splice. > > I've played with the sample code. > > - "free_buffer" just doesn't work when the process is mmap the ring buffer. > - After mmap the buffers, when the snapshot took, the IOCTL returns an error. > > OK, but I rather like to fail snapshot with -EBUSY when the buffer is mmaped. > > > + > > +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. > > + > > +Example > > +======= > > + > > +.. code-block:: c > > + > > + #include > > + #include > > + #include > > + #include > > + > > + #include > > + > > + #include > > + #include > > + > > + #define TRACE_PIPE_RAW "/sys/kernel/tracing/per_cpu/cpu0/trace_pipe_raw" > > + > > + int main(void) > > + { > > + int page_size = getpagesize(), fd, reader_id; > > + unsigned long meta_len, data_len; > > + struct trace_buffer_meta *meta; > > + void *map, *reader, *data; > > + > > + fd = open(TRACE_PIPE_RAW, O_RDONLY); > > + if (fd < 0) > > + exit(EXIT_FAILURE); > > + > > + map = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0); > > + if (map == MAP_FAILED) > > + exit(EXIT_FAILURE); > > + > > + meta = (struct trace_buffer_meta *)map; > > + meta_len = meta->meta_page_size; > > + > > + printf("entries: %lu\n", meta->entries); > > + printf("overrun: %lu\n", meta->overrun); > > + printf("read: %lu\n", meta->read); > > + printf("subbufs_touched:%lu\n", meta->subbufs_touched); > > + printf("subbufs_lost: %lu\n", meta->subbufs_lost); > > + printf("subbufs_read: %lu\n", meta->subbufs_read); > > + printf("nr_subbufs: %u\n", meta->nr_subbufs); > > + > > + data_len = meta->subbuf_size * meta->nr_subbufs; > > + data = mmap(NULL, data_len, PROT_READ, MAP_SHARED, fd, data_len); The above is buggy. It should be: data = mmap(NULL, data_len, PROT_READ, MAP_SHARED, fd, meta_len); The last parameter is where to start the mapping from, which is just after the meta page. The code is currently starting the map far away from that. -- Steve > > + if (data == MAP_FAILED) > > + exit(EXIT_FAILURE); > > + > > + if (ioctl(fd, TRACE_MMAP_IOCTL_GET_READER) < 0) > > + exit(EXIT_FAILURE); > > + > > + reader_id = meta->reader.id; > > + reader = data + meta->subbuf_size * reader_id; > > Also, this caused a bus error if I add below 2 lines here. > > printf("reader_id: %d, addr: %p\n", reader_id, reader); > printf("read data head: %lx\n", *(unsigned long *)reader); > > ----- > / # cd /sys/kernel/tracing/ > /sys/kernel/tracing # echo 1 > events/enable > [ 17.941894] Scheduler tracepoints stat_sleep, stat_iowait, stat_blocked and stat_runtime require the kernel parameter schedstats=enable or kernel.sched_schedstats=1 > /sys/kernel/tracing # > /sys/kernel/tracing # echo 1 > buffer_percent > /sys/kernel/tracing # /mnt/rbmap2 > entries: 245291 > overrun: 203741 > read: 0 > subbufs_touched:2041 > subbufs_lost: 1688 > subbufs_read: 0 > nr_subbufs: 355 > reader_id: 1, addr: 0x7f0cde51a000 > Bus error > ----- > > Is this expected behavior? how can I read the ring buffer? > > Thank you, > > > + > > + munmap(data, data_len); > > + munmap(meta, meta_len); > > + close (fd); > > + > > + return 0; > > + } > > -- > > 2.43.0.275.g3460e3d667-goog > > > >