From: Geetika <geetika@linux.ibm.com>
To: ltp@lists.linux.it
Cc: rpalethorpe@suse.de
Subject: [LTP] [PATCH v2] Migrating the libhugetlbfs/testcases/fallocate_stress.c
Date: Wed, 27 Mar 2024 20:29:25 +0530 [thread overview]
Message-ID: <20240327145925.18922-1-geetika@linux.ibm.com> (raw)
This test stress tests fallocate. This test starts three threads.
First thread will continually punch/fill holes via falloc.
Second thread will continually fault in those same pages.
Third thread will continually mmap/munmap that page range.
Verified that test runs with multiple iterations.
Verified that test has no warnings.
Signed-off-by: Geetika <geetika@linux.ibm.com>
---
Changes in v2:
- Added the origin of testcase in the description
- Removed 'arg' wherever applicable
- Added TERRNO wherever applicable instead of strerror
- Printed the seed value
- Used tst_get_hugepage_size to get the hugepage size
- Fixed other warnings
---
runtest/hugetlb | 1 +
testcases/kernel/mem/.gitignore | 1 +
.../hugetlb/hugefallocate/hugefallocate03.c | 211 ++++++++++++++++++
3 files changed, 213 insertions(+)
create mode 100644 testcases/kernel/mem/hugetlb/hugefallocate/hugefallocate03.c
diff --git a/runtest/hugetlb b/runtest/hugetlb
index 299c07ac9..eb09c7598 100644
--- a/runtest/hugetlb
+++ b/runtest/hugetlb
@@ -1,5 +1,6 @@
hugefallocate01 hugefallocate01
hugefallocate02 hugefallocate02
+hugefallocate03 hugefallocate03
hugefork01 hugefork01
hugefork02 hugefork02
diff --git a/testcases/kernel/mem/.gitignore b/testcases/kernel/mem/.gitignore
index 7258489ed..e9d3562cb 100644
--- a/testcases/kernel/mem/.gitignore
+++ b/testcases/kernel/mem/.gitignore
@@ -1,6 +1,7 @@
/cpuset/cpuset01
/hugetlb/hugefallocate/hugefallocate01
/hugetlb/hugefallocate/hugefallocate02
+/hugetlb/hugefallocate/hugefallocate03
/hugetlb/hugefork/hugefork01
/hugetlb/hugefork/hugefork02
/hugetlb/hugemmap/hugemmap01
diff --git a/testcases/kernel/mem/hugetlb/hugefallocate/hugefallocate03.c b/testcases/kernel/mem/hugetlb/hugefallocate/hugefallocate03.c
new file mode 100644
index 000000000..e5848a3b3
--- /dev/null
+++ b/testcases/kernel/mem/hugetlb/hugefallocate/hugefallocate03.c
@@ -0,0 +1,211 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*
+ * Copyright (C) 2015 Oracle Corporation
+ * Author: Mike Kravetz
+ */
+
+/*\
+ * [Description]
+ *
+ * Stress test fallocate. This test starts three threads.
+ * Thread one will continually punch/fill holes via falloc.
+ * Thread two will continually fault in those same pages.
+ * Thread three will continually mmap/munmap that page range.
+ *
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <sys/mount.h>
+#include <limits.h>
+#include <sys/param.h>
+#include <sys/types.h>
+#include <pthread.h>
+
+#include "hugetlb.h"
+#include "lapi/fallocate.h"
+#include "tst_safe_pthread.h"
+
+#define MNTPOINT "hugetlbfs/"
+#define min(a, b) (((a) < (b)) ? (a) : (b))
+#define MAX_PAGES_TO_USE 100
+#define FALLOCATE_ITERATIONS 100000
+
+static int fd = -1;
+static long nr_hpages_free;
+static unsigned long max_hpages;
+static int err;
+static long hpage_size;
+static unsigned long free_before, free_after;
+static unsigned long rsvd_before, rsvd_after;
+
+static void *thread_fallocate(void *)
+{
+ int i, err;
+ long tpage;
+
+ for (i = 0; i < FALLOCATE_ITERATIONS; i++) {
+ tpage = ((long long)random()) % (max_hpages);
+ err = fallocate(fd,
+ FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
+ tpage * hpage_size, hpage_size);
+ if (err)
+ tst_res(TFAIL|TERRNO, "fallocate():");
+ err = fallocate(fd, 0, tpage * hpage_size, hpage_size);
+ if (err)
+ tst_res(TFAIL|TERRNO, "fallocate():");
+ }
+ return NULL;
+}
+
+static void *fault_mmap_addr;
+
+static void thread_fault_cleanup(void *)
+{
+ if (fault_mmap_addr)
+ munmap(fault_mmap_addr, max_hpages * hpage_size);
+}
+
+static void *thread_fault(void *)
+{
+ long tpage;
+ char foo;
+ struct timespec ts;
+
+ fault_mmap_addr = SAFE_MMAP(NULL, max_hpages * hpage_size,
+ PROT_READ | PROT_WRITE, MAP_SHARED,
+ fd, 0);
+
+ pthread_cleanup_push(thread_fault_cleanup, NULL);
+
+ ts.tv_sec = 0;
+ ts.tv_nsec = 0;
+
+ while (1) {
+ tpage = ((long long)random()) % (max_hpages);
+ foo = *((char *)(fault_mmap_addr + (tpage * hpage_size)));
+ *((char *)(fault_mmap_addr + (tpage * hpage_size))) = foo;
+
+ nanosleep(&ts, NULL); /* thread cancellation point */
+ }
+
+ pthread_cleanup_pop(1);
+
+ return NULL;
+}
+
+static void *mmap_munmap_addr;
+
+static void thread_mmap_munmap_cleanup(void *)
+{
+ if (mmap_munmap_addr)
+ munmap(mmap_munmap_addr, max_hpages * hpage_size);
+}
+
+static void *thread_mmap_munmap(void *)
+{
+ struct timespec ts;
+
+ pthread_cleanup_push(thread_mmap_munmap_cleanup, NULL);
+
+ ts.tv_sec = 0;
+ ts.tv_nsec = 0;
+
+ while (1) {
+ mmap_munmap_addr = SAFE_MMAP(NULL, max_hpages * hpage_size,
+ PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ SAFE_MUNMAP(mmap_munmap_addr, max_hpages * hpage_size);
+ mmap_munmap_addr = NULL;
+
+ nanosleep(&ts, NULL); /* thread cancellation point */
+ }
+ pthread_cleanup_pop(1);
+ return NULL;
+}
+
+static void run_test(void)
+{
+ fd = tst_creat_unlinked(MNTPOINT, 0);
+ pthread_t falloc_th, fault_th, mmap_munmap_th;
+ void *falloc_th_ret, *fault_th_ret, *mmap_munmap_th_ret;
+
+ unsigned int seed = (int)getpid() * time(NULL);
+
+ srandom(seed);
+ tst_res(TINFO, "Seed = %d", seed);
+ nr_hpages_free = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE);
+ max_hpages = min(nr_hpages_free, MAX_PAGES_TO_USE);
+ free_before = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE);
+ rsvd_before = SAFE_READ_MEMINFO(MEMINFO_HPAGE_RSVD);
+
+ /* First preallocate file with max_hpages pages */
+ err = fallocate(fd, 0, 0, hpage_size * max_hpages);
+ if (err) {
+ if (errno == EOPNOTSUPP)
+ tst_brk(TCONF, "fallocate() Operation is not supported");
+ if (err) {
+ tst_res(TFAIL|TERRNO, "fallocate():");
+ goto windup;
+ }
+ }
+
+ free_after = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE);
+ if (free_before - free_after != max_hpages) {
+ tst_res(TFAIL, "fallocate did not preallocate %ld huge pages\n",
+ max_hpages);
+ goto windup;
+ }
+
+ SAFE_PTHREAD_CREATE(&falloc_th, NULL, thread_fallocate, NULL);
+
+ SAFE_PTHREAD_CREATE(&fault_th, NULL, thread_fault, NULL);
+
+ SAFE_PTHREAD_CREATE(&mmap_munmap_th, NULL, thread_mmap_munmap, NULL);
+
+ SAFE_PTHREAD_JOIN(falloc_th, &falloc_th_ret);
+
+ if (falloc_th_ret) {
+ tst_res(TFAIL, "thread_fallocate unexpected exit code");
+ goto windup;
+ }
+
+ SAFE_PTHREAD_CANCEL(fault_th);
+
+ SAFE_PTHREAD_JOIN(fault_th, &fault_th_ret);
+
+ SAFE_PTHREAD_CANCEL(mmap_munmap_th);
+
+ SAFE_PTHREAD_JOIN(mmap_munmap_th, &mmap_munmap_th_ret);
+
+windup:
+ SAFE_CLOSE(fd);
+
+ free_after = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE);
+ rsvd_after = SAFE_READ_MEMINFO(MEMINFO_HPAGE_RSVD);
+ if (free_after != free_before || rsvd_after != rsvd_before)
+ tst_res(TFAIL, "free or reserve counts incorrect after fallocate stress test");
+ else
+ tst_res(TPASS, "fallocate stress test passed");
+}
+
+static void setup(void)
+{
+ hpage_size = tst_get_hugepage_size();
+}
+
+static void cleanup(void)
+{
+ if (fd > 0)
+ SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+ .needs_root = 1,
+ .mntpoint = MNTPOINT,
+ .needs_hugetlbfs = 1,
+ .needs_tmpdir = 1,
+ .setup = setup,
+ .cleanup = cleanup,
+ .test_all = run_test,
+ .hugepages = {2, TST_NEEDS},
+};
--
2.39.3 (Apple Git-146)
--
Mailing list info: https://lists.linux.it/listinfo/ltp
next reply other threads:[~2024-03-27 15:02 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-27 14:59 Geetika [this message]
2024-05-10 16:08 ` [LTP] [PATCH v2] Migrating the libhugetlbfs/testcases/fallocate_stress.c Cyril Hrubis
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240327145925.18922-1-geetika@linux.ibm.com \
--to=geetika@linux.ibm.com \
--cc=ltp@lists.linux.it \
--cc=rpalethorpe@suse.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.