162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci#include <stdbool.h>
362306a36Sopenharmony_ci#include <stdlib.h>
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci#include "../kselftest.h"
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#define PAGE_SIZE 4096
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#define MB(x) (x << 20)
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#define USEC_PER_SEC	1000000L
1262306a36Sopenharmony_ci#define NSEC_PER_SEC	1000000000L
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#define TEST_UID	65534 /* usually nobody, any !root is fine */
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci/*
1762306a36Sopenharmony_ci * Checks if two given values differ by less than err% of their sum.
1862306a36Sopenharmony_ci */
1962306a36Sopenharmony_cistatic inline int values_close(long a, long b, int err)
2062306a36Sopenharmony_ci{
2162306a36Sopenharmony_ci	return abs(a - b) <= (a + b) / 100 * err;
2262306a36Sopenharmony_ci}
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ciextern int cg_find_unified_root(char *root, size_t len);
2562306a36Sopenharmony_ciextern char *cg_name(const char *root, const char *name);
2662306a36Sopenharmony_ciextern char *cg_name_indexed(const char *root, const char *name, int index);
2762306a36Sopenharmony_ciextern char *cg_control(const char *cgroup, const char *control);
2862306a36Sopenharmony_ciextern int cg_create(const char *cgroup);
2962306a36Sopenharmony_ciextern int cg_destroy(const char *cgroup);
3062306a36Sopenharmony_ciextern int cg_read(const char *cgroup, const char *control,
3162306a36Sopenharmony_ci		   char *buf, size_t len);
3262306a36Sopenharmony_ciextern int cg_read_strcmp(const char *cgroup, const char *control,
3362306a36Sopenharmony_ci			  const char *expected);
3462306a36Sopenharmony_ciextern int cg_read_strstr(const char *cgroup, const char *control,
3562306a36Sopenharmony_ci			  const char *needle);
3662306a36Sopenharmony_ciextern long cg_read_long(const char *cgroup, const char *control);
3762306a36Sopenharmony_cilong cg_read_key_long(const char *cgroup, const char *control, const char *key);
3862306a36Sopenharmony_ciextern long cg_read_lc(const char *cgroup, const char *control);
3962306a36Sopenharmony_ciextern int cg_write(const char *cgroup, const char *control, char *buf);
4062306a36Sopenharmony_ciint cg_write_numeric(const char *cgroup, const char *control, long value);
4162306a36Sopenharmony_ciextern int cg_run(const char *cgroup,
4262306a36Sopenharmony_ci		  int (*fn)(const char *cgroup, void *arg),
4362306a36Sopenharmony_ci		  void *arg);
4462306a36Sopenharmony_ciextern int cg_enter(const char *cgroup, int pid);
4562306a36Sopenharmony_ciextern int cg_enter_current(const char *cgroup);
4662306a36Sopenharmony_ciextern int cg_enter_current_thread(const char *cgroup);
4762306a36Sopenharmony_ciextern int cg_run_nowait(const char *cgroup,
4862306a36Sopenharmony_ci			 int (*fn)(const char *cgroup, void *arg),
4962306a36Sopenharmony_ci			 void *arg);
5062306a36Sopenharmony_ciextern int get_temp_fd(void);
5162306a36Sopenharmony_ciextern int alloc_pagecache(int fd, size_t size);
5262306a36Sopenharmony_ciextern int alloc_anon(const char *cgroup, void *arg);
5362306a36Sopenharmony_ciextern int is_swap_enabled(void);
5462306a36Sopenharmony_ciextern int set_oom_adj_score(int pid, int score);
5562306a36Sopenharmony_ciextern int cg_wait_for_proc_count(const char *cgroup, int count);
5662306a36Sopenharmony_ciextern int cg_killall(const char *cgroup);
5762306a36Sopenharmony_ciint proc_mount_contains(const char *option);
5862306a36Sopenharmony_ciextern ssize_t proc_read_text(int pid, bool thread, const char *item, char *buf, size_t size);
5962306a36Sopenharmony_ciextern int proc_read_strstr(int pid, bool thread, const char *item, const char *needle);
6062306a36Sopenharmony_ciextern pid_t clone_into_cgroup(int cgroup_fd);
6162306a36Sopenharmony_ciextern int clone_reap(pid_t pid, int options);
6262306a36Sopenharmony_ciextern int clone_into_cgroup_run_wait(const char *cgroup);
6362306a36Sopenharmony_ciextern int dirfd_open_opath(const char *dir);
6462306a36Sopenharmony_ciextern int cg_prepare_for_wait(const char *cgroup);
6562306a36Sopenharmony_ciextern int memcg_prepare_for_wait(const char *cgroup);
6662306a36Sopenharmony_ciextern int cg_wait_for(int fd);
67