1 /*
2 * Copyright (C) 2025 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 <fstream>
17 #include <sstream>
18 #include "executor/memory/parse/parse_dmabuf_info.h"
19 #include "hilog_wrapper.h"
20 #include "util/file_utils.h"
21 #include "string_ex.h"
22
23 namespace OHOS {
24 namespace HiviewDFX {
ParseDmaBufInfo()25 ParseDmaBufInfo::ParseDmaBufInfo()
26 {
27 }
28
~ParseDmaBufInfo()29 ParseDmaBufInfo::~ParseDmaBufInfo()
30 {
31 }
32
ParseHeaderLine(const std::string & line)33 void ParseDmaBufInfo::ParseHeaderLine(const std::string &line)
34 {
35 std::istringstream lineStream(line);
36 std::vector<std::string> headers;
37 std::string header;
38 while (lineStream >> header) {
39 headers.emplace_back(header);
40 }
41
42 headerMapTmp_.clear();
43 columnWidthsTmp_.clear();
44 headerMapTmp_.reserve(headers.size());
45 columnWidthsTmp_.reserve(headers.size());
46
47 for (size_t i = 0; i < headers.size(); ++i) {
48 headerMapTmp_[headers[i]] = static_cast<int>(i);
49 columnWidthsTmp_.emplace_back(static_cast<int>(headers[i].length()));
50 }
51
52 titlesTmp_ = headers;
53 detailTitle_ = line;
54 inDetailSection_ = true;
55 }
56
ParseDataLine(const std::string & line)57 void ParseDmaBufInfo::ParseDataLine(const std::string &line)
58 {
59 std::istringstream lineStream(line);
60 std::vector<std::string> row;
61 std::string item;
62 while (lineStream >> item) {
63 row.emplace_back(item);
64 }
65
66 for (size_t i = 0; i < row.size(); ++i) {
67 if (i >= columnWidthsTmp_.size()) {
68 columnWidthsTmp_.emplace_back(static_cast<int>(row[i].length()));
69 } else if (row[i].length() > static_cast<size_t>(columnWidthsTmp_[i])) {
70 columnWidthsTmp_[i] = static_cast<int>(row[i].length());
71 }
72 }
73
74 details_.emplace_back(line);
75 }
76
GetDmaBufInfo(const int32_t & pid,std::vector<std::string> & result,std::unordered_map<std::string,int> & headerMap,std::vector<int> & columnWidths,std::vector<std::string> & titles)77 bool ParseDmaBufInfo::GetDmaBufInfo(const int32_t &pid, std::vector<std::string> &result,
78 std::unordered_map<std::string, int> &headerMap,
79 std::vector<int> &columnWidths,
80 std::vector<std::string> &titles)
81 {
82 DUMPER_HILOGD(MODULE_SERVICE, "GetDmaBufInfo begin, pid:%{public}d", pid);
83
84 inDetailSection_ = false;
85 detailTitle_.clear();
86 details_.clear();
87 headerMapTmp_.clear();
88 columnWidthsTmp_.clear();
89 titlesTmp_.clear();
90
91 const std::string path = "/proc/" + std::to_string(pid) + "/mm_dmabuf_info";
92
93 bool ret = FileUtils::GetInstance().LoadStringFromProcCb(path, false, true,
94 [&](const std::string &line) -> void {
95 if (!inDetailSection_ && line.find("Process") != std::string::npos) {
96 ParseHeaderLine(line);
97 return;
98 }
99 if (inDetailSection_) {
100 ParseDataLine(line);
101 }
102 });
103
104 if (details_.empty()) {
105 DUMPER_HILOGE(MODULE_SERVICE, "detail dmabuf is empty.");
106 return false;
107 }
108
109 details_.insert(details_.begin(), detailTitle_);
110 result = std::move(details_);
111 headerMap = std::move(headerMapTmp_);
112 columnWidths = std::move(columnWidthsTmp_);
113 titles = std::move(titlesTmp_);
114
115 DUMPER_HILOGD(MODULE_SERVICE, "GetDmaBufInfo end, pid:%{public}d, ret:%{public}d", pid, ret);
116 return ret;
117 }
118 } // namespace HiviewDFX
119 } // namespace OHOS