1 /*
2 * Copyright (c) 2022-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 "jank_detector/rs_jank_detector.h"
17
18 #include <unistd.h>
19 #ifdef ROSEN_OHOS
20 #include "base/hiviewdfx/hisysevent/interfaces/native/innerkits/hisysevent/include/hisysevent.h"
21 #include "sandbox_utils.h"
22 #endif
23
24 #ifdef _WIN32
25 #define getuid() 0
26 #endif
27
28 namespace {
29 struct FrameMsg {
30 uint64_t totalTime = 0;
31 uint64_t uiDrawTime = 0;
32 uint64_t renderDrawTime = 0;
33 int dropUiFrameNum = 0;
34 std::string abilityName;
35 };
36
DrawEventReport(FrameMsg & frameMsg,std::string stringId)37 void DrawEventReport(FrameMsg& frameMsg, std::string stringId)
38 {
39 #ifdef ROSEN_OHOS
40 int32_t pid = OHOS::GetRealPid();
41 uint32_t uid = getuid();
42 std::string domain = "GRAPHIC";
43 std::string msg = "It took " + std::to_string(frameMsg.totalTime) + "ns to draw, "
44 + "UI took " + std::to_string(frameMsg.uiDrawTime) + "ns to draw, "
45 + "RSRenderThread took " + std::to_string(frameMsg.renderDrawTime) + "ns to draw, "
46 + "RSRenderThread dropped " + std::to_string(frameMsg.dropUiFrameNum) + " UI Frames";
47
48 OHOS::HiviewDFX::HiSysEvent::Write(domain, stringId,
49 OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
50 "PID", pid,
51 "UID", uid,
52 "ABILITY_NAME", frameMsg.abilityName,
53 "MSG", msg);
54 #endif
55 }
56 }
57
58 namespace OHOS {
59 namespace Rosen {
GetSysTimeNs()60 uint64_t RSJankDetector::GetSysTimeNs()
61 {
62 auto now = std::chrono::steady_clock::now().time_since_epoch();
63 return std::chrono::duration_cast<std::chrono::nanoseconds>(now).count();
64 }
65
SetRefreshPeriod(uint64_t refreshPeriod)66 void RSJankDetector::SetRefreshPeriod(uint64_t refreshPeriod)
67 {
68 refreshPeriod_ = refreshPeriod;
69 }
70
GetRefreshPeriod() const71 uint64_t RSJankDetector::GetRefreshPeriod() const
72 {
73 return refreshPeriod_;
74 }
75
UpdateUiDrawFrameMsg(uint64_t startTimeStamp,uint64_t endTimeStamp,const std::string & abilityName)76 void RSJankDetector::UpdateUiDrawFrameMsg(uint64_t startTimeStamp, uint64_t endTimeStamp,
77 const std::string& abilityName)
78 {
79 // In some scenes we don't have startTimeStamp(e.g. in OnSurfaceChanged),
80 // so we temporarily don't count this situation.
81 if (startTimeStamp == 0) {
82 return;
83 }
84
85 UiDrawFrameMsg uiFrame;
86 uiFrame.startTimeStamp = startTimeStamp;
87 uiFrame.endTimeStamp = endTimeStamp;
88 uiFrame.abilityName = abilityName;
89 uiDrawFrames_.emplace_back(uiFrame);
90 }
91
CalculateSkippedFrame(uint64_t renderStartTimeStamp,uint64_t renderEndTimeStamp)92 void RSJankDetector::CalculateSkippedFrame(uint64_t renderStartTimeStamp, uint64_t renderEndTimeStamp)
93 {
94 FrameMsg frameMsg;
95 uint64_t uiStartTimeStamp = 0;
96 uint64_t uiEndTimeStamp = 0;
97 if (!uiDrawFrames_.empty()) {
98 UiDrawFrameMsg uiDrawFrame = uiDrawFrames_.back();
99 frameMsg.dropUiFrameNum = uiDrawFrames_.size() - 1;
100 uiStartTimeStamp = uiDrawFrame.startTimeStamp;
101 uiEndTimeStamp = uiDrawFrame.endTimeStamp;
102 frameMsg.abilityName = uiDrawFrame.abilityName;
103 }
104
105 frameMsg.uiDrawTime = uiEndTimeStamp - uiStartTimeStamp;
106 frameMsg.renderDrawTime = renderEndTimeStamp - renderStartTimeStamp;
107 // Currently, we do not consider the time consumption of UI thread
108 frameMsg.totalTime = frameMsg.renderDrawTime;
109 uiDrawFrames_.clear();
110
111 int skippedFrame = static_cast<int>(frameMsg.totalTime / refreshPeriod_);
112 if ((skippedFrame >= JANK_SKIPPED_THRESHOLD) || (frameMsg.dropUiFrameNum >= JANK_SKIPPED_THRESHOLD)) {
113 DrawEventReport(frameMsg, "JANK_FRAME_SKIP");
114 }
115
116 if (frameMsg.renderDrawTime >= NO_DRAW_THRESHOLD) {
117 DrawEventReport(frameMsg, "NO_DRAW");
118 }
119 }
120 } // namespace Rosen
121 } // namespace OHOS
122