From patchwork Tue Jan 23 11:07:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Donnefort X-Patchwork-Id: 190857 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2553:b0:103:945f:af90 with SMTP id p19csp257166dyi; Tue, 23 Jan 2024 03:09:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IEjoLT6CUKEReAwCse7hAwzMw9G8XBzbdzQDDYc+JaMJmu1aqi8kO05X+btUHeqP/U5y+dC X-Received: by 2002:a05:6808:202a:b0:3bd:c4e6:3e16 with SMTP id q42-20020a056808202a00b003bdc4e63e16mr817334oiw.78.1706008186334; Tue, 23 Jan 2024 03:09:46 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706008186; cv=pass; d=google.com; s=arc-20160816; b=ahH+fui1n83ryadj4+6T4cjOjk7E9VV+sSAIMbn7p+7zCDNB2al6UdoW/DdaWUwhGr DStbrKXjAYAt6EDFsBN40h5zeOdwQlY5D9retj9gL8a77NE8eT9Z/SCbGv1CNMqsAhH+ lNoCoVRtikkUCHQmlh0G/TvtnFt4qsMX5CFyblNB0kwU70urpynBm+qHcsyT5YTOrfs+ buJXrQYKjrayXzjLtT8nB42vyG2g7V6Etj6YyN5MWD95XS2crnJ6TSI+Ie8KRCQw2moq BB9kRhyQpTYFfvr9x/ynBvarx0mRBLncjQLZByA8s+zDExjN4rz23vDlqGKCUbzseP3n 8Nmg== 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=on32GNvkxWDILvYMfSVIpQ/Zr8fKHLMLlVw8UqZ+BYg=; fh=YQhVWO2XL9584FKmBhrq7yC9i9qkutxA7K29vY+DUJE=; b=FBwNeJA5UXQyw8NUDuN7Cj+hFsVyFBaN2uErGvJJWL5jEGvk4FCeFBFK32eTpBxQwe mSu6Xy6krlRZiJHoPe5JLnHMrK2MUSGDQmdzPJRFi+J1a91vfjVMFT4p+iQtTPKPFDc2 jeP2HAevdAOsWpy5sdasYq4ujcWZIYRdtz2eA3wLaMtxmhImfJNEsNrgifPCxpjKqdug Eso4FH9Dq5zFTqsDERPP5pbVjafzgKWzkEG0QbG5HHPmsBa720QK0qSg/udFibbnf8SF qYMVyI+KIL2V/OiuD6qDS+hC6d75v5VBOdPz9ZwZdVkv5VlP5B0I4Mf+0RxGxW/UUIOk pN6g== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=21Y1a1Hg; 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-35163-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-35163-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id b5-20020ac84f05000000b004297b53b3a3si7212053qte.806.2024.01.23.03.09.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 03:09:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-35163-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=21Y1a1Hg; 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-35163-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-35163-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 12C581C23A0F for ; Tue, 23 Jan 2024 11:09:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DF0685EE8A; Tue, 23 Jan 2024 11:08:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="21Y1a1Hg" 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 D5C385D735 for ; Tue, 23 Jan 2024 11:08:16 +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=1706008098; cv=none; b=ESJl8rgLTyPvnv7dl1GikSjOFzjwK6Q16tJrmA8jJM2Vec3pz0WMWCgGO/doNx8o9zbBmgWuvJVhZbPqs6WocsBP+R3QD3uARRfiDgmSjmWUPE8CIzWfGkrfRUd7fa8UWY+rYcBiyeQzuJYHl3odu9vChPhTnWAk9NTGVKKXpvw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706008098; c=relaxed/simple; bh=jzpwCo0HMVI99saI3KiX/DjK1lxlN166EymbCtXFVp8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DuSS/y3feCn+tc2Bq6ZTq7XeWEZYAoOff3ztLQTahv6HSCXH5EwcEpbuUn2qhilAW8pPrIRv1SSlfOQp1LMViZiwVUSaWoFY0MwIEOJYZOMMMcBQkApzFKmtO+X0ibk2uIYTFEzwk6j5SgxdzgGpv8H1nJZ+uog67RY1pQJuayQ= 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=21Y1a1Hg; 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-40e5980dfdfso35097615e9.1 for ; Tue, 23 Jan 2024 03:08:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706008095; x=1706612895; 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=on32GNvkxWDILvYMfSVIpQ/Zr8fKHLMLlVw8UqZ+BYg=; b=21Y1a1HgqV17XDk72W8zaiQUgFOpz+A3umWCn6OacqzZ/FZgortPr4j30Gf+isxEfW +KSZSVeYFt6Ha69275CbVHA6s97JnciucZciv7lyhjfTt4LaXAbfwsj3EVV4r+KWgIiT HTSmNka/4NKIVB5b6vSgF/27Vie6QhFl6xkQtzV81T6iTx0+EthCA3WYGhNReiOHfQAY y1XCNYPukpRu0AI7NvaGLQZjsP8SqXGFF2lq0/BmZIDHmceyE7JwGirmmZAB8kBb4IFX gGN58ITwZB0UAOSlTUxZRG1Zz29S58OdfdFoLAc57ko0dBDjl0KOCTqKRjPgaIK+FjGd f11g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706008095; x=1706612895; 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=on32GNvkxWDILvYMfSVIpQ/Zr8fKHLMLlVw8UqZ+BYg=; b=lKn9aKVBi0mWQvi0vKnvRD7/TlJ5OvYHa5B+DapvfrM/Nx8NsO88dpcrhcRA9p3WdT DuvnqNYWEfx2tewcvKQlIgc1QtRUDOssizpZIGGTlWx1Gsk3PDkdTygD9neFnr7cfT3D dFtJtWcuM254neCd1smi5UUE9FVzlQ1RYwmwRZgEZJYEyZaTgnaAlAq3FJ+5yS37hvr5 CicRWsf5QEHHKwxJd1Iurg7OEBsv0OuvFQQfhEmvdlUUfmcC+oH36a2daMazMhhP/JHj W+UnBaiYDLvrC0MilhP/0F9ReQ7wMCaIkWJG8j4oNamOdmRySyzHFfK8dclYwX2Ch2Wi gZXA== X-Gm-Message-State: AOJu0YzyqEc2/fWTO/9JpMK+y9x6+lSXWfp5m8UtGzkti1X7XYxoz+sZ B7m5ByG8EW+1ty6stKIETKZvwP/5eDKhGfD7CmaT12VKJpWV2tHAMALvSpw7zZ6RD3eIy+riPDM tmYkFRclGJilGjv0GGA== X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a05:600c:5104:b0:40d:8587:3399 with SMTP id o4-20020a05600c510400b0040d85873399mr13616wms.6.1706008095318; Tue, 23 Jan 2024 03:08:15 -0800 (PST) Date: Tue, 23 Jan 2024 11:07:57 +0000 In-Reply-To: <20240123110757.3657908-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: <20240123110757.3657908-1-vdonnefort@google.com> X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240123110757.3657908-7-vdonnefort@google.com> Subject: [PATCH v12 6/6] ring-buffer/selftest: Add ring-buffer mapping test 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 , Shuah Khan , Shuah Khan , linux-kselftest@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788879239910416085 X-GMAIL-MSGID: 1788879239910416085 This test maps a ring-buffer and validate the meta-page after reset and after emitting few events. Cc: Shuah Khan Cc: Shuah Khan Cc: linux-kselftest@vger.kernel.org Signed-off-by: Vincent Donnefort diff --git a/tools/testing/selftests/ring-buffer/Makefile b/tools/testing/selftests/ring-buffer/Makefile new file mode 100644 index 000000000000..627c5fa6d1ab --- /dev/null +++ b/tools/testing/selftests/ring-buffer/Makefile @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: GPL-2.0 +CFLAGS += -Wl,-no-as-needed -Wall +CFLAGS += $(KHDR_INCLUDES) +CFLAGS += -D_GNU_SOURCE + +TEST_GEN_PROGS = map_test + +include ../lib.mk diff --git a/tools/testing/selftests/ring-buffer/config b/tools/testing/selftests/ring-buffer/config new file mode 100644 index 000000000000..ef8214661612 --- /dev/null +++ b/tools/testing/selftests/ring-buffer/config @@ -0,0 +1 @@ +CONFIG_FTRACE=y diff --git a/tools/testing/selftests/ring-buffer/map_test.c b/tools/testing/selftests/ring-buffer/map_test.c new file mode 100644 index 000000000000..ec891e956b83 --- /dev/null +++ b/tools/testing/selftests/ring-buffer/map_test.c @@ -0,0 +1,188 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Ring-buffer memory mapping tests + * + * Copyright (c) 2024 Vincent Donnefort + */ +#include +#include +#include +#include +#include +#include + +#include + +#include +#include + +#include "../user_events/user_events_selftests.h" /* share tracefs setup */ +#include "../kselftest_harness.h" + +#define TRACEFS_ROOT "/sys/kernel/tracing" + +static int __tracefs_write(const char *path, const char *value) +{ + FILE *file; + + file = fopen(path, "w"); + if (!file) + return -1; + + fputs(value, file); + fclose(file); + + return 0; +} + +static int __tracefs_write_int(const char *path, int value) +{ + char *str; + int ret; + + if (asprintf(&str, "%d", value) < 0) + return -1; + + ret = __tracefs_write(path, str); + + free(str); + + return ret; +} + +#define tracefs_write_int(path, value) \ + ASSERT_EQ(__tracefs_write_int((path), (value)), 0) + +static int tracefs_reset(void) +{ + if (__tracefs_write_int(TRACEFS_ROOT"/tracing_on", 0)) + return -1; + if (__tracefs_write_int(TRACEFS_ROOT"/trace", 0)) + return -1; + if (__tracefs_write(TRACEFS_ROOT"/set_event", "")) + return -1; + if (__tracefs_write(TRACEFS_ROOT"/current_tracer", "nop")) + return -1; + + return 0; +} + +FIXTURE(map) { + struct trace_buffer_meta *meta; + void *data; + int cpu_fd; + bool umount; +}; + +FIXTURE_VARIANT(map) { + int subbuf_size; +}; + +FIXTURE_VARIANT_ADD(map, subbuf_size_4k) { + .subbuf_size = 4, +}; + +FIXTURE_VARIANT_ADD(map, subbuf_size_8k) { + .subbuf_size = 8, +}; + +FIXTURE_SETUP(map) +{ + int cpu = sched_getcpu(), page_size = getpagesize(); + unsigned long meta_len, data_len; + char *cpu_path, *message; + bool fail, umount; + cpu_set_t cpu_mask; + void *map; + + if (!tracefs_enabled(&message, &fail, &umount)) { + if (fail) { + TH_LOG("Tracefs setup failed: %s", message); + ASSERT_FALSE(fail); + } + SKIP(return, "Skipping: %s", message); + } + + self->umount = umount; + + ASSERT_GE(cpu, 0); + + ASSERT_EQ(tracefs_reset(), 0); + + tracefs_write_int(TRACEFS_ROOT"/buffer_subbuf_size_kb", variant->subbuf_size); + + ASSERT_GE(asprintf(&cpu_path, + TRACEFS_ROOT"/per_cpu/cpu%d/trace_pipe_raw", + cpu), 0); + + self->cpu_fd = open(cpu_path, O_RDONLY | O_NONBLOCK); + ASSERT_GE(self->cpu_fd, 0); + free(cpu_path); + + map = mmap(NULL, page_size, PROT_READ, MAP_SHARED, self->cpu_fd, 0); + ASSERT_NE(map, MAP_FAILED); + self->meta = (struct trace_buffer_meta *)map; + + meta_len = self->meta->meta_page_size; + data_len = self->meta->subbuf_size * self->meta->nr_subbufs; + + map = mmap(NULL, data_len, PROT_READ, MAP_SHARED, self->cpu_fd, meta_len); + ASSERT_NE(map, MAP_FAILED); + self->data = map; + + /* + * Ensure generated events will be found on this very same ring-buffer. + */ + CPU_ZERO(&cpu_mask); + CPU_SET(cpu, &cpu_mask); + ASSERT_EQ(sched_setaffinity(0, sizeof(cpu_mask), &cpu_mask), 0); +} + +FIXTURE_TEARDOWN(map) +{ + tracefs_reset(); + + if (self->umount) + tracefs_unmount(); + + munmap(self->data, self->meta->subbuf_size * self->meta->nr_subbufs); + munmap(self->meta, self->meta->meta_page_size); + close(self->cpu_fd); +} + +TEST_F(map, meta_page_check) +{ + int cnt = 0; + + ASSERT_EQ(self->meta->entries, 0); + ASSERT_EQ(self->meta->overrun, 0); + ASSERT_EQ(self->meta->read, 0); + ASSERT_EQ(self->meta->subbufs_touched, 0); + ASSERT_EQ(self->meta->subbufs_lost, 0); + + ASSERT_EQ(self->meta->reader.id, 0); + ASSERT_EQ(self->meta->reader.read, 0); + + ASSERT_EQ(ioctl(self->cpu_fd, TRACE_MMAP_IOCTL_GET_READER), 0); + ASSERT_EQ(self->meta->reader.id, 0); + + tracefs_write_int(TRACEFS_ROOT"/tracing_on", 1); + for (int i = 0; i < 16; i++) + tracefs_write_int(TRACEFS_ROOT"/trace_marker", i); +again: + ASSERT_EQ(ioctl(self->cpu_fd, TRACE_MMAP_IOCTL_GET_READER), 0); + + ASSERT_EQ(self->meta->entries, 16); + ASSERT_EQ(self->meta->overrun, 0); + ASSERT_EQ(self->meta->read, 16); + /* subbufs_touched doesn't take into account the commit page */ + ASSERT_EQ(self->meta->subbufs_touched, 0); + ASSERT_EQ(self->meta->subbufs_lost, 0); + + ASSERT_EQ(self->meta->reader.id, 1); + + if (!(cnt++)) + goto again; +} + +TEST_HARNESS_MAIN