xref: /linux/tools/testing/selftests/cgroup/cgroup_util.h (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <stdbool.h>
3 #include <stdlib.h>
4 
5 #include "../kselftest.h"
6 
7 #define PAGE_SIZE 4096
8 
9 #define MB(x) (x << 20)
10 
11 #define USEC_PER_SEC	1000000L
12 #define NSEC_PER_SEC	1000000000L
13 
14 #define TEST_UID	65534 /* usually nobody, any !root is fine */
15 
16 /*
17  * Checks if two given values differ by less than err% of their sum.
18  */
19 static inline int values_close(long a, long b, int err)
20 {
21 	return labs(a - b) <= (a + b) / 100 * err;
22 }
23 
24 extern int cg_find_unified_root(char *root, size_t len, bool *nsdelegate);
25 extern char *cg_name(const char *root, const char *name);
26 extern char *cg_name_indexed(const char *root, const char *name, int index);
27 extern char *cg_control(const char *cgroup, const char *control);
28 extern int cg_create(const char *cgroup);
29 extern int cg_destroy(const char *cgroup);
30 extern int cg_read(const char *cgroup, const char *control,
31 		   char *buf, size_t len);
32 extern int cg_read_strcmp(const char *cgroup, const char *control,
33 			  const char *expected);
34 extern int cg_read_strstr(const char *cgroup, const char *control,
35 			  const char *needle);
36 extern long cg_read_long(const char *cgroup, const char *control);
37 extern long cg_read_long_fd(int fd);
38 long cg_read_key_long(const char *cgroup, const char *control, const char *key);
39 extern long cg_read_lc(const char *cgroup, const char *control);
40 extern int cg_write(const char *cgroup, const char *control, char *buf);
41 extern int cg_open(const char *cgroup, const char *control, int flags);
42 int cg_write_numeric(const char *cgroup, const char *control, long value);
43 extern int cg_run(const char *cgroup,
44 		  int (*fn)(const char *cgroup, void *arg),
45 		  void *arg);
46 extern int cg_enter(const char *cgroup, int pid);
47 extern int cg_enter_current(const char *cgroup);
48 extern int cg_enter_current_thread(const char *cgroup);
49 extern int cg_run_nowait(const char *cgroup,
50 			 int (*fn)(const char *cgroup, void *arg),
51 			 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);
57 extern int cg_wait_for_proc_count(const char *cgroup, int count);
58 extern int cg_killall(const char *cgroup);
59 int proc_mount_contains(const char *option);
60 extern ssize_t proc_read_text(int pid, bool thread, const char *item, char *buf, size_t size);
61 extern int proc_read_strstr(int pid, bool thread, const char *item, const char *needle);
62 extern pid_t clone_into_cgroup(int cgroup_fd);
63 extern int clone_reap(pid_t pid, int options);
64 extern int clone_into_cgroup_run_wait(const char *cgroup);
65 extern int dirfd_open_opath(const char *dir);
66 extern int cg_prepare_for_wait(const char *cgroup);
67 extern int memcg_prepare_for_wait(const char *cgroup);
68 extern int cg_wait_for(int fd);
69