xref: /kernel/liteos_a/apps/perf/include/perf.h (revision 0d163575)
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
33#ifndef _PERF_H
34#define _PERF_H
35
36#include <stdlib.h>
37
38#ifdef  __cplusplus
39#if  __cplusplus
40extern "C" {
41#endif /* __cplusplus */
42#endif /* __cplusplus */
43
44#define PERF_MAX_EVENT          7
45#define PERF_MAX_FILTER_TSKS    32
46
47#ifdef PERF_DEBUG
48#define printf_debug(fmt, ...) printf(fmt, ##__VA_ARGS__)
49#else
50#define printf_debug(fmt, ...)
51#endif
52
53/*
54 * Perf types
55 */
56enum PerfEventType {
57    PERF_EVENT_TYPE_HW,      /* boards common hw events */
58    PERF_EVENT_TYPE_TIMED,   /* hrtimer timed events */
59    PERF_EVENT_TYPE_SW,      /* software trace events */
60    PERF_EVENT_TYPE_RAW,     /* boards special hw events, see enum PmuEventType in corresponding arch headfile */
61
62    PERF_EVENT_TYPE_MAX
63};
64
65/*
66 * Common hardware pmu events
67 */
68enum PmuHwId {
69    PERF_COUNT_HW_CPU_CYCLES = 0,      /* cpu cycle event */
70    PERF_COUNT_HW_INSTRUCTIONS,        /* instruction event */
71    PERF_COUNT_HW_DCACHE_REFERENCES,   /* dcache access event */
72    PERF_COUNT_HW_DCACHE_MISSES,       /* dcache miss event */
73    PERF_COUNT_HW_ICACHE_REFERENCES,   /* icache access event */
74    PERF_COUNT_HW_ICACHE_MISSES,       /* icache miss event */
75    PERF_COUNT_HW_BRANCH_INSTRUCTIONS, /* software change of pc event */
76    PERF_COUNT_HW_BRANCH_MISSES,       /* branch miss event */
77
78    PERF_COUNT_HW_MAX,
79};
80
81/*
82 * Common hrtimer timed events
83 */
84enum PmuTimedId {
85    PERF_COUNT_CPU_CLOCK = 0,      /* hrtimer timed event */
86};
87
88/*
89 * Common software pmu events
90 */
91enum PmuSwId {
92    PERF_COUNT_SW_TASK_SWITCH = 1, /* task switch event */
93    PERF_COUNT_SW_IRQ_RESPONSE,    /* irq response event */
94    PERF_COUNT_SW_MEM_ALLOC,       /* memory alloc event */
95    PERF_COUNT_SW_MUX_PEND,        /* mutex pend event */
96
97    PERF_COUNT_SW_MAX,
98};
99
100/*
101 * perf sample data types
102 * Config it through PerfConfigAttr->sampleType.
103 */
104enum PerfSampleType {
105    PERF_RECORD_CPU       = 1U << 0, /* record current cpuid */
106    PERF_RECORD_TID       = 1U << 1, /* record current task id */
107    PERF_RECORD_TYPE      = 1U << 2, /* record event type */
108    PERF_RECORD_PERIOD    = 1U << 3, /* record event period */
109    PERF_RECORD_TIMESTAMP = 1U << 4, /* record timestamp */
110    PERF_RECORD_IP        = 1U << 5, /* record instruction pointer */
111    PERF_RECORD_CALLCHAIN = 1U << 6, /* record backtrace */
112    PERF_RECORD_PID       = 1U << 7, /* record current process id */
113};
114
115/*
116 * perf configuration sub event information
117 *
118 * This structure is used to config specific events attributes.
119 */
120typedef struct {
121    unsigned int type;              /* enum PerfEventType */
122    struct {
123        unsigned int eventId;       /* the specific event corresponds to the PerfEventType */
124        unsigned int period;        /* event period, for every "period"th occurrence of the event a
125                                        sample will be recorded */
126    } events[PERF_MAX_EVENT];       /* perf event list */
127    unsigned int eventsNr;          /* total perf event number */
128    size_t predivided;              /* whether to prescaler (once every 64 counts),
129                                        which only take effect on cpu cycle hardware event */
130} PerfEventConfig;
131
132/*
133 * perf configuration main information
134 *
135 * This structure is used to set perf sampling attributes, including events, tasks and other information.
136 */
137typedef struct {
138    PerfEventConfig         eventsCfg;                      /* perf event config */
139    unsigned int            taskIds[PERF_MAX_FILTER_TSKS];  /* perf task filter list (allowlist) */
140    unsigned int            taskIdsNr;                      /* task numbers of task filter allowlist,
141                                                                 if set 0 perf will sample all tasks */
142    unsigned int            processIds[PERF_MAX_FILTER_TSKS];  /* perf process filter list (allowlist) */
143    unsigned int            processIdsNr;                      /* process numbers of process filter allowlist,
144                                                                 if set 0 perf will sample all processes */
145    unsigned int            sampleType;                     /* type of data to sample defined in PerfSampleType */
146    size_t                  needSample;                     /* whether to sample data */
147} PerfConfigAttr;
148
149void PerfUsage(void);
150void PerfDumpAttr(PerfConfigAttr *attr);
151int PerfConfig(int fd, PerfConfigAttr *attr);
152void PerfStart(int fd, size_t sectionId);
153void PerfStop(int fd);
154ssize_t PerfRead(int fd, char *buf, size_t size);
155void PerfPrintBuffer(const char *buf, ssize_t num);
156
157#ifdef __cplusplus
158#if __cplusplus
159}
160#endif
161#endif
162
163#endif /* _PERF_H */
164