From patchwork Mon Dec 5 17:51:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13064931 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 7ECBFC4708E for ; Mon, 5 Dec 2022 17:51:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4C1CB8E0007; Mon, 5 Dec 2022 12:51:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 473088E0001; Mon, 5 Dec 2022 12:51:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 275918E0007; Mon, 5 Dec 2022 12:51:50 -0500 (EST) 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 148968E0001 for ; Mon, 5 Dec 2022 12:51:50 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id DCA81805A4 for ; Mon, 5 Dec 2022 17:51:49 +0000 (UTC) X-FDA: 80208995538.15.D96E916 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) by imf24.hostedemail.com (Postfix) with ESMTP id 7C007180009 for ; Mon, 5 Dec 2022 17:51:49 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=hEPkWYZK; spf=pass (imf24.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.214.181 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=1670262709; 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=Han8q7FoqziKRrDCsnqa8WckqolCToj11E76NTsZ6WI=; b=PVmDOVH0uvsqevJvLST5nkaGf7XiZT+5O6lWzYvzf0qAKXcRMf+uHb0BfkaXJFePxrYanh qW+9cRoPdi+LDIuaU17BfFGgGDWu6T+eMWwiIj4jOq8T41CGXzekfxYvKuAUpjG4iZ/3QU bMlH/Zmnl1sia6UC/FqnTfF/y8NIdP8= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=hEPkWYZK; spf=pass (imf24.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.214.181 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=1670262709; a=rsa-sha256; cv=none; b=Ss4sXkz57ISYKZ2k2jS9ZVwrNKQpaFWm0TQYnEEaYy0cgqlFZCEGuwZxOMJ7WI3GmUACWx 3AFquLmTUoWms97w+HEQvJgq5BVj9mkUs5yrZiTtGmHo6QdNVD5BLVzFjj9i8bONphY9Xb uKer6d5uamRnigLJTykdSYjBDk5GYh4= Received: by mail-pl1-f181.google.com with SMTP id w23so11508682ply.12 for ; Mon, 05 Dec 2022 09:51:49 -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=Han8q7FoqziKRrDCsnqa8WckqolCToj11E76NTsZ6WI=; b=hEPkWYZKlr36zkFuUBGQYmt3IUTD8l8z6Yjwu8TNj40vcXhAKSX3UvOySIlQHS8Fqq Z+Sz2xOdpuUi1XqQ6wf9TDuMMIJbGWAAuwatsfpzVTtec0Ub9WRKnRo82eZyJNdY/6Wo T8xRy+dlnmznXSjVCmnBCW8RPMao/atFS0S8rHLzqjheoIJF3/pAaKvdRCJWeUD3OJPh zQ3KhRaPf6IFWA/LZ3na5eLTbVahhmR1oElaoIjwGY5LAd/sxizjsCGCAmxmCIKGxw1p zxcsF86E3UsuzpemuIIkBhOtgMUsd+zoSyhbcyVvXdnxVUgWDr5ISafhrEHZsFfM3C0Y T6OA== 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=Han8q7FoqziKRrDCsnqa8WckqolCToj11E76NTsZ6WI=; b=XAo9HpIYbxlmk4IqdM+WrmUZyX34l2A5/4zqH00RVOY5euqYEGbRlfNE909LbLTIY6 MszZzgbsv2AX00iWw5dfrKrp2nl3vN1jYmTuVfCLohG7IIA9koXCxPkr3jcfWpiToJDO kt11SSVf9XodPQL/boY3FbbPvOkyyFtiOl/y6zfaun4sSpSA43BG0YuMa0epc4eexgKY yG7EcANMAuW8badcazM9ts3INWdgfbaIqQn+BdOvaW1DZznLWNEIsT235L30rIptx6Y7 0/053bCkQVZF0ilMgn28UnYDL0CUoVElZLE5g/Ao9/9/y0EpUqjX4yGyqzj/ZHLswq7G YEdw== X-Gm-Message-State: ANoB5plSCX2Ri0XI4qPkFHFXF2qbTYCs/4Nc9zbezRSyYW5M+yi7//En SjidSywChS9T3Au2gDat+60= X-Google-Smtp-Source: AA0mqf5lvaQy63EDV2t6S9BrgJR2GdaVOFzC/2UQZ+U6GNvKP7plvXWwuWiTfq/65vZrehbecl7LBQ== X-Received: by 2002:a17:902:f092:b0:189:9b43:a082 with SMTP id p18-20020a170902f09200b001899b43a082mr32749897pla.95.1670262708507; Mon, 05 Dec 2022 09:51:48 -0800 (PST) Received: from localhost (fwdproxy-prn-118.fbsv.net. [2a03:2880:ff:76::face:b00c]) by smtp.gmail.com with ESMTPSA id r19-20020a6560d3000000b0043c732e1536sm8576715pgv.45.2022.12.05.09.51.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Dec 2022 09:51:48 -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 v2 4/4] selftests: Add selftests for cachestat Date: Mon, 5 Dec 2022 09:51:40 -0800 Message-Id: <20221205175140.1543229-5-nphamcs@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221205175140.1543229-1-nphamcs@gmail.com> References: <20221205175140.1543229-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 7C007180009 X-Rspam-User: X-Stat-Signature: hstrzrttxf14xz953aysyd8byen1wysf X-Spamd-Result: default: False [0.60 / 9.00]; BAYES_HAM(-6.00)[100.00%]; SORBS_IRL_BL(3.00)[209.85.214.181:from]; R_MISSING_CHARSET(2.50)[]; MID_CONTAINS_FROM(1.00)[]; MIME_GOOD(-0.10)[text/plain]; RCVD_NO_TLS_LAST(0.10)[]; BAD_REP_POLICIES(0.10)[]; PREVIOUSLY_DELIVERED(0.00)[linux-mm@kvack.org]; R_DKIM_ALLOW(0.00)[gmail.com:s=20210112]; RCPT_COUNT_SEVEN(0.00)[7]; RCVD_VIA_SMTP_AUTH(0.00)[]; MIME_TRACE(0.00)[0:+]; FROM_EQ_ENVFROM(0.00)[]; R_SPF_ALLOW(0.00)[+ip4:209.85.128.0/17:c]; DMARC_POLICY_ALLOW(0.00)[gmail.com,none]; TO_MATCH_ENVRCPT_SOME(0.00)[]; TO_DN_NONE(0.00)[]; DKIM_TRACE(0.00)[gmail.com:+]; ARC_SIGNED(0.00)[hostedemail.com:s=arc-20220608:i=1]; RCVD_COUNT_THREE(0.00)[3]; FROM_HAS_DN(0.00)[]; ARC_NA(0.00)[] X-HE-Tag: 1670262709-108936 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. Signed-off-by: Nhat Pham --- MAINTAINERS | 1 + tools/testing/selftests/Makefile | 1 + tools/testing/selftests/cachestat/.gitignore | 2 + tools/testing/selftests/cachestat/Makefile | 7 + .../selftests/cachestat/test_cachestat.c | 186 ++++++++++++++++++ 5 files changed, 197 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 -- 2.30.2 diff --git a/MAINTAINERS b/MAINTAINERS index baa081a1fe52..1b8bc6151e86 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -4558,6 +4558,7 @@ M: Johannes Weiner L: linux-mm@kvack.org S: Maintained F: mm/cachestat.c +F: tools/testing/selftests/cachestat/test_cachestat.c CADENCE MIPI-CSI2 BRIDGES M: Maxime Ripard 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..04e84fd7d8ce --- /dev/null +++ b/tools/testing/selftests/cachestat/Makefile @@ -0,0 +1,7 @@ +# SPDX-License-Identifier: GPL-2.0 +TEST_GEN_PROGS := test_cachestat + +CFLAGS += $(KHDR_INCLUDES) +CFLAGS += -Wall + +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..120f42ba3a5d --- /dev/null +++ b/tools/testing/selftests/cachestat/test_cachestat.c @@ -0,0 +1,186 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE + +#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" +}; + +/* + * 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, int num_pages, int open_flags, mode_t open_mode) +{ + int cachestat_nr = 451; + size_t PS = sysconf(_SC_PAGESIZE); + int filesize = num_pages * PS; + bool ret = true; + int random_fd; + 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) { + char data[filesize]; + + random_fd = open("/dev/urandom", O_RDONLY); + + if (random_fd < 0) { + ksft_print_msg("Unable to access urandom.\n"); + ret = false; + goto out1; + } 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 out2; + } + + 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 out2; + } + + remained -= write_len; + cursor += write_len; + } + } + } + + syscall_ret = syscall(cachestat_nr, fd, 0, filesize, + sizeof(struct cachestat), &cs); + + ksft_print_msg("Cachestat call returned %ld\n", syscall_ret); + + if (syscall_ret) { + ksft_print_msg("Cachestat returned non-zero.\n"); + ret = false; + + if (write_random) + goto out2; + else + goto out1; + + } else { + 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); + + 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); + + ksft_print_msg("Cachestat call (after fsync) returned %ld\n", + syscall_ret); + + if (!syscall_ret) { + 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); + + 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; + + if (write_random) + goto out2; + else + goto out1; + } + } + } + +out2: + if (write_random) + close(random_fd); +out1: + close(fd); + + if (create) + remove(filename); +out: + return ret; +} + +int main(void) +{ + 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); + } + + 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"); + + return 0; +}