summaryrefslogtreecommitdiff
path: root/kern/perfmon.h
blob: b979e653e69815d05b99bf3dd6fb459ff0ce0c68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
 * Copyright (c) 2014-2018 Remy Noel.
 * Copyright (c) 2014 Richard Braun.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Performance monitoring based on hardware performance counters.
 */

#ifndef _KERN_PERFMON_H
#define _KERN_PERFMON_H

#include <stdint.h>

#include <kern/init.h>
#include <kern/thread.h>

/*
 * Performance event types.
 */
#define PERFMON_ET_GENERIC  0
#define PERFMON_ET_RAW      1

/*
 * IDs of generic performance events.
 */
#define PERFMON_EV_CYCLE            0
#define PERFMON_EV_REF_CYCLE        1
#define PERFMON_EV_INSTRUCTION      2
#define PERFMON_EV_CACHE_REF        3
#define PERFMON_EV_CACHE_MISS       4
#define PERFMON_EV_BRANCH           5
#define PERFMON_EV_BRANCH_MISS      6
#define PERFMON_NR_GENERIC_EVENTS   7

/*
 * Event flags.
 */
#define PERFMON_EF_KERN 0x1 /* Monitor events in kernel mode */
#define PERFMON_EF_USER 0x2 /* Monitor events in user mode */
#define PERFMON_EF_MASK (PERFMON_EF_KERN | PERFMON_EF_USER)

/*
 * Pmu operations.
 *
 * Set by calling perfmon_register_pmu_ops.
 */
struct perfmon_pmu_ops {
    void (*info)(void);
    int (*translate)(unsigned int *raw_event_idp, unsigned int event_id);
    int (*alloc)(unsigned int *pmc_idp, unsigned int raw_event_id);
    void (*free)(unsigned int pmc_id);
    void (*start)(unsigned int pmc_id, unsigned int raw_event_id);
    void (*stop)(unsigned int pmc_id);
    uint64_t (*read)(unsigned int pmc_id);
    uint8_t (*get_pmc_width)(void);
    void (*handle_of_intr)(struct trap_frame *frame);
#ifdef CONFIG_PERFMON_TEST
    void (*write)(unsigned int pmc_id, uint64_t value);
#endif /* CONFIG_PERFMON_TEST */
};

/*
 * Performance monitoring event.
 *
 * An event describes a single, well-defined state and records its
 * occurrences over a period of time. It must be added to exactly
 * one group before being used.
 */
struct perfmon_event;

/*
 * Group of performance monitoring events.
 *
 * A group must be attached to either a thread or a processor, and abstracts
 * all operations on hardware counters.
 *
 * Until a group is actually attached, it is assumed there is only one
 * reference on it, owned by the caller.
 *
 * For a thread-attached group, it is the user's responsability to make sure
 * that perfmon_stop is always called before the monitored thread is deleted.
 */
struct perfmon_group;

/*
 * Create an event.
 */
int perfmon_event_create(struct perfmon_event **eventp, unsigned int type,
                         unsigned int id, int flags);

/*
 * Destroy an event.
 *
 * Once an event is added to a group, it can only be destroyed by destroying
 * the group.
 */
void perfmon_event_destroy(struct perfmon_event *event);

/*
 * Obtain the number of occurrences of an event.
 *
 * Events are updated at specific points in time, which means the value
 * returned by this function can be outdated.
 *
 * See perfmon_group_update() and perfmon_group_stop().
 */
uint64_t perfmon_event_read(const struct perfmon_event *event);

/*
 * Reset the number of occurrences of an event to 0.
 *
 * The group containing the given event should be stopped when calling
 * this function.
 */
void perfmon_event_reset(struct perfmon_event *event);

/*
 * Create an event group.
 *
 * Events must be added to the group, which must then be attached to a
 * processor or a thread.
 */
int perfmon_group_create(struct perfmon_group **groupp);

/*
 * Destroy a group and all its events.
 *
 * A group can only be destroyed once stopped and detached.
 *
 * Will return EINVAL if the group is not detached.
 */
int perfmon_group_destroy(struct perfmon_group *group);

/*
 * Add an event into a group.
 *
 * Events can only be added when a group isn't attached.
 */
void perfmon_group_add(struct perfmon_group *group,
                       struct perfmon_event *event);

/*
 * Attach a group to, respectively, a thread or a processor, reserving
 * associated logical counter.
 *
 * A group can only be attached to one thread or processor at a time.
 */
int perfmon_group_attach(struct perfmon_group *group, struct thread *thread);
int perfmon_group_attach_cpu(struct perfmon_group *group, unsigned int cpu);

/*
 * Detach a group from a thread or a processor.
 *
 * It frees associated logical counters..
 *
 * returns EINVAL if the group is still enabled (not stopped).
 */
int perfmon_group_detach(struct perfmon_group *group);

/*
 * Start performance monitoring.
 *
 * A group must be attached before being started.
 */
int perfmon_group_start(struct perfmon_group *group);

/*
 * Update all events in the given group.
 */
void perfmon_group_update(struct perfmon_group *group);

/*
 * Stop performance monitoring.
 *
 * A group can't be detached before it's stopped. Events are implicitely
 * updated when calling this function.
 */
int perfmon_group_stop(struct perfmon_group *group);

/*
 * Initialize perfmon thread-specific data for the given thread.
 */
int perfmon_thread_init(struct thread *thread);

/*
 * Destroy perfmon thread-specific data for the given thread.
 */
void perfmon_thread_destroy(struct thread *thread);

/*
 * Load/unload the events associated to a thread on the current processor.
 *
 * These functions should only be used by the scheduler during context switch.
 * Interrupts and preemption must be disabled when calling this function.
 */
void perfmon_thread_load(struct thread *thread);
void perfmon_thread_unload(struct thread *thread);

/*
 * This init operation provides :
 *  - perfmon_thread_init()
 */
INIT_OP_DECLARE(perfmon_bootstrap);

/*
 * This init operation provides :
 *  - module fully initialized
 */
INIT_OP_DECLARE(perfmon_setup);

/*
 * Handle overflow interrupt.
 */
void perfmon_handle_of_intr(struct trap_frame *frame);

int perfmon_on_overflow(struct perfmon_pmu_ops *driver);

/*
 * Register an architecture-specific driver.
 */
int perfmon_pmu_register(struct perfmon_pmu_ops *driver);

/*
 * Get the last value of given pmc.
 */
uint64_t perfmon_cpu_pmc_get_prev(unsigned int pmc_id);

/*
 * Set the last value of given pmc.
 */
void perfmon_cpu_pmc_set_prev(unsigned int pmc_id, uint64_t prev);

/*
 * Increment overflow counter for given pmc.
 */
void perfmon_cpu_pmc_inc_of(unsigned int pmc_id);

#endif /* _KERN_PERFMON_H */