From patchwork Thu Jan 26 17:53:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13117564 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 185DDC61DA2 for ; Thu, 26 Jan 2023 17:54:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4CC1F6B007B; Thu, 26 Jan 2023 12:54:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 42BB76B007D; Thu, 26 Jan 2023 12:54:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 238756B007E; Thu, 26 Jan 2023 12:54:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 110016B007B for ; Thu, 26 Jan 2023 12:54:05 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id C5F9A40E4D for ; Thu, 26 Jan 2023 17:54:04 +0000 (UTC) X-FDA: 80397698808.20.6854C64 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) by imf03.hostedemail.com (Postfix) with ESMTP id F169C2001A for ; Thu, 26 Jan 2023 17:54:01 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b="B/hYSsq9"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf03.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.214.173 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674755642; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=OaOZQvWhqFGfMeSz4AFcJuI7rpVT7E7rM49XqO7Em38=; b=bQG+ClQYD3EJQHWZmdtjEBavyn/tKDnMc5yvO8/bGAMSo7LpXdvV6vRG59+zmxijmNNWjz Mu5Lz1i4ydaMa47981ozSeqfKph4XiQFCvrh0SbIQaIFABe1Wv0OZQntQXOGFELH0O+twu EeD5D/vQ8tfGzx6AwWx8rgYxlPr+nr0= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b="B/hYSsq9"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf03.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.214.173 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674755642; a=rsa-sha256; cv=none; b=MH0cJBUqDEuWSlprnEh353wBUqBovciQrA7HaQHMOb2C3oFt9GHY/Qw5G7vG7FkWNjtNCX xL/Cl7IJinx6g2RY0AazoegMrtDRAUzhafLH09bYcbW2qrIhIsAz+9UsQAjOodMXEPCNVc AVmM8jerdOl14OpIW5KxdgnuLnUImjk= Received: by mail-pl1-f173.google.com with SMTP id d9so2514890pll.9 for ; Thu, 26 Jan 2023 09:54:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=OaOZQvWhqFGfMeSz4AFcJuI7rpVT7E7rM49XqO7Em38=; b=B/hYSsq9sCErp5rDPvVr4tJZvj+AMACENONgac2ZHeKYTg5HcOk8v/Ra4+sLpoHDCN sf0Vc3OFvtz84eaLWSTvobQmwPBtll1NGXXWhsN6j7VXK6QJWGPc2q7Ca4n0R9e+kZhK 3rbkmHSDCRJrv6xtcM9b20P/NAcAb8uputKsq9cNv2RKYylLLv6bginLr6Nu+azutNNa UFRODEVRGFwjY4anWiVS0tixCyjhXTMlpZbRVbyIFNF0CodKJReJvltx1bQbPa2fDSgh 3GJBgQm3aqx6L6X052/CPnlDgoc8RnlIb571T9NI4rx3YP87S3mKzIZHRuIrP8Rnba+O A6pA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OaOZQvWhqFGfMeSz4AFcJuI7rpVT7E7rM49XqO7Em38=; b=USyV/eVYqHQbiolSycxBA++8UgNQxU0hzYdFty6pANgvrKaSj6iRdEjvBID40J/u9g n+jFIFzmUCy6oHjzHFxjU7hzxb//xfuaFacRiduDEaGn+XD4/9b6PweuWkc+NFcXycft TUPEmip+u6MHZQ3GEzIe4FAn/oR6XBqsN7clxP53Cy4AhFqbTAp305siqSSc+aqYdnfU po26PDVQjuPPKCd02I707fxqDu0/vSlKqLGFKAsFT1IFFKTVWFTqbepf0geie7s9GfI0 d5SotWPebItQHnYAw1hGt+CsILT3fGiqQWVP1BXMkmP8j25DYwwvv7Sj3FL+DiHVL14J UuZg== X-Gm-Message-State: AO0yUKX324jLTt1ZNQdwo7SKIBIgAHDdkdVmqFsuEM2+xJP5lPVXL/+N 4UistqwQwsjPWLf/evZtXx4= X-Google-Smtp-Source: AK7set9O6cvVx82fGPK3KuyR93xYpfhlMkOJuPTZJ2hj314mdLTUIDVqwddhIl6jgHEVNuRjrCjH0g== X-Received: by 2002:a17:902:e1c5:b0:193:33d4:d509 with SMTP id t5-20020a170902e1c500b0019333d4d509mr2227706pla.30.1674755640857; Thu, 26 Jan 2023 09:54:00 -0800 (PST) Received: from localhost (fwdproxy-prn-003.fbsv.net. [2a03:2880:ff:3::face:b00c]) by smtp.gmail.com with ESMTPSA id z1-20020a1709028f8100b001888cadf8f6sm1278794plo.49.2023.01.26.09.54.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Jan 2023 09:54:00 -0800 (PST) From: Nhat Pham To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, bfoster@redhat.com, willy@infradead.org, linux-api@vger.kernel.org, kernel-team@meta.com Subject: [PATCH v8 3/3] selftests: Add selftests for cachestat Date: Thu, 26 Jan 2023 09:53:56 -0800 Message-Id: <20230126175356.1582123-4-nphamcs@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230126175356.1582123-1-nphamcs@gmail.com> References: <20230126175356.1582123-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: F169C2001A X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: 987rg641khht5toejx7y3gc4swz7u71z X-HE-Tag: 1674755641-867589 X-HE-Meta: U2FsdGVkX1+74af0UAkm19psBMSM2i1d44GuTDev5Lg+YvJ3Vj1Wsx29ko7jg7rc7OPwA3HIPeRnqTh+Vn1ryidmfEgy6ayD0HWRTpBUN23jTJvztZU4p3a8v2JRFO+7O5h6yO8DQEb/Cy4MhsIQlik4IBZd7kNrZYZLRIarkTi3D7iIJ/zQJrg3DX7qqVO713Ci9bMudW8xUq21pySzPb1a+T8BdZkCb2DGSfoz5otHQJe4iiWd+1C2qELpKT5knOrNfeYx8slxzJERypGklyPmBcfTus2f1AL6LjvYdrA6SMd2gYtZw/MJciUa/5EUH4ZLHGvXdPeDojHB9m/MwnVOzBMAcGmVLSk2xU+iaPLqePPXhbaNjLDzDNFZfrsNaXoxgtwCmLTaor1y/IbU9EbttNqNWQDqcNG6P2IHEPFDG0LNaqPx4AQrLiS9ZSG+XuwhHHhhgW6JLB3lWm+PpwnMvLLt+DWzY7s5H/+IpeYMAWOk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Qm6cXbDkF1sLfnKCncWj5cOEWlzwksEJJZmYtFa3VsMaS4xeRG/p7h1qQ0LAaXsgzrKcCw4pSZkpOvNM58BPwN1SIqgwkOzxttDp3fUUt8Wr/b+ZQZx5FP5CKh0U7cWrNjKBJ5btM5TR X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Test cachestat on a newly created file, /dev/ files, and /proc/ files. Also test on a shmem file (which can also be tested with huge pages since tmpfs supports huge pages). Signed-off-by: Nhat Pham --- MAINTAINERS | 7 + tools/testing/selftests/Makefile | 1 + tools/testing/selftests/cachestat/.gitignore | 2 + tools/testing/selftests/cachestat/Makefile | 8 + .../selftests/cachestat/test_cachestat.c | 260 ++++++++++++++++++ 5 files changed, 278 insertions(+) create mode 100644 tools/testing/selftests/cachestat/.gitignore create mode 100644 tools/testing/selftests/cachestat/Makefile create mode 100644 tools/testing/selftests/cachestat/test_cachestat.c diff --git a/MAINTAINERS b/MAINTAINERS index a198da986146..792a866353ec 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -4552,6 +4552,13 @@ S: Supported F: Documentation/filesystems/caching/cachefiles.rst F: fs/cachefiles/ +CACHESTAT: PAGE CACHE STATS FOR A FILE +M: Nhat Pham +M: Johannes Weiner +L: linux-mm@kvack.org +S: Maintained +F: tools/testing/selftests/cachestat/test_cachestat.c + CADENCE MIPI-CSI2 BRIDGES M: Maxime Ripard L: linux-media@vger.kernel.org diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile index 0464b2c6c1e4..3cad0b38c5c2 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile @@ -4,6 +4,7 @@ TARGETS += amd-pstate TARGETS += arm64 TARGETS += bpf TARGETS += breakpoints +TARGETS += cachestat TARGETS += capabilities TARGETS += cgroup TARGETS += clone3 diff --git a/tools/testing/selftests/cachestat/.gitignore b/tools/testing/selftests/cachestat/.gitignore new file mode 100644 index 000000000000..d6c30b43a4bb --- /dev/null +++ b/tools/testing/selftests/cachestat/.gitignore @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0-only +test_cachestat diff --git a/tools/testing/selftests/cachestat/Makefile b/tools/testing/selftests/cachestat/Makefile new file mode 100644 index 000000000000..fca73aaa7d14 --- /dev/null +++ b/tools/testing/selftests/cachestat/Makefile @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: GPL-2.0 +TEST_GEN_PROGS := test_cachestat + +CFLAGS += $(KHDR_INCLUDES) +CFLAGS += -Wall +CFLAGS += -lrt + +include ../lib.mk diff --git a/tools/testing/selftests/cachestat/test_cachestat.c b/tools/testing/selftests/cachestat/test_cachestat.c new file mode 100644 index 000000000000..dc2894028eee --- /dev/null +++ b/tools/testing/selftests/cachestat/test_cachestat.c @@ -0,0 +1,260 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest.h" + +static const char * const dev_files[] = { + "/dev/zero", "/dev/null", "/dev/urandom", + "/proc/version", "/proc" +}; +static const int cachestat_nr = 451; +static const int cstat_version = 1; /* first version */ + +void print_cachestat(struct cachestat *cs) +{ + ksft_print_msg( + "Using cachestat: Cached: %lu, Dirty: %lu, Writeback: %lu, Evicted: %lu, Recently Evicted: %lu\n", + cs->nr_cache, cs->nr_dirty, cs->nr_writeback, + cs->nr_evicted, cs->nr_recently_evicted); +} + +bool write_exactly(int fd, size_t filesize) +{ + char data[filesize]; + bool ret = true; + int random_fd = open("/dev/urandom", O_RDONLY); + + if (random_fd < 0) { + ksft_print_msg("Unable to access urandom.\n"); + ret = false; + goto out; + } else { + int remained = filesize; + char *cursor = data; + + while (remained) { + ssize_t read_len = read(random_fd, cursor, remained); + + if (read_len <= 0) { + ksft_print_msg("Unable to read from urandom.\n"); + ret = false; + goto close_random_fd; + } + + remained -= read_len; + cursor += read_len; + } + + /* write random data to fd */ + remained = filesize; + cursor = data; + while (remained) { + ssize_t write_len = write(fd, cursor, remained); + + if (write_len <= 0) { + ksft_print_msg("Unable write random data to file.\n"); + ret = false; + goto close_random_fd; + } + + remained -= write_len; + cursor += write_len; + } + } + +close_random_fd: + close(random_fd); +out: + return ret; +} + +/* + * Open/create the file at filename, (optionally) write random data to it + * (exactly num_pages), then test the cachestat syscall on this file. + * + * If test_fsync == true, fsync the file, then check the number of dirty + * pages. + */ +bool test_cachestat(const char *filename, bool write_random, bool create, + bool test_fsync, unsigned long num_pages, int open_flags, + mode_t open_mode) +{ + size_t PS = sysconf(_SC_PAGESIZE); + int filesize = num_pages * PS; + bool ret = true; + long syscall_ret; + struct cachestat cs; + + int fd = open(filename, open_flags, open_mode); + + if (fd == -1) { + ksft_print_msg("Unable to create/open file.\n"); + goto out; + } else { + ksft_print_msg("Create/open %s\n", filename); + } + + if (write_random) { + if (!write_exactly(fd, filesize)) { + ksft_print_msg("Unable to access urandom.\n"); + ret = false; + goto out1; + } + } + + syscall_ret = syscall(cachestat_nr, fd, 0, filesize, + cstat_version, &cs, 0); + + ksft_print_msg("Cachestat call returned %ld\n", syscall_ret); + + if (syscall_ret) { + ksft_print_msg("Cachestat returned non-zero.\n"); + ret = false; + goto out1; + + } else { + print_cachestat(&cs); + + if (write_random) { + if (cs.nr_cache + cs.nr_evicted != num_pages) { + ksft_print_msg( + "Total number of cached and evicted pages is off.\n"); + ret = false; + } + } + } + + if (test_fsync) { + if (fsync(fd)) { + ksft_print_msg("fsync fails.\n"); + ret = false; + } else { + syscall_ret = syscall(cachestat_nr, fd, 0, filesize, + cstat_version, &cs, 0); + + ksft_print_msg("Cachestat call (after fsync) returned %ld\n", + syscall_ret); + + if (!syscall_ret) { + print_cachestat(&cs); + + if (cs.nr_dirty) { + ret = false; + ksft_print_msg( + "Number of dirty should be zero after fsync.\n"); + } + } else { + ksft_print_msg("Cachestat (after fsync) returned non-zero.\n"); + ret = false; + goto out1; + } + } + } + +out1: + close(fd); + + if (create) + remove(filename); +out: + return ret; +} + +bool test_cachestat_shmem(void) +{ + size_t PS = sysconf(_SC_PAGESIZE); + size_t filesize = PS * 512 * 2; /* 2 2MB huge pages */ + int syscall_ret; + off_t off = PS; + size_t compute_len = PS * 512; + char *filename = "tmpshmcstat"; + struct cachestat cs; + bool ret = true; + unsigned long num_pages = compute_len / PS; + int fd = shm_open(filename, O_CREAT | O_RDWR, 0600); + + if (fd < 0) { + ksft_print_msg("Unable to create shmem file.\n"); + ret = false; + goto out; + } + + if (ftruncate(fd, filesize)) { + ksft_print_msg("Unable to trucate shmem file.\n"); + ret = false; + goto close_fd; + } + + if (!write_exactly(fd, filesize)) { + ksft_print_msg("Unable to write to shmem file.\n"); + ret = false; + goto close_fd; + } + + syscall_ret = syscall(cachestat_nr, fd, off, compute_len, + cstat_version, &cs, 0); + + if (syscall_ret) { + ksft_print_msg("Cachestat returned non-zero.\n"); + ret = false; + goto close_fd; + } else { + print_cachestat(&cs); + if (cs.nr_cache + cs.nr_evicted != num_pages) { + ksft_print_msg( + "Total number of cached and evicted pages is off.\n"); + ret = false; + } + } + +close_fd: + shm_unlink(filename); +out: + return ret; +} + +int main(void) +{ + int ret = 0; + + for (int i = 0; i < 5; i++) { + const char *dev_filename = dev_files[i]; + + if (test_cachestat(dev_filename, false, false, false, + 4, O_RDONLY, 0400)) + ksft_test_result_pass("cachestat works with %s\n", dev_filename); + else { + ksft_test_result_fail("cachestat fails with %s\n", dev_filename); + ret = 1; + } + } + + if (test_cachestat("tmpfilecachestat", true, true, + true, 4, O_CREAT | O_RDWR, 0400 | 0600)) + ksft_test_result_pass("cachestat works with a normal file\n"); + else { + ksft_test_result_fail("cachestat fails with normal file\n"); + ret = 1; + } + + if (test_cachestat_shmem()) + ksft_test_result_pass("cachestat works with a shmem file\n"); + else { + ksft_test_result_fail("cachestat fails with a shmem file\n"); + ret = 1; + } + + return ret; +}