From patchwork Tue Feb 13 11:49:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Donnefort X-Patchwork-Id: 200404 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp480244dyb; Tue, 13 Feb 2024 03:52:20 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWpaIm178wCSa65rtmVTmrWq4Eh7vxPcVrMXF8kccDa2K4N+L+QRAlLvmZkBTCffpHPXIwhC1RLHyGKi7oMO+kMbIJrgg== X-Google-Smtp-Source: AGHT+IFhT+T3m6+jFwXBcoGk2A2mWQqTkhVTObYJVEDY18CgDOFTt3NMONvLguhoWWp9NiTqL4Vk X-Received: by 2002:a05:6358:7e42:b0:178:fd13:d6e4 with SMTP id p2-20020a0563587e4200b00178fd13d6e4mr10312618rwm.25.1707825140081; Tue, 13 Feb 2024 03:52:20 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707825140; cv=pass; d=google.com; s=arc-20160816; b=XxgCUAtsyedyD3N5oXw9g7265XBbfzt7QJTFY/k1RJr8O3dhOwYkHlxpcMKTZfOgQA z+oUNMgsnDsNgiIdCGSaOk5+/+wMptXO4ROYvhB7S0Rt8k01+9WwS4tZdfH80T2Sdk7z 3quB5aqvk81HfSy9udsnqeV6CYykp/9CYUAyCT6NwOwi42kRhm3kNAZaRHPaTCnbi6+p UR0ztYXbZ3ua0CBpF7eu8tyK21iYUmlTu6cHrNKPzOpJxY2TaTMxdttOPe/yT/7mXjQ9 aZcrKDhnVIMU3VxIyPQEm7z029wchgEy7Ok/C1PLQ44ngqAMV2GqW4bAgAW2SMIthTXn ZTfA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=33GrMjCaq0Ut3h1R/r8N5iPZtwZEhgImBME//MhSBpo=; fh=8vLyva9qfQXDR3XtW9GjUxa7ro7PLD1QXa1c4JnNz4U=; b=B5tnrRMYYj/NviKGr6ygic/we8WaOY2eEiAdfJYdsSC+M8o5AeoG35VTfDjeTtuhnm SXpw0y/gDEWrNvOjlWGS2gMytL8eCVvrEl2vdg9WsE6vcmuX4LNfDb+n/ce8h7MH2YGA mtlgoOO3lLh/Fs5pzLbqgQYDPsbm9P41axMOg6prKv2AGzP3P/IPxGB8DIS3pau1fh0y WIiABN3UL0LNl9uuysp+TAldgU1o+/CH3zO+b9GMdi2sLddiw+qZom3bS08TU1PJR01Q D+qFOCVaUzPoRyaMZQy/VQOhHhhuKJEcwgU9HVldEmbCMIu3ey9dy/HrUHTYXe/0EhzK DVaA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=tcMlBMqH; arc=pass (i=1 spf=pass spfdomain=flex--vdonnefort.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-63464-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-63464-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCUqgHRB9dELuhkBVIaRAucMZ8wMwwu0rGaQnHUVvTBLgHQoR04RJymTo+bs+C6cFX5FMaRXC2wvDDlWNamFQyr6ZViZYw== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id r18-20020a62e412000000b006e05346a290si6642758pfh.290.2024.02.13.03.52.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 03:52:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-63464-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=tcMlBMqH; arc=pass (i=1 spf=pass spfdomain=flex--vdonnefort.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-63464-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-63464-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id CC85C28A8EE for ; Tue, 13 Feb 2024 11:52:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 866934F8B9; Tue, 13 Feb 2024 11:51:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="tcMlBMqH" Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B4E1041C77 for ; Tue, 13 Feb 2024 11:51:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707825063; cv=none; b=EoWVYFrcWGHQkN0/7KkM9ATNstyQxm389ETu2OhPgrU5EqACw6pdtfcnwVwjD4GiHcs7sNbXo/smN52uwF1BkwDX44L3oM/V0j3Zq1VxoNmxr9JuXTihN/6B7wZjjpl+ZAyI9XXA+kyqeueq6GI2c2HlDR5xuwjQycLkwExdLzk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707825063; c=relaxed/simple; bh=5mO5XahKTZY/Pk1qlWQX3Pd3/FJTnrcbSWVUWZlckIg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=sgFC2c3wKeXBb2M0TyAGxXE9qsfYrgyNodBBY2KscTu+7gczQOZDfCsBEGeGg7s/v0NIAVXzbfK66JNN8NqRDExK01o3Yk1lH11FgfrT0axxQef6dPtqFnaeSEQF/PxGUQWepBZtcjvI8kQ8cFmNT8perY14lXMFmy4Ra1Q2BUM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vdonnefort.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=tcMlBMqH; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vdonnefort.bounces.google.com Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-410040e1fcdso24786515e9.0 for ; Tue, 13 Feb 2024 03:51:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707825060; x=1708429860; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=33GrMjCaq0Ut3h1R/r8N5iPZtwZEhgImBME//MhSBpo=; b=tcMlBMqH6BkY+CL3O+hJ7t7j05Vb4ih7Lm6+mczyInRkeq17hyXBOtgn0AfpK2vV53 LImUawvHQsnrbzDo6GXNjnKgFZGxCapcTvaZVAGSdMQYsNfVV8zgKKJixaG14FgWlL5l lnwzhkk73ppLtHxnaHtTM06n6EE+OH0Mva/LMqAPVShEzCnRs3Eqve5Nfo2FKCnkIqxC D3IhUMuyBqleXSvuBvKQhaYQWk4lymdzkL6bcI7xcGA/E4pa44Md1k4ng/ZUWzZ4y+PJ ZPotrMIcuaz/j7qm6+hHwWeRi7h6eatteFgzyGI6BFBNyYr3zU7foGSww2vwhjp0s3iD QbbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707825060; x=1708429860; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=33GrMjCaq0Ut3h1R/r8N5iPZtwZEhgImBME//MhSBpo=; b=IT3xpc+ETGUfHn8E72TYgv7khCaaDPNRDMNga9myi13t6Mdww6QGX3qXFAJ4yovjWn SNl1+v1HMQ6UnOCcYU0fABEHy2jKMIXeE7pjgCky7oPfVUzgOwzmnrNbuhDHTN4mcLb5 WkW/i7XfSfs7H4yIppWZGcd91mtdqSIzcQec5liQSO4sfImMfokBfune/acTI/9sq3+U gepBYZRxAEO8tpHnle2df0YhH6mZx4mNF3NH97uzYnt0tGM5UBdpHrjHqogHE47hfwxP uQNMUDi610W6aVsOeY66UE5XScoSlQ20Id+aeYwODMG9aqsAWZrAJcTjxe3USLSmag3P Do1Q== X-Forwarded-Encrypted: i=1; AJvYcCWyD3IK2LOcfjyq+Qm3MMeTo9qQzfmJFQpqt7CiGiUwQItXQxmBgvQNlREDAxPXYbRa1clqA/w0x3FJTMePBM2DljOBtZ0cTpcwxjFD X-Gm-Message-State: AOJu0Yx+DUy08Aid02aX0TeOCUIc/eyAhrVYigUpVu50TPJzsGjYJvle +5Lm/vt6K/yrHUjCnPkHbo0hb/8thJt0xqA8Of+O7HX0fOjHt2WsBajH55s8YF7EAegmdMrLX4+ m2ZZIpmJcNr6am3I8Fw== X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a05:600c:3d99:b0:411:ccc9:7bf1 with SMTP id bi25-20020a05600c3d9900b00411ccc97bf1mr2323wmb.1.1707825060011; Tue, 13 Feb 2024 03:51:00 -0800 (PST) Date: Tue, 13 Feb 2024 11:49:44 +0000 In-Reply-To: <20240213114945.3528801-1-vdonnefort@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240213114945.3528801-1-vdonnefort@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Message-ID: <20240213114945.3528801-6-vdonnefort@google.com> Subject: [PATCH v17 5/6] Documentation: tracing: Add ring-buffer mapping From: Vincent Donnefort To: rostedt@goodmis.org, mhiramat@kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: mathieu.desnoyers@efficios.com, kernel-team@android.com, Vincent Donnefort X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790784454194526395 X-GMAIL-MSGID: 1790784454194526395 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..0426ab4bcf3d --- /dev/null +++ b/Documentation/trace/ring-buffer-map.rst @@ -0,0 +1,106 @@ +.. 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 sub-buffer ID which can be safely +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 = 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 and causes splice to copy +the ring buffer data instead of using the copyless swap from the ring buffer. + +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 readers on +trace_pipe would be. + +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 | O_NONBLOCK); + 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: %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 = meta->subbuf_size * meta->nr_subbufs; + data = mmap(NULL, data_len, PROT_READ, MAP_SHARED, fd, meta_len); + 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; + + printf("Current reader address: %p\n", reader); + + munmap(data, data_len); + munmap(meta, meta_len); + close (fd); + + return 0; + }