Received: by 2002:ab2:1689:0:b0:1f7:5705:b850 with SMTP id d9csp1948865lqa; Tue, 30 Apr 2024 04:24:24 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVJRuB99QMckiD5xJxXSPCjf7v3iWP3uxiZ9m2zafs2CC0P0mx/JmN0Nc+1OpBwSuQS9KH/PBTSNxjj5wM0W/7//8Ar1C3GMZ7gKma4Yw== X-Google-Smtp-Source: AGHT+IELPbbjTSrTi2D5zsdUpUF8losVw9GfsMpiIfivGs/WhOIX96XPmsurumMIzRQksemeQ39x X-Received: by 2002:a05:6358:ed09:b0:18b:d44:52b6 with SMTP id hy9-20020a056358ed0900b0018b0d4452b6mr14032709rwb.16.1714476264036; Tue, 30 Apr 2024 04:24:24 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714476263; cv=pass; d=google.com; s=arc-20160816; b=GqenC0Z8btWGudKKbTGNHmIks3RcjWEavGHhcSc7jB9tmGBZJXptFGJHYne00VsRyh 71fAJdWNq0AbxRdPCFgc8RKl3GCcwfvwaPcSeQGnPyAZKC16Ue51ZgR1gB2JhIELRRRa 3GeMiPwmw9iGstZku+eswYCO9o0P+xI6uiKYNAMZcY34Wy0ZbkIg62E1Rh5fZKZqVPoL 8xmzN9KLtjcCZIpZ+yVA4z1b4Nh3YRGq1yGVp0Cq1SsYksxvkpOJhc33p5/eq7tqGMwC PJT/cib5z8ygQol6bJtUMF4YDCVjwAiv55gEDA+41h7giv32CoYjShAYw3yjDyU7iHF9 l0PQ== 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=bXcj5IZtpF0lNUXXYyqA1m4Vh7ioEK0Q5U3WBBOAq2Y=; b=sfbo4qOEDG5iR334W2aBty3D2LEl8sf5+Nm+X9gXwdg3bacmX5lWrqfMbbf2EGjHg5 gDO1WS5PnxsnuXC8iyq8Fck49h6KX3vcCpvL9bic/ALc5bRoeOvv+L/yhB6avmwFPSJb K3gdmT074MfNpXxg0/ldGCBiBCterpgep4dxRRCpjp5m+hojtWXUTn41/KLFvZ+qeNac TS1fjHvc94+vLxl7huJFF5HoTBk9gaAfpwg59vh1ZqoKeKdP40RCTu5aaUGnftXyib72 5/HawV1hghsZFdAQUyBl23eAzxyZDgHJuu1ZFSVAdMwLJXUdSf562pDsk6f9Pa0WtQng XcGw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="nI/VL3CY"; 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-163836-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-163836-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id w13-20020a63b74d000000b005dc5070656csi22031520pgt.785.2024.04.30.04.24.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Apr 2024 04:24:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-163836-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="nI/VL3CY"; 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-163836-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-163836-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id A68A92865A0 for ; Tue, 30 Apr 2024 11:14:53 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AB56712E1E7; Tue, 30 Apr 2024 11:14:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="nI/VL3CY" 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 E0D3F12DDB3 for ; Tue, 30 Apr 2024 11:14:09 +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=1714475651; cv=none; b=NzNG4MJXvETNPcb6aj4RvDtaTAYgMD6u2mHvyRB4TkHhKml19SKm5dC6TbfJXeEjhqHNW1AcwItwSigt/8VusUPggS6+x4OoK2z+eBkn7gQnI/BP3MHBgKhTK3E8HI1Cwi/UuuNTF2ziHBa/oXiRkS2NVBOT/idndXivSBC77FU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714475651; c=relaxed/simple; bh=nHh/TTjK2tW0sUwPgbpncHpLmKF71Gsdpp0iLEYgt+E=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DbxWo99pTEHTTH4e8pTZz8Uk7RcOJ/LkX2UCRsP6tH7jiYwzAoZ9/cr7KoT86BRhoDlGElKq431V3NklZ6IJWxxNUQpFHG2kePbBU9FnIJkuenQMKp2cLbItXf1jyMheVV2Qx95EpQ4Ds9syj6pGr/BeC86OFJa0xknHo1Z+a3E= 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=nI/VL3CY; 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-41c05442f92so13674615e9.2 for ; Tue, 30 Apr 2024 04:14:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1714475648; x=1715080448; 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=nI/VL3CYQVGuUYdqCSXzmTVQ0h7hokk9gTeENYdcLD8L/SWO+oPIwJC8uidEaRkQoI dez0xrr7vJs/c+dBR7yWH0/Jb2xbc2deJyrwLhtaglCYzWqC7F0WA/CT1yEWI6WlVb+7 2rQdTJcAM5Y0rnX8kaQVq8CYxtXcrKviOjoSd0/oaaTVoBGU+X3nR38tJtX79wRxk9HL z6KMQSOgO4yH2KKQWsV7n09W1NavoZd95WlG8N++uQAwFb+3o4IOOIf+4clE82Yl0Lhy dNbMe/FE5eCurq0oDB6YxDPLC7TfMJWqcrUbTHzrBFv5h0oroV3PVDUa81KbjIiyDl9X hPMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714475648; x=1715080448; 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=L2CMX0C1PPNGpLbGzEZ7lpa+8rWKS8X8GQGibVnx2mdEs2nWk2qHrO5ShuII+/MFgG paw1PaIM/Ranxgv0DQ96rLoHMdeYDQHO33dR/dP6M4wAfAbwTVyw96Zqqdco9hb8vJx8 BAJnTDxB/51l9SEhzFsURv3hKzfXUVOie94DGjXNTpIFrSYPfH4sHdvSB0rSOkF7dHV2 p6Wvk2n+K1imcWM7A4trb0l028l682ww07aQTcioAyKr6Edg1kJ5UuH6Rq5kGNzQVI0t 7PWIfPC7tKUKg8FLE434sTocOh+yLOFxOQbXewVgZOovmyUHAjWPTZ1ZxhWK4+Yi9DKo Lgxw== X-Forwarded-Encrypted: i=1; AJvYcCUG819Fjd1aPrpG1ej+ByLK+qbnLF/pkL3sHMlSLCBHKvedz3RStPjmKAQxGu2wXZUHHDK38zqnh2y8qPopXbA24OCQlWuw7EvqGaAG X-Gm-Message-State: AOJu0YxwovYqVOnAbB1+JnyjvF3xOFKS5iNcwwI2ytTes0rg44Rurh5D 8nM0gf7voCsKLN+RsNnpCN1jI/WEQLlVqHVDIymtDtGhpYhC2aS8qPCntEfi1i0tJh6740m+ZHz 1ZFvvs7RHRJjcHnYw/A== X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a05:600c:1d26:b0:418:37e1:3d43 with SMTP id l38-20020a05600c1d2600b0041837e13d43mr137262wms.0.1714475648416; Tue, 30 Apr 2024 04:14:08 -0700 (PDT) Date: Tue, 30 Apr 2024 12:13:53 +0100 In-Reply-To: <20240430111354.637356-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: <20240430111354.637356-1-vdonnefort@google.com> X-Mailer: git-send-email 2.44.0.769.g3c40516874-goog Message-ID: <20240430111354.637356-5-vdonnefort@google.com> Subject: [PATCH v22 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