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