Received: by 2002:ab2:3350:0:b0:1f4:6588:b3a7 with SMTP id o16csp517898lqe; Sat, 6 Apr 2024 10:38:09 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV8J1qiKFCwbqr1HyQu1M++Syh9jObo09Rn+JQcZ4JH5BEPxJi119ORrCeeBUenuCSfhnwUXHxn9dODSIJ/EInV9DC6MDGe4kpetVfmXg== X-Google-Smtp-Source: AGHT+IF5KcxH071vv4RNVx/oBdfDP7o50hs8sZYqi2g/0rcr7j2fNzZso/IIh1b3h3HPA1UdTKKq X-Received: by 2002:a17:903:120a:b0:1e2:b192:6975 with SMTP id l10-20020a170903120a00b001e2b1926975mr5323390plh.16.1712425089548; Sat, 06 Apr 2024 10:38:09 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712425089; cv=pass; d=google.com; s=arc-20160816; b=qw9/GsmVlqiyxqQnft5suLqqKqOvNX/XKCyLh3K2/EOdalMUBh5R8xUBM2DUY9bLag PyX1PEXTM9nqWdZhcCLXcbBunxdni8GTHAm0pXuVg1cEHGcIA3LSdo5/FB7ORts/VlPk xukoK4txzLbPx/dVyLMQOoTOUitKLX8yUqLbcYszWScu5QrPY+xwUTjnKpMtzRNrDS2y lw/r8+McQLtUMICnJWfocrr1kqRSzPCOByQzlRu4nctLkyAiKlPpUzgkBAcHXvWPt9ke bC1bsVA+vXh2Msm4PyCD9NrHSPuki5dHDibb0V2SHPFIRfLBirSHwGOLmpRkjmSVFzrY hcLg== 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=Ty21zwhpIK06gSNfvHnlQi0EabGuqqhLB368ZO8Z3zI=; fh=UNTLfNuD6ooadaqqTdr6IqRu8jpYGlhXwejGRdkUe2I=; b=iIFcRf86iZ8uDDDthOiRCwAZA6iH69UJ/Km7qo1DZhUFs1tevrlQ3OkftbxRKV8HFz pp0rXMbuxiCfZsfyFhopENn9cSoWHGcsz4Q6HLoIm8sDDnzpv0klT+taDcFquza+Ft4C GUSBA6Fh51sm/YqucIlpeEhaNbkpfUC0A4z05BpZUu/ZZUy7Dsv4wIcU7aLg/sLfnUlJ 7oTgama9joqeF77bAkKHz9Rk6loXU3x1kKtmAbnyTCj+jrBDl/wD21StGbV+8ujwXpCB Ze66prwWvZkDJyZAJTtrRkKO0uzq187fGDWuH0BPpaj5sB06X775S6QVHOWw5lO4mrcn XNPg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=hdqPCg6g; 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-134048-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-134048-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. [139.178.88.99]) by mx.google.com with ESMTPS id k11-20020a170902c40b00b001e0b60f9063si3463035plk.369.2024.04.06.10.38.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 06 Apr 2024 10:38:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-134048-linux.lists.archive=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=hdqPCg6g; 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-134048-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-134048-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 07E6D282D81 for ; Sat, 6 Apr 2024 17:38:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7AA6D4E1A8; Sat, 6 Apr 2024 17:37:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="hdqPCg6g" 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 B80544CB37 for ; Sat, 6 Apr 2024 17:37:18 +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=1712425040; cv=none; b=dCwGA6RMSfjOtZZsEs4MzwfINk/QUwD4y4X3bIQLv0cF2bs8N8DEaWV5OE2riB3eOoplOl8b7KW0sCNbmbb+gBj8d0CXR+u/lNHMHJUchcoFNuRlLQ/3XaPqBCJ0nWpBUkb0y+FP6a9VvuomOz/RflOSu1YYlYh8Ew0Fn92dVgk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712425040; c=relaxed/simple; bh=ZXKJqGIpOeNVjmTs2kjmqOaHkG1DnLLt0+tv6mFfRmc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=a1oDJUyS8HwbwvtfOfTlVmPgkjFdd8YKUjI5O2MIdaZ/VIHsV7pB/cbh3lwIbWa0UMsZDQOTzzEILXKCntEMAJkpB6+vDzl2xgMwHDdM2oY2szV+AfAkPFOndkH3HnKk2Eb/TzKSXoHpNrDLYB/qz6ty3UdkHaZ3RF7d9p3XhsU= 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=hdqPCg6g; 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-4162ba19021so13017985e9.2 for ; Sat, 06 Apr 2024 10:37:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1712425037; x=1713029837; 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=Ty21zwhpIK06gSNfvHnlQi0EabGuqqhLB368ZO8Z3zI=; b=hdqPCg6gevaNSIQmD2CeJAUDGTfne+gESGpgLQG5JXs+5NvDx2IWbCXlRYPaz6ctRq wnFBPNKFwp7CLrLAZawczAtGE9Hue6Qg1WyssBG3AxluvMJp7N9y2rNexrZB3EUF1zKY NwiZ0idxQkKVKr29gUWaLKcHluGsrD7Q+SzVsz+xPIt43SmUV0mAJBiZ+6zrA0RdINxk NS2VDzWNgm+UJHJOTUhs3PTVrDHkgFzL0HRSvOcQbHarVAtq91EyIc8cz4m1hbodzHEY eAWjmqBl5N28+D8w87KOot9jXGe/6iviXqdlFD86h55ItGwe4zU8wsfT3F56jJcmfr8+ Jktw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712425037; x=1713029837; 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=Ty21zwhpIK06gSNfvHnlQi0EabGuqqhLB368ZO8Z3zI=; b=RY4DyS7kuqY+t6Jxc+NvuNDKOf9J+bWTYb9iVUwi7v53mpr+Tnev13i3Lh/+7MsHvh rb8UlzzBJXeNcnXFpnjWzLVpzbyQkcv+mzh58RAanNbmc+N+bwtZuTD9p08c2CaheTnk E8xyTZnUHzYrPSmxJcBnZlebCaW6OfR8saEpNSo5mJjYSN4HlSA/JNTsw2KWJVC85JqY rTu6yn82WS4yqta9YDywHQBn4eWchHM+Ec9lItYkqYtyKTmOdECwJ39TGVAqQJ3c8Lqd iZ1w6r7ZmDe9dhN879c7tBghVBr0KLZa1wVzRb+lhMLiFvdgDZandChPAPlR8fKD7Xui tAFg== X-Forwarded-Encrypted: i=1; AJvYcCWmlGZBF5HhvvpWauLvH8yM41ndHKbPKrAoIiAC7l+6O3sFzjutqyZ2hzEmKXYXM1jwZ6AVu4xseduKp2ceQpcKaoGAgste5/LfkyKW X-Gm-Message-State: AOJu0YxBkAgF4oN6z2+aIP8BWb2Ynlbu8y/6TOs/ULrqUW18ML+3Fnn7 XjP5QOmWyVX1Zyh5YVc8GjfLFYaMgap6icyYa8mO72J0XMVdlFNSG68MSJKFQ9qQFhtAqJaJoY6 BME8LTJ4dvMVQSxzutQ== X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a05:600c:4f81:b0:416:43b8:c04a with SMTP id n1-20020a05600c4f8100b0041643b8c04amr2546wmq.1.1712425037413; Sat, 06 Apr 2024 10:37:17 -0700 (PDT) Date: Sat, 6 Apr 2024 18:36:48 +0100 In-Reply-To: <20240406173649.3210836-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: <20240406173649.3210836-1-vdonnefort@google.com> X-Mailer: git-send-email 2.44.0.478.gd926399ef9-goog Message-ID: <20240406173649.3210836-5-vdonnefort@google.com> Subject: [PATCH v20 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, 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.478.gd926399ef9-goog