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 "hiview_logger.h"
17 #include "input_monitor.h"
18 #include "jank_frame_monitor.h"
19 #include "perf_reporter.h"
20 #include "perf_trace.h"
21 #include "perf_utils.h"
22 #include "scene_monitor.h"
23 #include "white_block_monitor.h"
24
25
26 namespace OHOS {
27 namespace HiviewDFX {
28
29 DEFINE_LOG_LABEL(0xD002D66, "Hiview-PerfMonitor");
30
31 static constexpr int DELAY_MS = 200;
32
GetInstance()33 WhiteBlockMonitor& WhiteBlockMonitor::GetInstance()
34 {
35 static WhiteBlockMonitor instance;
36 return instance;
37 }
38
StartScroll()39 void WhiteBlockMonitor::StartScroll()
40 {
41 std::lock_guard<std::mutex> Lock(mMutex);
42 scrollStartTime = static_cast<uint64_t>(GetCurrentSystimeMs());
43 scrolling = true;
44 }
45
EndScroll()46 void WhiteBlockMonitor::EndScroll()
47 {
48 std::lock_guard<std::mutex> Lock(mMutex);
49 scrollEndTime = static_cast<uint64_t>(GetCurrentSystimeMs());
50 scrolling = false;
51 std::thread delayThread([this] { this->ReportWhiteBlockStat(); });
52 delayThread.detach();
53 }
54
StartRecordImageLoadStat(int64_t id)55 void WhiteBlockMonitor::StartRecordImageLoadStat(int64_t id)
56 {
57 std::lock_guard<std::mutex> Lock(mMutex);
58 if (!scrolling) {
59 HIVIEW_LOGD("not scrolling");
60 return;
61 }
62 if (RecordExist(id)) {
63 HIVIEW_LOGW("record already exists");
64 return;
65 }
66 ImageLoadInfo* record = new ImageLoadInfo();
67 record->id = id;
68 record->loadStartTime = static_cast<uint64_t>(GetCurrentSystimeMs());
69 mRecords.emplace(id, record);
70 }
71
EndRecordImageLoadStat(int64_t id,std::pair<int,int> size,const std::string & type,int state)72 void WhiteBlockMonitor::EndRecordImageLoadStat(int64_t id, std::pair<int, int> size, const std::string& type, int state)
73 {
74 std::lock_guard<std::mutex> Lock(mMutex);
75 ImageLoadInfo* record = GetRecord(id);
76 if (record == nullptr) {
77 HIVIEW_LOGD("record not exists");
78 return;
79 }
80 record->loadEndTime = static_cast<uint64_t>(GetCurrentSystimeMs());
81 record->imageType = type;
82 record->width = size.first;
83 record->height = size.second;
84 record->loadState = state;
85 }
86
RecordExist(int64_t id)87 bool WhiteBlockMonitor::RecordExist(int64_t id)
88 {
89 return mRecords.count(id);
90 }
91
GetRecord(int64_t id)92 ImageLoadInfo* WhiteBlockMonitor::GetRecord(int64_t id)
93 {
94 ImageLoadInfo* record = nullptr;
95 auto it = mRecords.find(id);
96 if (it != mRecords.end()) {
97 record = it->second;
98 }
99 return record;
100 }
101
ReportWhiteBlockStat()102 void WhiteBlockMonitor::ReportWhiteBlockStat()
103 {
104 std::this_thread::sleep_for(std::chrono::milliseconds(DELAY_MS));
105 std::lock_guard<std::mutex> Lock(mMutex);
106 PerfReporter::GetInstance().ReportWhiteBlockStat(scrollStartTime, scrollEndTime, mRecords);
107 CleanUpRecords();
108 }
109
CleanUpRecords()110 void WhiteBlockMonitor::CleanUpRecords()
111 {
112 for (auto iter = mRecords.begin(); iter != mRecords.end();) {
113 if (iter->second != nullptr) {
114 delete iter->second;
115 iter->second = nullptr;
116 }
117 iter = mRecords.erase(iter);
118 }
119 }
120 }
121 }