From patchwork Wed Jan 4 23:10:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13089184 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 89F53C53210 for ; Wed, 4 Jan 2023 23:11:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A509A900004; Wed, 4 Jan 2023 18:11:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9B516900002; Wed, 4 Jan 2023 18:11:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6CBE6900004; Wed, 4 Jan 2023 18:11:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 5AAEF900002 for ; Wed, 4 Jan 2023 18:11:05 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 2F929AB27B for ; Wed, 4 Jan 2023 23:11:05 +0000 (UTC) X-FDA: 80318664090.08.D614277 Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) by imf10.hostedemail.com (Postfix) with ESMTP id 82546C0005 for ; Wed, 4 Jan 2023 23:11:03 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=WIDhvVYh; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf10.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.216.43 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=1672873863; 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=FG5G46Dbq/DHdzwesOF9acAVNcEs0Ihso0tlt5RXEDY=; b=tOWoe8tq4jc4kOcQ9FPKvP3C71waqhN5aqRFw1BcToZeGnRmvyX5SMkUE1xB7Jvclw465m ICKwPuCj0u4sZCpNBqgA1LVKUa7NcFJ6eAGA4Ib5B8zV45w1qP+zaAhT+BHSVg7g2Ce0Ai /Cf7h7mGQ1x3BHX0xGq+xXflY7dmoKU= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=WIDhvVYh; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf10.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.216.43 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1672873863; a=rsa-sha256; cv=none; b=TKHe52rvqHtqdwANXBHuLSdhqRpk98AqmuRrrF9oudmfuN4NT1TJFVqrUDDeO92EpKYIwR hK6uzXAXii4XbX0xe/L+Jg60LXugpmfNCDk39w5SRSwrsA0TMRV9Ez3k9CpruHoIp35gaX EMhLRfnDQ6ud0lxdg9ezZCgufwopm5U= Received: by mail-pj1-f43.google.com with SMTP id z9-20020a17090a468900b00226b6e7aeeaso166322pjf.1 for ; Wed, 04 Jan 2023 15:11:03 -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=FG5G46Dbq/DHdzwesOF9acAVNcEs0Ihso0tlt5RXEDY=; b=WIDhvVYhs2FE7ZQ5xWM0noBKQ3k9CXmQuhR0dbjdNmgRmJR82rAVdMO1exBb136OB2 6CvgPjPYnksfFK1o1bp0DINiiVxYGtxtREE/MlaN9+4JqAnDXJNHIagemEBOAwVpHbKX eV+poEwKiXgcCyZAYf43B3OpqFJTBVHyBgEsH1P1/n8119F9qyMMuAIkZA/WdSngchVB qpk6ElRtu6F08yJW1GmikXPXW+0et3Rkt+M0Sq/bJoF6xC3n0CNEwhh5e8eujv8bJrqx GnlPxDA3vWDpBZnmcAnylfnTyLh58qQeAIn4WjgTqUGd08Iicc+m9h6qj3al4NcYI9kK regg== 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=FG5G46Dbq/DHdzwesOF9acAVNcEs0Ihso0tlt5RXEDY=; b=6gkDpOinBePCzetb0Kt03LMsJkwzNx4nXJFna1gjtSLKQ40m2sWUKid9/OVtmIA39F 9VAE9sTDblQAe7DHZbNuUigqec7oI4Tcvp9sEU1ji5O6bFAIxJft3WPx3LnDAU1VwdZe C96KLtwsX2EPhj9ZVoV+qC8OiPx3b7pGRiUtPEOJAAQNRAj3aQHDLFKP/5hPbZFLvmEU pw5uJHujx/cMEGwW2HJaQulG5VzxEC/ZVjeTuP+UHw3KcVG7Io/9n/s+/F6X327iXSeC g0J7MjIWzv8ekTHHoUXZXfjMFFuRWTPD1DBTPh5Ktp8bNri5UpeyrkM/gEHI9zonvxbT /GgQ== X-Gm-Message-State: AFqh2kp7zlHRnEIW3kOmJab48KwVN531taKU18rVW+BsxijA7EvF8jSA N5VXZ8YxAVViUkNQHqeP6cs= X-Google-Smtp-Source: AMrXdXvBp8s2Vpran5EiOYoUYHH6mteg+SXzxnUEneJ6AhQUEfHsB/O01BwKp6ruuM/6uIOQTR1b4Q== X-Received: by 2002:a17:903:2792:b0:192:8c1a:1bea with SMTP id jw18-20020a170903279200b001928c1a1beamr33323919plb.43.1672873862380; Wed, 04 Jan 2023 15:11:02 -0800 (PST) Received: from localhost (fwdproxy-prn-023.fbsv.net. [2a03:2880:ff:17::face:b00c]) by smtp.gmail.com with ESMTPSA id h19-20020a170902eed300b00186c5e8a8d7sm15421221plb.171.2023.01.04.15.11.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Jan 2023 15:11:01 -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, kernel-team@meta.com Subject: [PATCH v5 3/3] selftests: Add selftests for cachestat Date: Wed, 4 Jan 2023 15:10:57 -0800 Message-Id: <20230104231057.2632639-4-nphamcs@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230104231057.2632639-1-nphamcs@gmail.com> References: <20230104231057.2632639-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 82546C0005 X-Stat-Signature: hw38du11yitrd7zydrq1oy9etyffnts8 X-HE-Tag: 1672873863-532719 X-HE-Meta: U2FsdGVkX19e1VWiLqhvXMPu8/Xpo8gtKj+N8M+Ut6aDFdrExHG4qX4kFtljbaCRID8BC5xO6o+Uk7XkLv2zeI2wVeRgABOpyt0+Vdz17gMz5Iviq/hKpc2e6UXnAlzKNnX9UK/qbDna6plqCSBwZ5c6QqYd2YYNGsD5Tp+EWcQEMiQcP6Z3FCpfcPg7ukkh7ffdQzTqrVGP/wXg95EEW3GtZEYyl0DqCJpIPz99eSXCgiIs/ZGh4K7c5fMwAPSsohsk8GYR5wTRhhK44y6P0WQCQOHgt8ibKP9F1cEvYlKi+yFYZANFIy2T0PnGxwBp2BKk1lBiPcijPo446EODvuh2IXaufyTt0pHkY79gkD7RiFIzgCS1A5QocdwGbMNtP4UD55W+QyUY138a0WFEH+XdcjdJb8nev34zGI6duLcyrDpSaLEDiU4KJSukmki7ZlOGgD4a/aQhQRnKWcJekUvH6tEInhMKy/4Vqpm4k1CNRLDD3d+kQMPCq1J6h2oQzDOwAHARJh1hZwJY4g3egJdmEJyeTWjnczpMfXtSTiUBkweUZAkEyUIb8O6EcMhtL7ub85Ie6cY365aP4OadDIiB+mcgN5cJ8QAN0LA0pzqOENIS/u6rmOfOYUsmZRQ2TRiYej31uZQU1dEQFsR//WCrmWoMMivJna/blMvU3QhZYjNSsHjR7h3JnWmlUvmuOC6twc+LxpUifGjMSZchVsUUjhbN9oFNpTpI1W2rBQ7djbxu7goXpNU//n9SAeKP+lr1sPGy/UUzNPIjyX2g9CGMxi21T0nZXkDg7QDUK/TJnIb0AMtj0GpfDIOioqHCKjIRcZv61OlyC7Erbb0630avpVJ8vUv1lL7fnRIjYH0ERbNl5NMegb2e/nLVJ0gM9QoDan1LFeUNFb97wbomAMvWADrFnhI9s6gRLEIQfGnb3zRayesj6iwRwG9Gdbb5kWTES8I4EBi0jKreHZp bEiLQrwN o2TZGGduqavsJJ8EZBA/fWdsYE/SqbQKvnr4koL6B8s8WznaxX7S16obim7uDXQmU2uJtGWPlSxpgy9RCfxGIdSl8kidsQluYB5PsGX/ts8KICad04C0p4l023TNnfaWZy2Ar 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 | 259 ++++++++++++++++++ 5 files changed, 277 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..3f5cd5695c51 --- /dev/null +++ b/tools/testing/selftests/cachestat/test_cachestat.c @@ -0,0 +1,259 @@ +// 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"); + 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, + sizeof(struct cachestat), &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, + sizeof(struct cachestat), &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, + sizeof(struct cachestat), &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; +}