1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef OHOS_MEMORY_MEMMGR_COMMON_INCLUDE_KERNEL_INTERFACE_H
17#define OHOS_MEMORY_MEMMGR_COMMON_INCLUDE_KERNEL_INTERFACE_H
18
19#include <fcntl.h>
20#include <map>
21#include <string>
22#include <sys/types.h>
23#include <vector>
24
25#include "single_instance.h"
26
27namespace OHOS {
28namespace Memory {
29#define PAGE_TO_KB 4
30#define KB_PER_MB 1024
31#define MAX_BUFFER_KB 0x7fffffff
32
33struct ProcInfo {
34    int tgid;
35    int pid;
36    int pidfd;
37    int size;
38    std::string name;
39    std::string status;
40};
41
42class KernelInterface {
43    DECLARE_SINGLE_INSTANCE(KernelInterface);
44
45public:
46    bool EchoToPath(const char* path, const char* content);
47
48    // file operations
49    bool IsFileExists(const std::string& path);
50    bool CreateFile(const std::string& path); // default mode 644(-rw-r--r--)
51    bool CreateFile(const std::string& path, const mode_t& mode);
52    bool RemoveFile(const std::string& path);
53    bool WriteToFile(const std::string& path, const std::string& content, bool truncated = true);
54    bool ReadFromFile(const std::string& path, std::string& content);
55    bool ReadLinesFromFile(const std::string& path, std::vector<std::string>& lines);
56    // dir operations
57    bool IsDirExists(const std::string& path);
58    bool IsExists(const std::string& path); // file or dir
59    bool IsEmptyDir(const std::string& path);
60    bool CreateDir(const std::string& path); // create dir recursively 755
61    bool RemoveDirRecursively(const std::string& path);
62    std::string AddDelimiter(const std::string& path); // IncludeTrailingPathDelimiter
63    std::string RmDelimiter(const std::string& path); // ExcludeTrailingPathDelimiter
64    std::string JoinPath(const std::string& prefixPath, const std::string& subPath);
65    std::string JoinPath(const std::string& prefixPath, const std::string& midPath, const std::string& subPath);
66
67    void SplitOneLineByDelim(const std::string &input, const char delimiter, std::vector<std::string> &res);
68    void SplitOneLineByBlank(const std::string &input, std::vector<std::string> &res);
69
70    bool GetPidProcInfo(struct ProcInfo &procInfo);
71    bool GetProcNameByPid(int pid, std::string &name);
72    void ReadZswapdPressureShow(std::map<std::string, std::string>& result);
73    int GetCurrentBuffer();
74    int KillOneProcessByPid(int pid);
75    bool GetAllProcPids(std::vector<unsigned int>& pids);
76    bool GetUidByPid(unsigned int pid, unsigned int& uid);
77    bool ReadSwapOutKBSinceKernelBoot(const std::string &path, const std::string &tagStr, unsigned long long &ret);
78    int64_t GetSystemCurTime();
79    int64_t GetSystemTimeMs();
80
81    int GetTotalBuffer();
82    bool GetMemcgPids(const std::string &memcgPath, std::vector<int> &memcgPids);
83    bool GetAllUserIds(std::vector<int> &userIds);
84
85    static const std::string ROOT_PROC_PATH;
86    static const std::string MEMCG_BASE_PATH;
87    static const std::string FILE_MEMCG_PROCS;
88    static const std::string ZWAPD_PRESSURE_SHOW_PATH;
89    static const std::string ZWAPD_PRESSURE_SHOW_BUFFER_SIZE;
90    static const std::string MEMINFO_PATH;
91    static const std::string FILE_PROC_STATUS;
92    static const std::string TOTAL_MEMORY;
93
94    static constexpr mode_t FILE_MODE_666 = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; // -rw-rw-rw--
95    static constexpr mode_t FILE_MODE_664 = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH; // -rw-rw-r--
96    static constexpr mode_t FILE_MODE_644 = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; // -rw-r--r--
97    static constexpr mode_t FILE_MODE_660 = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
98    static constexpr mode_t FILE_MODE_755 = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
99    static constexpr mode_t FILE_MODE_775 = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH;
100    static constexpr mode_t FILE_MODE_770 = S_IRWXU | S_IRWXG;
101    static constexpr mode_t FILE_MODE_700 = S_IRWXU;
102private:
103    int ParseMeminfo(const std::string &contentStr, const std::string &itemName);
104
105    int totalBuffer_ = -1;
106};
107} // namespace Memory
108} // namespace OHOS
109#endif // OHOS_MEMORY_MEMMGR_COMMON_INCLUDE_KERNEL_INTERFACE_H
110