Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp420759rdb; Mon, 29 Jan 2024 06:29:57 -0800 (PST) X-Google-Smtp-Source: AGHT+IHbQyrfTMC33ZRYgmhppXUx4rTGd9BJ6NKcZcP7CG0M6AE15L9YZYOL/SWXUWFdo15ohkJB X-Received: by 2002:a05:6402:34d4:b0:559:d17f:bceb with SMTP id w20-20020a05640234d400b00559d17fbcebmr4466728edc.26.1706538597702; Mon, 29 Jan 2024 06:29:57 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706538597; cv=pass; d=google.com; s=arc-20160816; b=a+TUuDZt3/sgUXVo+TVq4kWjG5ReXWFFK8r2kdWJe4kv/3OCxBIvGO6oD12Rw7HgTj 4vukgWgbT1PR62V2aCKjAeS8QGCd9GmEscgGWdO9mXJagSY9kDqpEXr3ONwcqeTkRBxu EP7qHea3Nx9oIpLfOgBYob2tjWkOmxv+Q9VNyURxZR6CNRk5WRAd+ZTR7pkd3eajQIkm ZypeKbx+rrqhyPe6CmtM4gOQbzhL51NRkmq8cdC/0A4xdaxeIMk79Aa+vTf48pbRo9oY 3s/KFrjFJ8NzifryrVB0A9KG4F6aWWIYnqX40nJeMUTmw8HCXt9qbBup4GOzLF8zH3tX pfWA== 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=Rnsou9fzvhKkj/SAVsoOVdFIhdcGr/3PZjija/s4rd4=; fh=YQhVWO2XL9584FKmBhrq7yC9i9qkutxA7K29vY+DUJE=; b=frsVMDKmzlN7AtAArk3lrODNNXOrrMDhRibrf2ceMk9xyxYT/3/eqh8KeG7QxkpzGX Q/fpnRta93cG0bbDTWfquXH7bN2x0HU1CZvVSdbR6HarBSfFP3bsdI0vtM8aI4gFOmmX KTITX9t8V3rvw2whnPBThTrI3Fzav2z+Gi3T7eyZWi+lHinbjF/jQkTuwKQka6ZeijOX CZEydiJCuRK+kPpB6Zh8pS7JwE7WysrYCmk6BKp60IDBNsi4RPCuyoNT/GIiy1FNVR/N OudNQfQt8fwrQGP0/rqFfwhwNikeronbC9MnLPJJbBCUPaj3AheTypbTfkut+Y1GgDMS KR1A== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=AnjEfbgE; 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-42904-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-42904-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id ek22-20020a056402371600b0055cd006bbc6si3555381edb.300.2024.01.29.06.29.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jan 2024 06:29:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-42904-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=AnjEfbgE; 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-42904-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-42904-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 465BB1F253BF for ; Mon, 29 Jan 2024 14:29:57 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CEB9512CD9D; Mon, 29 Jan 2024 14:28:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="AnjEfbgE" 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 04C2612C54D for ; Mon, 29 Jan 2024 14:28:25 +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=1706538507; cv=none; b=iWNVM2Cl7Q7YrLvaxzacAdArTAsouSzrJJdNcFBydQ5kDBc7qYZcy4E/fmUIIzwG8h+jvUf41EVV8aJ0iJ1vrOsJWcZwcYJ+XdPJnvYWUiqBYsJ8uL2P+6BKK01qPcrw42FNE19k7L/FyEvVPqxME5UyD5cvjO8vLKrUPYiB47M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706538507; c=relaxed/simple; bh=3esjCeT5ERj+IquIYdRX9jRIVPfXD/PfIosNKFpR5DM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RU01XOzOZ/ixRnCP8LtbW/ghTr/C0wOpMuvBYc3BuDtkk8f/E59euy6q/inXhKQTPlSUanQBpxI3/1dNZL2xa/o0/1C5eYA1R4EhvFbD94cM3mZmnGhT5kfiImme0P/c7zadhqU6tn1hnnGZY09iFEABXoQxmCrYk1geS1d8D7w= 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=AnjEfbgE; 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-40ef9dea0d1so3986705e9.1 for ; Mon, 29 Jan 2024 06:28:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706538504; x=1707143304; 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=Rnsou9fzvhKkj/SAVsoOVdFIhdcGr/3PZjija/s4rd4=; b=AnjEfbgE1cDL5V6ICp19J8sEyNlku8X9iy5XnpuOe+EgrSyyge4o8ONw7LzzGsIk5V eKELiQ+VRyVksLyd4h/+SXYoOHInwy1iA/Unc2Z+cva895sdPjTOZLteIShb5VZ0Iuyk N26V1QIYKKVG6Qwdhs1CXk9B9SkKbhxRWYzDX/27bQOjrrQP92PT5wrWttUz3shj+wrt DmjWoCqRxAmPKy9dBvh3mRd7KAhYTayFpOKhHjhgXZxCrmzvtN26fejOkCkdXGwu1ER3 eq1ZcIpjrufhDBXi3gfoVjYIpV59Dpcc+azCyEkpKHlLz7NUo2/Ghe2PqcGUIkjOtwL/ YT/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706538504; x=1707143304; 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=Rnsou9fzvhKkj/SAVsoOVdFIhdcGr/3PZjija/s4rd4=; b=LutPyAWwAiS0SRuuOUkSlCS9Z2J1s7pLUP/Fsj6bvLOcOgWMVVwMHC9O/4kxvL2Aed ks2q5wGU8Bm3rbsVgRXE3P0eZhwWVIVmBGJ+zDInj/l4eVp1nxsyKFbz9smYZaScVXn9 6f/ODJMfFBuJ6D/dTsqieMoKtX6dnMnzWrvgOwP37RtbFGgur2u9OdoTZP++T9k+nvK1 8IqoEsUJxVrDlkZKpN1fIlU6mCyWFWXrHHtocjL9DRRmkN80v4Wa9FRkND2misnABeij KShQ5b8zwmPYgGU98nNBEmxi4FpXtEMRR5QOT35S8vAQIAUVYDs1xOSzp/1SYIH9drHJ Wegg== X-Gm-Message-State: AOJu0Yyzy/jNnGfnn7ax7qLcEoRRh8e/hwojcBMZlnDS4LQ7Q7ydcTID QD6dIrJ8lt5e2WvbkH+wYAT0dMJ+Uh0O4fBKH0Z4JNOUNC+fiQjDPQDL7Hz95rYGEWHTSGsExSx KI4P+TDRxEdt/NM90Kg== X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a05:600c:1f0c:b0:40e:fb3a:d85e with SMTP id bd12-20020a05600c1f0c00b0040efb3ad85emr3588wmb.5.1706538504423; Mon, 29 Jan 2024 06:28:24 -0800 (PST) Date: Mon, 29 Jan 2024 14:28:02 +0000 In-Reply-To: <20240129142802.2145305-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: <20240129142802.2145305-1-vdonnefort@google.com> X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240129142802.2145305-7-vdonnefort@google.com> Subject: [PATCH v13 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 Content-Type: text/plain; charset="UTF-8" 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..e3220f8d618b --- /dev/null +++ b/tools/testing/selftests/ring-buffer/map_test.c @@ -0,0 +1,183 @@ +// 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->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); + + ASSERT_EQ(self->meta->reader.id, 1); + + if (!(cnt++)) + goto again; +} + +TEST_HARNESS_MAIN -- 2.43.0.429.g432eaa2c6b-goog