From ca4b2f91b7a24abeb6ea7fa43c1816397fb966c4 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Mon, 29 Jul 2013 14:31:13 +0000 Subject: git-svn-id: svn://svn.code.sf.net/p/chibios/svn/branches/kernel_3_dev@6039 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/kernel/src/chstats.c | 17 +++++++- os/kernel/src/chsys.c | 7 +++- os/kernel/src/chtm.c | 101 +++++++++++++++++++++++------------------------- 3 files changed, 69 insertions(+), 56 deletions(-) (limited to 'os/kernel/src') diff --git a/os/kernel/src/chstats.c b/os/kernel/src/chstats.c index 1decf8f16..9d69e0023 100644 --- a/os/kernel/src/chstats.c +++ b/os/kernel/src/chstats.c @@ -20,9 +20,9 @@ /** * @file chstats.c - * @brief Real Time Counter and Measurement module code. + * @brief Statistics module code. * - * @addtogroup realtime_counter + * @addtogroup statistics * @details Statistics services. * @{ */ @@ -60,6 +60,19 @@ kernel_stats_t kernel_stats; /* Module exported functions. */ /*===========================================================================*/ +/** + * @brief Initializes the statistics module. + * + * @init + */ +void _stats_init(void) { + + kernel_stats.nirq = 0; + kernel_stats.nctxswc = 0; + chTMObjectInit(&kernel_stats.isr); + chTMObjectInit(&kernel_stats.critical); +} + #endif /* CH_DBG_STATISTICS */ /** @} */ diff --git a/os/kernel/src/chsys.c b/os/kernel/src/chsys.c index 91761b66b..0727532ee 100644 --- a/os/kernel/src/chsys.c +++ b/os/kernel/src/chsys.c @@ -105,8 +105,8 @@ void chSysInit(void) { port_init(); _scheduler_init(); _vt_init(); -#if CH_CFG_USE_RT - _rt_init(); +#if CH_CFG_USE_TM + _tm_init(); #endif #if CH_CFG_USE_MEMCORE _core_init(); @@ -114,6 +114,9 @@ void chSysInit(void) { #if CH_CFG_USE_HEAP _heap_init(); #endif +#if CH_DBG_STATISTICS + _stats_init(); +#endif #if CH_DBG_ENABLE_TRACE _trace_init(); #endif diff --git a/os/kernel/src/chtm.c b/os/kernel/src/chtm.c index a42223da3..0ae2dfeda 100644 --- a/os/kernel/src/chtm.c +++ b/os/kernel/src/chtm.c @@ -48,7 +48,7 @@ /*===========================================================================*/ /** - * @brief Subsystem calibration value. + * @brief Measurement calibration value. */ static rtcnt_t measurement_offset; @@ -56,25 +56,35 @@ static rtcnt_t measurement_offset; /* Module local functions. */ /*===========================================================================*/ +static inline void tm_stop(time_measurement_t *tmp, rtcnt_t now) { + + tmp->last = now - tmp->last - measurement_offset; + tmp->cumulative += tmp->last; + if (tmp->last > tmp->worst) + tmp->worst = tmp->last; + else if (tmp->last < tmp->best) + tmp->best = tmp->last; +} + /*===========================================================================*/ /* Module exported functions. */ /*===========================================================================*/ /** - * @brief Initializes the realtime counter unit. + * @brief Initializes the time measurement unit. * * @init */ -void _rt_init(void) { +void _tm_init(void) { time_measurement_t tm; /* Time Measurement subsystem calibration, it does a null measurement and calculates the call overhead which is subtracted to real measurements.*/ measurement_offset = 0; - chRTTimeMeasurementObjectInit(&tm); - chRTTimeMeasurementStartX(&tm); - chRTTimeMeasurementStopX(&tm); + chTMObjectInit(&tm); + chTMStartX(&tm); + chTMStopX(&tm); measurement_offset = tm.last; } @@ -85,61 +95,34 @@ void _rt_init(void) { * of the realtime counter wrapping to zero on overflow. * @note When start==end then the function returns always true because the * whole time range is specified. - * @note This function can be called from any context. - * - * @par Example 1 - * Example of a guarded loop using the realtime counter. The loop implements - * a timeout after one second. - * @code - * rtcnt_t start = chSysGetRealtimeCounterX(); - * rtcnt_t timeout = start + S2RTC(RTCCLK, 1); - * while (my_condition) { - * if (!chTMIsCounterWithin(start, timeout) - * return TIMEOUT; - * // Do something. - * } - * // Continue. - * @endcode - * - * @par Example 2 - * Example of a loop that lasts exactly 50 microseconds. - * @code - * rtcnt_t start = chSysGetRealtimeCounterX(); - * rtcnt_t timeout = start + US2RTC(RTCCLK, 50); - * while (chTMIsCounterWithin(start, timeout)) { - * // Do something. - * } - * // Continue. - * @endcode * + * @param[in] cnt the counter value to be tested * @param[in] start the start of the time window (inclusive) * @param[in] end the end of the time window (non inclusive) * @retval true current time within the specified time window. * @retval false current time not within the specified time window. * - * @special + * @xclass */ -bool chTMIsCounterWithin(rtcnt_t start, rtcnt_t end) { - rtcnt_t now = chSysGetRealtimeCounterX(); +bool chTMIsCounterWithinX(rtcnt_t cnt, rtcnt_t start, rtcnt_t end) { - return end > start ? (now >= start) && (now < end) : - (now >= start) || (now < end); + return end > start ? (cnt >= start) && (cnt < end) : + (cnt >= start) || (cnt < end); } /** * @brief Polled delay. * @note The real delay is always few cycles in excess of the specified * value. - * @note This function can be called from any context. * * @param[in] cycles number of cycles * - * @special + * @xclass */ -void chTMPolledDelay(rtcnt_t cycles) { +void chTMPolledDelayX(rtcnt_t cycles) { rtcnt_t start = chSysGetRealtimeCounterX(); rtcnt_t end = start + cycles; - while (chRTIsCounterWithin(start, end)) + while (chTMIsCounterWithinX(chSysGetRealtimeCounterX(), start, end)) ; } @@ -161,11 +144,10 @@ void chTMObjectInit(time_measurement_t *tmp) { /** * @brief Starts a measurement. * @pre The @p time_measurement_t structure must be initialized. - * @note This function can be invoked from any context. * * @param[in,out] tmp pointer to a @p TimeMeasurement structure * - * @special + * @xclass */ NOINLINE void chTMStartX(time_measurement_t *tmp) { @@ -175,23 +157,38 @@ NOINLINE void chTMStartX(time_measurement_t *tmp) { /** * @brief Stops a measurement. * @pre The @p time_measurement_t structure must be initialized. - * @note This function can be invoked from any context. * * @param[in,out] tmp pointer to a @p time_measurement_t structure * - * @special + * @xclass */ NOINLINE void chTMStopX(time_measurement_t *tmp) { - rtcnt_t now = chSysGetRealtimeCounterX(); - tmp->last = now - tmp->last - measurement_offset; - tmp->cumulative += tmp->last; - if (tmp->last > tmp->worst) - tmp->worst = tmp->last; - else if (tmp->last < tmp->best) - tmp->best = tmp->last; + tm_stop(tmp, chSysGetRealtimeCounterX()); } #endif /* CH_CFG_USE_TM */ +/** + * @brief Stops a measurement and chains to the next one using the same time + * stamp. + * + * @param[in,out] tmp1 pointer to the @p time_measurement_t structure to be + * stopped + * @param[in,out] tmp2 pointer to the @p time_measurement_t structure to be + * started + * + * + * @xclass + */ +NOINLINE void chTMChainToX(time_measurement_t *tmp1, + time_measurement_t *tmp2) { + + /* Starts new measurement.*/ + tmp2->last = chSysGetRealtimeCounterX(); + + /* Stops previous measurement using the same time stamp.*/ + tm_stop(tmp1, tmp2->last); +} + /** @} */ -- cgit v1.2.3