| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2022-2023, The Puppy RTOS Authors | ||
| 3 | * | ||
| 4 | * SPDX-License-Identifier: Apache-2.0 | ||
| 5 | */ | ||
| 6 | |||
| 7 | #include <puppy.h> | ||
| 8 | |||
| 9 | static atomic_int _g_tick; | ||
| 10 | static int _tick_persec; | ||
| 11 | static int _latency_perus; | ||
| 12 | |||
| 13 | static uint32_t latency_max = 0; | ||
| 14 | static uint32_t latency_avg = 0; | ||
| 15 | static uint32_t latency_sum = 0; | ||
| 16 | |||
| 17 | 1 | void p_tick_init(int tick_persec, int latency_perus) | |
| 18 | { | ||
| 19 | 1 | _tick_persec = tick_persec; | |
| 20 | 1 | _latency_perus = latency_perus; | |
| 21 | 1 | } | |
| 22 | |||
| 23 | 2116 | void p_tick_inc(int tick, int latency) | |
| 24 | { | ||
| 25 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2113 times.
|
2116 | if ((_g_tick % 1024) == 0) |
| 26 | { | ||
| 27 | 3 | latency_avg = (latency_avg + (latency_sum >> 10)) >> 1; | |
| 28 | 3 | latency_sum = latency; | |
| 29 | } | ||
| 30 | else | ||
| 31 | { | ||
| 32 | 2113 | latency_sum += latency; | |
| 33 | } | ||
| 34 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2106 times.
|
2116 | if (latency > latency_max) |
| 35 | { | ||
| 36 | 10 | latency_max = latency; | |
| 37 | } | ||
| 38 | 2116 | atomic_fetch_add(&_g_tick, tick); | |
| 39 | 2116 | thread_timeout_cb(_g_tick); | |
| 40 | 2116 | } | |
| 41 | |||
| 42 | 117 | p_tick_t p_tick_get(void) | |
| 43 | { | ||
| 44 | 117 | return _g_tick; | |
| 45 | } | ||
| 46 | |||
| 47 | 23 | int p_tick_persec(void) | |
| 48 | { | ||
| 49 | 23 | return _tick_persec; | |
| 50 | } | ||
| 51 | |||
| 52 | #if defined(ENABLE_NR_SHELL) | ||
| 53 | #include <nr_micro_shell.h> | ||
| 54 | ✗ | void shell_tickdump_cmd(char argc, char *argv) | |
| 55 | { | ||
| 56 | ✗ | uint32_t avg = latency_sum / ((_g_tick % 1024) + 1); | |
| 57 | ✗ | if (latency_avg != 0) | |
| 58 | { | ||
| 59 | ✗ | avg = (avg + latency_avg) >> 1; | |
| 60 | } | ||
| 61 | ✗ | printk("Current tick:%d\n", _g_tick); | |
| 62 | ✗ | avg = avg * 100 / _latency_perus; | |
| 63 | ✗ | printk("Average Latency:%d.%d us\n", avg / 100, avg % 100); | |
| 64 | ✗ | avg = latency_max * 100 / _latency_perus; | |
| 65 | ✗ | printk("Max Latency:%d.%d us\n", avg / 100, avg % 100); | |
| 66 | ✗ | } | |
| 67 | NR_SHELL_CMD_EXPORT(tick_dump, shell_tickdump_cmd); | ||
| 68 | #endif | ||
| 69 |