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 "wm_collector_impl.h"
17
18 #include <mutex>
19
20 #include <fcntl.h>
21 #include <unistd.h>
22
23 #include "common_util.h"
24 #include "common_utils.h"
25 #include "hiview_logger.h"
26 #include "string_ex.h"
27 #include "wm_decorator.h"
28
29 using namespace OHOS::HiviewDFX::UCollect;
30
31 namespace OHOS {
32 namespace HiviewDFX {
33 namespace UCollectUtil {
34 namespace {
35 DEFINE_LOG_TAG("UCollectUtil-WmCollector");
36 constexpr int32_t MAX_FILE_NUM = 10;
37 const std::string COLLECTION_WM_PATH = "/data/log/hiview/unified_collection/wm/";
38 const std::string GPU_MEMORY_PATH = "/proc/gpu_memory";
39 std::mutex g_memMutex;
40
CreateExportFileName(const std::string & filePrefix,const std::string & ext)41 std::string CreateExportFileName(const std::string& filePrefix, const std::string& ext)
42 {
43 std::unique_lock<std::mutex> lock(g_memMutex);
44 return CommonUtil::CreateExportFile(COLLECTION_WM_PATH, MAX_FILE_NUM, filePrefix, ext);
45 }
46 }
47
Create()48 std::shared_ptr<WmCollector> WmCollector::Create()
49 {
50 return std::make_shared<WmDecorator>(std::make_shared<WmCollectorImpl>());
51 }
52
ExportWindowsInfo()53 CollectResult<std::string> WmCollectorImpl::ExportWindowsInfo()
54 {
55 CollectResult<std::string> result;
56 result.retCode = UcError::UNSUPPORT;
57 std::vector<std::string> args;
58 args.push_back("hidumper");
59 args.push_back("-s");
60 args.push_back("WindowManagerService");
61 args.push_back("-a");
62 args.push_back("-a");
63 std::string fileName = CreateExportFileName("windows_info_", ".txt");
64 if (fileName.empty()) {
65 return result;
66 }
67 int fd = open(fileName.c_str(), O_WRONLY);
68 if (fd < 0) {
69 HIVIEW_LOGE("create fileName=%{public}s failed.", fileName.c_str());
70 return result;
71 }
72 if (CommonUtils::WriteCommandResultToFile(fd, "/system/bin/hidumper", args) == -1) {
73 HIVIEW_LOGE("write cmd to file=%{public}s failed.", fileName.c_str());
74 close(fd);
75 return result;
76 }
77 close(fd);
78 result.retCode = UcError::SUCCESS;
79 result.data = fileName;
80 return result;
81 }
82
ExportWindowsMemory()83 CollectResult<std::string> WmCollectorImpl::ExportWindowsMemory()
84 {
85 CollectResult<std::string> result;
86 result.retCode = UcError::UNSUPPORT;
87 std::vector<std::string> args;
88 args.push_back("hidumper");
89 args.push_back("-s");
90 args.push_back("RenderService");
91 args.push_back("-a");
92 args.push_back("dumpMem");
93 std::string fileName = CreateExportFileName("windows_memory_", ".txt");
94 if (fileName.empty()) {
95 return result;
96 }
97 int fd = open(fileName.c_str(), O_WRONLY);
98 if (fd < 0) {
99 HIVIEW_LOGE("create fileName=%{public}s failed.", fileName.c_str());
100 return result;
101 }
102 if (CommonUtils::WriteCommandResultToFile(fd, "/system/bin/hidumper", args) == -1) {
103 HIVIEW_LOGE("write cmd to file=%{public}s failed.", fileName.c_str());
104 close(fd);
105 return result;
106 }
107 close(fd);
108 result.retCode = UcError::SUCCESS;
109 result.data = fileName;
110 return result;
111 }
112
ExportGpuMemory()113 CollectResult<std::string> WmCollectorImpl::ExportGpuMemory()
114 {
115 CollectResult<std::string> result;
116 std::string content;
117 bool ret = FileUtil::LoadStringFromFile(GPU_MEMORY_PATH, content);
118 if (!ret) {
119 return result;
120 }
121 if (content.empty()) {
122 result.retCode = READ_FAILED;
123 return result;
124 }
125 std::string fileName = CreateExportFileName("gpu_memory_", ".txt");
126 if (fileName.empty()) {
127 result.retCode = SYSTEM_ERROR;
128 return result;
129 }
130 bool isSuccess = FileUtil::SaveStringToFile(fileName, content, true);
131 if (!isSuccess) {
132 result.retCode = WRITE_FAILED;
133 return result;
134 }
135 result.retCode = UcError::SUCCESS;
136 result.data = fileName;
137 return result;
138 }
139 } // namespace UCollectUtil
140 } // namespace HiviewDFX
141 } // namespace OHOS