18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci#include <stdbool.h> 38c2ecf20Sopenharmony_ci#include <stdlib.h> 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ci#define PAGE_SIZE 4096 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#define MB(x) (x << 20) 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci/* 128c2ecf20Sopenharmony_ci * Checks if two given values differ by less than err% of their sum. 138c2ecf20Sopenharmony_ci */ 148c2ecf20Sopenharmony_cistatic inline int values_close(long a, long b, int err) 158c2ecf20Sopenharmony_ci{ 168c2ecf20Sopenharmony_ci return abs(a - b) <= (a + b) / 100 * err; 178c2ecf20Sopenharmony_ci} 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ciextern int cg_find_unified_root(char *root, size_t len); 208c2ecf20Sopenharmony_ciextern char *cg_name(const char *root, const char *name); 218c2ecf20Sopenharmony_ciextern char *cg_name_indexed(const char *root, const char *name, int index); 228c2ecf20Sopenharmony_ciextern char *cg_control(const char *cgroup, const char *control); 238c2ecf20Sopenharmony_ciextern int cg_create(const char *cgroup); 248c2ecf20Sopenharmony_ciextern int cg_destroy(const char *cgroup); 258c2ecf20Sopenharmony_ciextern int cg_read(const char *cgroup, const char *control, 268c2ecf20Sopenharmony_ci char *buf, size_t len); 278c2ecf20Sopenharmony_ciextern int cg_read_strcmp(const char *cgroup, const char *control, 288c2ecf20Sopenharmony_ci const char *expected); 298c2ecf20Sopenharmony_ciextern int cg_read_strstr(const char *cgroup, const char *control, 308c2ecf20Sopenharmony_ci const char *needle); 318c2ecf20Sopenharmony_ciextern long cg_read_long(const char *cgroup, const char *control); 328c2ecf20Sopenharmony_cilong cg_read_key_long(const char *cgroup, const char *control, const char *key); 338c2ecf20Sopenharmony_ciextern long cg_read_lc(const char *cgroup, const char *control); 348c2ecf20Sopenharmony_ciextern int cg_write(const char *cgroup, const char *control, char *buf); 358c2ecf20Sopenharmony_ciextern int cg_run(const char *cgroup, 368c2ecf20Sopenharmony_ci int (*fn)(const char *cgroup, void *arg), 378c2ecf20Sopenharmony_ci void *arg); 388c2ecf20Sopenharmony_ciextern int cg_enter(const char *cgroup, int pid); 398c2ecf20Sopenharmony_ciextern int cg_enter_current(const char *cgroup); 408c2ecf20Sopenharmony_ciextern int cg_enter_current_thread(const char *cgroup); 418c2ecf20Sopenharmony_ciextern int cg_run_nowait(const char *cgroup, 428c2ecf20Sopenharmony_ci int (*fn)(const char *cgroup, void *arg), 438c2ecf20Sopenharmony_ci void *arg); 448c2ecf20Sopenharmony_ciextern int get_temp_fd(void); 458c2ecf20Sopenharmony_ciextern int alloc_pagecache(int fd, size_t size); 468c2ecf20Sopenharmony_ciextern int alloc_anon(const char *cgroup, void *arg); 478c2ecf20Sopenharmony_ciextern int is_swap_enabled(void); 488c2ecf20Sopenharmony_ciextern int set_oom_adj_score(int pid, int score); 498c2ecf20Sopenharmony_ciextern int cg_wait_for_proc_count(const char *cgroup, int count); 508c2ecf20Sopenharmony_ciextern int cg_killall(const char *cgroup); 518c2ecf20Sopenharmony_ciextern ssize_t proc_read_text(int pid, bool thread, const char *item, char *buf, size_t size); 528c2ecf20Sopenharmony_ciextern int proc_read_strstr(int pid, bool thread, const char *item, const char *needle); 538c2ecf20Sopenharmony_ciextern pid_t clone_into_cgroup(int cgroup_fd); 548c2ecf20Sopenharmony_ciextern int clone_reap(pid_t pid, int options); 558c2ecf20Sopenharmony_ciextern int clone_into_cgroup_run_wait(const char *cgroup); 568c2ecf20Sopenharmony_ciextern int dirfd_open_opath(const char *dir); 57