1 /*
2  * Copyright (c) 2024 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 "display_buffer_dfx.h"
17 #include <hdf_log.h>
18 
19 #define TIME_1000 1000
20 #define TIME_10 10
21 #define HICOLLIE_TIMEOUT 10
22 
23 namespace OHOS {
24 namespace HDI {
25 namespace Display {
26 namespace Buffer {
27 namespace V1_0 {
DisplayBufferDfx(const std::string & name)28 DisplayBufferDfx::DisplayBufferDfx(const std::string& name)
29     : dfxName_(name),
30     timeId_(0),
31     flag_(false),
32     startTimeStamp({0, 0}),
33     stopTimeStamp({0, 0})
34 {
35 }
36 
~DisplayBufferDfx()37 DisplayBufferDfx::~DisplayBufferDfx()
38 {
39     if (timeId_ != 0) {
40         CancelTimer();
41     }
42     if (flag_) {
43         StopTimeStamp();
44     }
45 }
46 
SetTimer()47 void DisplayBufferDfx::SetTimer()
48 {
49 #ifdef DISPLAY_HICOLLIE_ENABLE
50     timeId_ = HiviewDFX::XCollie::GetInstance().SetTimer(dfxName_, HICOLLIE_TIMEOUT, nullptr, nullptr,
51         HiviewDFX::XCOLLIE_FLAG_LOG | HiviewDFX::XCOLLIE_FLAG_RECOVERY);
52 #endif
53 }
54 
CancelTimer()55 void DisplayBufferDfx::CancelTimer()
56 {
57 #ifdef DISPLAY_HICOLLIE_ENABLE
58     HiviewDFX::XCollie::GetInstance().CancelTimer(timeId_);
59 #endif
60 }
61 
StartTimeStamp()62 void DisplayBufferDfx::StartTimeStamp()
63 {
64     gettimeofday(&startTimeStamp, nullptr);
65     flag_ = true;
66 }
67 
StopTimeStamp()68 void DisplayBufferDfx::StopTimeStamp()
69 {
70     gettimeofday(&stopTimeStamp, nullptr);
71     int32_t runTime = (int32_t)((stopTimeStamp.tv_sec - startTimeStamp.tv_sec) * TIME_1000 +
72         (stopTimeStamp.tv_usec - startTimeStamp.tv_usec) / TIME_1000);
73     if (runTime > TIME_10) {
74         HDF_LOGW("run %{public}s over time, [%{public}d]ms", dfxName_.c_str(), runTime);
75     }
76     flag_ = false;
77 }
78 } // namespace V1_0
79 } // namespace Buffer
80 } // namespace Display
81 } // namespace HDI
82 } // namespace OHOS