linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Yongting Lin <linyongting@bytedance.com>
To: anthony.yznaga@oracle.com, khalid@kernel.org, shuah@kernel.org,
	linyongting@bytedance.com
Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
	akpm@linux-foundation.org, linux-mm@kvack.org
Subject: [PATCH 6/8] mshare: selftests: Add some helper functions for reading and controlling cgroup
Date: Mon, 25 Aug 2025 22:57:17 +0800	[thread overview]
Message-ID: <20250825145719.29455-15-linyongting@bytedance.com> (raw)
In-Reply-To: <20250825145719.29455-1-linyongting@bytedance.com>

Before verify some complicated memory functionalities such as swap memory
and THP, we need add some helper functions to controlling the cgroup
(specifically, memcg).

These helper functions consist:
  Create and destroy individual cgroup for test cases
  attach and dettach the test process to specified cgroup
  Read swap size and thp size from testing cgroup

Signed-off-by: Yongting Lin <linyongting@bytedance.com>
---
 tools/testing/selftests/mshare/util.c | 128 ++++++++++++++++++++++++++
 1 file changed, 128 insertions(+)

diff --git a/tools/testing/selftests/mshare/util.c b/tools/testing/selftests/mshare/util.c
index 75f6ff25aa2c..94fddaea2c56 100644
--- a/tools/testing/selftests/mshare/util.c
+++ b/tools/testing/selftests/mshare/util.c
@@ -121,3 +121,131 @@ int mshare_ioctl_munmap(int fd, size_t size)
 
 	return ioctl(fd, MSHAREFS_UNMAP, &munmap);
 }
+
+/*
+ * Helper functions for cgroup
+ */
+
+#define CGROUP_BASE "/sys/fs/cgroup/"
+#define CGROUP_TEST "mshare-test-XXXXXX"
+
+bool is_cgroup_v2;
+
+__attribute__((constructor))
+void get_cgroup_version(void)
+{
+	if (access(CGROUP_BASE "cgroup.controllers", F_OK) == 0)
+		is_cgroup_v2 = true;
+}
+
+int create_mshare_test_cgroup(char *cgroup, size_t len)
+{
+	if (is_cgroup_v2)
+		snprintf(cgroup, len, "%s/%s", CGROUP_BASE, CGROUP_TEST);
+	else
+		snprintf(cgroup, len, "%s/memory/%s", CGROUP_BASE, CGROUP_TEST);
+
+	char *path = mkdtemp(cgroup);
+
+	if (!path) {
+		perror("mkdtemp");
+		return -1;
+	}
+
+	return 0;
+}
+
+int remove_cgroup(char *cgroup)
+{
+	return rmdir(cgroup);
+}
+
+int write_data_to_cgroup(char *cgroup, char *file, char *data)
+{
+	char filename[128];
+	int fd;
+	int ret;
+
+	snprintf(filename, sizeof(filename), "%s/%s", cgroup, file);
+	fd = open(filename, O_RDWR);
+
+	if (fd == -1)
+		return -1;
+
+	ret = write(fd, data, strlen(data));
+	close(fd);
+
+	return ret;
+}
+
+int attach_to_cgroup(char *cgroup)
+{
+	char pid_str[32];
+
+	snprintf(pid_str, sizeof(pid_str), "%d", getpid());
+	return write_data_to_cgroup(cgroup, "cgroup.procs", pid_str);
+}
+
+/*
+ * Simplely, just move the pid to root memcg as avoid
+ * complicated consideration.
+ */
+int dettach_from_cgroup(char *cgroup)
+{
+	char pid_str[32];
+	char *root_memcg;
+
+	if (is_cgroup_v2)
+		root_memcg = CGROUP_BASE;
+	else
+		root_memcg = CGROUP_BASE "memory";
+
+	snprintf(pid_str, sizeof(pid_str), "%d", getpid());
+	return write_data_to_cgroup(root_memcg, "cgroup.procs", pid_str);
+}
+
+size_t read_data_from_cgroup(char *cgroup, char *file, char *field)
+{
+	char filename[128];
+	FILE *fp;
+	char line[80];
+	size_t size = -1;
+
+	snprintf(filename, sizeof(filename), "%s/%s", cgroup, file);
+	fp = fopen(filename, "r");
+	if (!fp) {
+		perror("fopen");
+		return -1;
+	}
+
+	while (fgets(line, sizeof(line), fp)) {
+		if (!strncmp(line, field, strlen(field))) {
+			char *value = line + strlen(field) + 1;
+
+			size = atol(value);
+			break;
+		}
+	}
+
+	fclose(fp);
+
+	return size;
+}
+
+size_t read_swap_from_cgroup(char *cgroup)
+{
+	if (is_cgroup_v2)
+		return read_data_from_cgroup(cgroup, "memory.stat", "pswpout");
+	else
+		return read_data_from_cgroup(cgroup, "memory.stat", "swap");
+}
+
+size_t read_huge_from_cgroup(char *cgroup)
+{
+	if (is_cgroup_v2)
+		return read_data_from_cgroup(cgroup, "memory.stat", "file_thp")
+		     + read_data_from_cgroup(cgroup, "memory.stat", "anon_thp")
+		     + read_data_from_cgroup(cgroup, "memory.stat", "shmem_thp");
+	else
+		return read_data_from_cgroup(cgroup, "memory.stat", "rss_huge");
+}
-- 
2.20.1


  parent reply	other threads:[~2025-08-25 14:58 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-25 14:57 [PATCH 0/8] Add selftests for mshare Yongting Lin
2025-08-25 14:57 ` [PATCH 1/8] mshare: Add selftests Yongting Lin
2025-08-25 14:57 ` [PATCH 2/8] mshare: selftests: Adding config fragment Yongting Lin
2025-08-25 14:57 ` [PATCH 3/8] mshare: selftests: Add some helper function for mshare filesystem Yongting Lin
2025-08-25 14:57 ` [PATCH 4/8] mshare: selftests: Add test case shared memory Yongting Lin
2025-08-29  0:59   ` Anthony Yznaga
2025-09-01 12:50     ` [External] " Yongting Lin
2025-09-02 21:34       ` Anthony Yznaga
2025-08-25 14:57 ` [PATCH 5/8] mshare: selftests: Add test case ioctl unmap Yongting Lin
2025-08-25 14:57 ` [PATCH 6/8] mshare: selftests: Add some helper functions for reading and controlling cgroup Yongting Lin
2025-08-25 14:57 ` [PATCH 7/8] mshare: selftests: Add test case to demostrate the swaping of mshare memory Yongting Lin
2025-08-25 14:57 ` [PATCH 8/8] mshare: selftests: Add test case to demostrate that mshare doesn't support THP Yongting Lin
2025-08-29  1:04   ` Anthony Yznaga
2025-08-25 14:57 ` [PATCH 1/8] mshare: Add selftests Yongting Lin
2025-08-25 14:57 ` [PATCH 2/8] mshare: selftests: Adding config fragment Yongting Lin
2025-08-25 14:57 ` [PATCH 3/8] mshare: selftests: Add some helper function for mshare filesystem Yongting Lin
2025-08-25 14:57 ` [PATCH 4/8] mshare: selftests: Add test case shared memory Yongting Lin
2025-08-25 14:57 ` [PATCH 5/8] mshare: selftests: Add test case ioctl unmap Yongting Lin
2025-08-25 14:57 ` Yongting Lin [this message]
2025-08-25 14:57 ` [PATCH 7/8] mshare: selftests: Add test case to demostrate the swaping of mshare memory Yongting Lin
2025-08-25 14:57 ` [PATCH 8/8] mshare: selftests: Add test case to demostrate that mshare doesn't support THP Yongting Lin
2025-08-26 11:16 ` [PATCH 0/8] Add selftests for mshare Yongting Lin

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=20250825145719.29455-15-linyongting@bytedance.com \
    --to=linyongting@bytedance.com \
    --cc=akpm@linux-foundation.org \
    --cc=anthony.yznaga@oracle.com \
    --cc=khalid@kernel.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;
as well as URLs for NNTP newsgroup(s).