1 /*
2  * Copyright (c) 2024, Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_INCLUDE_DEBUG_CPU_LOAD_H_
8 #define ZEPHYR_INCLUDE_DEBUG_CPU_LOAD_H_
9 
10 #include <stdbool.h>
11 #include <stdint.h>
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 /** @defgroup cpu_load CPU load monitor
18  *  @ingroup debug
19  *  @brief Module for monitoring CPU Load
20  *
21  *  This module allow monitoring of the CPU load.
22  *  @{
23  */
24 
25 /** @brief Hook called by the application specific hook on entering CPU idle. */
26 void cpu_load_on_enter_idle(void);
27 
28 /** @brief Hook called by the application specific hook on exiting CPU idle. */
29 void cpu_load_on_exit_idle(void);
30 
31 /** @brief Get CPU load.
32  *
33  * CPU load is measured using a timer which tracks amount of time spent in the
34  * CPU idle. Since it is a software tracking there is some small overhead.
35  * Precision depends on the frequency of the timer in relation to the CPU frequency.
36  *
37  * @param reset Reset the measurement after reading.
38  *
39  * @retval >=0 Positive number - CPU load in per mille.
40  * @retval <0 Negative number - error code.
41  */
42 int cpu_load_get(bool reset);
43 
44 /** @brief Control periodic CPU statistics report.
45  *
46  * Report logging is by default enabled.
47  *
48  * @param enable true to enable report logging and false to disable.
49  */
50 void cpu_load_log_control(bool enable);
51 
52 /* Optional callback for cpu_load_cb_reg
53  *
54  * This will be called from the k_timer expiry_fn used for periodic logging.
55  * CONFIG_CPU_LOAD_LOG_PERIODICALLY must be configured to a positive value.
56  * Time spent in this callback must be kept to a minimum.
57  */
58 typedef void (*cpu_load_cb_t)(uint8_t percent);
59 
60 /** @brief Optional registration of callback when load is greater or equal to the threshold.
61  *
62  * @param cb Pointer to the callback function. NULL will cancel the callback.
63  * @param threshold_percent Threshold [0...100]. CPU load equal or greater that this
64  * will trigger the callback.
65  *
66  * @retval 0 - Callback registered/cancelled.
67  * @retval -EINVAL if the threshold is invalid.
68  */
69 int cpu_load_cb_reg(cpu_load_cb_t cb, uint8_t threshold_percent);
70 
71 /**
72  * @}
73  */
74 
75 #ifdef __cplusplus
76 }
77 #endif
78 
79 
80 #endif /* ZEPHYR_INCLUDE_DEBUG_CPU_LOAD_H_ */
81