Received: by 2002:a89:48b:0:b0:1f5:f2ab:c469 with SMTP id a11csp99335lqd; Tue, 23 Apr 2024 16:29:18 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXvLq6W983kgMdFoptC8CRp55XIkeIqKR3upVCdk6ji1yVNY6CEnSpJv7vm2Pz8u2Gl5Rwl8KHxjk8n+V6nxjGdgPQxNTpUILoxGgwlEA== X-Google-Smtp-Source: AGHT+IFYfjn3fqSiPYjBQ/l2dtvH0EVTxzBQWi8vlD7+5KqSatQWfzfpMUCs+UwcdaUP0UqQHw3J X-Received: by 2002:a05:6a21:182:b0:1a7:9fd2:8bbb with SMTP id le2-20020a056a21018200b001a79fd28bbbmr926678pzb.24.1713914958091; Tue, 23 Apr 2024 16:29:18 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713914958; cv=pass; d=google.com; s=arc-20160816; b=a90BYpwhCxYm87pLrCJjwAsu4qCaUyIJ+TVWdHwG3/Qwc899N+O4zRx3mVRpxc5Aao F+blXr5IWnu/xSLuHmLSN8cQ9KZBjww0fZRYuXvhRQVEydR42GWKPUUb0S/g5e7lVhvF MHX8hbeRLx/klBZFb7AnqXw09uxMl/qttMabaLp0zE1pAFiH3SgH9aOWtwEEa20qDdoF ApDZSg1WIWZzaQe0cDHhlfaWCxV51p4ijubLW+pS8cgjvpkRuAef8cgteynTWtnJWd2/ HNJH3vF2xJfwh3fcXpBQfKeLKrfIBl2UfAa+gitJWHzny4MW9Vp53ZXzcN6tEChmJNoj iP8g== 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=5feaOC8gyTsjpelUvOVH7lOOqHGrllvmXVDESYI+Fsc=; fh=PVWxwIrUuvrv7kFVMJRSNa0SO4lXHnc+bNF2N8hAeG8=; b=s7s32d738Kf/RoJXR3ga0/LZw3iGKHuYFjlIplRp/AC6dfi5ATHPvnw/m6AdpUz6mS fDPJsOKpxv6mqkFO+Vp45BBZ4I3UI8iMM3kYKoe+eA8YFOKzLi7SCT7gIW+9hYW4Szmz ILTpvuDMqEmh+HTNPFl5jXaIbHNeHKOXmJuVwdYuKb8QVW5QjO0/8XEbS2kkhT5HeO4J MeP27XbkaDokdcIu/HDL3cQ/R3LbBmELO9aH5lHBGCyuV8w3lvScT+hadTRq3NdFyScR 1KC5xprzyogDeAONudDoX2gAdvW3s7S7wFD4XgFLXXiH9kNfNssAfSiOy8t4BM5iGl6Q 6T9g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Pm2vw3HM; 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-156006-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-156006-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id o25-20020a634e59000000b005f0a5269689si10523157pgl.659.2024.04.23.16.29.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Apr 2024 16:29:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-156006-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Pm2vw3HM; 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-156006-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-156006-linux.lists.archive=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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 4917EB22BD8 for ; Tue, 23 Apr 2024 23:28:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 878A253818; Tue, 23 Apr 2024 23:27:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Pm2vw3HM" Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (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 EAC0853E01 for ; Tue, 23 Apr 2024 23:27:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713914872; cv=none; b=neLx5J/VC4/0vX24GqeV6nyHx0bvfaBRbpqVRnbpIerxQOKTaNiucv2r1oHn1iOxkAyOVasFwYlzDtqKbWhqXXK7VSwibJcJBK0d0NLqqT7TMcwzD615bF3+a+ChRqbHC0c3q9E98Jf5qfgGXCY26SY9f9yUtX7KTT2/nda27Bo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713914872; c=relaxed/simple; bh=nHh/TTjK2tW0sUwPgbpncHpLmKF71Gsdpp0iLEYgt+E=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=t6XK2A465E7A+W9LgarGAqUvOzmy+yBFjsHKWUp0kfRYuV6r3Qk50lssxvButUdSd4rMCQ/5OY0206Fex/olmmAJAb0JDS0l+ttZCT2OBa0zppAV9GvyOSwwAoWFsgUgY2fbta1oeOuRe3j2xmTvvO76WUIxVEE6KKKffcWWLEw= 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=Pm2vw3HM; arc=none smtp.client-ip=209.85.128.74 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-f74.google.com with SMTP id 5b1f17b1804b1-41a8440a8cfso11487865e9.3 for ; Tue, 23 Apr 2024 16:27:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1713914869; x=1714519669; 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=5feaOC8gyTsjpelUvOVH7lOOqHGrllvmXVDESYI+Fsc=; b=Pm2vw3HMzksnC6J9Xal9ruHZyCPx0ckLCTKir7QxJDKr36f1XYCagvqJ9NPtr0kfKx cpaS0RhW/BPq2cHHVPjz8c8UivXgXTJvpz4b0IDmZdwU626EjHEaj0QOak5hkxs5LWFx ry1vFT6appD3o+jUBc2ynhOxpp5NrymjHFOS5JJYd+k1OTXN6SGeOQp00WEhc68ZCRkX 926t7p6V95DpmwEDo+2CXagee6GRoXbdyzqFg3wicRkfbc5QMKOG1xjRN1BcOZFpItCg hsGCDwX2+4tR363zd3eMdCE7DG2l58CFhpzLgSwUulDzbXF2V0Wx8zupawkXtwWEInmg BoiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713914869; x=1714519669; 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=5feaOC8gyTsjpelUvOVH7lOOqHGrllvmXVDESYI+Fsc=; b=Ey3PZcKx9VRwcfqnJJEjvIT+VABQbNZPCdszA/YcBptc3jIwKEkFA7tR5JKiwZyC/9 QsCgr/L6HjUvMrrqkxk0tPGnQKIvWXp+a8clb9wzqpD/I/6hj/ewTJ5AlOSYy5mHryBT CEbDpGz27f4Ca0Oa7h/U9cjiiZpMRaqEa3hC/h0KTcxLkKiSQ7JA81VTb9vmtsc9IOWH WjwSouF5HEjE3+agcyQZq18Dsbc9gWDnBGkPbANWHXndCXFIhurCmLgTsakxsBqXwee2 dDvjv142yELZzRWLuIWRYFwrFKpOW7dc1cjh5Y1VHsDu/pEbzpFp5zpllBZH6omoNCBX uanA== X-Forwarded-Encrypted: i=1; AJvYcCVbqd+XifePYQSGqF+8OEdlfO5i4M+kyDXsXKkyBcva/touZGsPhQaTU4isomLkNbnrPGxQDY7nj4LwqNHBx1JcBccTchVvl8p2Xn5w X-Gm-Message-State: AOJu0YxaSNEaCVgu39Vxqh5y8Kok+0bzPl67KhtR9Gp7jdwJdxveB1QQ Iwup2Y/43qOMED6xPW+yK6H4bEIdNfUpxfCJeoAi4Nz3xN/vBLL3OmGeaZBlteW+shFO0AVTGGh cl33ZlETkqgLS7I1gMw== X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a05:600c:1c85:b0:419:f572:66f8 with SMTP id k5-20020a05600c1c8500b00419f57266f8mr3676wms.3.1713914869501; Tue, 23 Apr 2024 16:27:49 -0700 (PDT) Date: Wed, 24 Apr 2024 00:27:27 +0100 In-Reply-To: <20240423232728.1492340-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: <20240423232728.1492340-1-vdonnefort@google.com> X-Mailer: git-send-email 2.44.0.769.g3c40516874-goog Message-ID: <20240423232728.1492340-5-vdonnefort@google.com> Subject: [PATCH v21 4/5] 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, rdunlap@infradead.org, rppt@kernel.org, david@redhat.com, Vincent Donnefort Content-Type: text/plain; charset="UTF-8" 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..8e296bcc0d7f --- /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 to as the meta-page. One of the most important +fields 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 ascending 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; + } -- 2.44.0.769.g3c40516874-goog