From mboxrd@z Thu Jan 1 00:00:00 1970 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 369DEC77B76 for ; Fri, 21 Apr 2023 23:14:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B5F036B0074; Fri, 21 Apr 2023 19:14:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AE7436B0075; Fri, 21 Apr 2023 19:14:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8CF876B007B; Fri, 21 Apr 2023 19:14:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 7B03F6B0074 for ; Fri, 21 Apr 2023 19:14:30 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 7C0D7406D4 for ; Fri, 21 Apr 2023 23:14:29 +0000 (UTC) X-FDA: 80706954258.27.6098B60 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf26.hostedemail.com (Postfix) with ESMTP id A78F7140014 for ; Fri, 21 Apr 2023 23:14:27 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b="gHAmo/FT"; spf=pass (imf26.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1682118867; 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=hcShAfvpt14hVWyDNGhMZ2wZHOjCKrr8AIiOuxdUffs=; b=bkDQwlbG+kjlrrMTB1j1vQtn3W4gw/X+VBZ/K/8hbyuIY6Byn6RiIrYlcOoXtX7DWQU20X CeiJ/KU6Fo+Ya278qr4RHh3ZPrGI/Mfb6D/g16VhJHASqv8bNajVybrCJeYQUdiTM2EYXB 1HRXVcxUw+UGRNuqtIU4LyNZZBlGUVA= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b="gHAmo/FT"; spf=pass (imf26.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682118867; a=rsa-sha256; cv=none; b=t/EdQoxcLc+/9hY1GhN6dbeGG5rvA9yMZD1yPGpzbEFtd2oSx5X4D3u6eXur13IbFj9YVh ZcoHqkiWJKF/eayz1uf6h2JN2gX2B4hT6DQyoCsMs7hz84gMK97AVu3qaeyDZIdHW+OB6w E0TAvmxgO9Fd4bXHNPg6OUo8OWZgSbU= Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1a6ebc66ca4so21952175ad.3 for ; Fri, 21 Apr 2023 16:14:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1682118866; x=1684710866; 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=hcShAfvpt14hVWyDNGhMZ2wZHOjCKrr8AIiOuxdUffs=; b=gHAmo/FT5zsct2dfOsiGtdfgb6mGYcAxkp75r0xMgUgOkWsYYqDQcd9aIimlWSBLUB cBDc+RyMsI3zey55MCJ5kj2wrSiMJVuvWLsPnEuV+3hqJVgjHpCJvrEq24UCEjLdrkAn F3AIJPPlMWlyvvwUFHIw0NuDVsWyyDCLPkIdfIKd6X5qnlKEsst0l7zciff/aN3JICu8 n4K52O6wQjclkG43t+vtACDpOWda1iz0QBZ2edOz4JlelSpLqyCXvXGjMKdh1Ijah35O 6qYx3bcIdxbWZhU4LvVUaKYfyfHK7nvtoMi69eYZBpdetlVMG9nJCGUexR59SevQpW/E 9fsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682118866; x=1684710866; 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=hcShAfvpt14hVWyDNGhMZ2wZHOjCKrr8AIiOuxdUffs=; b=WbOHk2DELGhjda72FiJ7uVqMKLNJMNhOu4OtE5DAkV6RIuy7GDf7TyuwWiqGDhiNUW mwGR/Cyylm9nJ+NJrV5DKWx6owvFXhbMD5U2odS5dYn2JrbIpE96u4Oef5SF6c0SA3bI k3AJFXFx2cdrmAHD60gjNrHYuRetgwaq/RO1am1yWGeKNKYfvwB0Uv5L/0Af93aZIlQ5 0YDSLUFKc5YmqvjHRp/5uyqtoQNSVhRh2rgLSJKRVMNvuaBKRUCtD+XnLVrpBRWCUlKO BHWx6wo7vcSgOWqxmbZ9CGsASu4CvDwfplepwx/nbhZValRqcM2O/DYcPu969rs1jLO9 iGhw== X-Gm-Message-State: AAQBX9d4B7i1c0pQM1YNa7R/rCkagLRDT4YBoOXu9XBveQSXFaaVsVM/ gC//wmpg1Vqx/79Ly2jk7IE= X-Google-Smtp-Source: AKy350YgTMDAtnhPqORjI9DRY2XqHp2DJb8nG2cVc/pEfKHX+Ss38HNjkYMaclutCF04f4Xgk2OR1w== X-Received: by 2002:a17:902:c40f:b0:1a1:bede:5e4a with SMTP id k15-20020a170902c40f00b001a1bede5e4amr8394083plk.49.1682118866560; Fri, 21 Apr 2023 16:14:26 -0700 (PDT) Received: from localhost (fwdproxy-prn-002.fbsv.net. [2a03:2880:ff:2::face:b00c]) by smtp.gmail.com with ESMTPSA id ij8-20020a170902ab4800b0019c93ee6902sm3171579plb.109.2023.04.21.16.14.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Apr 2023 16:14:26 -0700 (PDT) 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 v12 3/3] selftests: Add selftests for cachestat Date: Fri, 21 Apr 2023 16:14:21 -0700 Message-Id: <20230421231421.2401346-4-nphamcs@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230421231421.2401346-1-nphamcs@gmail.com> References: <20230421231421.2401346-1-nphamcs@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: A78F7140014 X-Stat-Signature: outynxmync89icgt1ajdeydrnuxipxwn X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1682118867-312047 X-HE-Meta: U2FsdGVkX1+ovPRSpGxeTsN5WleyayBHWzQePqv3bGdy7t8IpvWqSk2aUOuF5WAC7rndpSai1qGnwXlVnBl0bpwHpFOQIHAfbzSOOwOa2EXOgO47ehQTopqf18qdr4hueh7qYWd/7aePtVD5CCL/JFfDC2oH3glGR+52oqEqOQNPS3pJM+qU9Eh2A0ZRuQ/At+7rtheJvq/4PZfccgNIpEH4gslWmUEYkpc3P+EDGcxZYZUkzSUdedzUWy5okM6KJ+Cz0ZQqosPYuooy21dLT2PT2X/bfGj9J792N/x/r0AFXfhrNRkQM0TBbvZiR4itg0EWGhfb7XEiGMJGzemVp0oxFb1SFfDfrYvffsb7ain3wtoMtPMNTzZOxpqEOOCY2sF7eQu9vMG0vVnThTDDg/Lx1kmJZlYRIF5Nptu4sfpB6n10GEOkr0iD4ta6FTNgN6eMAKZ5a/guuo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LOTq6iWcfE8pdeR52E8Iike7ugKj82FVholB2199GgNJ/eZRCSF 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 | 257 ++++++++++++++++++ 5 files changed, 275 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 1b48ddff4159..072159a79e57 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -4490,6 +4490,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 13a6837a0c6b..93dbf0f0e814 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..f62ffb30f8ca --- /dev/null +++ b/tools/testing/selftests/cachestat/test_cachestat.c @@ -0,0 +1,257 @@ +// 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; + +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"); + ret = false; + 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, filesize, 0, &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, filesize, 0, &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, compute_len, off, &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; +} -- 2.34.1