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 #ifndef FOUNDATION_ACE_FRAMEWORKS_BASE_LOG_ACE_PERFORMANCE_MONITOR_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_LOG_ACE_PERFORMANCE_MONITOR_H
18 
19 #include <chrono>
20 #include <cstdint>
21 #include <map>
22 
23 namespace OHOS::Ace {
24 
25 enum class MonitorTag {
26     COMPONENT_CREATION = 0,
27     COMPONENT_LIFECYCLE,
28     COMPONENT_UPDATE,
29     JS_CALLBACK,
30     STATIC_API,
31     OTHER,
32 };
33 
34 enum class MonitorStatus {
35     IDLE = 0,
36     RUNNING,
37 };
38 
39 #define COMPONENT_CREATION_DURATION() ScopedMonitor scopedMonitor(MonitorTag::COMPONENT_CREATION)
40 #define COMPONENT_LIFECYCLE_DURATION() ScopedMonitor scopedMonitor(MonitorTag::COMPONENT_LIFECYCLE)
41 #define COMPONENT_UPDATE_DURATION() ScopedMonitor scopedMonitor(MonitorTag::COMPONENT_UPDATE)
42 #define JS_CALLBACK_DURATION() ScopedMonitor scopedMonitor(MonitorTag::JS_CALLBACK)
43 #define STATIC_API_DURATION() ScopedMonitor scopedMonitor(MonitorTag::STATIC_API)
44 #define OTHER_DURATION() ScopedMonitor scopedMonitor(MonitorTag::OTHER)
45 
46 typedef std::chrono::steady_clock::time_point TimePoint;
47 
48 class ScopedMonitor {
49 public:
50     explicit ScopedMonitor(MonitorTag tag);
51     ~ScopedMonitor();
52 
53 private:
54     MonitorTag tag_;
55     TimePoint begin_;
56     TimePoint end_;
57 };
58 
59 class ArkUIPerfMonitor {
60 public:
61     static ArkUIPerfMonitor& GetInstance();
62     ArkUIPerfMonitor();
63     void StartPerf();
64     void FinishPerf();
65     void RecordTimeSlice(MonitorTag tag, int64_t duration);
66     void RecordStateMgmtNode(int64_t num);
67     void RecordLayoutNode(int64_t num = 1);
68     void RecordRenderNode(int64_t num = 1);
69     void RecordDisplaySyncRate(int32_t displaySyncRate);
70     void SetRecordingStatus(MonitorTag tag, MonitorStatus status);
71 
72 private:
73     void InitPerfMonitor();
74     void ClearPerfMonitor();
75     void FlushPerfMonitor();
76     std::map<MonitorTag, int64_t> timeSlice_;
77     int64_t propertyNum_;
78     int64_t stateMgmtNodeNum_;
79     int64_t layoutNodeNum_;
80     int64_t renderNodeNum_;
81     TimePoint begin_;
82     TimePoint end_;
83     int64_t monitorStatus_;
84     int32_t displaySyncRate_ = 0;
85 };
86 } // namespace OHOS::Ace
87 
88 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_LOG_ACE_PERFORMANCE_MONITOR_H
89