add __cpu_time__

This commit is contained in:
Gabriel Wang 2023-05-20 17:29:21 +01:00
parent d4855492b1
commit da09fda0bc
5 changed files with 116 additions and 10 deletions

View File

@ -193,5 +193,11 @@ int main (void)
if (perfc_is_time_out_ms(1000)) {
printf("\r[%010d]", get_system_ms());
}
__cpu_time__(10) {
delay_us(3000);
}
delay_us(7000);
}
}

Binary file not shown.

View File

@ -306,6 +306,19 @@ __asm(".global __ensure_systick_wrapper\n\t");
* @{
*/
/*!
* \brief measure the cycle count of a given code segment
* \param[in] __STR a description string for the measurement
* \param[in] ... an optional code segement, in which we can read the measured
* result from __cycle_count__.
* \details Here is an example:
E.g.
\code
__cycleof__("printf") {
printf("hello world\r\n");
}
\endcode
*/
#define __cycleof__(__STR, ...) \
using(int64_t _ = get_system_ticks(), __cycle_count__ = _, \
_=_, { \
@ -324,7 +337,47 @@ __asm(".global __ensure_systick_wrapper\n\t");
}; \
})
/*!
* \brief measure the cpu usage for a given code segment and print out the
* result in percentage.
* \param[in] __CNT generate result on every given iterations
* \param[in] ... an optional code segement, in which we can read the measured
* result from __usage__ which is a float value.
* \details Here is an example, 50% cpu time:
E.g.
\code
while (1) {
__cpu_time__(100) {
delay_us(5000);
}
delay_us(5000);
}
\endcode
*/
#define __cpu_time__(__CNT, ...) \
static int64_t SAFE_NAME(s_lTimestamp) = 0, SAFE_NAME(s_lTotal) = 0; \
static uint32_t s_wLoopCounter = (__CNT); \
using(int64_t lStart = 0, ({ \
if (0 == s_wLoopCounter) { \
float __usage__ = (float)((double)SAFE_NAME(s_lTotal) \
/ (double)( get_system_ticks() \
- SAFE_NAME(s_lTimestamp))); \
__usage__ *= 100.0f; \
SAFE_NAME(s_lTimestamp) = 0; \
SAFE_NAME(s_lTotal) = 0; \
if (__PLOOC_VA_NUM_ARGS(__VA_ARGS__) == 0) { \
printf("CPU Usage %3.2f%%\r\n", (double)__usage__); \
} else { \
__VA_ARGS__ \
} \
} \
if (0 == SAFE_NAME(s_lTimestamp)) { \
SAFE_NAME(s_lTimestamp) = get_system_ticks(); \
s_wLoopCounter = (__CNT); \
} \
lStart = get_system_ticks();}), \
({SAFE_NAME(s_lTotal) += get_system_ticks() - lStart; \
s_wLoopCounter--;}))
/*!
* \addtogroup gBasicTimerService 1.2 Timer Service
@ -387,9 +440,6 @@ __asm(".global __ensure_systick_wrapper\n\t");
CONNECT2(perfc_is_time_out_ms, __PLOOC_VA_NUM_ARGS(__VA_ARGS__)) \
(__VA_ARGS__)
/*!
* \brief set an alarm with given period in us and check the status
*
@ -400,7 +450,7 @@ __asm(".global __ensure_systick_wrapper\n\t");
* \return bool whether it is timeout
*/
#define perfc_is_time_out_us3(__us, __timestamp_ptr, __auto_reload) \
({ static int64_t SAFE_NAME(s_lTimestamp); \
({ static int64_t SAFE_NAME(s_lTimestamp); (void)SAFE_NAME(s_lTimestamp) \
__perfc_is_time_out(perfc_convert_us_to_ticks(__us), \
(__timestamp_ptr), (__auto_reload));})

Binary file not shown.

View File

@ -306,6 +306,19 @@ __asm(".global __ensure_systick_wrapper\n\t");
* @{
*/
/*!
* \brief measure the cycle count of a given code segment
* \param[in] __STR a description string for the measurement
* \param[in] ... an optional code segement, in which we can read the measured
* result from __cycle_count__.
* \details Here is an example:
E.g.
\code
__cycleof__("printf") {
printf("hello world\r\n");
}
\endcode
*/
#define __cycleof__(__STR, ...) \
using(int64_t _ = get_system_ticks(), __cycle_count__ = _, \
_=_, { \
@ -324,7 +337,47 @@ __asm(".global __ensure_systick_wrapper\n\t");
}; \
})
/*!
* \brief measure the cpu usage for a given code segment and print out the
* result in percentage.
* \param[in] __CNT generate result on every given iterations
* \param[in] ... an optional code segement, in which we can read the measured
* result from __usage__ which is a float value.
* \details Here is an example, 50% cpu time:
E.g.
\code
while (1) {
__cpu_time__(100) {
delay_us(5000);
}
delay_us(5000);
}
\endcode
*/
#define __cpu_time__(__CNT, ...) \
static int64_t SAFE_NAME(s_lTimestamp) = 0, SAFE_NAME(s_lTotal) = 0; \
static uint32_t s_wLoopCounter = (__CNT); \
using(int64_t lStart = 0, ({ \
if (0 == s_wLoopCounter) { \
float __usage__ = (float)((double)SAFE_NAME(s_lTotal) \
/ (double)( get_system_ticks() \
- SAFE_NAME(s_lTimestamp))); \
__usage__ *= 100.0f; \
SAFE_NAME(s_lTimestamp) = 0; \
SAFE_NAME(s_lTotal) = 0; \
if (__PLOOC_VA_NUM_ARGS(__VA_ARGS__) == 0) { \
printf("CPU Usage %3.2f%%\r\n", (double)__usage__); \
} else { \
__VA_ARGS__ \
} \
} \
if (0 == SAFE_NAME(s_lTimestamp)) { \
SAFE_NAME(s_lTimestamp) = get_system_ticks(); \
s_wLoopCounter = (__CNT); \
} \
lStart = get_system_ticks();}), \
({SAFE_NAME(s_lTotal) += get_system_ticks() - lStart; \
s_wLoopCounter--;}))
/*!
* \addtogroup gBasicTimerService 1.2 Timer Service
@ -387,9 +440,6 @@ __asm(".global __ensure_systick_wrapper\n\t");
CONNECT2(perfc_is_time_out_ms, __PLOOC_VA_NUM_ARGS(__VA_ARGS__)) \
(__VA_ARGS__)
/*!
* \brief set an alarm with given period in us and check the status
*
@ -400,7 +450,7 @@ __asm(".global __ensure_systick_wrapper\n\t");
* \return bool whether it is timeout
*/
#define perfc_is_time_out_us3(__us, __timestamp_ptr, __auto_reload) \
({ static int64_t SAFE_NAME(s_lTimestamp); \
({ static int64_t SAFE_NAME(s_lTimestamp); (void)SAFE_NAME(s_lTimestamp) \
__perfc_is_time_out(perfc_convert_us_to_ticks(__us), \
(__timestamp_ptr), (__auto_reload));})