• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 "render_thread/jank_detector/rs_jank_detector.h"
17 #include "platform/common/rs_log.h"
18 #include <unistd.h>
19 #ifdef ROSEN_OHOS
20 #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     if (uiEndTimeStamp < uiStartTimeStamp || renderEndTimeStamp < renderStartTimeStamp) {
104         uiDrawFrames_.clear();
105         ROSEN_LOGD("RSJankDetector::CalculateSkippedFrame, timestamp is invalid, "
106             "uiStartTimeStamp:%{public}" PRIu64 ", "
107             "uiEndTimeStamp:%{public}" PRIu64 ", "
108             "renderStartTimeStamp:%{public}" PRIu64 ", "
109             "renderEndTimeStamp:%{public}" PRIu64,
110             uiStartTimeStamp, uiEndTimeStamp, renderStartTimeStamp, renderEndTimeStamp);
111         return;
112     }
113     frameMsg.uiDrawTime = uiEndTimeStamp - uiStartTimeStamp;
114     frameMsg.renderDrawTime = renderEndTimeStamp - renderStartTimeStamp;
115     // Currently, we do not consider the time consumption of UI thread
116     frameMsg.totalTime = frameMsg.renderDrawTime;
117     uiDrawFrames_.clear();
118     int skippedFrame = 0;
119     if (refreshPeriod_ != 0) {
120         skippedFrame = static_cast<int>(frameMsg.totalTime / refreshPeriod_);
121     }
122     if ((skippedFrame >= JANK_SKIPPED_THRESHOLD) || (frameMsg.dropUiFrameNum >= JANK_SKIPPED_THRESHOLD)) {
123         DrawEventReport(frameMsg, "JANK_FRAME_SKIP");
124     }
125 
126     if (frameMsg.renderDrawTime >= NO_DRAW_THRESHOLD) {
127         DrawEventReport(frameMsg, "NO_DRAW");
128     }
129 }
130 } // namespace Rosen
131 } // namespace OHOS
132