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 EC06CC7115C for ; Wed, 25 Jun 2025 04:58:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5C52B8D0005; Wed, 25 Jun 2025 00:58:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 59C9B8D0001; Wed, 25 Jun 2025 00:58:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4B31D8D0005; Wed, 25 Jun 2025 00:58:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 390A68D0001 for ; Wed, 25 Jun 2025 00:58:53 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id DF544BF3D4 for ; Wed, 25 Jun 2025 04:58:52 +0000 (UTC) X-FDA: 83592718104.20.3795A47 Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) by imf27.hostedemail.com (Postfix) with ESMTP id 0066340004 for ; Wed, 25 Jun 2025 04:58:50 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=iHhijXbL; spf=pass (imf27.hostedemail.com: domain of suresh.k.chandrappa@gmail.com designates 209.85.221.48 as permitted sender) smtp.mailfrom=suresh.k.chandrappa@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=1750827531; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Tphu/Z/bXlYZSKeS4hM0dMTRZKxU6pwgJDcgjThU9Mk=; b=5tmekawT9EWr8rz1RQ+BQvtr+KGvWlL5Mv0MgKP6aOecfVEuEHfthicfkQoD0x4qqSKRn6 AUUCBwj/Vi8eYeizl3Cb7qOWzl2GVoQvM0vDvgg2o2RdxAzJ/+32LkXCzYYox3ufX2+Q0A STrKAifu0KU3D+v6bEiMqUqa+/375JU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1750827531; a=rsa-sha256; cv=none; b=nNcqkbUr481zuFRiSZ03HNeeZbkz7AA66VsfZefZ48GtPfJyb9NP9LOysEmyKJylL/n1BH IJ7Uaz6dvLJZRE90mwd5xYwhT0youSaY76Jp7mvve0F+KE/m7C7jXfFLepJmFE9WO0OzlQ tqu5skofxi8qHZyA67OuUl1848iI2bE= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=iHhijXbL; spf=pass (imf27.hostedemail.com: domain of suresh.k.chandrappa@gmail.com designates 209.85.221.48 as permitted sender) smtp.mailfrom=suresh.k.chandrappa@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-3a50fc819f2so1031834f8f.2 for ; Tue, 24 Jun 2025 21:58:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750827529; x=1751432329; darn=kvack.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=Tphu/Z/bXlYZSKeS4hM0dMTRZKxU6pwgJDcgjThU9Mk=; b=iHhijXbLGVnnqJ2WvTVWDyaiusoMtX/9STECXX7Cq1g5tPxURUQHinmghQvGgeOP/L cA9ZpF9NSm2P2Dw6B8aM9KLxsVkpt9ZbFeSiMwjcuwfPDpBWSMnpWPmZ74udCEltjo7e mHrnhpaffzfin+nuwkRmpkZ1tK8FPpOwN7ro9tcMXbwrFUpCUQgoGjiwXzSHH00Ri0cK C4gODTW8GRur7ZqN6SfEczvJeYrgy1IFMxEpQIinZUjb25I9ze11wS1UJAh6vH4rXlX4 Dh2hOiozRBxnmFMC6Pxn1lm+t3r4lKeAmzg3soqP1xIKHoY/O+p/V628zOQRghLGBAlE lAIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750827529; x=1751432329; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Tphu/Z/bXlYZSKeS4hM0dMTRZKxU6pwgJDcgjThU9Mk=; b=L5p4koOsX1DZY9e5ro359cfxWX6g4jcv8T+c3RZLKRACwLStwy6NfQtY1CRHZajZHC BZakAOj3ez9N8U+5hfToBv6D7y8lEsf2h8egSphuIsUqIj7sgr+PjLsZ9EV+8lvUlq/t l81Z7jaR6wn+f3raRsmvB8VSVGY6AgKSvP7pGkqbHjA4XUod90t1NPczGg9q5w0cQOe5 +GfR+NDyz5RscUWBq+aVEsD1ilMH0PuOB/cI20KB29n2mqqaNzvJT0IVm7gcBs9WwZfQ l46Tu5ZdmEExU9JMcmKYCq64jskTJzmQMhGDnbGw5y3KRpCNnVOd27aD1h3jvbzNnLc9 9SiA== X-Gm-Message-State: AOJu0Yzs0M4iIEVm1PDvJANgCvHN9bh/iE/UToftSbkoXi9Ul1S+mhqy 67OjYv5NTeu0Rg3TRvVP+w6LxZ8wI7ZNn+qF56Yg3B8uyxMt0YkhlwF3tWH+fyQNzGMz/1SicMP vcmqV+chgpZKvZPzxfVV8Y2vuy9tEm3s= X-Gm-Gg: ASbGncuIg6tOLWji4wH3xMxN/p8nF8/wnjhuYSBq7gQt4vhRTvWPerSafWu6xk9lK5D 62AehxSPqYsLvyVATzI6vEoUSo0VdeS35aBTQO2p+3kZ9laJco74pkci1D18PzQmaEh8F2hJ72P TPtrElBaoTJLOoKXsdIZZsyEmU/UDZ0IZuw3t1qJZIPm3iUXvxSXqFhLrv X-Google-Smtp-Source: AGHT+IGf5KOdaSqeXiRMdcFRmHzgPO2pZR9oVjaMpGuU0oCfeWNgMfmNyFrPbS31psRaIJml8KYWZaSSqvNtRNvnmOo= X-Received: by 2002:a05:6000:25ca:b0:3a4:e65d:5d8c with SMTP id ffacd0b85a97d-3a6ed64662emr902997f8f.40.1750827529171; Tue, 24 Jun 2025 21:58:49 -0700 (PDT) MIME-Version: 1.0 References: <20250611180936.12886-1-suresh.k.chandrappa@gmail.com> In-Reply-To: <20250611180936.12886-1-suresh.k.chandrappa@gmail.com> From: Suresh Chandrappa Date: Wed, 25 Jun 2025 10:28:36 +0530 X-Gm-Features: Ac12FXxefA7txphaOFlvHVistRRoSGnJG5w-3aR2lbzT08t1OJhhan5QYFVioNo Message-ID: Subject: Re: [PATCH v2] selftests: cachestat: Refactor test to remove duplication To: nphamcs@gmail.com, hannes@cmpxchg.org, shuah@kernel.org Cc: linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: multipart/alternative; boundary="000000000000f0766906385e4ce1" X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 0066340004 X-Stat-Signature: uoeob6ryppcujsm9uqea5z57o3kskem7 X-Rspam-User: X-HE-Tag: 1750827530-342876 X-HE-Meta: U2FsdGVkX1+tJAZ35Qn+fX/2jBU/bVOLFcvKnLQNrGbUCluTrXRfQfMN/nci9jzAHYfZ2cfVOEE43XcHTlyHccXgr8+xMWB53w+SqzVT9mAouMQ3plBFvVcrfcwmsZeJyPu4Wz0B4+mbJ6EKo/71C7tzU0ZArGH/Ms2gYrP0YIT270belngvyi1DO4FRdUrYqhYJjyGfxovbk8wm4Fv+diRZtiDj7ckujYcRIhbhQEEQVA3Dz9xfH6YMs8D3m0V4qDsMZldwjOWYTfF8i1Ldny6yju8S6Cp3z8PbceIoLkWikn1gBidysWmGIyedk5ls7xSi0vkz+KkwsFA+L3Tc7pK2nkKv1t7ObPPW5nF6EE/kXgVmtNmnOpyd40gIHpA0tHoPADN+6l73fg5vVVRei9DqhLI5r9QVdmh6vgEV/6LaICONNj6E/NOZjr2lDw1HreteRWbyeV38l4rWfKd6Hs9Um+rMC2SXqMUZL2dW0U9ChoLkrywVLOU0QGBNASKfE3MMHOdk3AC6388pd9GBeMehKEq80mJrLbqDW4qP+PaPBwOxbIup1NUKA30jc++Vdwx+Arkz2bia20MsllojvFFEltQK3xCx+8R5yC1JfN852jgBi/b9weS8TUNdTG2/KvMxRCQC8WcA8+Nw6h4+k807TeS4/tGEa15LXVYP+80brz7fp1/KoDjEpM5fwfOg2BuLLMohkddeZSE/vk8p0qJlxAbVkvIHt5rEgPWyuX/zA5dUafSftnDLIK9A1alIYTjVf6qq+ld8nhPN7Z33wp90rZLzB/gpalilzJUy6oVCP24ZiErSaP/mIP4r0C1/xTHqgRK/cKZJjDkY2qdUNQYure68chOwk+U1nIlgxjlJwFkAwwOem5ZQaZF2bzBFmpLy3MmccPFDM+ZI7Pz/jvOSSGg7dIxvTjCY/MAbVqXkwYmISZ87/QOl1syfX9GuowugF+hl5ko6/L0bnyx 2hzaYiHX SNHA6HvbbU1l+S33STOsQHTiUWRzD0K/9digPy6+KjSCbXOCKdInVRxxPm75t+C9DYf3XJvPRThkSSRulfEvIsthi+/Rrdxvk5xfh5YodsSK7BFahw3m9AQm0WPh+VbJnMPQy6G0Q5qKQD2jssKbibmsR8aivGloh7fDgzktbYlbs2NSLkua7xqGNbhsxxiImmZgUgX3pc+WVK2rAcgQ892qouKP0TJAR3M92o1wxvyrmZeztTKXROwm8JX8Rq1uswbvvNmoCxVr1TElvZ7GuMxbncg2mni7S/tUpMqNS3lYipp0MBjHdzZ+AWrshRDGPTNJ2jXGXpnb8g/AOo/AELjzEUkogGaLNFwosrbyc4TsQKGwPH7Rby2PNf6LQjz0ZichcmQGf/hAuImani4Q0S2TjTnql7Nwq+yuSv7MLJ+gyvyFa/+r6UWJto91qYFuJEg8b5XzQuoPecxSM5w00nRBqyMmH/sG2NxCq 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: List-Subscribe: List-Unsubscribe: --000000000000f0766906385e4ce1 Content-Type: text/plain; charset="UTF-8" Hi @nphamcs Can you please check the modified change Thanks Suresh K C On Wed, 11 Jun 2025, 23:39 Suresh K C, wrote: > From: Suresh K C > > Refactored the mmap and shmem test logic into a common function > to reduce code duplication and improve maintainability > > Changes in v2: > Refactored mmap and shmem tests into a common function > Renamed test function to run_cachestat_test() > Removed test for /proc/cpuinfo as a general /proc test case already > exists > > Signed-off-by: Suresh K C > --- > .../selftests/cachestat/test_cachestat.c | 97 ++++++------------- > 1 file changed, 30 insertions(+), 67 deletions(-) > > diff --git a/tools/testing/selftests/cachestat/test_cachestat.c > b/tools/testing/selftests/cachestat/test_cachestat.c > index 81e7f6dd2279..7c2f64175943 100644 > --- a/tools/testing/selftests/cachestat/test_cachestat.c > +++ b/tools/testing/selftests/cachestat/test_cachestat.c > @@ -22,7 +22,7 @@ > > static const char * const dev_files[] = { > "/dev/zero", "/dev/null", "/dev/urandom", > - "/proc/version","/proc/cpuinfo","/proc" > + "/proc/version","/proc" > }; > > void print_cachestat(struct cachestat *cs) > @@ -33,6 +33,11 @@ void print_cachestat(struct cachestat *cs) > cs->nr_evicted, cs->nr_recently_evicted); > } > > +enum file_type { > + FILE_MMAP, > + FILE_SHMEM > +}; > + > bool write_exactly(int fd, size_t filesize) > { > int random_fd = open("/dev/urandom", O_RDONLY); > @@ -202,66 +207,8 @@ static int test_cachestat(const char *filename, bool > write_random, bool create, > return ret; > } > > -bool test_cachestat_mmap(void){ > - > - size_t PS = sysconf(_SC_PAGESIZE); > - size_t filesize = PS * 512 * 2;; > - int syscall_ret; > - size_t compute_len = PS * 512; > - struct cachestat_range cs_range = { PS, compute_len }; > - char *filename = "tmpshmcstat"; > - unsigned long num_pages = compute_len / PS; > - struct cachestat cs; > - bool ret = true; > - int fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0666); > - if (fd < 0) { > - ksft_print_msg("Unable to create mmap file.\n"); > - ret = false; > - goto out; > - } > - if (ftruncate(fd, filesize)) { > - ksft_print_msg("Unable to truncate mmap file.\n"); > - ret = false; > - goto close_fd; > - } > - if (!write_exactly(fd, filesize)) { > - ksft_print_msg("Unable to write to mmap file.\n"); > - ret = false; > - goto close_fd; > - } > - char *map = mmap(NULL, filesize, PROT_READ | PROT_WRITE, > MAP_SHARED, fd, 0); > - if (map == MAP_FAILED) { > - ksft_print_msg("mmap failed.\n"); > - ret = false; > - goto close_fd; > - } > - > - for (int i = 0; i < filesize; i++) { > - map[i] = 'A'; > - } > - map[filesize - 1] = 'X'; > - > - syscall_ret = syscall(__NR_cachestat, fd, &cs_range, &cs, 0); > - > - if (syscall_ret) { > - ksft_print_msg("Cachestat returned non-zero.\n"); > - ret = false; > - } 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: > - close(fd); > - unlink(filename); > -out: > - return ret; > -} > > -bool test_cachestat_shmem(void) > +bool run_cachestat_test(enum file_type type) > { > size_t PS = sysconf(_SC_PAGESIZE); > size_t filesize = PS * 512 * 2; /* 2 2MB huge pages */ > @@ -271,27 +218,43 @@ bool test_cachestat_shmem(void) > char *filename = "tmpshmcstat"; > struct cachestat cs; > bool ret = true; > + int fd; > unsigned long num_pages = compute_len / PS; > - int fd = shm_open(filename, O_CREAT | O_RDWR, 0600); > + if (type == FILE_SHMEM) > + fd = shm_open(filename, O_CREAT | O_RDWR, 0600); > + else > + fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0666); > > if (fd < 0) { > - ksft_print_msg("Unable to create shmem file.\n"); > + ksft_print_msg("Unable to create file.\n"); > ret = false; > goto out; > } > > if (ftruncate(fd, filesize)) { > - ksft_print_msg("Unable to truncate shmem file.\n"); > + ksft_print_msg("Unable to truncate file.\n"); > ret = false; > goto close_fd; > } > > if (!write_exactly(fd, filesize)) { > - ksft_print_msg("Unable to write to shmem file.\n"); > + ksft_print_msg("Unable to write to file.\n"); > ret = false; > goto close_fd; > } > > + if (type == FILE_MMAP){ > + char *map = mmap(NULL, filesize, PROT_READ | PROT_WRITE, > MAP_SHARED, fd, 0); > + if (map == MAP_FAILED) { > + ksft_print_msg("mmap failed.\n"); > + ret = false; > + goto close_fd; > + } > + for (int i = 0; i < filesize; i++) { > + map[i] = 'A'; > + } > + map[filesize - 1] = 'X'; > + } > syscall_ret = syscall(__NR_cachestat, fd, &cs_range, &cs, 0); > > if (syscall_ret) { > @@ -333,7 +296,7 @@ int main(void) > ret = 1; > } > > - for (int i = 0; i < 6; i++) { > + for (int i = 0; i < 5; i++) { > const char *dev_filename = dev_files[i]; > > if (test_cachestat(dev_filename, false, false, false, > @@ -367,14 +330,14 @@ int main(void) > break; > } > > - if (test_cachestat_shmem()) > + if (run_cachestat_test(FILE_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; > } > > - if (test_cachestat_mmap()) > + if (run_cachestat_test(FILE_MMAP)) > ksft_test_result_pass("cachestat works with a mmap > file\n"); > else { > ksft_test_result_fail("cachestat fails with a mmap > file\n"); > -- > 2.43.0 > > --000000000000f0766906385e4ce1 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Hi @nphamcs
Can you please check the modified change

Thanks
Suresh K C


On Wed, 11 Jun 2025, 23:39 Suresh K C, <suresh.k.chandrappa@gmail.com> wrot= e:
From: Suresh K C <= suresh.k.chandrappa@gmail.com>

Refactored the mmap and shmem test logic into a common function
to reduce code duplication and improve maintainability

Changes in v2:
=C2=A0 =C2=A0 Refactored mmap and shmem tests into a common function
=C2=A0 =C2=A0 Renamed test function to run_cachestat_test()
=C2=A0 =C2=A0 Removed test for /proc/cpuinfo as a general /proc test case a= lready exists

Signed-off-by: Suresh K C <suresh.k.chandrappa@gmail.com&= gt;
---
=C2=A0.../selftests/cachestat/test_cachestat.c=C2=A0 =C2=A0 =C2=A0 | 97 +++= +++-------------
=C2=A01 file changed, 30 insertions(+), 67 deletions(-)

diff --git a/tools/testing/selftests/cachestat/test_cachestat.c b/tools/tes= ting/selftests/cachestat/test_cachestat.c
index 81e7f6dd2279..7c2f64175943 100644
--- a/tools/testing/selftests/cachestat/test_cachestat.c
+++ b/tools/testing/selftests/cachestat/test_cachestat.c
@@ -22,7 +22,7 @@

=C2=A0static const char * const dev_files[] =3D {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 "/dev/zero", "/dev/null", &= quot;/dev/urandom",
-=C2=A0 =C2=A0 =C2=A0 =C2=A0"/proc/version","/proc/cpuinfo&q= uot;,"/proc"
+=C2=A0 =C2=A0 =C2=A0 =C2=A0"/proc/version","/proc"
=C2=A0};

=C2=A0void print_cachestat(struct cachestat *cs)
@@ -33,6 +33,11 @@ void print_cachestat(struct cachestat *cs)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 cs->nr_evicted, cs->nr_recently_evicted);=
=C2=A0}

+enum file_type {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0FILE_MMAP,
+=C2=A0 =C2=A0 =C2=A0 =C2=A0FILE_SHMEM
+};
+
=C2=A0bool write_exactly(int fd, size_t filesize)
=C2=A0{
=C2=A0 =C2=A0 =C2=A0 =C2=A0 int random_fd =3D open("/dev/urandom"= , O_RDONLY);
@@ -202,66 +207,8 @@ static int test_cachestat(const char *filename, bool w= rite_random, bool create,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return ret;
=C2=A0}

-bool test_cachestat_mmap(void){
-
-=C2=A0 =C2=A0 =C2=A0 =C2=A0size_t PS =3D sysconf(_SC_PAGESIZE);
-=C2=A0 =C2=A0 =C2=A0 =C2=A0size_t filesize =3D PS * 512 * 2;;
-=C2=A0 =C2=A0 =C2=A0 =C2=A0int syscall_ret;
-=C2=A0 =C2=A0 =C2=A0 =C2=A0size_t compute_len =3D PS * 512;
-=C2=A0 =C2=A0 =C2=A0 =C2=A0struct cachestat_range cs_range =3D { PS, compu= te_len };
-=C2=A0 =C2=A0 =C2=A0 =C2=A0char *filename =3D "tmpshmcstat";
-=C2=A0 =C2=A0 =C2=A0 =C2=A0unsigned long num_pages =3D compute_len / PS; -=C2=A0 =C2=A0 =C2=A0 =C2=A0struct cachestat cs;
-=C2=A0 =C2=A0 =C2=A0 =C2=A0bool ret =3D true;
-=C2=A0 =C2=A0 =C2=A0 =C2=A0int fd =3D open(filename, O_RDWR | O_CREAT | O_= TRUNC, 0666);
-=C2=A0 =C2=A0 =C2=A0 =C2=A0if (fd < 0) {
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0ksft_print_msg(&quo= t;Unable to create mmap file.\n");
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0ret =3D false;
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0goto out;
-=C2=A0 =C2=A0 =C2=A0 =C2=A0}
-=C2=A0 =C2=A0 =C2=A0 =C2=A0if (ftruncate(fd, filesize)) {
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0ksft_print_msg(&quo= t;Unable to truncate mmap file.\n");
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0ret =3D false;
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0goto close_fd;
-=C2=A0 =C2=A0 =C2=A0 =C2=A0}
-=C2=A0 =C2=A0 =C2=A0 =C2=A0if (!write_exactly(fd, filesize)) {
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0ksft_print_msg(&quo= t;Unable to write to mmap file.\n");
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0ret =3D false;
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0goto close_fd;
-=C2=A0 =C2=A0 =C2=A0 =C2=A0}
-=C2=A0 =C2=A0 =C2=A0 =C2=A0char *map =3D mmap(NULL, filesize, PROT_READ | = PROT_WRITE, MAP_SHARED, fd, 0);
-=C2=A0 =C2=A0 =C2=A0 =C2=A0if (map =3D=3D MAP_FAILED) {
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0ksft_print_msg(&quo= t;mmap failed.\n");
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0ret =3D false;
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0goto close_fd;
-=C2=A0 =C2=A0 =C2=A0 =C2=A0}
-
-=C2=A0 =C2=A0 =C2=A0 =C2=A0for (int i =3D 0; i < filesize; i++) {
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0map[i] =3D 'A&#= 39;;
-=C2=A0 =C2=A0 =C2=A0 =C2=A0}
-=C2=A0 =C2=A0 =C2=A0 =C2=A0map[filesize - 1] =3D 'X';
-=C2=A0 =C2=A0 =C2=A0 =C2=A0
-=C2=A0 =C2=A0 =C2=A0 =C2=A0syscall_ret =3D syscall(__NR_cachestat, fd, &am= p;cs_range, &cs, 0);
-=C2=A0 =C2=A0 =C2=A0 =C2=A0
-=C2=A0 =C2=A0 =C2=A0 =C2=A0if (syscall_ret) {
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0ksft_print_msg(&quo= t;Cachestat returned non-zero.\n");
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0ret =3D false;
-=C2=A0 =C2=A0 =C2=A0 =C2=A0} else {
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0print_cachestat(&am= p;cs);
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if (cs.nr_cache + c= s.nr_evicted !=3D num_pages) {
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0ksft_print_msg("Total number of cached and evicted pages is = off.\n");
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0ret =3D false;
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0}
-=C2=A0 =C2=A0 =C2=A0 =C2=A0}
-
-close_fd:
-=C2=A0 =C2=A0 =C2=A0 =C2=A0close(fd);
-=C2=A0 =C2=A0 =C2=A0 =C2=A0unlink(filename);
-out:
-=C2=A0 =C2=A0 =C2=A0 =C2=A0return ret;
-}

-bool test_cachestat_shmem(void)
+bool run_cachestat_test(enum file_type type)
=C2=A0{
=C2=A0 =C2=A0 =C2=A0 =C2=A0 size_t PS =3D sysconf(_SC_PAGESIZE);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 size_t filesize =3D PS * 512 * 2; /* 2 2MB huge= pages */
@@ -271,27 +218,43 @@ bool test_cachestat_shmem(void)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 char *filename =3D "tmpshmcstat";
=C2=A0 =C2=A0 =C2=A0 =C2=A0 struct cachestat cs;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 bool ret =3D true;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0int fd;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 unsigned long num_pages =3D compute_len / PS; -=C2=A0 =C2=A0 =C2=A0 =C2=A0int fd =3D shm_open(filename, O_CREAT | O_RDWR,= 0600);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0if (type =3D=3D FILE_SHMEM)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0fd =3D shm_open(fil= ename, O_CREAT | O_RDWR, 0600);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0else
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0fd =3D open(filenam= e, O_RDWR | O_CREAT | O_TRUNC, 0666);

=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (fd < 0) {
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0ksft_print_msg(&quo= t;Unable to create shmem file.\n");
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0ksft_print_msg(&quo= t;Unable to create file.\n");
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ret =3D false;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 goto out;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 }

=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (ftruncate(fd, filesize)) {
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0ksft_print_msg(&quo= t;Unable to truncate shmem file.\n");
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0ksft_print_msg(&quo= t;Unable to truncate file.\n");
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ret =3D false;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 goto close_fd;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 }

=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (!write_exactly(fd, filesize)) {
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0ksft_print_msg(&quo= t;Unable to write to shmem file.\n");
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0ksft_print_msg(&quo= t;Unable to write to file.\n");
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ret =3D false;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 goto close_fd;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 }

+=C2=A0 =C2=A0 =C2=A0 =C2=A0if (type =3D=3D FILE_MMAP){
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0char *map =3D mmap(= NULL, filesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if (map =3D=3D MAP_= FAILED) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0ksft_print_msg("mmap failed.\n");
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0ret =3D false;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0goto close_fd;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0}
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0for (int i =3D 0; i= < filesize; i++) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0map[i] =3D 'A';
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0}
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0map[filesize - 1] = =3D 'X';
+=C2=A0 =C2=A0 =C2=A0 =C2=A0}
=C2=A0 =C2=A0 =C2=A0 =C2=A0 syscall_ret =3D syscall(__NR_cachestat, fd, &am= p;cs_range, &cs, 0);

=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (syscall_ret) {
@@ -333,7 +296,7 @@ int main(void)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ret =3D 1;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 }

-=C2=A0 =C2=A0 =C2=A0 =C2=A0for (int i =3D 0; i < 6; i++) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0for (int i =3D 0; i < 5; i++) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 const char *dev_fil= ename =3D dev_files[i];

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (test_cachestat(= dev_filename, false, false, false,
@@ -367,14 +330,14 @@ int main(void)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 break;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 }

-=C2=A0 =C2=A0 =C2=A0 =C2=A0if (test_cachestat_shmem())
+=C2=A0 =C2=A0 =C2=A0 =C2=A0if (run_cachestat_test(FILE_SHMEM))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ksft_test_result_pa= ss("cachestat works with a shmem file\n");
=C2=A0 =C2=A0 =C2=A0 =C2=A0 else {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ksft_test_result_fa= il("cachestat fails with a shmem file\n");
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ret =3D 1;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 }

-=C2=A0 =C2=A0 =C2=A0 =C2=A0if (test_cachestat_mmap())
+=C2=A0 =C2=A0 =C2=A0 =C2=A0if (run_cachestat_test(FILE_MMAP))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ksft_test_result_pa= ss("cachestat works with a mmap file\n");
=C2=A0 =C2=A0 =C2=A0 =C2=A0 else {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ksft_test_result_fa= il("cachestat fails with a mmap file\n");
--
2.43.0

--000000000000f0766906385e4ce1--