1 /*
2  * Copyright (c) 2024 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 #include "process_collector_impl.h"
17 
18 #include <dlfcn.h>
19 #include <fstream>
20 
21 #include "common_util.h"
22 #include "file_util.h"
23 #include "hiview_logger.h"
24 #include "process_decorator.h"
25 
26 using namespace OHOS::HiviewDFX::UCollect;
27 
28 namespace OHOS {
29 namespace HiviewDFX {
30 namespace UCollectUtil {
31 namespace {
32 DEFINE_LOG_TAG("UCollectUtil-ProcessCollector");
33 const std::string LIB_NAME = "libucollection_utility_ex.z.so";
34 const std::string GET_MEM_CG_PROCESSES_FUNC_NAME = "GetMemCgProcesses";
35 const std::string PROCESS_COLLECTOT_DIR = "/data/log/hiview/unified_collection/process/";
36 constexpr int32_t MAX_FILE_NUM = 10;
37 const std::string PREFIX = "memcg_process_";
38 const std::string SUFFIX = ".txt";
39 }
40 
ProcessCollectorImpl()41 ProcessCollectorImpl::ProcessCollectorImpl()
42 {
43     handle_ = dlopen(LIB_NAME.c_str(), RTLD_LAZY);
44     if (handle_ == nullptr) {
45         HIVIEW_LOGW("dlopen failed, error: %{public}s", dlerror());
46     }
47 }
48 
~ProcessCollectorImpl()49 ProcessCollectorImpl::~ProcessCollectorImpl()
50 {
51     if (handle_ != nullptr) {
52         dlclose(handle_);
53     }
54 }
55 
Create()56 std::shared_ptr<ProcessCollector> ProcessCollector::Create()
57 {
58     static std::shared_ptr<ProcessCollector> instance_ =
59         std::make_shared<ProcessDecorator>(std::make_shared<ProcessCollectorImpl>());
60     return instance_;
61 }
62 
GetMemCgProcesses()63 CollectResult<std::unordered_set<int32_t>> ProcessCollectorImpl::GetMemCgProcesses()
64 {
65     CollectResult<std::unordered_set<int32_t>> result;
66     if (handle_ == nullptr) {
67         return result;
68     }
69 
70     using GetMemCgProcesses = bool (*)(std::unordered_set<int32_t>&);
71     GetMemCgProcesses getMemCgProcesses =
72         reinterpret_cast<GetMemCgProcesses>(dlsym(handle_, GET_MEM_CG_PROCESSES_FUNC_NAME.c_str()));
73     if (!getMemCgProcesses) {
74         HIVIEW_LOGW("dlsym failed, %{public}s.", dlerror());
75         return result;
76     }
77 
78     std::unordered_set<int32_t> memCgProcs;
79     if (!getMemCgProcesses(memCgProcs)) {
80         result.retCode = UcError::READ_FAILED;
81         return result;
82     }
83     result.data = memCgProcs;
84     result.retCode = UcError::SUCCESS;
85     return result;
86 }
87 
IsMemCgProcess(int32_t pid)88 CollectResult<bool> ProcessCollectorImpl::IsMemCgProcess(int32_t pid)
89 {
90     CollectResult<bool> result;
91     auto memCgProcsResult = GetMemCgProcesses();
92     if (memCgProcsResult.retCode != UcError::SUCCESS) {
93         result.retCode = memCgProcsResult.retCode;
94         return result;
95     }
96 
97     auto memCgProcs = memCgProcsResult.data;
98     result.data = memCgProcs.find(pid) != memCgProcs.end();
99     result.retCode = UcError::SUCCESS;
100     return result;
101 }
102 
ExportMemCgProcesses()103 CollectResult<std::string> ProcessCollectorImpl::ExportMemCgProcesses()
104 {
105     CollectResult<std::string> result;
106     auto memCgProcsResult = GetMemCgProcesses();
107     if (memCgProcsResult.retCode != UcError::SUCCESS) {
108         result.retCode = memCgProcsResult.retCode;
109         return result;
110     }
111 
112     std::string filePath;
113     {
114         std::unique_lock<std::mutex> lock(fileMutex_);
115         filePath = CommonUtil::CreateExportFile(PROCESS_COLLECTOT_DIR, MAX_FILE_NUM, PREFIX, SUFFIX);
116     }
117 
118     std::ofstream file;
119     file.open(filePath.c_str(), std::ios::out | std::ios::trunc);
120     if (!file.is_open()) {
121         HIVIEW_LOGW("open %{public}s failed.",  FileUtil::ExtractFileName(filePath).c_str());
122         result.retCode = UcError::WRITE_FAILED;
123         return result;
124     }
125     for (const auto& proc : memCgProcsResult.data) {
126         file << proc << std::endl;
127     }
128     file.close();
129 
130     result.data = filePath;
131     result.retCode = UcError::SUCCESS;
132     return result;
133 }
134 } // UCollectUtil
135 } // HiViewDFX
136 } // OHOS
137