xref: /linux/tools/testing/selftests/cgroup/lib/include/cgroup_util.h (revision 755fa5b4fb36627796af19932a432d343220ec63)
12c754a84SJames Houghton /* SPDX-License-Identifier: GPL-2.0 */
22c754a84SJames Houghton #include <stdbool.h>
32c754a84SJames Houghton #include <stdlib.h>
42c754a84SJames Houghton 
52c754a84SJames Houghton #ifndef PAGE_SIZE
62c754a84SJames Houghton #define PAGE_SIZE 4096
72c754a84SJames Houghton #endif
82c754a84SJames Houghton 
92c754a84SJames Houghton #define MB(x) (x << 20)
102c754a84SJames Houghton 
112c754a84SJames Houghton #define USEC_PER_SEC	1000000L
122c754a84SJames Houghton #define NSEC_PER_SEC	1000000000L
132c754a84SJames Houghton 
142c754a84SJames Houghton #define TEST_UID	65534 /* usually nobody, any !root is fine */
152c754a84SJames Houghton 
160925275aSMichal Koutný #define CG_THREADS_FILE (!cg_test_v1_named ? "cgroup.threads" : "tasks")
170925275aSMichal Koutný #define CG_NAMED_NAME "selftest"
180925275aSMichal Koutný #define CG_PATH_FORMAT (!cg_test_v1_named ? "0::%s" : (":name=" CG_NAMED_NAME ":%s"))
190925275aSMichal Koutný 
202c754a84SJames Houghton /*
212c754a84SJames Houghton  * Checks if two given values differ by less than err% of their sum.
222c754a84SJames Houghton  */
values_close(long a,long b,int err)232c754a84SJames Houghton static inline int values_close(long a, long b, int err)
242c754a84SJames Houghton {
252c754a84SJames Houghton 	return labs(a - b) <= (a + b) / 100 * err;
262c754a84SJames Houghton }
272c754a84SJames Houghton 
282c754a84SJames Houghton extern ssize_t read_text(const char *path, char *buf, size_t max_len);
292c754a84SJames Houghton extern ssize_t write_text(const char *path, char *buf, ssize_t len);
302c754a84SJames Houghton 
3138e1dd57SSean Christopherson extern int cg_find_controller_root(char *root, size_t len, const char *controller);
322c754a84SJames Houghton extern int cg_find_unified_root(char *root, size_t len, bool *nsdelegate);
332c754a84SJames Houghton extern char *cg_name(const char *root, const char *name);
342c754a84SJames Houghton extern char *cg_name_indexed(const char *root, const char *name, int index);
352c754a84SJames Houghton extern char *cg_control(const char *cgroup, const char *control);
362c754a84SJames Houghton extern int cg_create(const char *cgroup);
372c754a84SJames Houghton extern int cg_destroy(const char *cgroup);
382c754a84SJames Houghton extern int cg_read(const char *cgroup, const char *control,
392c754a84SJames Houghton 		   char *buf, size_t len);
402c754a84SJames Houghton extern int cg_read_strcmp(const char *cgroup, const char *control,
412c754a84SJames Houghton 			  const char *expected);
422c754a84SJames Houghton extern int cg_read_strstr(const char *cgroup, const char *control,
432c754a84SJames Houghton 			  const char *needle);
442c754a84SJames Houghton extern long cg_read_long(const char *cgroup, const char *control);
452c754a84SJames Houghton extern long cg_read_long_fd(int fd);
462c754a84SJames Houghton long cg_read_key_long(const char *cgroup, const char *control, const char *key);
472c754a84SJames Houghton extern long cg_read_lc(const char *cgroup, const char *control);
482c754a84SJames Houghton extern int cg_write(const char *cgroup, const char *control, char *buf);
492c754a84SJames Houghton extern int cg_open(const char *cgroup, const char *control, int flags);
502c754a84SJames Houghton int cg_write_numeric(const char *cgroup, const char *control, long value);
512c754a84SJames Houghton extern int cg_run(const char *cgroup,
522c754a84SJames Houghton 		  int (*fn)(const char *cgroup, void *arg),
532c754a84SJames Houghton 		  void *arg);
542c754a84SJames Houghton extern int cg_enter(const char *cgroup, int pid);
552c754a84SJames Houghton extern int cg_enter_current(const char *cgroup);
562c754a84SJames Houghton extern int cg_enter_current_thread(const char *cgroup);
572c754a84SJames Houghton extern int cg_run_nowait(const char *cgroup,
582c754a84SJames Houghton 			 int (*fn)(const char *cgroup, void *arg),
592c754a84SJames Houghton 			 void *arg);
602c754a84SJames Houghton extern int cg_wait_for_proc_count(const char *cgroup, int count);
612c754a84SJames Houghton extern int cg_killall(const char *cgroup);
622c754a84SJames Houghton int proc_mount_contains(const char *option);
63*3b0dec68SMichal Koutný int cgroup_feature(const char *feature);
642c754a84SJames Houghton extern ssize_t proc_read_text(int pid, bool thread, const char *item, char *buf, size_t size);
652c754a84SJames Houghton extern int proc_read_strstr(int pid, bool thread, const char *item, const char *needle);
662c754a84SJames Houghton extern pid_t clone_into_cgroup(int cgroup_fd);
672c754a84SJames Houghton extern int clone_reap(pid_t pid, int options);
682c754a84SJames Houghton extern int clone_into_cgroup_run_wait(const char *cgroup);
692c754a84SJames Houghton extern int dirfd_open_opath(const char *dir);
702c754a84SJames Houghton extern int cg_prepare_for_wait(const char *cgroup);
712c754a84SJames Houghton extern int memcg_prepare_for_wait(const char *cgroup);
722c754a84SJames Houghton extern int cg_wait_for(int fd);
730925275aSMichal Koutný extern bool cg_test_v1_named;
74