1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef _LOS_PERF_PRI_H
33 #define _LOS_PERF_PRI_H
34
35 #include "los_perf.h"
36 #include "perf.h"
37 #include "los_mp.h"
38 #include "los_task_pri.h"
39 #include "los_exc_pri.h"
40
41 #ifdef __cplusplus
42 #if __cplusplus
43 extern "C" {
44 #endif /* __cplusplus */
45 #endif /* __cplusplus */
46
47 #define PERF_EVENT_TO_CODE 0
48 #define PERF_CODE_TO_EVENT 1
49 #define PERF_DATA_MAGIC_WORD 0xEFEFEF00
50
51 #define SMP_CALL_PERF_FUNC(func) OsMpFuncCall(OS_MP_CPU_ALL, (SMP_FUNC_CALL)func, NULL)
52
53 enum PmuStatus {
54 PERF_PMU_STOPED,
55 PERF_PMU_STARTED,
56 };
57
58 typedef struct {
59 UINTPTR pc;
60 UINTPTR fp;
61 } PerfRegs;
62
63 typedef struct {
64 UINT32 ipNr;
65 IpInfo ip[PERF_MAX_CALLCHAIN_DEPTH];
66 } PerfBackTrace;
67
68 typedef struct {
69 UINT32 cpuid; /* cpu id */
70 UINT32 taskId; /* task id */
71 UINT32 processId; /* process id */
72 UINT32 eventId; /* record type */
73 UINT32 period; /* record period */
74 UINT64 time; /* record time */
75 IpInfo pc; /* instruction pointer */
76 PerfBackTrace callChain; /* number of callChain ips */
77 } PerfSampleData;
78
79 typedef struct {
80 UINT32 magic; /* magic number */
81 UINT32 eventType; /* enum PerfEventType */
82 UINT32 len; /* sample data file length */
83 UINT32 sampleType; /* IP | TID | TIMESTAMP... */
84 UINT32 sectionId; /* section id */
85 } PerfDataHdr;
86
87 typedef struct {
88 UINT32 counter;
89 UINT32 eventId;
90 UINT32 period;
91 UINT64 count[LOSCFG_KERNEL_CORE_NUM];
92 } Event;
93
94 typedef struct {
95 Event per[PERF_MAX_EVENT];
96 UINT8 nr;
97 UINT8 cntDivided;
98 } PerfEvent;
99
100 typedef struct {
101 enum PerfEventType type;
102 PerfEvent events;
103 UINT32 (*config)(VOID);
104 UINT32 (*start)(VOID);
105 UINT32 (*stop)(VOID);
106 CHAR *(*getName)(Event *event);
107 } Pmu;
108
109 typedef struct {
110 /* time info */
111 UINT64 startTime;
112 UINT64 endTime;
113
114 /* instrumentation status */
115 enum PerfStatus status;
116 enum PmuStatus pmuStatusPerCpu[LOSCFG_KERNEL_CORE_NUM];
117
118 /* configuration info */
119 UINT32 sampleType;
120 UINT32 taskIds[PERF_MAX_FILTER_TSKS];
121 UINT8 taskIdsNr;
122 UINT32 processIds[PERF_MAX_FILTER_TSKS];
123 UINT8 processIdsNr;
124 UINT8 needSample;
125 } PerfCB;
126
127 #ifndef OsPerfArchFetchIrqRegs
OsPerfArchFetchIrqRegs(PerfRegs * regs,LosTaskCB * curTask)128 STATIC INLINE VOID OsPerfArchFetchIrqRegs(PerfRegs *regs, LosTaskCB *curTask) {}
129 #endif
130
OsPerfFetchIrqRegs(PerfRegs * regs)131 STATIC INLINE VOID OsPerfFetchIrqRegs(PerfRegs *regs)
132 {
133 LosTaskCB *curTask = OsCurrTaskGet();
134 OsPerfArchFetchIrqRegs(regs, curTask);
135 PRINT_DEBUG("pc = 0x%x, fp = 0x%x\n", regs->pc, regs->fp);
136 }
137
138 #ifndef OsPerfArchFetchCallerRegs
OsPerfArchFetchCallerRegs(PerfRegs * regs)139 STATIC INLINE VOID OsPerfArchFetchCallerRegs(PerfRegs *regs) {}
140 #endif
141
OsPerfFetchCallerRegs(PerfRegs * regs)142 STATIC INLINE VOID OsPerfFetchCallerRegs(PerfRegs *regs)
143 {
144 OsPerfArchFetchCallerRegs(regs);
145 PRINT_DEBUG("pc = 0x%x, fp = 0x%x\n", regs->pc, regs->fp);
146 }
147
148 extern VOID OsPerfSetIrqRegs(UINTPTR pc, UINTPTR fp);
149 extern VOID OsPerfUpdateEventCount(Event *event, UINT32 value);
150 extern VOID OsPerfHandleOverFlow(Event *event, PerfRegs *regs);
151
152 #ifdef __cplusplus
153 #if __cplusplus
154 }
155 #endif /* __cplusplus */
156 #endif /* __cplusplus */
157
158 #endif /* _LOS_PERF_PRI_H */
159