forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usage.c
345 lines (259 loc) · 7.32 KB
/
usage.c
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
* Copyright (c) 2018 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/timing/timing.h>
#include <ksched.h>
#include <zephyr/spinlock.h>
#include <zephyr/sys/check.h>
/* Need one of these for this to work */
#if !defined(CONFIG_USE_SWITCH) && !defined(CONFIG_INSTRUMENT_THREAD_SWITCHING)
#error "No data backend configured for CONFIG_SCHED_THREAD_USAGE"
#endif
static struct k_spinlock usage_lock;
static uint32_t usage_now(void)
{
uint32_t now;
#ifdef CONFIG_THREAD_RUNTIME_STATS_USE_TIMING_FUNCTIONS
now = (uint32_t)timing_counter_get();
#else
now = k_cycle_get_32();
#endif
/* Edge case: we use a zero as a null ("stop() already called") */
return (now == 0) ? 1 : now;
}
#ifdef CONFIG_SCHED_THREAD_USAGE_ALL
static void sched_cpu_update_usage(struct _cpu *cpu, uint32_t cycles)
{
if (!cpu->usage.track_usage) {
return;
}
if (cpu->current != cpu->idle_thread) {
cpu->usage.total += cycles;
#ifdef CONFIG_SCHED_THREAD_USAGE_ANALYSIS
cpu->usage.current += cycles;
if (cpu->usage.longest < cpu->usage.current) {
cpu->usage.longest = cpu->usage.current;
}
} else {
cpu->usage.current = 0;
cpu->usage.num_windows++;
#endif
}
}
#else
#define sched_cpu_update_usage(cpu, cycles) do { } while (0)
#endif
static void sched_thread_update_usage(struct k_thread *thread, uint32_t cycles)
{
thread->base.usage.total += cycles;
#ifdef CONFIG_SCHED_THREAD_USAGE_ANALYSIS
thread->base.usage.current += cycles;
if (thread->base.usage.longest < thread->base.usage.current) {
thread->base.usage.longest = thread->base.usage.current;
}
#endif
}
void z_sched_usage_start(struct k_thread *thread)
{
#ifdef CONFIG_SCHED_THREAD_USAGE_ANALYSIS
k_spinlock_key_t key;
key = k_spin_lock(&usage_lock);
_current_cpu->usage0 = usage_now(); /* Always update */
if (thread->base.usage.track_usage) {
thread->base.usage.num_windows++;
thread->base.usage.current = 0;
}
k_spin_unlock(&usage_lock, key);
#else
/* One write through a volatile pointer doesn't require
* synchronization as long as _usage() treats it as volatile
* (we can't race with _stop() by design).
*/
_current_cpu->usage0 = usage_now();
#endif
}
void z_sched_usage_stop(void)
{
k_spinlock_key_t k = k_spin_lock(&usage_lock);
struct _cpu *cpu = _current_cpu;
uint32_t u0 = cpu->usage0;
if (u0 != 0) {
uint32_t cycles = usage_now() - u0;
if (cpu->current->base.usage.track_usage) {
sched_thread_update_usage(cpu->current, cycles);
}
sched_cpu_update_usage(cpu, cycles);
}
cpu->usage0 = 0;
k_spin_unlock(&usage_lock, k);
}
#ifdef CONFIG_SCHED_THREAD_USAGE_ALL
void z_sched_cpu_usage(uint8_t cpu_id, struct k_thread_runtime_stats *stats)
{
k_spinlock_key_t key;
struct _cpu *cpu;
key = k_spin_lock(&usage_lock);
cpu = _current_cpu;
if (&_kernel.cpus[cpu_id] == cpu) {
uint32_t now = usage_now();
uint32_t cycles = now - cpu->usage0;
/*
* Getting stats for the current CPU. Update both its
* current thread stats and the CPU stats as the CPU's
* [usage0] field will also get updated. This keeps all
* that information up-to-date.
*/
if (cpu->current->base.usage.track_usage) {
sched_thread_update_usage(cpu->current, cycles);
}
sched_cpu_update_usage(cpu, cycles);
cpu->usage0 = now;
}
stats->total_cycles = cpu->usage.total;
#ifdef CONFIG_SCHED_THREAD_USAGE_ANALYSIS
stats->current_cycles = cpu->usage.current;
stats->peak_cycles = cpu->usage.longest;
if (cpu->usage.num_windows == 0) {
stats->average_cycles = 0;
} else {
stats->average_cycles = stats->total_cycles /
cpu->usage.num_windows;
}
#endif
stats->idle_cycles =
_kernel.cpus[cpu_id].idle_thread->base.usage.total;
stats->execution_cycles = stats->total_cycles + stats->idle_cycles;
k_spin_unlock(&usage_lock, key);
}
#endif
void z_sched_thread_usage(struct k_thread *thread,
struct k_thread_runtime_stats *stats)
{
struct _cpu *cpu;
k_spinlock_key_t key;
key = k_spin_lock(&usage_lock);
cpu = _current_cpu;
if (thread == cpu->current) {
uint32_t now = usage_now();
uint32_t cycles = now - cpu->usage0;
/*
* Getting stats for the current thread. Update both the
* current thread stats and its CPU stats as the CPU's
* [usage0] field will also get updated. This keeps all
* that information up-to-date.
*/
if (thread->base.usage.track_usage) {
sched_thread_update_usage(thread, cycles);
}
sched_cpu_update_usage(cpu, cycles);
cpu->usage0 = now;
}
stats->execution_cycles = thread->base.usage.total;
stats->total_cycles = thread->base.usage.total;
/* Copy-out the thread's usage stats */
#ifdef CONFIG_SCHED_THREAD_USAGE_ANALYSIS
stats->current_cycles = thread->base.usage.current;
stats->peak_cycles = thread->base.usage.longest;
if (thread->base.usage.num_windows == 0) {
stats->average_cycles = 0;
} else {
stats->average_cycles = stats->total_cycles /
thread->base.usage.num_windows;
}
#endif
#ifdef CONFIG_SCHED_THREAD_USAGE_ALL
stats->idle_cycles = 0;
#endif
stats->execution_cycles = thread->base.usage.total;
k_spin_unlock(&usage_lock, key);
}
#ifdef CONFIG_SCHED_THREAD_USAGE_ANALYSIS
int k_thread_runtime_stats_enable(k_tid_t thread)
{
k_spinlock_key_t key;
CHECKIF(thread == NULL) {
return -EINVAL;
}
key = k_spin_lock(&usage_lock);
if (!thread->base.usage.track_usage) {
thread->base.usage.track_usage = true;
thread->base.usage.num_windows++;
thread->base.usage.current = 0;
}
k_spin_unlock(&usage_lock, key);
return 0;
}
int k_thread_runtime_stats_disable(k_tid_t thread)
{
k_spinlock_key_t key;
CHECKIF(thread == NULL) {
return -EINVAL;
}
key = k_spin_lock(&usage_lock);
struct _cpu *cpu = _current_cpu;
if (thread->base.usage.track_usage) {
thread->base.usage.track_usage = false;
if (thread == cpu->current) {
uint32_t cycles = usage_now() - cpu->usage0;
sched_thread_update_usage(thread, cycles);
sched_cpu_update_usage(cpu, cycles);
}
}
k_spin_unlock(&usage_lock, key);
return 0;
}
#endif
#ifdef CONFIG_SCHED_THREAD_USAGE_ALL
void k_sys_runtime_stats_enable(void)
{
k_spinlock_key_t key;
key = k_spin_lock(&usage_lock);
if (_current_cpu->usage.track_usage) {
/*
* Usage tracking is already enabled on the current CPU
* and thus on all other CPUs (if applicable). There is
* nothing left to do.
*/
k_spin_unlock(&usage_lock, key);
return;
}
/* Enable gathering of runtime stats on each CPU */
unsigned int num_cpus = arch_num_cpus();
for (uint8_t i = 0; i < num_cpus; i++) {
_kernel.cpus[i].usage.track_usage = true;
#ifdef CONFIG_SCHED_THREAD_USAGE_ANALYSIS
_kernel.cpus[i].usage.num_windows++;
_kernel.cpus[i].usage.current = 0;
#endif
}
k_spin_unlock(&usage_lock, key);
}
void k_sys_runtime_stats_disable(void)
{
struct _cpu *cpu;
k_spinlock_key_t key;
key = k_spin_lock(&usage_lock);
if (!_current_cpu->usage.track_usage) {
/*
* Usage tracking is already disabled on the current CPU
* and thus on all other CPUs (if applicable). There is
* nothing left to do.
*/
k_spin_unlock(&usage_lock, key);
return;
}
uint32_t now = usage_now();
unsigned int num_cpus = arch_num_cpus();
for (uint8_t i = 0; i < num_cpus; i++) {
cpu = &_kernel.cpus[i];
if (cpu->usage0 != 0) {
sched_cpu_update_usage(cpu, now - cpu->usage0);
}
cpu->usage.track_usage = false;
}
k_spin_unlock(&usage_lock, key);
}
#endif