• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "memory/rs_skia_memory_tracer.h"
17 #include <numeric>
18 namespace OHOS::Rosen {
19 constexpr uint32_t MEMUNIT_RATE = 1024;
20 
SkiaMemoryTracer(std::vector<ResourcePair> resourceMap,bool itemizeType)21 SkiaMemoryTracer::SkiaMemoryTracer(std::vector<ResourcePair> resourceMap, bool itemizeType)
22     : resourceMap_(resourceMap), itemizeType_(itemizeType), totalSize_("bytes", 0), purgeableSize_("bytes", 0)
23 {}
24 
SkiaMemoryTracer(const char * categoryKey,bool itemizeType)25 SkiaMemoryTracer::SkiaMemoryTracer(const char* categoryKey, bool itemizeType)
26     : categoryKey_(categoryKey), itemizeType_(itemizeType), totalSize_("bytes", 0), purgeableSize_("bytes", 0)
27 {}
28 
MapName(const char * resourceName)29 const char* SkiaMemoryTracer::MapName(const char* resourceName)
30 {
31     for (auto& resource : resourceMap_) {
32         if (SkStrContains(resourceName, resource.first)) {
33             return resource.second;
34         }
35     }
36     return nullptr;
37 }
38 
ProcessElement()39 void SkiaMemoryTracer::ProcessElement()
40 {
41     if (!currentElement_.empty()) {
42         // Only count elements that contain "size", other values just provide metadata.
43         auto sizeResult = currentValues_.find("size");
44         if (sizeResult != currentValues_.end()) {
45             totalSize_.value += sizeResult->second.value;
46             totalSize_.count++;
47         } else {
48             currentElement_.clear();
49             currentValues_.clear();
50             return;
51         }
52 
53         // find the purgeable size if one exists
54         auto purgeableResult = currentValues_.find("purgeable_size");
55         if (purgeableResult != currentValues_.end()) {
56             purgeableSize_.value += purgeableResult->second.value;
57             purgeableSize_.count++;
58         }
59 
60         // find the type if one exists
61         const char* type;
62         auto typeResult = currentValues_.find("type");
63         if (typeResult != currentValues_.end()) {
64             type = typeResult->second.units;
65         } else if (itemizeType_) {
66             type = "Other";
67         }
68 
69         // compute the type if we are itemizing or use the default "size" if we are not
70         std::string key = (itemizeType_) ? type : sizeResult->first;
71 
72         // compute the top level element name using either the map or category key
73         const char* resourceName = MapName(currentElement_.c_str());
74         if (categoryKey_ != nullptr) {
75             // find the category if one exists
76             auto categoryResult = currentValues_.find(categoryKey_);
77             if (categoryResult != currentValues_.end()) {
78                 resourceName = categoryResult->second.units;
79             }
80         }
81 
82         // if we don't have a resource name then we don't know how to label the
83         // data and should abort.
84         if (resourceName == nullptr) {
85             resourceName = currentElement_.c_str();
86         }
87 
88         auto result = results_.find(resourceName);
89         if (result == results_.end()) {
90             TraceValue sizeValue = sizeResult->second;
91             currentValues_.clear();
92             currentValues_.insert({ key, sizeValue });
93             results_.insert({ resourceName, currentValues_ });
94         } else {
95             auto& resourceValues = result->second;
96             typeResult = resourceValues.find(key);
97             if (typeResult == resourceValues.end()) {
98                 resourceValues.insert({ key, sizeResult->second });
99             } else {
100                 typeResult->second.value += sizeResult->second.value;
101                 typeResult->second.count++;
102             }
103         }
104     }
105 
106     currentElement_.clear();
107     currentValues_.clear();
108 }
109 
dumpNumericValue(const char * dumpName,const char * valueName,const char * units,uint64_t value)110 void SkiaMemoryTracer::dumpNumericValue(const char* dumpName, const char* valueName, const char* units, uint64_t value)
111 {
112     if (currentElement_ != dumpName) {
113         ProcessElement();
114         currentElement_ = dumpName;
115     }
116     currentValues_.insert({ valueName, { units, value } });
117 }
118 
LogOutput(DfxString & log)119 void SkiaMemoryTracer::LogOutput(DfxString& log)
120 {
121     // process any remaining elements
122     ProcessElement();
123 
124     for (const auto& namedItem : results_) {
125         if (itemizeType_) {
126             log.AppendFormat("  %s:\n", namedItem.first.c_str()); // skia/sk_glyph_cache
127             for (const auto& typedValue : namedItem.second) {
128                 TraceValue traceValue = ConvertUnits(typedValue.second);
129                 const char* entry = (traceValue.count > 1) ? "entries" : "entry";
130                 log.AppendFormat("    %s: %.2f %s (%d %s)\n", typedValue.first.c_str(), traceValue.value,
131                     traceValue.units, traceValue.count, entry);
132             }
133         } else {
134             auto result = namedItem.second.find("size");
135             if (result != namedItem.second.end()) {
136                 TraceValue traceValue = ConvertUnits(result->second);
137                 const char* entry = (traceValue.count > 1) ? "entries" : "entry";
138                 log.AppendFormat("  %s: %.2f %s (%d %s)\n", namedItem.first.c_str(), traceValue.value, traceValue.units,
139                     traceValue.count, entry);
140             }
141         }
142     }
143 }
144 
GetGLMemorySize()145 float SkiaMemoryTracer::GetGLMemorySize()
146 {
147     ProcessElement();
148     // exclude scratch memory
149     // cause scratch memory is generated by animation and effect. which is not response by app
150     size_t totalGpuSizeApp = 0;
151     for (const auto& namedItem : results_) {
152         if (namedItem.first == "Scratch") {
153             continue;
154         }
155         totalGpuSizeApp = std::accumulate(namedItem.second.begin(), namedItem.second.end(), totalGpuSizeApp,
156             [](int total, const auto& typedValue) { return total + typedValue.second.value; });
157     }
158     return totalGpuSizeApp;
159 }
160 
LogTotals(DfxString & log)161 void SkiaMemoryTracer::LogTotals(DfxString& log)
162 {
163     TraceValue total = ConvertUnits(totalSize_);
164     TraceValue purgeable = ConvertUnits(purgeableSize_);
165     log.AppendFormat("    %.0f bytes, %.2f %s (%.2f %s is purgeable)\n", totalSize_.value, total.value, total.units,
166         purgeable.value, purgeable.units);
167 }
168 
ConvertUnits(const TraceValue & value)169 SkiaMemoryTracer::TraceValue SkiaMemoryTracer::ConvertUnits(const TraceValue& value)
170 {
171     TraceValue output(value);
172     if (SkString(output.units) == SkString("bytes") && output.value >= MEMUNIT_RATE) {
173         output.value = output.value / MEMUNIT_RATE;
174         output.units = "KB";
175     }
176     if (SkString(output.units) == SkString("KB") && output.value >= MEMUNIT_RATE) {
177         output.value = output.value / MEMUNIT_RATE;
178         output.units = "MB";
179     }
180     return output;
181 }
182 
183 } // namespace OHOS::Rosen