1 /*
2  * Copyright (c) 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 #ifndef __FFRT_PERF_H__
17 #define __FFRT_PERF_H__
18 #include "ffrt_trace.h"
19 
20 // default disabled for ffrt, enable it for debugging or playback
21 #ifdef FFRT_PERF_ENABLE
22 struct PerfEU {
23     static constexpr size_t qos_max = 15;
24     static inline std::atomic_int worker_num[qos_max] = {0};
25     static inline const char* worker_num_tag[qos_max] = {
26         "qos0_wrk",
27         "qos1_wrk",
28         "qos2_wrk",
29         "qos3_wrk",
30         "qos4_wrk",
31         "qos5_wrk",
32         "qos6_wrk",
33         "qos7_wrk",
34         "qos8_wrk",
35         "qos9_wrk",
36         "qos10_wrk",
37         "qos11_wrk",
38         "qos12_wrk",
39         "qos13_wrk",
40         "qos14_wrk"
41     };
42 
43     static inline const char* task_num_tag[qos_max] = {
44         "qos0_tsk",
45         "qos1_tsk",
46         "qos2_tsk",
47         "qos3_tsk",
48         "qos4_tsk",
49         "qos5_tsk",
50         "qos6_tsk",
51         "qos7_tsk",
52         "qos8_tsk",
53         "qos9_tsk",
54         "qos10_tsk",
55         "qos11_tsk",
56         "qos12_tsk",
57         "qos13_tsk",
58         "qos14_tsk"
59     };
60 
61     static inline const char* worker_wake_tag[qos_max] = {
62         "qos0_wake",
63         "qos1_wake",
64         "qos2_wake",
65         "qos3_wake",
66         "qos4_wake",
67         "qos5_wake",
68         "qos6_wake",
69         "qos7_wake",
70         "qos8_wake",
71         "qos9_wake",
72         "qos10_wake",
73         "qos11_wake",
74         "qos12_wake",
75         "qos13_wake",
76         "qos14_wake"
77     };
78 };
79 
FFRTPerfWorkerIdle(int qos)80 inline void FFRTPerfWorkerIdle(int qos)
81 {
82     if (qos >= 0 && qos < static_cast<int>(PerfEU::qos_max)) {
83         FFRT_TRACE_COUNT(PerfEU::worker_num_tag[qos],
84             PerfEU::worker_num[qos].fetch_sub(1, std::memory_order_relaxed) - 1);
85     }
86 }
87 
FFRTPerfWorkerAwake(int qos)88 inline void FFRTPerfWorkerAwake(int qos)
89 {
90     if (qos >= 0 && qos < static_cast<int>(PerfEU::qos_max)) {
91         FFRT_TRACE_COUNT(PerfEU::worker_num_tag[qos],
92             PerfEU::worker_num[qos].fetch_add(1, std::memory_order_relaxed) + 1);
93     }
94 }
95 
FFRTPerfWorkerWake(int qos)96 inline void FFRTPerfWorkerWake(int qos)
97 {
98     if (qos >= 0 && qos < static_cast<int>(PerfEU::qos_max)) {
99         FFRT_TRACE_COUNT(PerfEU::worker_wake_tag[qos], 0);
100     }
101 }
102 
FFRTPerfTaskNum(int qos,int taskn)103 inline void FFRTPerfTaskNum(int qos, int taskn)
104 {
105     if (qos >= 0 && qos < static_cast<int>(PerfEU::qos_max)) {
106         FFRT_TRACE_COUNT(PerfEU::task_num_tag[qos], taskn);
107     }
108 }
109 
110 #define FFRT_PERF_WORKER_IDLE(qos) FFRTPerfWorkerIdle(qos)
111 #define FFRT_PERF_WORKER_AWAKE(qos) FFRTPerfWorkerAwake(qos)
112 #define FFRT_PERF_WORKER_WAKE(qos) FFRTPerfWorkerWake(qos)
113 #define FFRT_PERF_TASK_NUM(qos, taskn) FFRTPerfTaskNum(qos, taskn)
114 #else
115 #define FFRT_PERF_WORKER_IDLE(qos)
116 #define FFRT_PERF_WORKER_AWAKE(qos)
117 #define FFRT_PERF_WORKER_WAKE(qos)
118 #define FFRT_PERF_TASK_NUM(qos, taskn)
119 #endif
120 
121 #endif // __FFRT_PERF_H__