From patchwork Mon Feb 5 16:34:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Donnefort X-Patchwork-Id: 196926 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp991851dyb; Mon, 5 Feb 2024 08:36:09 -0800 (PST) X-Google-Smtp-Source: AGHT+IHWjcCzzPqwMLI8Af4yIkqgw4h8vhaAfDJH2rkZeCzCQHpUCVreoFGmpt+S3eSythrNNDow X-Received: by 2002:a05:6214:5090:b0:68c:aa46:c624 with SMTP id kk16-20020a056214509000b0068caa46c624mr2766087qvb.24.1707150968827; Mon, 05 Feb 2024 08:36:08 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707150968; cv=pass; d=google.com; s=arc-20160816; b=PygoGtXa0ER5R7giriOmXoTeTXXybB8XaZbhkX24PZ00LmZhY7FD2Buvribj1NlqfM EEH7xBtqBfC5sfs6AZ/58Q2xTrJuap3Qi2Xlm8QWmHm/tMmO0rFscyHHJGANXPKUqyaw +fiy5Q2t+uLpEP4RbafhlITSMfapEyjBqIJYjdbQR/hTBD1ILmViGZDufxfmF9P1oNne HcxmiBITdvrdsFTNOMfZkqzlzCFEjPfSPVagLOSGpUzLuMlHH7xQy8p6gRjfX2bmiKuH +1uktTH8EBsUStd+6nS1JlmRgYUOL/ux4weeVmZ1tGh79CAL8cqrj8Zi7AcKiJEFkHLD 5Kkg== 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=MlKarA43yH71Vjbty3RM0+B2KNuJ6FxA9d0o2uA0tIg=; fh=kSnEJoqgbOWQqI9wG3r3tXDF0g3czLCrVt56yYGiNzQ=; b=dpKiR0f3GoANZG0zKDWvgmNcg5B+6xn01MBAW3FLbh2oflhlb3z5nMKXKBeEvcavsy 82PbSRSVcp5tSPih3s6SVoUvvnZ8v1kFM0kHi+bpL0sVgxVMMA+Kj+5AclDAk0IvU8kK irAC5bSPxLMZduLp5ZihQaJWnfLJ2b475WM9ht8dRid/u6/SXhu1YhVHz3uQAosr6jXP qlJfAQ9Xs6+J/qtxBwCsBY2loj3p+liglh5XeUdfd1xMci28GJH5cKjuKWqgQpEGH5rf qKISrY2QwTCND4qipmJFNHzghTevjD1v0avbDxsKVfxbGogt4H//ZxKNrrbCgk/Oce5i qy+A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Cl+6aiHP; 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-53020-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-53020-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCW+nvBZW6pN5yOXsRry8VVWt040hlCrTmqfPBhEel4kDFcCLh+59XM7b52Y5TZfmIJjCtrAidWv0nlTMIXY+O1Y/NymnA== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id 14-20020a0562140d0e00b0068ca83708e6si287141qvh.343.2024.02.05.08.36.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 08:36:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-53020-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Cl+6aiHP; 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-53020-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-53020-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id B4D241C2237C for ; Mon, 5 Feb 2024 16:36:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D35754779C; Mon, 5 Feb 2024 16:34:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Cl+6aiHP" Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.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 93D9344C92 for ; Mon, 5 Feb 2024 16:34:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707150872; cv=none; b=caPiyEs9Q6dNUOSXCDrYv70acr18Toc/lKmkegnOpcBreoTTeY0LPJYj7n2uFi682YxEvzKYq9vtp1DcmfDZr2L2tziUm2GtnmYgi5J0NmgBEa/7PZFW3TMItGzl2byvOUVltM/9CsHWQgZfM9g4pd3KsjT+s+NiNfVI1uv/Nkw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707150872; c=relaxed/simple; bh=R5o/R3ePscrxpSb0R1Xmqc1DrGhjORR4iN3JNJ+9WVo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=CK4ORdoUA7CoAmcADKVGKu2HxXW7wFhiGNzwAIFqxbQos4QG6OAgIEKji0/1WuB4Pll3frGytsmF3zxaIKYWPYNi/tomkYdjKk0IHOsLmnQKKPYUoA0VL6y1Pv6rvk8UDqk2BxuRHkSCN/1mepn8+J9Ook+fivoAkYKvfklWHQ0= 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=Cl+6aiHP; arc=none smtp.client-ip=209.85.221.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-wr1-f73.google.com with SMTP id ffacd0b85a97d-33b0c264abbso2436493f8f.2 for ; Mon, 05 Feb 2024 08:34:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707150869; x=1707755669; 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=MlKarA43yH71Vjbty3RM0+B2KNuJ6FxA9d0o2uA0tIg=; b=Cl+6aiHPjphQcAPgVl3+pCSTHRn5D/8tO0L56/m1W0O+SpgECYKuWSwOko/ASk9f+4 5HXMEnTYgWyyZQQdz4e2KuCCBglzkayp5ByIjMAyWmYGJqcv8Nze9LMb/cMJQqPLORhL IqIcl/8yJtjE3AeGovMXzugBDIwIQw8VUCdYPnEzvfFo9JIXMUtdJHEo288YYCp9pGNe e82q6bP/tZYJ10jLw13rUeZmXTgBvQE3+o76uJZIGK2qOWIRjg420pdqxbnglKqsnL1V V7Wj17ANze6+hZqacUvx5byt0zyvXPC1bwpd7a92aa3U9LTx4AJJrgXA5ahiZSrQlS6B LKIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707150869; x=1707755669; 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=MlKarA43yH71Vjbty3RM0+B2KNuJ6FxA9d0o2uA0tIg=; b=XJu3YuKb5nxgeVqM/BVnsRj9gzSZVgmCrdF7F/m2bHi4zxxWZvetx11Rf6uZIQI7aj E4kt3fzXm9UKcAdHRgyaNeMrcxtDFOOeqiR8SAtdhI9lxcGNwGokZGESlwtFKoFV2IQF 6o9tiOrTguJjiNe/9Qx83y5ERJ6iWvRLE+Wfi42GIsPRNj3BUGArDsVjUoA1Pdgf36iM JkGQs/17N6zUDpKniiy+2Gewt+QMUvBXNA+dcfIy9Vq0ROKpyRRUMGDoMXoTYSfLoJQi AumMC1m41vOtz2vGURL/7DjUDALSca8B9h229kw1CBqXGPAslhHjBx6u+D/o/MmK3pvI r1lw== X-Gm-Message-State: AOJu0YxKUQHrA5K474/RBPIlzQPwpBPgj/Od9QgQWGDGyFyN5Yw1dAmT b24HxBCuTwH5nO0Kee1hcat/5SsR+G3cXfiTqKwHEP5X9fMwSXBrfnLWCZzQ8QV2TilgXtn/RHr vPJ2yWjYaHvM/WT68ow== X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a05:6000:70f:b0:33b:d17:296f with SMTP id bs15-20020a056000070f00b0033b0d17296fmr73wrb.3.1707150868822; Mon, 05 Feb 2024 08:34:28 -0800 (PST) Date: Mon, 5 Feb 2024 16:34:09 +0000 In-Reply-To: <20240205163410.2296552-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: <20240205163410.2296552-1-vdonnefort@google.com> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog Message-ID: <20240205163410.2296552-6-vdonnefort@google.com> Subject: [PATCH v14 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: 1790077533936388814 X-GMAIL-MSGID: 1790077533936388814 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..628254e63830 --- /dev/null +++ b/Documentation/trace/ring-buffer-map.rst @@ -0,0 +1,104 @@ +.. 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 or splice. + +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 | 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; + }