#23 Add accurate per-thread cycle counter

closed
nobody
None
5
2014-02-21
2012-10-08
No

Currently only system time (in ticks) is recorded per thread. On architectures that have a freerunning CPU cycle counter, implement measurement and accumulation per thread.

This can be implemented at the moment at least in Cortex-M using THREAD_EXT_FIELDS, THREAD_EXT_INIT_HOOK and THREAD_CONTEXT_SWITCH_HOOK, plus a global variable, as follows:

#define THREAD_EXT_FIELDS \ uint64_t p_ctime; \ uint32_t p_cref;
#endif

#define THREAD_EXT_INIT_HOOK(tp) { \ tp->p_ctime = 0; \ tp->p_cref = DWT_CYCCNT; \ }
#endif

#define THREAD_CONTEXT_SWITCH_HOOK(ntp, otp) { \ ntp->p_cref = DWT_CYCCNT; \ if (otp) { \ uint32_t cnt = ntp->p_cref - otp->p_cref; \ otp->p_ctime += cnt; \ ctime += cnt; \ } \ }
#endif

(the global ctime variable is needed if one wants later to calculate CPU usage percentages, and since the freerunning cycle counter is 32 bit long and overflows, a larger 64 bit accumulation counter needs to be elapsed on each context switch)

The above approach is not ideal because:

- Is not portable (requires explicit usage of the cycle counter register instead of the HAL #define) and requires almost exact implementation on each architecture that has a freerunning cycle counter.

- Requires an external user provided global variable (ctime).

I would like this feature to be automatically implemented in architectures that support it. Maybe could be implemented on all architectures with a fallback to system ticks on those architectures that don't support it (or maybe allow usage of a peripheral timer in freerunning mode).

Discussion

  • Giovanni Di Sirio

    Schduled for ChibiOS kernel 3.x.

     
  • Giovanni Di Sirio

    • status: open --> closed
    • Group: --> Next_Release_(example)
     
  • Giovanni Di Sirio

    Implemented in 3.0 branch.

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks