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 constexpr char COLLECTION_WM_PATH[] = "/data/log/hiview/unified_collection/wm/";
38 constexpr char 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 fdsan_exchange_owner_tag(fd, 0, logLabelDomain);
73 if (CommonUtils::WriteCommandResultToFile(fd, "/system/bin/hidumper", args) == -1) {
74 HIVIEW_LOGE("write cmd to file=%{public}s failed.", fileName.c_str());
75 fdsan_close_with_tag(fd, logLabelDomain);
76 return result;
77 }
78 fdsan_close_with_tag(fd, logLabelDomain);
79 result.retCode = UcError::SUCCESS;
80 result.data = fileName;
81 return result;
82 }
83
ExportWindowsMemory()84 CollectResult<std::string> WmCollectorImpl::ExportWindowsMemory()
85 {
86 CollectResult<std::string> result;
87 result.retCode = UcError::UNSUPPORT;
88 std::vector<std::string> args;
89 args.push_back("hidumper");
90 args.push_back("-s");
91 args.push_back("RenderService");
92 args.push_back("-a");
93 args.push_back("dumpMem");
94 std::string fileName = CreateExportFileName("windows_memory_", ".txt");
95 if (fileName.empty()) {
96 return result;
97 }
98 int fd = open(fileName.c_str(), O_WRONLY);
99 if (fd < 0) {
100 HIVIEW_LOGE("create fileName=%{public}s failed.", fileName.c_str());
101 return result;
102 }
103 fdsan_exchange_owner_tag(fd, 0, logLabelDomain);
104 if (CommonUtils::WriteCommandResultToFile(fd, "/system/bin/hidumper", args) == -1) {
105 HIVIEW_LOGE("write cmd to file=%{public}s failed.", fileName.c_str());
106 fdsan_close_with_tag(fd, logLabelDomain);
107 return result;
108 }
109 fdsan_close_with_tag(fd, logLabelDomain);
110 result.retCode = UcError::SUCCESS;
111 result.data = fileName;
112 return result;
113 }
114
ExportGpuMemory()115 CollectResult<std::string> WmCollectorImpl::ExportGpuMemory()
116 {
117 CollectResult<std::string> result;
118 std::string content;
119 bool ret = FileUtil::LoadStringFromFile(GPU_MEMORY_PATH, content);
120 if (!ret) {
121 return result;
122 }
123 if (content.empty()) {
124 result.retCode = READ_FAILED;
125 return result;
126 }
127 std::string fileName = CreateExportFileName("gpu_memory_", ".txt");
128 if (fileName.empty()) {
129 result.retCode = SYSTEM_ERROR;
130 return result;
131 }
132 bool isSuccess = FileUtil::SaveStringToFile(fileName, content, true);
133 if (!isSuccess) {
134 result.retCode = WRITE_FAILED;
135 return result;
136 }
137 result.retCode = UcError::SUCCESS;
138 result.data = fileName;
139 return result;
140 }
141 } // namespace UCollectUtil
142 } // namespace HiviewDFX
143 } // namespace OHOS