Skip to content

Commit 3a7f9e5

Browse files
committed
cgroup: selftests: Move memcontrol specific helpers out of common cgroup_util.c
Move a handful of helpers out of cgroup_util.c and into test_memcontrol.c that have nothing to with cgroups in general, in anticipation of making cgroup_util.c a generic library that can be used by other selftests. Make read_text() and write_text() non-static so test_memcontrol.c can use them. Signed-off-by: James Houghton <[email protected]> Acked-by: Michal Koutný <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sean Christopherson <[email protected]>
1 parent 26dcdfa commit 3a7f9e5

File tree

3 files changed

+83
-85
lines changed

3 files changed

+83
-85
lines changed

tools/testing/selftests/cgroup/cgroup_util.c

Lines changed: 2 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "../clone3/clone3_selftests.h"
2121

2222
/* Returns read len on success, or -errno on failure. */
23-
static ssize_t read_text(const char *path, char *buf, size_t max_len)
23+
ssize_t read_text(const char *path, char *buf, size_t max_len)
2424
{
2525
ssize_t len;
2626
int fd;
@@ -39,7 +39,7 @@ static ssize_t read_text(const char *path, char *buf, size_t max_len)
3939
}
4040

4141
/* Returns written len on success, or -errno on failure. */
42-
static ssize_t write_text(const char *path, char *buf, ssize_t len)
42+
ssize_t write_text(const char *path, char *buf, ssize_t len)
4343
{
4444
int fd;
4545

@@ -488,84 +488,6 @@ int cg_run_nowait(const char *cgroup,
488488
return pid;
489489
}
490490

491-
int get_temp_fd(void)
492-
{
493-
return open(".", O_TMPFILE | O_RDWR | O_EXCL);
494-
}
495-
496-
int alloc_pagecache(int fd, size_t size)
497-
{
498-
char buf[PAGE_SIZE];
499-
struct stat st;
500-
int i;
501-
502-
if (fstat(fd, &st))
503-
goto cleanup;
504-
505-
size += st.st_size;
506-
507-
if (ftruncate(fd, size))
508-
goto cleanup;
509-
510-
for (i = 0; i < size; i += sizeof(buf))
511-
read(fd, buf, sizeof(buf));
512-
513-
return 0;
514-
515-
cleanup:
516-
return -1;
517-
}
518-
519-
int alloc_anon(const char *cgroup, void *arg)
520-
{
521-
size_t size = (unsigned long)arg;
522-
char *buf, *ptr;
523-
524-
buf = malloc(size);
525-
for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE)
526-
*ptr = 0;
527-
528-
free(buf);
529-
return 0;
530-
}
531-
532-
int is_swap_enabled(void)
533-
{
534-
char buf[PAGE_SIZE];
535-
const char delim[] = "\n";
536-
int cnt = 0;
537-
char *line;
538-
539-
if (read_text("/proc/swaps", buf, sizeof(buf)) <= 0)
540-
return -1;
541-
542-
for (line = strtok(buf, delim); line; line = strtok(NULL, delim))
543-
cnt++;
544-
545-
return cnt > 1;
546-
}
547-
548-
int set_oom_adj_score(int pid, int score)
549-
{
550-
char path[PATH_MAX];
551-
int fd, len;
552-
553-
sprintf(path, "/proc/%d/oom_score_adj", pid);
554-
555-
fd = open(path, O_WRONLY | O_APPEND);
556-
if (fd < 0)
557-
return fd;
558-
559-
len = dprintf(fd, "%d", score);
560-
if (len < 0) {
561-
close(fd);
562-
return len;
563-
}
564-
565-
close(fd);
566-
return 0;
567-
}
568-
569491
int proc_mount_contains(const char *option)
570492
{
571493
char buf[4 * PAGE_SIZE];

tools/testing/selftests/cgroup/cgroup_util.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ static inline int values_close(long a, long b, int err)
2121
return labs(a - b) <= (a + b) / 100 * err;
2222
}
2323

24+
extern ssize_t read_text(const char *path, char *buf, size_t max_len);
25+
extern ssize_t write_text(const char *path, char *buf, ssize_t len);
26+
2427
extern int cg_find_unified_root(char *root, size_t len, bool *nsdelegate);
2528
extern char *cg_name(const char *root, const char *name);
2629
extern char *cg_name_indexed(const char *root, const char *name, int index);
@@ -49,11 +52,6 @@ extern int cg_enter_current_thread(const char *cgroup);
4952
extern int cg_run_nowait(const char *cgroup,
5053
int (*fn)(const char *cgroup, void *arg),
5154
void *arg);
52-
extern int get_temp_fd(void);
53-
extern int alloc_pagecache(int fd, size_t size);
54-
extern int alloc_anon(const char *cgroup, void *arg);
55-
extern int is_swap_enabled(void);
56-
extern int set_oom_adj_score(int pid, int score);
5755
extern int cg_wait_for_proc_count(const char *cgroup, int count);
5856
extern int cg_killall(const char *cgroup);
5957
int proc_mount_contains(const char *option);

tools/testing/selftests/cgroup/test_memcontrol.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,84 @@
2424
static bool has_localevents;
2525
static bool has_recursiveprot;
2626

27+
int get_temp_fd(void)
28+
{
29+
return open(".", O_TMPFILE | O_RDWR | O_EXCL);
30+
}
31+
32+
int alloc_pagecache(int fd, size_t size)
33+
{
34+
char buf[PAGE_SIZE];
35+
struct stat st;
36+
int i;
37+
38+
if (fstat(fd, &st))
39+
goto cleanup;
40+
41+
size += st.st_size;
42+
43+
if (ftruncate(fd, size))
44+
goto cleanup;
45+
46+
for (i = 0; i < size; i += sizeof(buf))
47+
read(fd, buf, sizeof(buf));
48+
49+
return 0;
50+
51+
cleanup:
52+
return -1;
53+
}
54+
55+
int alloc_anon(const char *cgroup, void *arg)
56+
{
57+
size_t size = (unsigned long)arg;
58+
char *buf, *ptr;
59+
60+
buf = malloc(size);
61+
for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE)
62+
*ptr = 0;
63+
64+
free(buf);
65+
return 0;
66+
}
67+
68+
int is_swap_enabled(void)
69+
{
70+
char buf[PAGE_SIZE];
71+
const char delim[] = "\n";
72+
int cnt = 0;
73+
char *line;
74+
75+
if (read_text("/proc/swaps", buf, sizeof(buf)) <= 0)
76+
return -1;
77+
78+
for (line = strtok(buf, delim); line; line = strtok(NULL, delim))
79+
cnt++;
80+
81+
return cnt > 1;
82+
}
83+
84+
int set_oom_adj_score(int pid, int score)
85+
{
86+
char path[PATH_MAX];
87+
int fd, len;
88+
89+
sprintf(path, "/proc/%d/oom_score_adj", pid);
90+
91+
fd = open(path, O_WRONLY | O_APPEND);
92+
if (fd < 0)
93+
return fd;
94+
95+
len = dprintf(fd, "%d", score);
96+
if (len < 0) {
97+
close(fd);
98+
return len;
99+
}
100+
101+
close(fd);
102+
return 0;
103+
}
104+
27105
/*
28106
* This test creates two nested cgroups with and without enabling
29107
* the memory controller.

0 commit comments

Comments
 (0)