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 <sys/types.h> 20 #include <fcntl.h> 21 22 #include <map> 23 #include <string> 24 #include <vector> 25 26 #include "single_instance.h" 27 28 namespace OHOS { 29 namespace Memory { 30 #define PAGE_TO_KB 4 31 #define KB_PER_MB 1024 32 #define MAX_BUFFER_KB 0x7fffffff 33 34 struct ProcInfo { 35 int tgid; 36 int pid; 37 int pidfd; 38 int size; 39 std::string name; 40 std::string status; 41 }; 42 43 class KernelInterface { 44 DECLARE_SINGLE_INSTANCE(KernelInterface); 45 46 public: 47 bool EchoToPath(const char* path, const char* content); 48 49 // file operations 50 bool IsFileExists(const std::string& path); 51 bool CreateFile(const std::string& path); // default mode 644(-rw-r--r--) 52 bool CreateFile(const std::string& path, const mode_t& mode); 53 bool RemoveFile(const std::string& path); 54 bool WriteToFile(const std::string& path, const std::string& content, bool truncated = true); 55 bool WriteLinesToFile(const std::string& path, const std::vector<std::string>& lines, bool truncated = true); 56 bool ReadFromFile(const std::string& path, std::string& content); 57 bool ReadLinesFromFile(const std::string& path, std::vector<std::string>& lines); 58 // dir operations 59 bool IsDirExists(const std::string& path); 60 bool IsExists(const std::string& path); // file or dir 61 bool IsEmptyDir(const std::string& path); 62 bool CreateDir(const std::string& path); // create dir recursively 755 63 bool RemoveDirRecursively(const std::string& path); 64 std::string AddDelimiter(const std::string& path); // IncludeTrailingPathDelimiter 65 std::string RmDelimiter(const std::string& path); // ExcludeTrailingPathDelimiter 66 std::string JoinPath(const std::string& prefixPath, const std::string& subPath); 67 std::string JoinPath(const std::string& prefixPath, const std::string& midPath, const std::string& subPath); 68 69 bool GetPidProcInfo(struct ProcInfo &procInfo); 70 void ReadZswapdPressureShow(std::map<std::string, std::string>& result); 71 int GetCurrentBuffer(); 72 int KillOneProcessByPid(int pid); 73 74 static const std::string MEMCG_BASE_PATH; 75 static const std::string ZWAPD_PRESSURE_SHOW_PATH; 76 static const std::string ZWAPD_PRESSURE_SHOW_BUFFER_SIZE; 77 static constexpr mode_t FILE_MODE_664 = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH; // -rw-rw-r-- 78 static constexpr mode_t FILE_MODE_644 = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; // -rw-r--r-- 79 static constexpr mode_t FILE_MODE_660 = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; 80 static constexpr mode_t FILE_MODE_755 = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; 81 static constexpr mode_t FILE_MODE_775 = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH; 82 static constexpr mode_t FILE_MODE_770 = S_IRWXU | S_IRWXG; 83 static constexpr mode_t FILE_MODE_700 = S_IRWXU; 84 }; 85 } // namespace Memory 86 } // namespace OHOS 87 #endif // OHOS_MEMORY_MEMMGR_COMMON_INCLUDE_KERNEL_INTERFACE_H 88