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