linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Aryabhatta Dey <aryabhattadey35@gmail.com>
To: akpm@linux-foundation.org, shuah@kernel.org, linux-mm@kvack.org,
	 linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [RFC PATCH] selftests/mm: compaction_test: Move often used filepaths to strings
Date: Fri, 16 Aug 2024 22:51:02 +0530	[thread overview]
Message-ID: <al5jalqx6ng4w2qyf7nctxpm7u6cdjrazcixcemzi5mbvyluoo@rc5e7gqrwby7> (raw)

Add defines for file path names to avoid duplicate strings
in print messages and make it easier to maintain.

Signed-off-by: Aryabhatta Dey <aryabhattadey35@gmail.com>
---
 tools/testing/selftests/mm/compaction_test.c | 46 ++++++++++----------
 1 file changed, 24 insertions(+), 22 deletions(-)

diff --git a/tools/testing/selftests/mm/compaction_test.c b/tools/testing/selftests/mm/compaction_test.c
index e140558e6f53..541ac0373258 100644
--- a/tools/testing/selftests/mm/compaction_test.c
+++ b/tools/testing/selftests/mm/compaction_test.c
@@ -21,6 +21,9 @@
 #define MAP_SIZE_MB	100
 #define MAP_SIZE	(MAP_SIZE_MB * 1024 * 1024)
 
+#define COMPACT_UNEVICTABLE_ALLOWED "/proc/sys/vm/compact_unevictable_allowed"
+#define NR_HUGEPAGES "/proc/sys/vm/nr_hugepages"
+
 struct map_list {
 	void *map;
 	struct map_list *next;
@@ -59,17 +62,16 @@ int prereq(void)
 	char allowed;
 	int fd;
 
-	fd = open("/proc/sys/vm/compact_unevictable_allowed",
-		  O_RDONLY | O_NONBLOCK);
+	fd = open(COMPACT_UNEVICTABLE_ALLOWED, O_RDONLY | O_NONBLOCK);
 	if (fd < 0) {
-		ksft_print_msg("Failed to open /proc/sys/vm/compact_unevictable_allowed: %s\n",
-			       strerror(errno));
+		ksft_print_msg("Failed to open %s: %s\n",
+			       COMPACT_UNEVICTABLE_ALLOWED, strerror(errno));
 		return -1;
 	}
 
 	if (read(fd, &allowed, sizeof(char)) != sizeof(char)) {
-		ksft_print_msg("Failed to read from /proc/sys/vm/compact_unevictable_allowed: %s\n",
-			       strerror(errno));
+		ksft_print_msg("Failed to read from %s: %s\n",
+			       COMPACT_UNEVICTABLE_ALLOWED, strerror(errno));
 		close(fd);
 		return -1;
 	}
@@ -97,10 +99,10 @@ int check_compaction(unsigned long mem_free, unsigned long hugepage_size,
 	   in to play */
 	mem_free = mem_free * 0.8;
 
-	fd = open("/proc/sys/vm/nr_hugepages", O_RDWR | O_NONBLOCK);
+	fd = open(NR_HUGEPAGES, O_RDWR | O_NONBLOCK);
 	if (fd < 0) {
-		ksft_print_msg("Failed to open /proc/sys/vm/nr_hugepages: %s\n",
-			       strerror(errno));
+		ksft_print_msg("Failed to open %s: %s\n",
+			       NR_HUGEPAGES, strerror(errno));
 		ret = -1;
 		goto out;
 	}
@@ -108,16 +110,16 @@ int check_compaction(unsigned long mem_free, unsigned long hugepage_size,
 	/* Request a large number of huge pages. The Kernel will allocate
 	   as much as it can */
 	if (write(fd, "100000", (6*sizeof(char))) != (6*sizeof(char))) {
-		ksft_print_msg("Failed to write 100000 to /proc/sys/vm/nr_hugepages: %s\n",
-			       strerror(errno));
+		ksft_print_msg("Failed to write 100000 to %s: %s\n",
+			       NR_HUGEPAGES, strerror(errno));
 		goto close_fd;
 	}
 
 	lseek(fd, 0, SEEK_SET);
 
 	if (read(fd, nr_hugepages, sizeof(nr_hugepages)) <= 0) {
-		ksft_print_msg("Failed to re-read from /proc/sys/vm/nr_hugepages: %s\n",
-			       strerror(errno));
+		ksft_print_msg("Failed to re-read from %s: %s\n",
+			       NR_HUGEPAGES, strerror(errno));
 		goto close_fd;
 	}
 
@@ -134,8 +136,8 @@ int check_compaction(unsigned long mem_free, unsigned long hugepage_size,
 
 	if (write(fd, init_nr_hugepages, strlen(init_nr_hugepages))
 	    != strlen(init_nr_hugepages)) {
-		ksft_print_msg("Failed to write value to /proc/sys/vm/nr_hugepages: %s\n",
-			       strerror(errno));
+		ksft_print_msg("Failed to write value to %s: %s\n",
+			       NR_HUGEPAGES, strerror(errno));
 		goto close_fd;
 	}
 
@@ -162,15 +164,15 @@ int set_zero_hugepages(unsigned long *initial_nr_hugepages)
 	int fd, ret = -1;
 	char nr_hugepages[20] = {0};
 
-	fd = open("/proc/sys/vm/nr_hugepages", O_RDWR | O_NONBLOCK);
+	fd = open(NR_HUGEPAGES, O_RDWR | O_NONBLOCK);
 	if (fd < 0) {
-		ksft_print_msg("Failed to open /proc/sys/vm/nr_hugepages: %s\n",
-			       strerror(errno));
+		ksft_print_msg("Failed to open %s: %s\n",
+			       NR_HUGEPAGES, strerror(errno));
 		goto out;
 	}
 	if (read(fd, nr_hugepages, sizeof(nr_hugepages)) <= 0) {
-		ksft_print_msg("Failed to read from /proc/sys/vm/nr_hugepages: %s\n",
-			       strerror(errno));
+		ksft_print_msg("Failed to read from %s: %s\n",
+			       NR_HUGEPAGES, strerror(errno));
 		goto close_fd;
 	}
 
@@ -178,8 +180,8 @@ int set_zero_hugepages(unsigned long *initial_nr_hugepages)
 
 	/* Start with the initial condition of 0 huge pages */
 	if (write(fd, "0", sizeof(char)) != sizeof(char)) {
-		ksft_print_msg("Failed to write 0 to /proc/sys/vm/nr_hugepages: %s\n",
-			       strerror(errno));
+		ksft_print_msg("Failed to write 0 to %s: %s\n",
+			       NR_HUGEPAGES, strerror(errno));
 		goto close_fd;
 	}
 
-- 
2.46.0



             reply	other threads:[~2024-08-16 17:24 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-16 17:21 Aryabhatta Dey [this message]
2024-08-16 17:54 ` Aryabhatta Dey
2024-08-16 18:08 Aryabhatta Dey

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=al5jalqx6ng4w2qyf7nctxpm7u6cdjrazcixcemzi5mbvyluoo@rc5e7gqrwby7 \
    --to=aryabhattadey35@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=shuah@kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox