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