1ba5c3796Sopenharmony_ci/*
2ba5c3796Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3ba5c3796Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4ba5c3796Sopenharmony_ci * you may not use this file except in compliance with the License.
5ba5c3796Sopenharmony_ci * You may obtain a copy of the License at
6ba5c3796Sopenharmony_ci *
7ba5c3796Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8ba5c3796Sopenharmony_ci *
9ba5c3796Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10ba5c3796Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11ba5c3796Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12ba5c3796Sopenharmony_ci * See the License for the specific language governing permissions and
13ba5c3796Sopenharmony_ci * limitations under the License.
14ba5c3796Sopenharmony_ci */
15ba5c3796Sopenharmony_ci
16ba5c3796Sopenharmony_ci#include "gtest/gtest.h"
17ba5c3796Sopenharmony_ci
18ba5c3796Sopenharmony_ci#include "utils.h"
19ba5c3796Sopenharmony_ci#include "kernel_interface.h"
20ba5c3796Sopenharmony_ci
21ba5c3796Sopenharmony_ci#define private public
22ba5c3796Sopenharmony_ci#define protected public
23ba5c3796Sopenharmony_ci#include "memcg_mgr.h"
24ba5c3796Sopenharmony_ci#undef private
25ba5c3796Sopenharmony_ci#undef protected
26ba5c3796Sopenharmony_ci
27ba5c3796Sopenharmony_cinamespace OHOS {
28ba5c3796Sopenharmony_cinamespace Memory {
29ba5c3796Sopenharmony_ciusing namespace testing;
30ba5c3796Sopenharmony_ciusing namespace testing::ext;
31ba5c3796Sopenharmony_ci
32ba5c3796Sopenharmony_ciclass MemcgMgrTest : public testing::Test {
33ba5c3796Sopenharmony_cipublic:
34ba5c3796Sopenharmony_ci    static void SetUpTestCase();
35ba5c3796Sopenharmony_ci    static void TearDownTestCase();
36ba5c3796Sopenharmony_ci    void SetUp();
37ba5c3796Sopenharmony_ci    void TearDown();
38ba5c3796Sopenharmony_ci};
39ba5c3796Sopenharmony_ci
40ba5c3796Sopenharmony_civoid MemcgMgrTest::SetUpTestCase()
41ba5c3796Sopenharmony_ci{
42ba5c3796Sopenharmony_ci}
43ba5c3796Sopenharmony_ci
44ba5c3796Sopenharmony_civoid MemcgMgrTest::TearDownTestCase()
45ba5c3796Sopenharmony_ci{
46ba5c3796Sopenharmony_ci}
47ba5c3796Sopenharmony_ci
48ba5c3796Sopenharmony_civoid MemcgMgrTest::SetUp()
49ba5c3796Sopenharmony_ci{
50ba5c3796Sopenharmony_ci}
51ba5c3796Sopenharmony_ci
52ba5c3796Sopenharmony_civoid MemcgMgrTest::TearDown()
53ba5c3796Sopenharmony_ci{
54ba5c3796Sopenharmony_ci}
55ba5c3796Sopenharmony_ci
56ba5c3796Sopenharmony_ciHWTEST_F(MemcgMgrTest, GetRootMemcgTest, TestSize.Level1)
57ba5c3796Sopenharmony_ci{
58ba5c3796Sopenharmony_ci    Memcg* memcg = MemcgMgr::GetInstance().GetRootMemcg();
59ba5c3796Sopenharmony_ci    EXPECT_EQ(memcg != nullptr, true);
60ba5c3796Sopenharmony_ci    EXPECT_STREQ(memcg->GetMemcgPath_().c_str(), "/dev/memcg");
61ba5c3796Sopenharmony_ci}
62ba5c3796Sopenharmony_ci
63ba5c3796Sopenharmony_ciHWTEST_F(MemcgMgrTest, SetRootMemcgParaTest, TestSize.Level1)
64ba5c3796Sopenharmony_ci{
65ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().SetRootMemcgPara(), true);
66ba5c3796Sopenharmony_ci}
67ba5c3796Sopenharmony_ci
68ba5c3796Sopenharmony_ciHWTEST_F(MemcgMgrTest, GetUserMemcgTest, TestSize.Level1)
69ba5c3796Sopenharmony_ci{
70ba5c3796Sopenharmony_ci    unsigned int memcgId = 123456u; // ensure it is a new ID
71ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().GetUserMemcg(memcgId) == nullptr, true);
72ba5c3796Sopenharmony_ci}
73ba5c3796Sopenharmony_ci
74ba5c3796Sopenharmony_ciHWTEST_F(MemcgMgrTest, AddUserMemcgTest, TestSize.Level1)
75ba5c3796Sopenharmony_ci{
76ba5c3796Sopenharmony_ci    unsigned int memcgId = 123456u; // ensure it is a new ID
77ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().GetUserMemcg(memcgId) == nullptr, true);
78ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().AddUserMemcg(memcgId) != nullptr, true);
79ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().GetUserMemcg(memcgId) != nullptr, true);
80ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().RemoveUserMemcg(memcgId), true);
81ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().GetUserMemcg(memcgId) == nullptr, true);
82ba5c3796Sopenharmony_ci}
83ba5c3796Sopenharmony_ci
84ba5c3796Sopenharmony_ciHWTEST_F(MemcgMgrTest, RemoveUserMemcgTest, TestSize.Level1)
85ba5c3796Sopenharmony_ci{
86ba5c3796Sopenharmony_ci    unsigned int memcgId = 123456u; // ensure it is a new ID
87ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().AddUserMemcg(memcgId) != nullptr, true);
88ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().RemoveUserMemcg(memcgId), true);
89ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().GetUserMemcg(memcgId) == nullptr, true);
90ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().RemoveUserMemcg(memcgId), false);
91ba5c3796Sopenharmony_ci}
92ba5c3796Sopenharmony_ci
93ba5c3796Sopenharmony_ciHWTEST_F(MemcgMgrTest, UpdateMemcgScoreAndReclaimRatiosTest, TestSize.Level1)
94ba5c3796Sopenharmony_ci{
95ba5c3796Sopenharmony_ci    unsigned int memcgId = 123456u; // ensure it is a new ID
96ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().AddUserMemcg(memcgId) != nullptr, true);
97ba5c3796Sopenharmony_ci    ReclaimRatios ratios;
98ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().UpdateMemcgScoreAndReclaimRatios(memcgId, 200, ratios), true);
99ba5c3796Sopenharmony_ci    MemcgMgr::GetInstance().RemoveUserMemcg(memcgId);
100ba5c3796Sopenharmony_ci}
101ba5c3796Sopenharmony_ci
102ba5c3796Sopenharmony_ciHWTEST_F(MemcgMgrTest, AddProcToMemcgTest, TestSize.Level1)
103ba5c3796Sopenharmony_ci{
104ba5c3796Sopenharmony_ci    unsigned int memcgId = 123456u; // ensure it is a new ID
105ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().AddProcToMemcg(1234567, memcgId), false);
106ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().AddProcToMemcg(1, memcgId), true);
107ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().GetUserMemcg(memcgId) != nullptr, true);
108ba5c3796Sopenharmony_ci    KernelInterface::GetInstance().WriteToFile("/dev/memcg/cgroup.procs", "1", false);
109ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().RemoveUserMemcg(memcgId), true);
110ba5c3796Sopenharmony_ci}
111ba5c3796Sopenharmony_ci
112ba5c3796Sopenharmony_ciHWTEST_F(MemcgMgrTest, SwapInMemcgTest, TestSize.Level1)
113ba5c3796Sopenharmony_ci{
114ba5c3796Sopenharmony_ci    unsigned int memcgId = 123456u; // ensure it is a new ID
115ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().SwapInMemcg(memcgId), false);
116ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().AddUserMemcg(memcgId) != nullptr, true);
117ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().SwapInMemcg(memcgId), true);
118ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().RemoveUserMemcg(memcgId), true);
119ba5c3796Sopenharmony_ci}
120ba5c3796Sopenharmony_ci
121ba5c3796Sopenharmony_ciHWTEST_F(MemcgMgrTest, GetMemcgSwapInfoTest, TestSize.Level1)
122ba5c3796Sopenharmony_ci{
123ba5c3796Sopenharmony_ci    unsigned int memcgId = 123456u; // ensure it is a new ID
124ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().GetMemcgSwapInfo(memcgId) == nullptr, true);
125ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().AddUserMemcg(memcgId) != nullptr, true);
126ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().GetMemcgSwapInfo(memcgId) != nullptr, true);
127ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().RemoveUserMemcg(memcgId), true);
128ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().GetMemcgSwapInfo(memcgId) == nullptr, true);
129ba5c3796Sopenharmony_ci}
130ba5c3796Sopenharmony_ci
131ba5c3796Sopenharmony_ciHWTEST_F(MemcgMgrTest, GetMemcgMemInfoTest, TestSize.Level1)
132ba5c3796Sopenharmony_ci{
133ba5c3796Sopenharmony_ci    unsigned int memcgId = 123456u; // ensure it is a new ID
134ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().GetMemcgMemInfo(memcgId) == nullptr, true);
135ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().AddUserMemcg(memcgId) != nullptr, true);
136ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().GetMemcgMemInfo(memcgId) != nullptr, true);
137ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().RemoveUserMemcg(memcgId), true);
138ba5c3796Sopenharmony_ci    EXPECT_EQ(MemcgMgr::GetInstance().GetMemcgMemInfo(memcgId) == nullptr, true);
139ba5c3796Sopenharmony_ci}
140ba5c3796Sopenharmony_ci}
141ba5c3796Sopenharmony_ci}
142