1 Star 0 Fork 49

jake/src-openEuler-iSulad

forked from src-openEuler/iSulad 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
0029-refactor-cgroup-module.patch 80.69 KB
一键复制 编辑 原始数据 按行查看 历史
jake 提交于 2024-04-02 19:50 +08:00 . upgrade from upstream
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538
From c26604ff3150babae729890c549f2784212073a1 Mon Sep 17 00:00:00 2001
From: zhongtao <zhongtao17@huawei.com>
Date: Wed, 20 Mar 2024 15:53:56 +0800
Subject: [PATCH 29/43] refactor cgroup module
Signed-off-by: zhongtao <zhongtao17@huawei.com>
---
src/cmd/isulad/main.c | 6 +
src/daemon/common/CMakeLists.txt | 4 +
src/daemon/common/cgroup.c | 625 ----------------
src/daemon/common/cgroup/CMakeLists.txt | 5 +
src/daemon/common/cgroup/cgroup.c | 136 ++++
src/daemon/common/cgroup/cgroup.h | 48 ++
src/daemon/common/cgroup/cgroup_common.c | 131 ++++
.../{cgroup.h => cgroup/cgroup_common.h} | 62 +-
src/daemon/common/{ => cgroup}/cgroup_v1.c | 699 +++++++++++++++---
src/daemon/common/cgroup/cgroup_v1.h | 30 +
src/daemon/common/{ => cgroup}/cgroup_v2.c | 162 ++--
src/daemon/common/cgroup/cgroup_v2.h | 30 +
src/daemon/common/sysinfo.c | 33 +-
src/daemon/common/sysinfo.h | 4 +-
.../v1/v1_cri_pod_sandbox_manager_service.cc | 13 +-
.../cri_pod_sandbox_manager_service.cc | 13 +-
src/daemon/executor/container_cb/execution.c | 13 +-
17 files changed, 1124 insertions(+), 890 deletions(-)
delete mode 100644 src/daemon/common/cgroup.c
create mode 100644 src/daemon/common/cgroup/CMakeLists.txt
create mode 100644 src/daemon/common/cgroup/cgroup.c
create mode 100644 src/daemon/common/cgroup/cgroup.h
create mode 100644 src/daemon/common/cgroup/cgroup_common.c
rename src/daemon/common/{cgroup.h => cgroup/cgroup_common.h} (60%)
rename src/daemon/common/{ => cgroup}/cgroup_v1.c (52%)
create mode 100644 src/daemon/common/cgroup/cgroup_v1.h
rename src/daemon/common/{ => cgroup}/cgroup_v2.c (88%)
create mode 100644 src/daemon/common/cgroup/cgroup_v2.h
diff --git a/src/cmd/isulad/main.c b/src/cmd/isulad/main.c
index 7c0c072e..9fa87bdb 100644
--- a/src/cmd/isulad/main.c
+++ b/src/cmd/isulad/main.c
@@ -82,6 +82,7 @@
#include "network_api.h"
#endif
#include "id_name_manager.h"
+#include "cgroup.h"
sem_t g_daemon_shutdown_sem;
sem_t g_daemon_wait_shutdown_sem;
@@ -1706,6 +1707,11 @@ static int pre_init_daemon(int argc, char **argv)
goto out;
}
+ if (cgroup_ops_init() != 0) {
+ ERROR("Failed to init cgroup");
+ goto out;
+ }
+
if (server_conf_parse_save(argc, (const char **)argv)) {
ERROR("%s", g_isulad_errmsg ? g_isulad_errmsg : "Failed to parse and save server conf");
goto out;
diff --git a/src/daemon/common/CMakeLists.txt b/src/daemon/common/CMakeLists.txt
index d634507b..e88578dd 100644
--- a/src/daemon/common/CMakeLists.txt
+++ b/src/daemon/common/CMakeLists.txt
@@ -9,16 +9,20 @@ if (GRPC_CONNECTOR)
add_subdirectory(cri)
endif()
+add_subdirectory(cgroup)
+
set(local_daemon_common_srcs ${daemon_common_top_srcs})
set(DAEMON_COMMON_SRCS
${COMMON_CRI_SRCS}
+ ${COMMON_CGROUP_SRCS}
${local_daemon_common_srcs}
PARENT_SCOPE
)
set(DAEMON_COMMON_INCS
${COMMON_CRI_INCS}
+ ${COMMON_CGROUP_INCS}
${CMAKE_CURRENT_SOURCE_DIR}
PARENT_SCOPE
)
diff --git a/src/daemon/common/cgroup.c b/src/daemon/common/cgroup.c
deleted file mode 100644
index 3c58f7fa..00000000
--- a/src/daemon/common/cgroup.c
+++ /dev/null
@@ -1,625 +0,0 @@
-/******************************************************************************
- * Copyright (c) Huawei Technologies Co., Ltd. 2017-2023. All rights reserved.
- * iSulad licensed under the Mulan PSL v2.
- * You can use this software according to the terms and conditions of the Mulan PSL v2.
- * You may obtain a copy of Mulan PSL v2 at:
- * http://license.coscl.org.cn/MulanPSL2
- * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
- * PURPOSE.
- * See the Mulan PSL v2 for more details.
- * Author: zhangxiaoyu
- * Create: 2023-03-29
- * Description: provide cgroup functions
- ******************************************************************************/
-#include "cgroup.h"
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <sys/vfs.h>
-#include <linux/magic.h>
-#include <sys/stat.h>
-
-#include <isula_libutils/auto_cleanup.h>
-
-#include "err_msg.h"
-#include "utils.h"
-#include "utils_array.h"
-#include "sysinfo.h"
-
-#ifndef CGROUP_SUPER_MAGIC
-#define CGROUP_SUPER_MAGIC 0x27e0eb
-#endif
-
-static cgroup_layer_t *new_cgroup_layer(size_t len)
-{
- cgroup_layer_t *layers = NULL;
-
- if (len == 0) {
- return NULL;
- }
-
- layers = (cgroup_layer_t *)util_common_calloc_s(sizeof(cgroup_layer_t));
- if (layers == NULL) {
- ERROR("Out of memory");
- return NULL;
- }
-
- layers->items = (cgroup_layers_item **)util_smart_calloc_s(sizeof(cgroup_layers_item *), len);
- if (layers->items == NULL) {
- ERROR("Out of memory");
- free(layers);
- return NULL;
- }
-
- layers->len = 0;
- layers->cap = len;
-
- return layers;
-}
-
-static int add_cgroup_layer(cgroup_layer_t *layers, char **clist, char *mountpoint)
-{
-#define CGROUP_LAYER_MAX_CAPABILITY 1024
- size_t new_size;
- cgroup_layers_item *newh = NULL;
- cgroup_layers_item **tmp = NULL;
-
- if (layers->len >= CGROUP_LAYER_MAX_CAPABILITY) {
- ERROR("Too many cgroup layers");
- return -1;
- }
-
- newh = util_common_calloc_s(sizeof(cgroup_layers_item));
- if (newh == NULL) {
- ERROR("Out of memory");
- return -1;
- }
- newh->controllers = clist;
- newh->mountpoint = mountpoint;
-
- if (layers->len < layers->cap) {
- goto out;
- }
-
- if (layers->cap > CGROUP_LAYER_MAX_CAPABILITY / 2) {
- new_size = CGROUP_LAYER_MAX_CAPABILITY;
- } else {
- new_size = layers->cap * 2;
- }
-
- if (util_mem_realloc((void **)&tmp, new_size * sizeof(cgroup_layers_item *),
- layers->items, layers->cap * sizeof(cgroup_layers_item *)) != 0) {
- ERROR("Failed to realloc memory");
- free(newh);
- return -1;
- }
-
- layers->items = tmp;
- tmp = NULL;
- layers->cap = new_size;
-
-out:
- layers->items[layers->len] = newh;
- layers->len++;
- return 0;
-}
-
-void common_free_cgroup_layer(cgroup_layer_t *layers)
-{
- size_t i;
-
- if (layers == NULL) {
- return;
- }
-
- for (i = 0; i < layers->len && layers->items[i]; i++) {
- free(layers->items[i]->mountpoint);
- layers->items[i]->mountpoint = NULL;
- util_free_array(layers->items[i]->controllers);
- layers->items[i]->controllers = NULL;
- free(layers->items[i]);
- layers->items[i] = NULL;
- }
-
- free(layers->items);
- layers->items = NULL;
- layers->len = 0;
- layers->cap = 0;
-
- free(layers);
-}
-
-static int append_subsystem_to_list(char ***klist, char ***nlist, const char *ptoken)
-{
- int ret = 0;
-
- if (strncmp(ptoken, "name=", strlen("name=")) == 0) {
- ret = util_array_append(nlist, ptoken);
- if (ret != 0) {
- ERROR("Failed to append string");
- return -1;
- }
- } else {
- ret = util_array_append(klist, ptoken);
- if (ret != 0) {
- ERROR("Failed to append string");
- return -1;
- }
- }
-
- return 0;
-}
-
-static int get_cgroup_subsystems(char ***klist, char ***nlist)
-{
- int ret = 0;
- size_t length = 0;
- FILE *fp = NULL;
- char *pline = NULL;
-
- fp = util_fopen("/proc/self/cgroup", "r");
- if (fp == NULL) {
- return -1;
- }
-
- while (getline(&pline, &length, fp) != -1) {
- char *pos = NULL;
- char *pos2 = NULL;
- char *ptoken = NULL;
- char *psave = NULL;
- pos = strchr(pline, ':');
- if (pos == NULL) {
- ERROR("Invalid cgroup entry: must contain at least two colons: %s", pline);
- ret = -1;
- goto out;
- }
- pos++;
- pos2 = strchr(pos, ':');
- if (pos2 == NULL) {
- ERROR("Invalid cgroup entry: must contain at least two colons: %s", pline);
- ret = -1;
- goto out;
- }
- *pos2 = '\0';
-
- if ((pos2 - pos) == 0) {
- INFO("Not supported cgroup entry: %s", pline);
- continue;
- }
-
- for (ptoken = strtok_r(pos, ",", &psave); ptoken; ptoken = strtok_r(NULL, ",", &psave)) {
- if (append_subsystem_to_list(klist, nlist, ptoken)) {
- goto out;
- }
- }
- }
-
-out:
- free(pline);
- fclose(fp);
- if (ret != 0) {
- util_free_array(*klist);
- *klist = NULL;
- util_free_array(*nlist);
- *nlist = NULL;
- }
- return ret;
-}
-
-static int append_controller(const char **klist, const char **nlist, char ***clist, const char *entry)
-{
- int ret = 0;
- char *dup_entry = NULL;
-
- if (util_array_contain(klist, entry) && util_array_contain(nlist, entry)) {
- ERROR("Refusing to use ambiguous controller \"%s\"", entry);
- ERROR("It is both a named and kernel subsystem");
- return -1;
- }
-
- if (strncmp(entry, "name=", 5) == 0) {
- dup_entry = util_strdup_s(entry);
- } else if (util_array_contain(klist, entry)) {
- dup_entry = util_strdup_s(entry);
- } else {
- dup_entry = util_string_append(entry, "name=");
- }
- if (dup_entry == NULL) {
- ERROR("Out of memory");
- return -1;
- }
-
- ret = util_array_append(clist, dup_entry);
- if (ret != 0) {
- ERROR("Failed to append array");
- }
-
- free(dup_entry);
- return ret;
-}
-
-static inline bool is_cgroup_mountpoint(const char *mp)
-{
- return strncmp(mp, "/sys/fs/cgroup/", strlen("/sys/fs/cgroup/")) == 0;
-}
-
-static char **cgroup_get_controllers(const char **klist, const char **nlist, const char *line)
-{
- int index;
- char *dup = NULL;
- char *pos2 = NULL;
- char *tok = NULL;
- const char *pos = line;
- char *psave = NULL;
- char *sep = ",";
- char **pret = NULL;
-
- // line example
- // 108 99 0:55 / /sys/fs/cgroup rw,nosuid,nodev,noexec,relatime - tmpfs tmpfs rw,mode=755
- for (index = 0; index < 4; index++) {
- pos = strchr(pos, ' ');
- if (pos == NULL) {
- ERROR("Invalid mountinfo format \"%s\"", line);
- return NULL;
- }
- pos++;
- }
-
- if (!is_cgroup_mountpoint(pos)) {
- return NULL;
- }
-
- pos += strlen("/sys/fs/cgroup/");
- pos2 = strchr(pos, ' ');
- if (pos2 == NULL) {
- ERROR("Invalid mountinfo format \"%s\"", line);
- return NULL;
- }
-
- *pos2 = '\0';
- dup = util_strdup_s(pos);
- *pos2 = ' ';
-
- for (tok = strtok_r(dup, sep, &psave); tok; tok = strtok_r(NULL, sep, &psave)) {
- if (append_controller(klist, nlist, &pret, tok)) {
- ERROR("Failed to append controller");
- util_free_array(pret);
- pret = NULL;
- break;
- }
- }
-
- free(dup);
-
- return pret;
-}
-
-int cgroup_get_mountpoint_and_root(char *pline, char **mountpoint, char **root)
-{
- int index;
- char *posmp = NULL;
- char *posrt = NULL;
- char *pos = pline;
-
- // find root
- // line example
- // 108 99 0:55 / /sys/fs/cgroup rw,nosuid,nodev,noexec,relatime - tmpfs tmpfs rw,mode=755
- for (index = 0; index < 3; index++) {
- pos = strchr(pos, ' ');
- if (pos == NULL) {
- return -1;
- }
- pos++;
- }
- posrt = pos;
-
- // find mountpoint
- pos = strchr(pos, ' ');
- if (pos == NULL) {
- return -1;
- }
-
- *pos = '\0';
- if (root != NULL) {
- *root = util_strdup_s(posrt);
- }
-
- pos++;
- posmp = pos;
-
- if (!is_cgroup_mountpoint(posmp)) {
- return -1;
- }
-
- pos = strchr(pos + strlen("/sys/fs/cgroup/"), ' ');
- if (pos == NULL) {
- return -1;
- }
- *pos = '\0';
-
- if (mountpoint != NULL) {
- *mountpoint = util_strdup_s(posmp);
- }
-
- return 0;
-}
-
-static bool lists_intersect(const char **controllers, const char **list)
-{
- int index;
-
- if (controllers == NULL || list == NULL) {
- return false;
- }
-
- for (index = 0; controllers[index]; index++) {
- if (util_array_contain(list, controllers[index])) {
- return true;
- }
- }
-
- return false;
-}
-
-static bool controller_list_is_dup(const cgroup_layer_t *llist, const char **clist)
-{
- size_t index;
-
- if (llist == NULL) {
- return false;
- }
-
- for (index = 0; index < llist->len && llist->items[index]; index++) {
- if (lists_intersect((const char **)llist->items[index]->controllers, (const char **)clist)) {
- return true;
- }
- }
-
- return false;
-}
-
-cgroup_layer_t *common_cgroup_layers_find(void)
-{
- int nret;
- int ret = 0;
- FILE *fp = NULL;
- size_t length = 0;
- const size_t cgroup_layer_item_num = 10;
- char *pline = NULL;
- char **klist = NULL;
- char **nlist = NULL;
- cgroup_layer_t *layers = NULL;
-
- layers = new_cgroup_layer(cgroup_layer_item_num);
- if (layers == NULL) {
- ERROR("Failed to new cgroup layer");
- return NULL;
- }
-
- ret = get_cgroup_subsystems(&klist, &nlist);
- if (ret != 0) {
- ERROR("Failed to retrieve available legacy cgroup controllers\n");
- goto out;
- }
-
- fp = util_fopen("/proc/self/mountinfo", "r");
- if (fp == NULL) {
- ERROR("Failed to open \"/proc/self/mountinfo\"\n");
- ret = -1;
- goto out;
- }
-
- while (getline(&pline, &length, fp) != -1) {
- char *mountpoint = NULL;
- char **clist = NULL;
- int mret;
-
- clist = cgroup_get_controllers((const char **)klist, (const char **)nlist, pline);
- if (clist == NULL) {
- goto list_out;
- }
-
- if (controller_list_is_dup(layers, (const char **)clist)) {
- goto list_out;
- }
-
- mret = cgroup_get_mountpoint_and_root(pline, &mountpoint, NULL);
- if (mret != 0 || mountpoint == NULL) {
- ERROR("Failed parsing mountpoint from \"%s\"\n", pline);
- goto list_out;
- }
-
- nret = add_cgroup_layer(layers, clist, mountpoint);
- if (nret != 0) {
- ERROR("Failed to add hierarchies");
- goto list_out;
- }
-
- continue;
-list_out:
- util_free_array(clist);
- free(mountpoint);
- }
-out:
- util_free_array(klist);
- util_free_array(nlist);
- if (fp != NULL) {
- fclose(fp);
- }
- free(pline);
-
- if (ret != 0) {
- common_free_cgroup_layer(layers);
- return NULL;
- }
-
- return layers;
-}
-
-char *common_find_cgroup_subsystem_mountpoint(const cgroup_layer_t *layers, const char *subsystem)
-{
- size_t i;
-
- for (i = 0; i < layers->len && layers->items[i]; i++) {
- char **cit = NULL;
-
- for (cit = layers->items[i]->controllers; cit && *cit; cit++) {
- if (strcmp(*cit, subsystem) == 0) {
- return layers->items[i]->mountpoint;
- }
- }
- }
- return NULL;
-}
-
-/* find cgroup mountpoint and root */
-int common_find_cgroup_mnt_and_root(const char *subsystem, char **mountpoint, char **root)
-{
- int ret = 0;
- FILE *fp = NULL;
- size_t length = 0;
- char *pline = NULL;
-
- if (subsystem == NULL) {
- ERROR("Empty subsystem");
- return -1;
- }
-
- fp = util_fopen("/proc/self/mountinfo", "r");
- if (fp == NULL) {
- ERROR("Failed to open \"/proc/self/mountinfo\"\n");
- ret = -1;
- goto free_out;
- }
-
- while (getline(&pline, &length, fp) != -1) {
- char *dup = NULL;
- char *p = NULL;
- char *tok = NULL;
- char *mp = NULL;
- char *rt = NULL;
- char *saveptr = NULL;
- char *sep = ",";
- int mret;
-
- mret = cgroup_get_mountpoint_and_root(pline, &mp, &rt);
- if (mret != 0 || mp == NULL || rt == NULL) {
- goto mp_out;
- }
-
- p = mp;
- p += strlen("/sys/fs/cgroup/");
- dup = util_strdup_s(p);
- if (dup == NULL) {
- ERROR("Out of memory");
- free(mp);
- ret = -1;
- goto free_out;
- }
-
- for (tok = strtok_r(dup, sep, &saveptr); tok; tok = strtok_r(NULL, sep, &saveptr)) {
- if (strcmp(tok, subsystem) != 0) {
- continue;
- }
- if (mountpoint != NULL) {
- *mountpoint = mp;
- } else {
- free(mp);
- }
- if (root != NULL) {
- *root = rt;
- } else {
- free(rt);
- }
- free(dup);
- goto free_out;
- }
- free(dup);
-mp_out:
- free(mp);
- free(rt);
- continue;
- }
-free_out:
- if (fp != NULL) {
- fclose(fp);
- }
- free(pline);
- return ret;
-}
-
-int common_get_cgroup_version(void)
-{
- struct statfs fs = { 0 };
-
- if (statfs(CGROUP_MOUNTPOINT, &fs) != 0) {
- SYSERROR("failed to statfs %s", CGROUP_MOUNTPOINT);
- return -1;
- }
-
- if (fs.f_type == CGROUP2_SUPER_MAGIC) {
- return CGROUP_VERSION_2;
- }
-
- return CGROUP_VERSION_1;
-}
-
-static int get_value_ull(const char *content, void *result)
-{
- uint64_t ull_result = 0;
-
- if (util_safe_uint64(content, &ull_result) != 0) {
- ERROR("Failed to convert %s to uint64", content);
- return -1;
- }
-
- *(uint64_t *)result = ull_result;
- return 0;
-}
-
-int get_match_value_ull(const char *content, const char *match, void *result)
-{
- __isula_auto_free char *llu_string = NULL;
- __isula_auto_free char *match_with_space = NULL;
- __isula_auto_array_t char **lines = NULL;
- char **worker = NULL;
-
- if (match == NULL) {
- return get_value_ull(content, result);
- }
-
- // match full string
- match_with_space = util_string_append(" ", match);
- if (match_with_space == NULL) {
- ERROR("Failed to append string");
- return -1;
- }
-
- lines = util_string_split(content, '\n');
- if (lines == NULL) {
- ERROR("Failed to split content %s", content);
- return -1;
- }
-
- for (worker = lines; worker && *worker; worker++) {
- if (util_has_prefix(*worker, match_with_space)) {
- break;
- }
- }
- if (*worker == NULL) {
- ERROR("Cannot find match string %s", match);
- return -1;
- }
-
- llu_string = util_sub_string(*worker, strlen(match_with_space), strlen(*worker) - strlen(match_with_space));
- if (llu_string == NULL) {
- ERROR("Failed to sub string");
- return -1;
- }
- llu_string = util_trim_space(llu_string);
-
- return get_value_ull(llu_string, result);
-}
\ No newline at end of file
diff --git a/src/daemon/common/cgroup/CMakeLists.txt b/src/daemon/common/cgroup/CMakeLists.txt
new file mode 100644
index 00000000..e8c1726c
--- /dev/null
+++ b/src/daemon/common/cgroup/CMakeLists.txt
@@ -0,0 +1,5 @@
+# get current directory sources files
+aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} local_common_cgroup_srcs)
+
+set(COMMON_CGROUP_SRCS ${local_common_cgroup_srcs} PARENT_SCOPE)
+set(COMMON_CGROUP_INCS ${CMAKE_CURRENT_SOURCE_DIR} PARENT_SCOPE)
diff --git a/src/daemon/common/cgroup/cgroup.c b/src/daemon/common/cgroup/cgroup.c
new file mode 100644
index 00000000..837b514a
--- /dev/null
+++ b/src/daemon/common/cgroup/cgroup.c
@@ -0,0 +1,136 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2017-2023. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: zhangxiaoyu
+ * Create: 2023-03-29
+ * Description: provide cgroup functions
+ ******************************************************************************/
+#include "cgroup.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/vfs.h>
+#include <linux/magic.h>
+#include <sys/stat.h>
+
+#include <isula_libutils/auto_cleanup.h>
+
+#include "err_msg.h"
+#include "utils.h"
+#include "utils_array.h"
+#include "sysinfo.h"
+#include "cgroup_v1.h"
+#include "cgroup_v2.h"
+#include "path.h"
+
+#ifndef CGROUP_SUPER_MAGIC
+#define CGROUP_SUPER_MAGIC 0x27e0eb
+#endif
+
+static cgroup_ops g_cgroup_ops;
+
+static int get_cgroup_version_for_init(void)
+{
+ struct statfs fs = { 0 };
+
+ if (statfs(CGROUP_MOUNTPOINT, &fs) != 0) {
+ SYSERROR("failed to statfs %s", CGROUP_MOUNTPOINT);
+ return -1;
+ }
+
+ if (fs.f_type == CGROUP2_SUPER_MAGIC) {
+ return CGROUP_VERSION_2;
+ }
+
+ return CGROUP_VERSION_1;
+}
+
+/* connect client ops init */
+int cgroup_ops_init(void)
+{
+ (void)memset(&g_cgroup_ops, 0, sizeof(g_cgroup_ops));
+ int cgroupVersion = get_cgroup_version_for_init();
+ if (cgroupVersion < 0) {
+ ERROR("Invalid cgroup version");
+ return -1;
+ }
+
+ if (cgroupVersion == CGROUP_VERSION_1) {
+ return cgroup_v1_ops_init(&g_cgroup_ops);
+ } else {
+ return cgroup_v2_ops_init(&g_cgroup_ops);
+ }
+}
+
+int common_get_cgroup_version(void)
+{
+ if (g_cgroup_ops.get_cgroup_version == NULL) {
+ ERROR("Unimplemented get_cgroup_version ops");
+ return -1;
+ }
+
+ return g_cgroup_ops.get_cgroup_version();
+}
+
+int common_get_cgroup_info(cgroup_mem_info_t *meminfo, cgroup_cpu_info_t *cpuinfo,
+ cgroup_hugetlb_info_t *hugetlbinfo, cgroup_blkio_info_t *blkioinfo,
+ cgroup_cpuset_info_t *cpusetinfo, cgroup_pids_info_t *pidsinfo,
+ cgroup_files_info_t *filesinfo, bool quiet)
+{
+ if (g_cgroup_ops.get_cgroup_info == NULL) {
+ ERROR("Unimplemented get_cgroup_info ops");
+ return -1;
+ }
+
+ return g_cgroup_ops.get_cgroup_info(meminfo, cpuinfo, hugetlbinfo, blkioinfo, cpusetinfo, pidsinfo, filesinfo, quiet);
+}
+
+int common_get_cgroup_metrics(const char *cgroup_path, cgroup_metrics_t *cgroup_metrics)
+{
+ if (g_cgroup_ops.get_cgroup_metrics == NULL) {
+ ERROR("Unimplemented get_cgroup_metrics ops");
+ return -1;
+ }
+
+ return g_cgroup_ops.get_cgroup_metrics(cgroup_path, cgroup_metrics);
+}
+
+int common_get_cgroup_mnt_and_root_path(const char *subsystem, char **mountpoint, char **root)
+{
+ if (g_cgroup_ops.get_cgroup_mnt_and_root_path == NULL) {
+ ERROR("Unimplemented get_cgroup_mnt_and_root_path ops");
+ return -1;
+ }
+
+ return g_cgroup_ops.get_cgroup_mnt_and_root_path(subsystem, mountpoint, root);
+}
+
+// only for cgroup v1
+char *common_get_init_cgroup_path(const char *subsystem)
+{
+ if (g_cgroup_ops.get_init_cgroup_path == NULL) {
+ ERROR("Unimplemented get_init_cgroup_path ops");
+ return NULL;
+ }
+
+ return g_cgroup_ops.get_init_cgroup_path(subsystem);
+}
+
+char *common_get_own_cgroup_path(const char *subsystem)
+{
+ if (g_cgroup_ops.get_own_cgroup_path == NULL) {
+ ERROR("Unimplemented get_own_cgroup_path ops");
+ return NULL;
+ }
+
+ return g_cgroup_ops.get_own_cgroup_path(subsystem);
+}
\ No newline at end of file
diff --git a/src/daemon/common/cgroup/cgroup.h b/src/daemon/common/cgroup/cgroup.h
new file mode 100644
index 00000000..1efc3ca6
--- /dev/null
+++ b/src/daemon/common/cgroup/cgroup.h
@@ -0,0 +1,48 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2017-2023. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: zhangxiaoyu
+ * Create: 2023-03-29
+ * Description: provide cgroup definition
+ ******************************************************************************/
+#ifndef DAEMON_COMMON_CGROUP_H
+#define DAEMON_COMMON_CGROUP_H
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+
+#include <isula_libutils/log.h>
+
+#include "cgroup_common.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int cgroup_ops_init(void);
+
+int common_get_cgroup_version(void);
+int common_get_cgroup_info(cgroup_mem_info_t *meminfo, cgroup_cpu_info_t *cpuinfo,
+ cgroup_hugetlb_info_t *hugetlbinfo, cgroup_blkio_info_t *blkioinfo,
+ cgroup_cpuset_info_t *cpusetinfo, cgroup_pids_info_t *pidsinfo,
+ cgroup_files_info_t *filesinfo, bool quiet);
+int common_get_cgroup_metrics(const char *cgroup_path, cgroup_metrics_t *cgroup_metrics);
+int common_get_cgroup_mnt_and_root_path(const char *subsystem, char **mountpoint, char **root);
+
+// only for cgroup v1
+char *common_get_init_cgroup_path(const char *subsystem);
+char *common_get_own_cgroup_path(const char *subsystem);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // DAEMON_COMMON_CGROUP_H
diff --git a/src/daemon/common/cgroup/cgroup_common.c b/src/daemon/common/cgroup/cgroup_common.c
new file mode 100644
index 00000000..0fb9405c
--- /dev/null
+++ b/src/daemon/common/cgroup/cgroup_common.c
@@ -0,0 +1,131 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: zhongtao
+ * Create: 2024-03-22
+ * Description: provide cgroup common func definition
+ ******************************************************************************/
+#include "cgroup_common.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/vfs.h>
+#include <linux/magic.h>
+#include <sys/stat.h>
+
+#include <isula_libutils/auto_cleanup.h>
+
+#include "err_msg.h"
+#include "utils.h"
+#include "utils_array.h"
+#include "sysinfo.h"
+#include "cgroup_v1.h"
+#include "cgroup_v2.h"
+#include "path.h"
+
+static int get_value_ull(const char *content, void *result)
+{
+ uint64_t ull_result = 0;
+
+ if (util_safe_uint64(content, &ull_result) != 0) {
+ ERROR("Failed to convert %s to uint64", content);
+ return -1;
+ }
+
+ *(uint64_t *)result = ull_result;
+ return 0;
+}
+
+int get_match_value_ull(const char *content, const char *match, void *result)
+{
+ __isula_auto_free char *llu_string = NULL;
+ __isula_auto_free char *match_with_space = NULL;
+ __isula_auto_array_t char **lines = NULL;
+ char **worker = NULL;
+
+ if (match == NULL) {
+ return get_value_ull(content, result);
+ }
+
+ // match full string
+ match_with_space = util_string_append(" ", match);
+ if (match_with_space == NULL) {
+ ERROR("Failed to append string");
+ return -1;
+ }
+
+ lines = util_string_split(content, '\n');
+ if (lines == NULL) {
+ ERROR("Failed to split content %s", content);
+ return -1;
+ }
+
+ for (worker = lines; worker && *worker; worker++) {
+ if (util_has_prefix(*worker, match_with_space)) {
+ break;
+ }
+ }
+ if (*worker == NULL) {
+ ERROR("Cannot find match string %s", match);
+ return -1;
+ }
+
+ llu_string = util_sub_string(*worker, strlen(match_with_space), strlen(*worker) - strlen(match_with_space));
+ if (llu_string == NULL) {
+ ERROR("Failed to sub string");
+ return -1;
+ }
+ llu_string = util_trim_space(llu_string);
+
+ return get_value_ull(llu_string, result);
+}
+
+int get_cgroup_value_helper(const char *path, struct cgfile_t *cgfile, void *result)
+{
+ int nret = 0;
+ char file_path[PATH_MAX] = { 0 };
+ char real_path[PATH_MAX] = { 0 };
+ char *content = NULL;
+
+ if (path == NULL || strlen(path) == 0 || result == NULL) {
+ ERROR("%s: Invalid arguments", cgfile->name);
+ return -1;
+ }
+
+ nret = snprintf(file_path, sizeof(file_path), "%s/%s", path, cgfile->file);
+ if (nret < 0 || (size_t)nret >= sizeof(file_path)) {
+ ERROR("%s: failed to snprintf", cgfile->name);
+ return -1;
+ }
+
+ if (util_clean_path(file_path, real_path, sizeof(real_path)) == NULL) {
+ ERROR("%s: failed to clean path %s", cgfile->name, file_path);
+ return -1;
+ }
+
+ content = util_read_content_from_file(real_path);
+ if (content == NULL) {
+ ERROR("%s: failed to read file %s", cgfile->name, real_path);
+ return -1;
+ }
+
+ util_trim_newline(content);
+ content = util_trim_space(content);
+
+ nret = cgfile->get_value(content, cgfile->match, result);
+ if (nret != 0) {
+ ERROR("%s: failed to get value", cgfile->name);
+ }
+
+ free(content);
+ return nret;
+}
\ No newline at end of file
diff --git a/src/daemon/common/cgroup.h b/src/daemon/common/cgroup/cgroup_common.h
similarity index 60%
rename from src/daemon/common/cgroup.h
rename to src/daemon/common/cgroup/cgroup_common.h
index 251e3a3d..2a0935cb 100644
--- a/src/daemon/common/cgroup.h
+++ b/src/daemon/common/cgroup/cgroup_common.h
@@ -1,5 +1,5 @@
/******************************************************************************
- * Copyright (c) Huawei Technologies Co., Ltd. 2017-2023. All rights reserved.
+ * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved.
* iSulad licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
@@ -8,12 +8,12 @@
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
- * Author: zhangxiaoyu
- * Create: 2023-03-29
- * Description: provide cgroup definition
+ * Author: zhongtao
+ * Create: 2024-03-22
+ * Description: provide cgroup common func definition
******************************************************************************/
-#ifndef DAEMON_COMMON_CGROUP_H
-#define DAEMON_COMMON_CGROUP_H
+#ifndef DAEMON_COMMON_CGROUP_COMMON_H
+#define DAEMON_COMMON_CGROUP_COMMON_H
#include <stdbool.h>
#include <stdint.h>
@@ -40,10 +40,6 @@ struct cgfile_t {
int get_match_value_ull(const char *content, const char *match, void *result);
-int common_get_cgroup_version(void);
-
-int common_find_cgroup_mnt_and_root(const char *subsystem, char **mountpoint, char **root);
-
static inline void common_cgroup_do_log(bool quiet, bool do_log, const char *msg)
{
if (!quiet && do_log) {
@@ -51,23 +47,7 @@ static inline void common_cgroup_do_log(bool quiet, bool do_log, const char *msg
}
}
-typedef struct {
- char **controllers;
- char *mountpoint;
-} cgroup_layers_item;
-
-typedef struct {
- cgroup_layers_item **items;
- size_t len;
- size_t cap;
-} cgroup_layer_t;
-
-char *common_find_cgroup_subsystem_mountpoint(const cgroup_layer_t *layers, const char *subsystem);
-
-cgroup_layer_t *common_cgroup_layers_find(void);
-
-void common_free_cgroup_layer(cgroup_layer_t *layers);
-
+int get_cgroup_value_helper(const char *path, struct cgfile_t *cgfile, void *result);
typedef struct {
bool limit;
@@ -113,17 +93,6 @@ typedef struct {
bool fileslimit;
} cgroup_files_info_t;
-int common_get_cgroup_info_v1(cgroup_mem_info_t *meminfo, cgroup_cpu_info_t *cpuinfo,
- cgroup_hugetlb_info_t *hugetlbinfo, cgroup_blkio_info_t *blkioinfo,
- cgroup_cpuset_info_t *cpusetinfo, cgroup_pids_info_t *pidsinfo,
- cgroup_files_info_t *filesinfo, bool quiet);
-
-int common_get_cgroup_info_v2(cgroup_mem_info_t *meminfo, cgroup_cpu_info_t *cpuinfo,
- cgroup_hugetlb_info_t *hugetlbinfo, cgroup_blkio_info_t *blkioinfo,
- cgroup_cpuset_info_t *cpusetinfo, cgroup_pids_info_t *pidsinfo,
- cgroup_files_info_t *filesinfo, bool quiet);
-
-
typedef struct {
uint64_t cpu_use_nanos;
} cgroup_cpu_metrics_t;
@@ -147,15 +116,22 @@ typedef struct {
cgroup_pids_metrics_t cgpids_metrics;
} cgroup_metrics_t;
-int common_get_cgroup_v1_metrics(const char *cgroup_path, cgroup_metrics_t *cgroup_metrics);
-int common_get_cgroup_v2_metrics(const char *cgroup_path, cgroup_metrics_t *cgroup_metrics);
+typedef struct {
+ int (*get_cgroup_version)(void);
+ int (*get_cgroup_info)(cgroup_mem_info_t *meminfo, cgroup_cpu_info_t *cpuinfo,
+ cgroup_hugetlb_info_t *hugetlbinfo, cgroup_blkio_info_t *blkioinfo,
+ cgroup_cpuset_info_t *cpusetinfo, cgroup_pids_info_t *pidsinfo,
+ cgroup_files_info_t *filesinfo, bool quiet);
+ int (*get_cgroup_metrics)(const char *cgroup_path, cgroup_metrics_t *cgroup_metrics);
-char *common_get_init_cgroup(const char *subsystem);
+ int (*get_cgroup_mnt_and_root_path)(const char *subsystem, char **mountpoint, char **root);
-char *common_get_own_cgroup(const char *subsystem);
+ char *(*get_init_cgroup_path)(const char *subsystem);
+ char *(*get_own_cgroup_path)(const char *subsystem);
+} cgroup_ops;
#ifdef __cplusplus
}
#endif
-#endif // DAEMON_COMMON_CGROUP_H
+#endif // DAEMON_COMMON_CGROUP_COMMON_H
diff --git a/src/daemon/common/cgroup_v1.c b/src/daemon/common/cgroup/cgroup_v1.c
similarity index 52%
rename from src/daemon/common/cgroup_v1.c
rename to src/daemon/common/cgroup/cgroup_v1.c
index e34100bc..51cf7512 100644
--- a/src/daemon/common/cgroup_v1.c
+++ b/src/daemon/common/cgroup/cgroup_v1.c
@@ -18,13 +18,31 @@
#include <stdlib.h>
#include "utils.h"
-#include "path.h"
#include "sysinfo.h"
+#include "err_msg.h"
#define CGROUP_HUGETLB_LIMIT "hugetlb.%s.limit_in_bytes"
+#define CGROUP_MOUNT_PATH_PREFIX "/sys/fs/cgroup/"
-static int get_value_ll(const char *content, const char *match, void *result);
-static int get_value_string(const char *content, const char *match, void *result);
+
+static int get_value_ll(const char *content, const char *match, void *result)
+{
+ long long ll_result = 0;
+
+ if (util_safe_llong(content, &ll_result) != 0) {
+ ERROR("Failed to convert %s to long long", content);
+ return -1;
+ }
+
+ *(int64_t *)result = (int64_t)ll_result;
+ return 0;
+}
+
+static int get_value_string(const char *content, const char *match, void *result)
+{
+ *(char **)result = util_strdup_s(content);
+ return 0;
+}
typedef enum {
// CPU subsystem
@@ -90,105 +108,579 @@ static struct cgfile_t g_cgroup_v1_files[] = {
[PIDS_CURRENT] = {"pids_current", "pids.current", NULL, get_match_value_ull},
};
-static int get_value_ll(const char *content, const char *match, void *result)
+typedef struct {
+ char **controllers;
+ char *mountpoint;
+} cgroup_layers_item;
+
+typedef struct {
+ cgroup_layers_item **items;
+ size_t len;
+ size_t cap;
+} cgroup_layer_t;
+
+static char *common_find_cgroup_subsystem_mountpoint(const cgroup_layer_t *layers, const char *subsystem)
{
- long long ll_result = 0;
+ size_t i;
- if (util_safe_llong(content, &ll_result) != 0) {
- ERROR("Failed to convert %s to long long", content);
+ for (i = 0; i < layers->len && layers->items[i]; i++) {
+ char **cit = NULL;
+
+ for (cit = layers->items[i]->controllers; cit && *cit; cit++) {
+ if (strcmp(*cit, subsystem) == 0) {
+ return layers->items[i]->mountpoint;
+ }
+ }
+ }
+ return NULL;
+}
+
+
+static cgroup_layer_t *new_cgroup_layer(size_t len)
+{
+ cgroup_layer_t *layers = NULL;
+
+ if (len == 0) {
+ return NULL;
+ }
+
+ layers = (cgroup_layer_t *)util_common_calloc_s(sizeof(cgroup_layer_t));
+ if (layers == NULL) {
+ ERROR("Out of memory");
+ return NULL;
+ }
+
+ layers->items = (cgroup_layers_item **)util_smart_calloc_s(sizeof(cgroup_layers_item *), len);
+ if (layers->items == NULL) {
+ ERROR("Out of memory");
+ free(layers);
+ return NULL;
+ }
+
+ layers->len = 0;
+ layers->cap = len;
+
+ return layers;
+}
+
+static int add_cgroup_layer(cgroup_layer_t *layers, char **clist, char *mountpoint)
+{
+#define CGROUP_LAYER_MAX_CAPABILITY 1024
+ size_t new_size;
+ cgroup_layers_item *newh = NULL;
+ cgroup_layers_item **tmp = NULL;
+
+ if (layers->len >= CGROUP_LAYER_MAX_CAPABILITY) {
+ ERROR("Too many cgroup layers");
return -1;
}
- *(int64_t *)result = (int64_t)ll_result;
+ newh = util_common_calloc_s(sizeof(cgroup_layers_item));
+ if (newh == NULL) {
+ ERROR("Out of memory");
+ return -1;
+ }
+ newh->controllers = clist;
+ newh->mountpoint = mountpoint;
+
+ if (layers->len < layers->cap) {
+ goto out;
+ }
+
+ if (layers->cap > CGROUP_LAYER_MAX_CAPABILITY / 2) {
+ new_size = CGROUP_LAYER_MAX_CAPABILITY;
+ } else {
+ new_size = layers->cap * 2;
+ }
+
+ if (util_mem_realloc((void **)&tmp, new_size * sizeof(cgroup_layers_item *),
+ layers->items, layers->cap * sizeof(cgroup_layers_item *)) != 0) {
+ ERROR("Failed to realloc memory");
+ free(newh);
+ return -1;
+ }
+
+ layers->items = tmp;
+ tmp = NULL;
+ layers->cap = new_size;
+
+out:
+ layers->items[layers->len] = newh;
+ layers->len++;
return 0;
}
-static int get_value_string(const char *content, const char *match, void *result)
+static void common_free_cgroup_layer(cgroup_layer_t *layers)
{
- *(char **)result = util_strdup_s(content);
+ size_t i;
+
+ if (layers == NULL) {
+ return;
+ }
+
+ for (i = 0; i < layers->len && layers->items[i]; i++) {
+ free(layers->items[i]->mountpoint);
+ layers->items[i]->mountpoint = NULL;
+ util_free_array(layers->items[i]->controllers);
+ layers->items[i]->controllers = NULL;
+ free(layers->items[i]);
+ layers->items[i] = NULL;
+ }
+
+ free(layers->items);
+ layers->items = NULL;
+ layers->len = 0;
+ layers->cap = 0;
+
+ free(layers);
+}
+
+static int append_subsystem_to_list(char ***klist, char ***nlist, const char *ptoken)
+{
+ int ret = 0;
+
+ if (strncmp(ptoken, "name=", strlen("name=")) == 0) {
+ ret = util_array_append(nlist, ptoken);
+ if (ret != 0) {
+ ERROR("Failed to append string");
+ return -1;
+ }
+ } else {
+ ret = util_array_append(klist, ptoken);
+ if (ret != 0) {
+ ERROR("Failed to append string");
+ return -1;
+ }
+ }
+
return 0;
}
-static bool check_cgroup_v1_helper(const char *mountpoint, const cgroup_v1_files_index index, const bool quiet)
+static int get_cgroup_subsystems(char ***klist, char ***nlist)
{
- int nret = 0;
- char path[PATH_MAX] = { 0 };
+ int ret = 0;
+ size_t length = 0;
+ FILE *fp = NULL;
+ char *pline = NULL;
- if (index >= CGROUP_V1_FILES_INDEX_MAXS) {
- ERROR("Index out of range");
- return false;
+ fp = util_fopen("/proc/self/cgroup", "r");
+ if (fp == NULL) {
+ return -1;
}
- if (mountpoint == NULL) {
- ERROR("%s: invalid arguments", g_cgroup_v1_files[index].name);
- return false;
+ while (getline(&pline, &length, fp) != -1) {
+ char *pos = NULL;
+ char *pos2 = NULL;
+ char *ptoken = NULL;
+ char *psave = NULL;
+ pos = strchr(pline, ':');
+ if (pos == NULL) {
+ ERROR("Invalid cgroup entry: must contain at least two colons: %s", pline);
+ ret = -1;
+ goto out;
+ }
+ pos++;
+ pos2 = strchr(pos, ':');
+ if (pos2 == NULL) {
+ ERROR("Invalid cgroup entry: must contain at least two colons: %s", pline);
+ ret = -1;
+ goto out;
+ }
+ *pos2 = '\0';
+
+ if ((pos2 - pos) == 0) {
+ INFO("Not supported cgroup entry: %s", pline);
+ continue;
+ }
+
+ for (ptoken = strtok_r(pos, ",", &psave); ptoken; ptoken = strtok_r(NULL, ",", &psave)) {
+ if (append_subsystem_to_list(klist, nlist, ptoken)) {
+ goto out;
+ }
+ }
}
- nret = snprintf(path, sizeof(path), "%s/%s", mountpoint, g_cgroup_v1_files[index].file);
- if (nret < 0 || (size_t)nret >= sizeof(path)) {
- ERROR("%s: failed to snprintf", g_cgroup_v1_files[index].name);
- return false;
+out:
+ free(pline);
+ fclose(fp);
+ if (ret != 0) {
+ util_free_array(*klist);
+ *klist = NULL;
+ util_free_array(*nlist);
+ *nlist = NULL;
}
+ return ret;
+}
- if (util_file_exists(path)) {
- return true;
+static int append_controller(const char **klist, const char **nlist, char ***clist, const char *entry)
+{
+ int ret = 0;
+ char *dup_entry = NULL;
+
+ if (util_array_contain(klist, entry) && util_array_contain(nlist, entry)) {
+ ERROR("Refusing to use ambiguous controller \"%s\"", entry);
+ ERROR("It is both a named and kernel subsystem");
+ return -1;
}
- if (!quiet) {
- WARN("Your kernel does not support cgroup %s", g_cgroup_v1_files[index].name);
+ if (strncmp(entry, "name=", 5) == 0) {
+ dup_entry = util_strdup_s(entry);
+ } else if (util_array_contain(klist, entry)) {
+ dup_entry = util_strdup_s(entry);
+ } else {
+ dup_entry = util_string_append(entry, "name=");
+ }
+ if (dup_entry == NULL) {
+ ERROR("Out of memory");
+ return -1;
+ }
+
+ ret = util_array_append(clist, dup_entry);
+ if (ret != 0) {
+ ERROR("Failed to append array");
+ }
+
+ free(dup_entry);
+ return ret;
+}
+
+static inline bool is_cgroup_mountpoint(const char *mp)
+{
+ return strncmp(mp, CGROUP_MOUNT_PATH_PREFIX, strlen(CGROUP_MOUNT_PATH_PREFIX)) == 0;
+}
+
+static char **cgroup_get_controllers(const char **klist, const char **nlist, const char *line)
+{
+ int index;
+ char *dup = NULL;
+ char *pos2 = NULL;
+ char *tok = NULL;
+ const char *pos = line;
+ char *psave = NULL;
+ char *sep = ",";
+ char **pret = NULL;
+
+ // line example
+ // 108 99 0:55 / /sys/fs/cgroup rw,nosuid,nodev,noexec,relatime - tmpfs tmpfs rw,mode=755
+ for (index = 0; index < 4; index++) {
+ pos = strchr(pos, ' ');
+ if (pos == NULL) {
+ ERROR("Invalid mountinfo format \"%s\"", line);
+ return NULL;
+ }
+ pos++;
+ }
+
+ if (!is_cgroup_mountpoint(pos)) {
+ return NULL;
+ }
+
+ pos += strlen(CGROUP_MOUNT_PATH_PREFIX);
+ pos2 = strchr(pos, ' ');
+ if (pos2 == NULL) {
+ ERROR("Invalid mountinfo format \"%s\"", line);
+ return NULL;
+ }
+
+ *pos2 = '\0';
+ dup = util_strdup_s(pos);
+ *pos2 = ' ';
+
+ for (tok = strtok_r(dup, sep, &psave); tok; tok = strtok_r(NULL, sep, &psave)) {
+ if (append_controller(klist, nlist, &pret, tok)) {
+ ERROR("Failed to append controller");
+ util_free_array(pret);
+ pret = NULL;
+ break;
+ }
+ }
+
+ free(dup);
+
+ return pret;
+}
+
+static bool lists_intersect(const char **controllers, const char **list)
+{
+ int index;
+
+ if (controllers == NULL || list == NULL) {
+ return false;
+ }
+
+ for (index = 0; controllers[index]; index++) {
+ if (util_array_contain(list, controllers[index])) {
+ return true;
+ }
}
return false;
}
-static int get_cgroup_v1_value_helper(const char *path, const cgroup_v1_files_index index,
- void *result)
+static bool controller_list_is_dup(const cgroup_layer_t *llist, const char **clist)
{
- int nret = 0;
- char file_path[PATH_MAX] = { 0 };
- char real_path[PATH_MAX] = { 0 };
- char *content = NULL;
+ size_t index;
- if (index >= CGROUP_V1_FILES_INDEX_MAXS) {
- ERROR("Index out of range");
+ if (llist == NULL) {
return false;
}
- if (path == NULL || strlen(path) == 0 || result == NULL) {
- ERROR("%s: Invalid arguments", g_cgroup_v1_files[index].name);
+ for (index = 0; index < llist->len && llist->items[index]; index++) {
+ if (lists_intersect((const char **)llist->items[index]->controllers, (const char **)clist)) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+static int cgroup_get_mountpoint_and_root(char *pline, char **mountpoint, char **root)
+{
+ int index;
+ char *posmp = NULL;
+ char *posrt = NULL;
+ char *pos = pline;
+
+ // find root
+ // line example
+ // 108 99 0:55 / /sys/fs/cgroup rw,nosuid,nodev,noexec,relatime - tmpfs tmpfs rw,mode=755
+ for (index = 0; index < 3; index++) {
+ pos = strchr(pos, ' ');
+ if (pos == NULL) {
+ return -1;
+ }
+ pos++;
+ }
+ posrt = pos;
+
+ // find mountpoint
+ pos = strchr(pos, ' ');
+ if (pos == NULL) {
return -1;
}
- nret = snprintf(file_path, sizeof(file_path), "%s/%s", path, g_cgroup_v1_files[index].file);
- if (nret < 0 || (size_t)nret >= sizeof(file_path)) {
- ERROR("%s: failed to snprintf", g_cgroup_v1_files[index].name);
+ *pos = '\0';
+ if (root != NULL) {
+ *root = util_strdup_s(posrt);
+ }
+
+ pos++;
+ posmp = pos;
+
+ if (!is_cgroup_mountpoint(posmp)) {
return -1;
}
- if (util_clean_path(file_path, real_path, sizeof(real_path)) == NULL) {
- ERROR("%s: failed to clean path %s", g_cgroup_v1_files[index].name, file_path);
+ pos = strchr(pos + strlen(CGROUP_MOUNT_PATH_PREFIX), ' ');
+ if (pos == NULL) {
return -1;
}
+ *pos = '\0';
+
+ if (mountpoint != NULL) {
+ *mountpoint = util_strdup_s(posmp);
+ }
+
+ return 0;
+}
+
+/* find cgroup mountpoint and root */
+static int get_cgroup_mnt_and_root_path_v1(const char *subsystem, char **mountpoint, char **root)
+{
+ int ret = 0;
+ FILE *fp = NULL;
+ size_t length = 0;
+ char *pline = NULL;
- content = util_read_content_from_file(real_path);
- if (content == NULL) {
- ERROR("%s: failed to read file %s", g_cgroup_v1_files[index].name, real_path);
+ if (subsystem == NULL) {
+ ERROR("Empty subsystem");
return -1;
}
- util_trim_newline(content);
- content = util_trim_space(content);
+ fp = util_fopen("/proc/self/mountinfo", "r");
+ if (fp == NULL) {
+ ERROR("Failed to open \"/proc/self/mountinfo\"\n");
+ ret = -1;
+ goto free_out;
+ }
+
+ while (getline(&pline, &length, fp) != -1) {
+ char *dup = NULL;
+ char *p = NULL;
+ char *tok = NULL;
+ char *mp = NULL;
+ char *rt = NULL;
+ char *saveptr = NULL;
+ char *sep = ",";
+ int mret;
+
+ mret = cgroup_get_mountpoint_and_root(pline, &mp, &rt);
+ if (mret != 0 || mp == NULL || rt == NULL) {
+ goto mp_out;
+ }
+
+ p = mp;
+ p += strlen(CGROUP_MOUNT_PATH_PREFIX);
+ dup = util_strdup_s(p);
+ if (dup == NULL) {
+ ERROR("Out of memory");
+ free(mp);
+ ret = -1;
+ goto free_out;
+ }
+
+ for (tok = strtok_r(dup, sep, &saveptr); tok; tok = strtok_r(NULL, sep, &saveptr)) {
+ if (strcmp(tok, subsystem) != 0) {
+ continue;
+ }
+ if (mountpoint != NULL) {
+ *mountpoint = mp;
+ } else {
+ free(mp);
+ }
+ if (root != NULL) {
+ *root = rt;
+ } else {
+ free(rt);
+ }
+ free(dup);
+ goto free_out;
+ }
+ free(dup);
+mp_out:
+ free(mp);
+ free(rt);
+ continue;
+ }
+free_out:
+ if (fp != NULL) {
+ fclose(fp);
+ }
+ free(pline);
+ return ret;
+}
+
+static cgroup_layer_t *common_cgroup_layers_find(void)
+{
+ int nret;
+ int ret = 0;
+ FILE *fp = NULL;
+ size_t length = 0;
+ const size_t cgroup_layer_item_num = 10;
+ char *pline = NULL;
+ char **klist = NULL;
+ char **nlist = NULL;
+ cgroup_layer_t *layers = NULL;
+
+ layers = new_cgroup_layer(cgroup_layer_item_num);
+ if (layers == NULL) {
+ ERROR("Failed to new cgroup layer");
+ return NULL;
+ }
+
+ ret = get_cgroup_subsystems(&klist, &nlist);
+ if (ret != 0) {
+ ERROR("Failed to retrieve available legacy cgroup controllers\n");
+ goto out;
+ }
+
+ fp = util_fopen("/proc/self/mountinfo", "r");
+ if (fp == NULL) {
+ ERROR("Failed to open \"/proc/self/mountinfo\"\n");
+ ret = -1;
+ goto out;
+ }
+
+ while (getline(&pline, &length, fp) != -1) {
+ char *mountpoint = NULL;
+ char **clist = NULL;
+ int mret;
+
+ clist = cgroup_get_controllers((const char **)klist, (const char **)nlist, pline);
+ if (clist == NULL) {
+ goto list_out;
+ }
+
+ if (controller_list_is_dup(layers, (const char **)clist)) {
+ goto list_out;
+ }
+
+ mret = cgroup_get_mountpoint_and_root(pline, &mountpoint, NULL);
+ if (mret != 0 || mountpoint == NULL) {
+ ERROR("Failed parsing mountpoint from \"%s\"\n", pline);
+ goto list_out;
+ }
+
+ nret = add_cgroup_layer(layers, clist, mountpoint);
+ if (nret != 0) {
+ ERROR("Failed to add hierarchies");
+ goto list_out;
+ }
+
+ continue;
+list_out:
+ util_free_array(clist);
+ free(mountpoint);
+ }
+out:
+ util_free_array(klist);
+ util_free_array(nlist);
+ if (fp != NULL) {
+ fclose(fp);
+ }
+ free(pline);
+
+ if (ret != 0) {
+ common_free_cgroup_layer(layers);
+ return NULL;
+ }
+
+ return layers;
+}
+
+static int get_cgroup_v1_value_helper(const char *path, const cgroup_v1_files_index index, void *result)
+{
+ if (index >= CGROUP_V1_FILES_INDEX_MAXS) {
+ ERROR("Index out of range");
+ return false;
+ }
+
+ return get_cgroup_value_helper(path, &g_cgroup_v1_files[index], result);
+}
+
+static bool check_cgroup_v1_file_exists(const char *mountpoint, const cgroup_v1_files_index index, const bool quiet)
+{
+ int nret = 0;
+ char path[PATH_MAX] = { 0 };
+
+ if (index >= CGROUP_V1_FILES_INDEX_MAXS) {
+ ERROR("Index out of range");
+ return false;
+ }
+
+ if (mountpoint == NULL) {
+ ERROR("%s: invalid arguments", g_cgroup_v1_files[index].name);
+ return false;
+ }
+
+ nret = snprintf(path, sizeof(path), "%s/%s", mountpoint, g_cgroup_v1_files[index].file);
+ if (nret < 0 || (size_t)nret >= sizeof(path)) {
+ ERROR("%s: failed to snprintf", g_cgroup_v1_files[index].name);
+ return false;
+ }
- nret = g_cgroup_v1_files[index].get_value(content, g_cgroup_v1_files[index].match, result);
- if (nret != 0) {
- ERROR("%s: failed to get value", g_cgroup_v1_files[index].name);
+ if (util_file_exists(path)) {
+ return true;
}
- free(content);
- return nret;
+ if (!quiet) {
+ WARN("Your kernel does not support cgroup %s", g_cgroup_v1_files[index].name);
+ }
+
+ return false;
}
-static void check_cgroup_v1_cpu(const cgroup_layer_t *layers, const bool quiet, cgroup_cpu_info_t *cpuinfo)
+static void get_cgroup_v1_cpu_info(const cgroup_layer_t *layers, const bool quiet, cgroup_cpu_info_t *cpuinfo)
{
char *mountpoint = NULL;
@@ -198,14 +690,14 @@ static void check_cgroup_v1_cpu(const cgroup_layer_t *layers, const bool quiet,
return;
}
- cpuinfo->cpu_rt_period = check_cgroup_v1_helper(mountpoint, CPU_RT_PERIOD, quiet);
- cpuinfo->cpu_rt_runtime = check_cgroup_v1_helper(mountpoint, CPU_RT_RUNTIME, quiet);
- cpuinfo->cpu_shares = check_cgroup_v1_helper(mountpoint, CPU_SHARES, quiet);
- cpuinfo->cpu_cfs_period = check_cgroup_v1_helper(mountpoint, CPU_CFS_PERIOD, quiet);
- cpuinfo->cpu_cfs_quota = check_cgroup_v1_helper(mountpoint, CPU_CFS_QUOTA, quiet);
+ cpuinfo->cpu_rt_period = check_cgroup_v1_file_exists(mountpoint, CPU_RT_PERIOD, quiet);
+ cpuinfo->cpu_rt_runtime = check_cgroup_v1_file_exists(mountpoint, CPU_RT_RUNTIME, quiet);
+ cpuinfo->cpu_shares = check_cgroup_v1_file_exists(mountpoint, CPU_SHARES, quiet);
+ cpuinfo->cpu_cfs_period = check_cgroup_v1_file_exists(mountpoint, CPU_CFS_PERIOD, quiet);
+ cpuinfo->cpu_cfs_quota = check_cgroup_v1_file_exists(mountpoint, CPU_CFS_QUOTA, quiet);
}
-static void check_cgroup_v1_cpuset(const cgroup_layer_t *layers, const bool quiet, cgroup_cpuset_info_t *cpusetinfo)
+static void get_cgroup_v1_cpuset_info(const cgroup_layer_t *layers, const bool quiet, cgroup_cpuset_info_t *cpusetinfo)
{
char *mountpoint = NULL;
@@ -215,10 +707,10 @@ static void check_cgroup_v1_cpuset(const cgroup_layer_t *layers, const bool quie
return;
}
- if (!check_cgroup_v1_helper(mountpoint, CPUSET_CPUS, quiet)) {
+ if (!check_cgroup_v1_file_exists(mountpoint, CPUSET_CPUS, quiet)) {
return;
}
- if (!check_cgroup_v1_helper(mountpoint, CPUSET_MEMS, quiet)) {
+ if (!check_cgroup_v1_file_exists(mountpoint, CPUSET_MEMS, quiet)) {
return;
}
@@ -238,7 +730,7 @@ static void check_cgroup_v1_cpuset(const cgroup_layer_t *layers, const bool quie
cpusetinfo->cpuset = true;
}
-static void check_cgroup_v1_mem(const cgroup_layer_t *layers, const bool quiet, cgroup_mem_info_t *meminfo)
+static void get_cgroup_v1_mem_info(const cgroup_layer_t *layers, const bool quiet, cgroup_mem_info_t *meminfo)
{
char *mountpoint = NULL;
@@ -248,15 +740,15 @@ static void check_cgroup_v1_mem(const cgroup_layer_t *layers, const bool quiet,
return;
}
- meminfo->limit = check_cgroup_v1_helper(mountpoint, MEMORY_LIMIT, quiet);
- meminfo->swap = check_cgroup_v1_helper(mountpoint, MEMORY_SW_LIMIT, quiet);
- meminfo->reservation = check_cgroup_v1_helper(mountpoint, MEMORY_SOFT_LIMIT, quiet);
- meminfo->oomkilldisable = check_cgroup_v1_helper(mountpoint, MEMORY_OOM_CONTROL, quiet);
- meminfo->swappiness = check_cgroup_v1_helper(mountpoint, MEMORY_SWAPPINESS, quiet);
- meminfo->kernel = check_cgroup_v1_helper(mountpoint, MEMORY_KMEM_LIMIT, quiet);
+ meminfo->limit = check_cgroup_v1_file_exists(mountpoint, MEMORY_LIMIT, quiet);
+ meminfo->swap = check_cgroup_v1_file_exists(mountpoint, MEMORY_SW_LIMIT, quiet);
+ meminfo->reservation = check_cgroup_v1_file_exists(mountpoint, MEMORY_SOFT_LIMIT, quiet);
+ meminfo->oomkilldisable = check_cgroup_v1_file_exists(mountpoint, MEMORY_OOM_CONTROL, quiet);
+ meminfo->swappiness = check_cgroup_v1_file_exists(mountpoint, MEMORY_SWAPPINESS, quiet);
+ meminfo->kernel = check_cgroup_v1_file_exists(mountpoint, MEMORY_KMEM_LIMIT, quiet);
}
-static void check_cgroup_v1_blkio(const cgroup_layer_t *layers, const bool quiet, cgroup_blkio_info_t *blkioinfo)
+static void get_cgroup_v1_blkio_info(const cgroup_layer_t *layers, const bool quiet, cgroup_blkio_info_t *blkioinfo)
{
char *mountpoint = NULL;
@@ -266,15 +758,15 @@ static void check_cgroup_v1_blkio(const cgroup_layer_t *layers, const bool quiet
return;
}
- blkioinfo->blkio_weight = check_cgroup_v1_helper(mountpoint, BLKIO_WEIGTH, quiet);
- blkioinfo->blkio_weight_device = check_cgroup_v1_helper(mountpoint, BLKIO_WEIGTH_DEVICE, quiet);
- blkioinfo->blkio_read_bps_device = check_cgroup_v1_helper(mountpoint, BLKIO_READ_BPS, quiet);
- blkioinfo->blkio_write_bps_device = check_cgroup_v1_helper(mountpoint, BLKIO_WRITE_BPS, quiet);
- blkioinfo->blkio_read_iops_device = check_cgroup_v1_helper(mountpoint, BLKIO_READ_IOPS, quiet);
- blkioinfo->blkio_write_iops_device = check_cgroup_v1_helper(mountpoint, BLKIO_WRITE_IOPS, quiet);
+ blkioinfo->blkio_weight = check_cgroup_v1_file_exists(mountpoint, BLKIO_WEIGTH, quiet);
+ blkioinfo->blkio_weight_device = check_cgroup_v1_file_exists(mountpoint, BLKIO_WEIGTH_DEVICE, quiet);
+ blkioinfo->blkio_read_bps_device = check_cgroup_v1_file_exists(mountpoint, BLKIO_READ_BPS, quiet);
+ blkioinfo->blkio_write_bps_device = check_cgroup_v1_file_exists(mountpoint, BLKIO_WRITE_BPS, quiet);
+ blkioinfo->blkio_read_iops_device = check_cgroup_v1_file_exists(mountpoint, BLKIO_READ_IOPS, quiet);
+ blkioinfo->blkio_write_iops_device = check_cgroup_v1_file_exists(mountpoint, BLKIO_WRITE_IOPS, quiet);
}
-static void check_cgroup_v1_hugetlb(const cgroup_layer_t *layers, const bool quiet, cgroup_hugetlb_info_t *hugetlbinfo)
+static void get_cgroup_v1_hugetlb_info(const cgroup_layer_t *layers, const bool quiet, cgroup_hugetlb_info_t *hugetlbinfo)
{
int nret;
char *mountpoint = NULL;
@@ -306,7 +798,7 @@ free_out:
free(defaultpagesize);
}
-static void check_cgroup_v1_pids(const cgroup_layer_t *layers, const bool quiet, cgroup_pids_info_t *pidsinfo)
+static void get_cgroup_v1_pids_info(const cgroup_layer_t *layers, const bool quiet, cgroup_pids_info_t *pidsinfo)
{
char *mountpoint = NULL;
@@ -319,7 +811,7 @@ static void check_cgroup_v1_pids(const cgroup_layer_t *layers, const bool quiet,
pidsinfo->pidslimit = true;
}
-static void check_cgroup_v1_files(const cgroup_layer_t *layers, const bool quiet, cgroup_files_info_t *filesinfo)
+static void get_cgroup_v1_files_info(const cgroup_layer_t *layers, const bool quiet, cgroup_files_info_t *filesinfo)
{
char *mountpoint = NULL;
@@ -332,7 +824,7 @@ static void check_cgroup_v1_files(const cgroup_layer_t *layers, const bool quiet
filesinfo->fileslimit = true;
}
-int common_get_cgroup_info_v1(cgroup_mem_info_t *meminfo, cgroup_cpu_info_t *cpuinfo,
+static int get_cgroup_info_v1(cgroup_mem_info_t *meminfo, cgroup_cpu_info_t *cpuinfo,
cgroup_hugetlb_info_t *hugetlbinfo, cgroup_blkio_info_t *blkioinfo,
cgroup_cpuset_info_t *cpusetinfo, cgroup_pids_info_t *pidsinfo,
cgroup_files_info_t *filesinfo, bool quiet)
@@ -345,13 +837,13 @@ int common_get_cgroup_info_v1(cgroup_mem_info_t *meminfo, cgroup_cpu_info_t *cpu
return -1;
}
- check_cgroup_v1_cpu(layers, quiet, cpuinfo);
- check_cgroup_v1_cpuset(layers, quiet, cpusetinfo);
- check_cgroup_v1_mem(layers, quiet, meminfo);
- check_cgroup_v1_blkio(layers, quiet, blkioinfo);
- check_cgroup_v1_hugetlb(layers, quiet, hugetlbinfo);
- check_cgroup_v1_pids(layers, quiet, pidsinfo);
- check_cgroup_v1_files(layers, quiet, filesinfo);
+ get_cgroup_v1_cpu_info(layers, quiet, cpuinfo);
+ get_cgroup_v1_cpuset_info(layers, quiet, cpusetinfo);
+ get_cgroup_v1_mem_info(layers, quiet, meminfo);
+ get_cgroup_v1_blkio_info(layers, quiet, blkioinfo);
+ get_cgroup_v1_hugetlb_info(layers, quiet, hugetlbinfo);
+ get_cgroup_v1_pids_info(layers, quiet, pidsinfo);
+ get_cgroup_v1_files_info(layers, quiet, filesinfo);
common_free_cgroup_layer(layers);
@@ -432,7 +924,7 @@ static void get_cgroup_v1_metrics_pid(const cgroup_layer_t *layers, const char *
get_cgroup_v1_value_helper(path, PIDS_CURRENT, (void *)&cgroup_pids_metrics->pid_current);
}
-int common_get_cgroup_v1_metrics(const char *cgroup_path, cgroup_metrics_t *cgroup_metrics)
+static int get_cgroup_metrics_v1(const char *cgroup_path, cgroup_metrics_t *cgroup_metrics)
{
cgroup_layer_t *layers = NULL;
@@ -553,22 +1045,31 @@ static char *common_get_cgroup_path(const char *path, const char *subsystem)
return res;
}
-char *common_get_init_cgroup(const char *subsystem)
+char *get_init_cgroup_path_v1(const char *subsystem)
{
- if (common_get_cgroup_version() != CGROUP_VERSION_1) {
- ERROR("Not implemented for cgroup v2 hierarchy");
- return NULL;
- }
-
return common_get_cgroup_path("/proc/1/cgroup", subsystem);
}
-char *common_get_own_cgroup(const char *subsystem)
+char *get_own_cgroup_v1(const char *subsystem)
{
- if (common_get_cgroup_version() != CGROUP_VERSION_1) {
- ERROR("Not implemented for cgroup v2 hierarchy");
- return NULL;
- }
-
return common_get_cgroup_path("/proc/self/cgroup", subsystem);
}
+
+int get_cgroup_version_v1()
+{
+ return CGROUP_VERSION_1;
+}
+
+int cgroup_v1_ops_init(cgroup_ops *ops)
+{
+ if (ops == NULL) {
+ return -1;
+ }
+ ops->get_cgroup_version = get_cgroup_version_v1;
+ ops->get_cgroup_info = get_cgroup_info_v1;
+ ops->get_cgroup_metrics = get_cgroup_metrics_v1;
+ ops->get_cgroup_mnt_and_root_path = get_cgroup_mnt_and_root_path_v1;
+ ops->get_init_cgroup_path = get_init_cgroup_path_v1;
+ ops->get_own_cgroup_path = get_own_cgroup_v1;
+ return 0;
+}
\ No newline at end of file
diff --git a/src/daemon/common/cgroup/cgroup_v1.h b/src/daemon/common/cgroup/cgroup_v1.h
new file mode 100644
index 00000000..c2696725
--- /dev/null
+++ b/src/daemon/common/cgroup/cgroup_v1.h
@@ -0,0 +1,30 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: zhongtao
+ * Create: 2024-02-19
+ * Description: provide cgroup v1 definition
+ ******************************************************************************/
+#ifndef DAEMON_COMMON_CGROUP_CGROUP_V1_H
+#define DAEMON_COMMON_CGROUP_CGROUP_V1_H
+
+#include "cgroup.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int cgroup_v1_ops_init(cgroup_ops *ops);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // DAEMON_COMMON_CGROUP_CGROUP_V1_H
diff --git a/src/daemon/common/cgroup_v2.c b/src/daemon/common/cgroup/cgroup_v2.c
similarity index 88%
rename from src/daemon/common/cgroup_v2.c
rename to src/daemon/common/cgroup/cgroup_v2.c
index 25509bda..65cf90d8 100644
--- a/src/daemon/common/cgroup_v2.c
+++ b/src/daemon/common/cgroup/cgroup_v2.c
@@ -105,48 +105,12 @@ static struct cgfile_t g_cgroup_v2_files[] = {
static int get_cgroup_v2_value_helper(const char *path, const cgroup_v2_files_index index, void *result)
{
- int nret = 0;
- char file_path[PATH_MAX] = { 0 };
- char real_path[PATH_MAX] = { 0 };
- char *content = NULL;
-
if (index >= CGROUP_V2_FILES_INDEX_MAXS) {
ERROR("Index out of range");
return false;
}
- if (path == NULL || strlen(path) == 0 || result == NULL) {
- ERROR("%s: Invalid arguments", g_cgroup_v2_files[index].name);
- return -1;
- }
-
- nret = snprintf(file_path, sizeof(file_path), "%s/%s", path, g_cgroup_v2_files[index].file);
- if (nret < 0 || (size_t)nret >= sizeof(file_path)) {
- ERROR("%s: failed to snprintf", g_cgroup_v2_files[index].name);
- return -1;
- }
-
- if (util_clean_path(file_path, real_path, sizeof(real_path)) == NULL) {
- ERROR("%s: failed to clean path %s", g_cgroup_v2_files[index].name, file_path);
- return -1;
- }
-
- content = util_read_content_from_file(real_path);
- if (content == NULL) {
- ERROR("%s: failed to read file %s", g_cgroup_v2_files[index].name, real_path);
- return -1;
- }
-
- util_trim_newline(content);
- content = util_trim_space(content);
-
- nret = g_cgroup_v2_files[index].get_value(content, g_cgroup_v2_files[index].match, result);
- if (nret != 0) {
- ERROR("%s: failed to get value", g_cgroup_v2_files[index].name);
- }
-
- free(content);
- return nret;
+ return get_cgroup_value_helper(path, &g_cgroup_v2_files[index], result);
}
static void get_cgroup_v2_metrics_cpu(const char *cgroup_path, cgroup_cpu_metrics_t *cgroup_cpu_metrics)
@@ -306,28 +270,18 @@ static bool cgroup_v2_enabled(const char *mountpoint, const char *name)
return util_file_exists(path);
}
-int common_get_cgroup_info_v2(cgroup_mem_info_t *meminfo, cgroup_cpu_info_t *cpuinfo,
- cgroup_hugetlb_info_t *hugetlbinfo, cgroup_blkio_info_t *blkioinfo,
- cgroup_cpuset_info_t *cpusetinfo, cgroup_pids_info_t *pidsinfo,
- cgroup_files_info_t *filesinfo, bool quiet)
+static void get_cgroup_v2_cpu_info(const bool quiet, cgroup_cpu_info_t *cpuinfo)
{
- int ret = 0;
- int nret = 0;
- __isula_auto_free char *size = NULL;
- char path[PATH_MAX] = { 0 };
-
- if (make_sure_cgroup2_isulad_path_exist() != 0) {
- return -1;
- }
-
- // cpu cgroup
cpuinfo->cpu_shares = cgroup_v2_enabled(CGROUP_ISULAD_PATH, CGROUP2_CPU_WEIGHT);
common_cgroup_do_log(quiet, !(cpuinfo->cpu_shares), "Your kernel does not support cgroup2 cpu weight");
cpuinfo->cpu_cfs_period = cgroup_v2_enabled(CGROUP_ISULAD_PATH, CGROUP2_CPU_MAX);
cpuinfo->cpu_cfs_quota = cpuinfo->cpu_cfs_period;
common_cgroup_do_log(quiet, !(cpuinfo->cpu_cfs_period), "Your kernel does not support cgroup2 cpu max");
+}
+static int get_cgroup_v2_cpuset_info(const bool quiet, cgroup_cpuset_info_t *cpusetinfo)
+{
cpusetinfo->cpuset = cgroup_v2_enabled(CGROUP_ISULAD_PATH, CGROUP2_CPUSET_CPUS_EFFECTIVE) &&
cgroup_v2_enabled(CGROUP_ISULAD_PATH, CGROUP2_CPUSET_CPUS) &&
cgroup_v2_enabled(CGROUP_ISULAD_PATH, CGROUP2_CPUSET_MEMS_EFFECTIVE) &&
@@ -343,8 +297,23 @@ int common_get_cgroup_info_v2(cgroup_mem_info_t *meminfo, cgroup_cpu_info_t *cpu
cpusetinfo->cpus = util_trim_space(cpusetinfo->cpus);
cpusetinfo->mems = util_trim_space(cpusetinfo->mems);
}
+ return 0;
+}
+
+static void get_cgroup_v2_mem_info(const bool quiet, cgroup_mem_info_t *meminfo)
+{
+ meminfo->limit = cgroup_v2_enabled(CGROUP_ISULAD_PATH, CGROUP2_MEMORY_MAX);
+ common_cgroup_do_log(quiet, !(meminfo->limit), "Your kernel does not support cgroup2 memory max");
+
+ meminfo->reservation = cgroup_v2_enabled(CGROUP_ISULAD_PATH, CGROUP2_MEMORY_LOW);
+ common_cgroup_do_log(quiet, !(meminfo->reservation), "Your kernel does not support cgroup2 memory low");
- // io cgroup
+ meminfo->swap = cgroup_v2_enabled(CGROUP_ISULAD_PATH, CGROUP2_MEMORY_SWAP_MAX);
+ common_cgroup_do_log(quiet, !(meminfo->swap), "Your kernel does not support cgroup2 memory swap max");
+}
+
+static void get_cgroup_v2_blkio_info(const bool quiet, cgroup_blkio_info_t *blkioinfo)
+{
blkioinfo->blkio_weight = cgroup_v2_enabled(CGROUP_ISULAD_PATH, CGROUP2_IO_BFQ_WEIGHT) ||
cgroup_v2_enabled(CGROUP_ISULAD_PATH, CGROUP2_IO_WEIGHT);
blkioinfo->blkio_weight_device = blkioinfo->blkio_weight;
@@ -355,22 +324,14 @@ int common_get_cgroup_info_v2(cgroup_mem_info_t *meminfo, cgroup_cpu_info_t *cpu
blkioinfo->blkio_read_iops_device = blkioinfo->blkio_read_bps_device;
blkioinfo->blkio_write_iops_device = blkioinfo->blkio_read_bps_device;
common_cgroup_do_log(quiet, !(blkioinfo->blkio_read_bps_device), "Your kernel does not support cgroup2 io max");
+}
- // memory cgroup
- meminfo->limit = cgroup_v2_enabled(CGROUP_ISULAD_PATH, CGROUP2_MEMORY_MAX);
- common_cgroup_do_log(quiet, !(meminfo->limit), "Your kernel does not support cgroup2 memory max");
-
- meminfo->reservation = cgroup_v2_enabled(CGROUP_ISULAD_PATH, CGROUP2_MEMORY_LOW);
- common_cgroup_do_log(quiet, !(meminfo->reservation), "Your kernel does not support cgroup2 memory low");
-
- meminfo->swap = cgroup_v2_enabled(CGROUP_ISULAD_PATH, CGROUP2_MEMORY_SWAP_MAX);
- common_cgroup_do_log(quiet, !(meminfo->swap), "Your kernel does not support cgroup2 memory swap max");
-
- // pids cgroup
- pidsinfo->pidslimit = cgroup_v2_enabled(CGROUP_ISULAD_PATH, CGROUP2_PIDS_MAX);
- common_cgroup_do_log(quiet, !(pidsinfo->pidslimit), "Your kernel does not support cgroup2 pids max");
+static int get_cgroup_v2_hugetlb_info(const bool quiet, cgroup_hugetlb_info_t *hugetlbinfo)
+{
+ __isula_auto_free char *size = NULL;
+ int nret = 0;
+ char path[PATH_MAX] = { 0 };
- // hugetlb cgroup
size = get_default_huge_page_size();
if (size != NULL) {
nret = snprintf(path, sizeof(path), CGROUP2_HUGETLB_MAX, size);
@@ -383,15 +344,55 @@ int common_get_cgroup_info_v2(cgroup_mem_info_t *meminfo, cgroup_cpu_info_t *cpu
} else {
WARN("Your kernel does not support cgroup2 hugetlb limit");
}
+ return 0;
+}
+
+static void get_cgroup_v2_pids_info(const bool quiet, cgroup_pids_info_t *pidsinfo)
+{
+ pidsinfo->pidslimit = cgroup_v2_enabled(CGROUP_ISULAD_PATH, CGROUP2_PIDS_MAX);
+ common_cgroup_do_log(quiet, !(pidsinfo->pidslimit), "Your kernel does not support cgroup2 pids max");
+
+}
- // files cgroup
+static void get_cgroup_v2_files_info(const bool quiet, cgroup_files_info_t *filesinfo)
+{
filesinfo->fileslimit = cgroup_v2_enabled(CGROUP_ISULAD_PATH, CGROUP2_FILES_LIMIT);
common_cgroup_do_log(quiet, !(filesinfo->fileslimit), "Your kernel does not support cgroup2 files limit");
- return ret;
}
-int common_get_cgroup_v2_metrics(const char *cgroup_path, cgroup_metrics_t *cgroup_metrics)
+static int get_cgroup_info_v2(cgroup_mem_info_t *meminfo, cgroup_cpu_info_t *cpuinfo,
+ cgroup_hugetlb_info_t *hugetlbinfo, cgroup_blkio_info_t *blkioinfo,
+ cgroup_cpuset_info_t *cpusetinfo, cgroup_pids_info_t *pidsinfo,
+ cgroup_files_info_t *filesinfo, bool quiet)
+{
+ int ret = 0;
+ if (make_sure_cgroup2_isulad_path_exist() != 0) {
+ return -1;
+ }
+
+ get_cgroup_v2_cpu_info(quiet, cpuinfo);
+
+ ret = get_cgroup_v2_cpuset_info(quiet, cpusetinfo);
+ if (ret != 0) {
+ return ret;
+ }
+
+ get_cgroup_v2_mem_info(quiet, meminfo);
+ get_cgroup_v2_blkio_info(quiet, blkioinfo);
+
+ ret = get_cgroup_v2_hugetlb_info(quiet, hugetlbinfo);
+ if (ret != 0) {
+ return ret;
+ }
+
+ get_cgroup_v2_pids_info(quiet, pidsinfo);
+ get_cgroup_v2_files_info(quiet, filesinfo);
+
+ return 0;
+}
+
+static int get_cgroup_metrics_v2(const char *cgroup_path, cgroup_metrics_t *cgroup_metrics)
{
if (cgroup_path == NULL || strlen(cgroup_path) == 0 || cgroup_metrics == NULL) {
ERROR("Invalid arguments");
@@ -404,3 +405,26 @@ int common_get_cgroup_v2_metrics(const char *cgroup_path, cgroup_metrics_t *cgro
return 0;
}
+
+static int get_cgroup_mnt_and_root_v2(const char *subsystem, char **mountpoint, char **root)
+{
+ *mountpoint = util_strdup_s(CGROUP_ISULAD_PATH);
+ return 0;
+}
+
+int get_cgroup_version_v2()
+{
+ return CGROUP_VERSION_2;
+}
+
+int cgroup_v2_ops_init(cgroup_ops *ops)
+{
+ if (ops == NULL) {
+ return -1;
+ }
+ ops->get_cgroup_version = get_cgroup_version_v2;
+ ops->get_cgroup_info = get_cgroup_info_v2;
+ ops->get_cgroup_metrics = get_cgroup_metrics_v2;
+ ops->get_cgroup_mnt_and_root_path = get_cgroup_mnt_and_root_v2;
+ return 0;
+}
\ No newline at end of file
diff --git a/src/daemon/common/cgroup/cgroup_v2.h b/src/daemon/common/cgroup/cgroup_v2.h
new file mode 100644
index 00000000..0e8e4818
--- /dev/null
+++ b/src/daemon/common/cgroup/cgroup_v2.h
@@ -0,0 +1,30 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: zhongtao
+ * Create: 2024-02-19
+ * Description: provide cgroup v2 definition
+ ******************************************************************************/
+#ifndef DAEMON_COMMON_CGROUP_CGROUP_V2_H
+#define DAEMON_COMMON_CGROUP_CGROUP_V2_H
+
+#include "cgroup.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int cgroup_v2_ops_init(cgroup_ops *ops);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // DAEMON_COMMON_CGROUP_CGROUP_V2_H
diff --git a/src/daemon/common/sysinfo.c b/src/daemon/common/sysinfo.c
index a082f717..e369c3e3 100644
--- a/src/daemon/common/sysinfo.c
+++ b/src/daemon/common/sysinfo.c
@@ -188,15 +188,10 @@ static char **get_huge_page_sizes()
struct dirent *info_archivo = NULL;
int cgroup_version = 0;
- cgroup_version = common_get_cgroup_version();
- if (cgroup_version == CGROUP_VERSION_2) {
- hugetlbmp = util_strdup_s(CGROUP_ISULAD_PATH);
- } else {
- ret = common_find_cgroup_mnt_and_root("hugetlb", &hugetlbmp, NULL);
- if (ret != 0 || hugetlbmp == NULL) {
- ERROR("Hugetlb cgroup not supported");
- return NULL;
- }
+ ret = common_get_cgroup_mnt_and_root_path("hugetlb", &hugetlbmp, NULL);
+ if (ret != 0 || hugetlbmp == NULL) {
+ ERROR("Hugetlb cgroup not supported");
+ return NULL;
}
dir = opendir(hugetlbmp);
@@ -211,6 +206,7 @@ static char **get_huge_page_sizes()
char *pos = NULL;
char *dot2 = NULL;
+ cgroup_version = common_get_cgroup_version();
if (cgroup_version == CGROUP_VERSION_2) {
if (!is_hugetlb_max(info_archivo->d_name)) {
continue;
@@ -375,7 +371,6 @@ void free_sysinfo(sysinfo_t *sysinfo)
/* get sys info */
sysinfo_t *get_sys_info(bool quiet)
{
- int cgroup_version = 0;
sysinfo_t *sysinfo = NULL;
int ret = 0;
@@ -388,21 +383,9 @@ sysinfo_t *get_sys_info(bool quiet)
sysinfo->ncpus = get_nprocs();
sysinfo->ncpus_conf = get_nprocs_conf();
- cgroup_version = common_get_cgroup_version();
- if (cgroup_version < 0) {
- ret = -1;
- goto out;
- }
-
- if (cgroup_version == CGROUP_VERSION_1) {
- ret = common_get_cgroup_info_v1(&sysinfo->cgmeminfo, &sysinfo->cgcpuinfo, &sysinfo->hugetlbinfo,
+ ret = common_get_cgroup_info(&sysinfo->cgmeminfo, &sysinfo->cgcpuinfo, &sysinfo->hugetlbinfo,
&sysinfo->blkioinfo, &sysinfo->cpusetinfo, &sysinfo->pidsinfo,
&sysinfo->filesinfo, quiet);
- } else {
- ret = common_get_cgroup_info_v2(&sysinfo->cgmeminfo, &sysinfo->cgcpuinfo, &sysinfo->hugetlbinfo,
- &sysinfo->blkioinfo, &sysinfo->cpusetinfo, &sysinfo->pidsinfo,
- &sysinfo->filesinfo, quiet);
- }
if (ret != 0) {
goto out;
}
@@ -563,7 +546,7 @@ free_out:
return minfos;
}
-char *sysinfo_cgroup_controller_cpurt_mnt_path(void)
+char *sysinfo_get_cpurt_mnt_path(void)
{
int nret = 0;
__isula_auto_free char *mnt = NULL;
@@ -583,7 +566,7 @@ char *sysinfo_cgroup_controller_cpurt_mnt_path(void)
return NULL;
}
- nret = common_find_cgroup_mnt_and_root("cpu", &mnt, &root);
+ nret = common_get_cgroup_mnt_and_root_path("cpu", &mnt, &root);
if (nret != 0 || mnt == NULL || root == NULL) {
ERROR("Can not find cgroup mnt and root path for subsystem 'cpu'");
isulad_set_error_message("Can not find cgroup mnt and root path for subsystem 'cpu'");
diff --git a/src/daemon/common/sysinfo.h b/src/daemon/common/sysinfo.h
index cb44d1c5..6142487b 100644
--- a/src/daemon/common/sysinfo.h
+++ b/src/daemon/common/sysinfo.h
@@ -95,13 +95,13 @@ mountinfo_t *find_mount_info(mountinfo_t **minfos, const char *dir);
void free_mounts_info(mountinfo_t **minfos);
-char *sysinfo_cgroup_controller_cpurt_mnt_path(void);
-
// define auto free function callback for sysinfo_t
define_auto_cleanup_callback(free_sysinfo, sysinfo_t)
// define auto free macro for sysinfo_t
#define __isula_auto_sysinfo_t auto_cleanup_tag(free_sysinfo)
+char *sysinfo_get_cpurt_mnt_path(void);
+
#ifdef __cplusplus
}
#endif
diff --git a/src/daemon/entry/cri/v1/v1_cri_pod_sandbox_manager_service.cc b/src/daemon/entry/cri/v1/v1_cri_pod_sandbox_manager_service.cc
index 3bdc3af8..f125e714 100644
--- a/src/daemon/entry/cri/v1/v1_cri_pod_sandbox_manager_service.cc
+++ b/src/daemon/entry/cri/v1/v1_cri_pod_sandbox_manager_service.cc
@@ -872,18 +872,7 @@ void PodSandboxManagerService::GetPodSandboxCgroupMetrics(const std::string &cgr
return;
}
- auto cgroupVersion = common_get_cgroup_version();
- if (cgroupVersion < 0) {
- error.Errorf("Invalid cgroup version");
- return;
- }
-
- if (cgroupVersion == CGROUP_VERSION_1) {
- nret = common_get_cgroup_v1_metrics(cgroupParent.c_str(), &cgroupMetrics);
- } else {
- nret = common_get_cgroup_v2_metrics(cgroupParent.c_str(), &cgroupMetrics);
- }
-
+ nret = common_get_cgroup_metrics(cgroupParent.c_str(), &cgroupMetrics);
if (nret != 0) {
error.Errorf("Failed to get cgroup metrics");
}
diff --git a/src/daemon/entry/cri/v1alpha/cri_pod_sandbox_manager_service.cc b/src/daemon/entry/cri/v1alpha/cri_pod_sandbox_manager_service.cc
index 49a7ca54..af6b5fff 100644
--- a/src/daemon/entry/cri/v1alpha/cri_pod_sandbox_manager_service.cc
+++ b/src/daemon/entry/cri/v1alpha/cri_pod_sandbox_manager_service.cc
@@ -1328,18 +1328,7 @@ void PodSandboxManagerService::GetPodSandboxCgroupMetrics(const container_inspec
return;
}
- auto cgroupVersion = common_get_cgroup_version();
- if (cgroupVersion < 0) {
- error.Errorf("Invalid cgroup version");
- return;
- }
-
- if (cgroupVersion == CGROUP_VERSION_1) {
- nret = common_get_cgroup_v1_metrics(cgroupParent, &cgroupMetrics);
- } else {
- nret = common_get_cgroup_v2_metrics(cgroupParent, &cgroupMetrics);
- }
-
+ nret = common_get_cgroup_metrics(cgroupParent, &cgroupMetrics);
if (nret != 0) {
error.Errorf("Failed to get cgroup metrics");
}
diff --git a/src/daemon/executor/container_cb/execution.c b/src/daemon/executor/container_cb/execution.c
index 7ed8e837..88c6b354 100644
--- a/src/daemon/executor/container_cb/execution.c
+++ b/src/daemon/executor/container_cb/execution.c
@@ -413,6 +413,13 @@ static int cpurt_controller_init(const char *id, const host_config *host_spec)
char *dirpath = NULL;
int64_t cpu_rt_period = 0;
int64_t cpu_rt_runtime = 0;
+ int cgroup_version = 0;
+
+ // cgroup v2 is not support cpurt
+ cgroup_version = common_get_cgroup_version();
+ if (cgroup_version == CGROUP_VERSION_2) {
+ return 0;
+ }
cgroups_path = merge_container_cgroups_path(id, host_spec);
if (cgroups_path == NULL || strcmp(cgroups_path, "/") == 0 || strcmp(cgroups_path, ".") == 0) {
@@ -433,13 +440,13 @@ static int cpurt_controller_init(const char *id, const host_config *host_spec)
// should iSulad set cpu.rt_runtime_us and cpu.rt_period_us for the parent path?
// in fact, even if system.slice is used,
// cpu.rt_runtime_us and cpu.rt_period_us might still needed to be set manually
- __isula_auto_free char *init_cgroup = common_get_init_cgroup("cpu");
+ __isula_auto_free char *init_cgroup = common_get_init_cgroup_path("cpu");
if (init_cgroup == NULL) {
ERROR("Failed to get init cgroup");
return -1;
}
// make sure that the own cgroup path for cpu existed
- __isula_auto_free char *own_cgroup = common_get_own_cgroup("cpu");
+ __isula_auto_free char *own_cgroup = common_get_own_cgroup_path("cpu");
if (own_cgroup == NULL) {
ERROR("Failed to get own cgroup");
return -1;
@@ -453,7 +460,7 @@ static int cpurt_controller_init(const char *id, const host_config *host_spec)
cgroups_path = new_cgroups_path;
}
- mnt_root = sysinfo_cgroup_controller_cpurt_mnt_path();
+ mnt_root = sysinfo_get_cpurt_mnt_path();
if (mnt_root == NULL) {
ERROR("Failed to get cpu rt controller mnt root path");
return -1;
--
2.34.1
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jikai11/src-openEuler-iSulad.git
git@gitee.com:jikai11/src-openEuler-iSulad.git
jikai11
src-openEuler-iSulad
src-openEuler-iSulad
master

搜索帮助