forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusage.c
485 lines (361 loc) · 11 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
* 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 /* !CONFIG_USE_SWITCH && !CONFIG_INSTRUMENT_THREAD_SWITCHING */
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 /* CONFIG_THREAD_RUNTIME_STATS_USE_TIMING_FUNCTIONS */
/* 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 /* CONFIG_SCHED_THREAD_USAGE_ANALYSIS */
}
}
#else
#define sched_cpu_update_usage(cpu, cycles) do { } while (0)
#endif /* CONFIG_SCHED_THREAD_USAGE_ALL */
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 /* CONFIG_SCHED_THREAD_USAGE_ANALYSIS */
}
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 /* CONFIG_SCHED_THREAD_USAGE_ANALYSIS */
}
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 /* CONFIG_SCHED_THREAD_USAGE_ANALYSIS */
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 /* CONFIG_SCHED_THREAD_USAGE_ALL */
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 /* CONFIG_SCHED_THREAD_USAGE_ANALYSIS */
#ifdef CONFIG_SCHED_THREAD_USAGE_ALL
stats->idle_cycles = 0;
#endif /* CONFIG_SCHED_THREAD_USAGE_ALL */
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 /* CONFIG_SCHED_THREAD_USAGE_ANALYSIS */
#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 /* CONFIG_SCHED_THREAD_USAGE_ANALYSIS */
}
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 /* CONFIG_SCHED_THREAD_USAGE_ALL */
#ifdef CONFIG_OBJ_CORE_STATS_THREAD
int z_thread_stats_raw(struct k_obj_core *obj_core, void *stats)
{
k_spinlock_key_t key;
key = k_spin_lock(&usage_lock);
memcpy(stats, obj_core->stats, sizeof(struct k_cycle_stats));
k_spin_unlock(&usage_lock, key);
return 0;
}
int z_thread_stats_query(struct k_obj_core *obj_core, void *stats)
{
struct k_thread *thread;
thread = CONTAINER_OF(obj_core, struct k_thread, obj_core);
z_sched_thread_usage(thread, stats);
return 0;
}
int z_thread_stats_reset(struct k_obj_core *obj_core)
{
k_spinlock_key_t key;
struct k_cycle_stats *stats;
struct k_thread *thread;
thread = CONTAINER_OF(obj_core, struct k_thread, obj_core);
key = k_spin_lock(&usage_lock);
stats = obj_core->stats;
stats->total = 0ULL;
#ifdef CONFIG_SCHED_THREAD_USAGE_ANALYSIS
stats->current = 0ULL;
stats->longest = 0ULL;
stats->num_windows = (thread->base.usage.track_usage) ? 1U : 0U;
#endif /* CONFIG_SCHED_THREAD_USAGE_ANALYSIS */
if (thread != _current_cpu->current) {
/*
* If the thread is not running, there is nothing else to do.
* If the thread is running on another core, then it is not
* safe to do anything else but unlock and return (and pretend
* that its stats were reset at the start of its execution
* window.
*/
k_spin_unlock(&usage_lock, key);
return 0;
}
/* Update the current CPU stats. */
uint32_t now = usage_now();
uint32_t cycles = now - _current_cpu->usage0;
sched_cpu_update_usage(_current_cpu, cycles);
_current_cpu->usage0 = now;
k_spin_unlock(&usage_lock, key);
return 0;
}
int z_thread_stats_disable(struct k_obj_core *obj_core)
{
#ifdef CONFIG_SCHED_THREAD_USAGE_ANALYSIS
struct k_thread *thread;
thread = CONTAINER_OF(obj_core, struct k_thread, obj_core);
return k_thread_runtime_stats_disable(thread);
#else
return -ENOTSUP;
#endif /* CONFIG_SCHED_THREAD_USAGE_ANALYSIS */
}
int z_thread_stats_enable(struct k_obj_core *obj_core)
{
#ifdef CONFIG_SCHED_THREAD_USAGE_ANALYSIS
struct k_thread *thread;
thread = CONTAINER_OF(obj_core, struct k_thread, obj_core);
return k_thread_runtime_stats_enable(thread);
#else
return -ENOTSUP;
#endif /* CONFIG_SCHED_THREAD_USAGE_ANALYSIS */
}
#endif /* CONFIG_OBJ_CORE_STATS_THREAD */
#ifdef CONFIG_OBJ_CORE_STATS_SYSTEM
int z_cpu_stats_raw(struct k_obj_core *obj_core, void *stats)
{
k_spinlock_key_t key;
key = k_spin_lock(&usage_lock);
memcpy(stats, obj_core->stats, sizeof(struct k_cycle_stats));
k_spin_unlock(&usage_lock, key);
return 0;
}
int z_cpu_stats_query(struct k_obj_core *obj_core, void *stats)
{
struct _cpu *cpu;
cpu = CONTAINER_OF(obj_core, struct _cpu, obj_core);
z_sched_cpu_usage(cpu->id, stats);
return 0;
}
#endif /* CONFIG_OBJ_CORE_STATS_SYSTEM */
#ifdef CONFIG_OBJ_CORE_STATS_SYSTEM
int z_kernel_stats_raw(struct k_obj_core *obj_core, void *stats)
{
k_spinlock_key_t key;
key = k_spin_lock(&usage_lock);
memcpy(stats, obj_core->stats,
CONFIG_MP_MAX_NUM_CPUS * sizeof(struct k_cycle_stats));
k_spin_unlock(&usage_lock, key);
return 0;
}
int z_kernel_stats_query(struct k_obj_core *obj_core, void *stats)
{
ARG_UNUSED(obj_core);
return k_thread_runtime_stats_all_get(stats);
}
#endif /* CONFIG_OBJ_CORE_STATS_SYSTEM */