Page 1 of 1

Interrupt hooks

Posted: Thu Jun 26, 2014 2:23 pm
by mobyfab
Hi,

I'm trying to measure processor usage on one of my projects, but there is no real way to mesure interrupt processor usage.

For the threads CPU time I use these hooks:

Code: Select all

#define THREAD_EXT_FIELDS                                                   \
  /* Add threads custom fields here.*/                                      \
  uint32_t lasttick; \
  uint32_t runtime;
#endif

#define THREAD_EXT_INIT_HOOK(tp) {                                          \
  /* Add threads initialization code here.*/                                \
  tp->lasttick = chTimeNow();\
  tp->runtime = 0; \
}
#endif

#define THREAD_CONTEXT_SWITCH_HOOK(ntp, otp) {                              \
  /* System halt code here.*/                                               \
  otp->runtime += chTimeNow() - otp->lasttick; \
  ntp->lasttick = chTimeNow(); \
}
#endif

I'm using a different tick counter so that I can reset it every 500ms. (I want the average load of the last 500ms)
So far it works fine for threads.

Could it be possible to add similar hooks to CH_IRQ_PROLOGUE and CH_IRQ_EPILOGUE ?
That way I can make measurements.
I'm using 2.6.5.

Or if you have any other idea... :)

Re: Interrupt hooks

Posted: Thu Jun 26, 2014 3:11 pm
by Giovanni
There is no way in ChibiOS 2.6 directly, however ISRs usage gets included in threads used time.

I will look into adding something in 3.0 but it is an hard problem.

Giovanni

Re: Interrupt hooks

Posted: Thu Jun 26, 2014 3:29 pm
by mobyfab
Does the systick IRQs happen when already in an interrupt handler ?

If not, I could increase my runtime integer using SYSTEM_TICK_EVENT_HOOK, then compare the result with a hardware timer.
This method has a bit more overhead (1-2 instructions per tick I guess) but I can afford that.

threads_ticks = sum of all thread ticks
threads_ticks - all_ticks = interrupt_ticks

Does that makes sense?

I will make some more tests tonight.

Re: Interrupt hooks

Posted: Thu Jun 26, 2014 3:38 pm
by Giovanni
Interrupts can preempt each other, including systick, this is what makes measurement an hard problem. The time of a given ISR would also potentially include the time of ISRs at higher priorities.

The Cortex-M has some ISR-related measurement counters but I haven't looked into them yet.

Giovanni

Re: Interrupt hooks

Posted: Thu Jun 26, 2014 4:01 pm
by mobyfab
Found, it's DWT_EXCCNT

DWT Exception Overhead Count Register
Use the DWT Exception Overhead Count Register to count the total cycles spent in interrupt processing.


I can compare it with DWT_CYCCNT to get the interrupt ratio. :)

This gives me thread running time, free of interrupt time:

Code: Select all

#define THREAD_EXT_FIELDS                                                   \
  /* Add threads custom fields here.*/                                      \
  uint32_t runoffset; \
  uint32_t runtime; \
  uint32_t irqoffset; \
  uint32_t irqtime;
#endif

#define THREAD_EXT_INIT_HOOK(tp) {                                          \
  /* Add threads initialization code here.*/                                \
  tp->runoffset = DWT_CYCCNT;\
  tp->runtime = 0; \
  tp->irqoffset = DWT_EXCCNT; \
  tp->irqtime = 0; \
}
#endif

#define THREAD_CONTEXT_SWITCH_HOOK(ntp, otp) {                              \
  /* System halt code here.*/                                               \
  otp->irqtime = (DWT_EXCCNT - otp->irqoffset); \
  otp->runtime += DWT_CYCCNT - otp->runoffset - otp->irqtime; \
  ntp->runoffset = DWT_CYCCNT; \
  ntp->irqoffset = DWT_EXCCNT; \
}
#endif


Processing data:

Code: Select all

WORKING_AREA(waThreadMonitor, 256);
msg_t ThreadMonitor(void *arg)
{
  (void)arg;
  chRegSetThreadName("Monitor");
  uint32_t thTicks[7];
  uint32_t irq_offset, run_offset, irq_ticks, total_ticks;
  Thread* pThreadMonitor = chThdSelf();

  while (TRUE)
  {
   chSysLock();

   pThreadBDU->runtime = 0;
   pThreadSDU->runtime = 0;
   pThreadCAN->runtime = 0;
   pThreadKnock->runtime = 0;
   pThreadSensors->runtime = 0;
   pThreadMonitor->runtime = 0;
   chSysGetIdleThread()->runtime = 0;

   irq_offset = DWT_EXCCNT;
   run_offset = DWT_CYCCNT;

   chSysUnlock();

   /* Populate load data */
   chThdSleepMilliseconds(500);

   /* Check for counters wrapping */
   if (irq_offset > DWT_EXCCNT || run_offset > DWT_CYCCNT)
      continue;

   chSysLock();

   total_ticks = (DWT_CYCCNT - run_offset) / 100000; // Should be ~3600 for 500ms
   irq_ticks = (DWT_EXCCNT - irq_offset);

   thTicks[0] = pThreadBDU->runtime;
   thTicks[1] = pThreadSDU->runtime;
   thTicks[2] = pThreadCAN->runtime;
   thTicks[3] = pThreadKnock->runtime;
   thTicks[4] = pThreadSensors->runtime;
   thTicks[5] = pThreadMonitor->runtime;
   thTicks[6] = chSysGetIdleThread()->runtime;

   chSysUnlock();

   /* Total ticks are already divided by 100000
    * 36000000/3600 = 10000 or 100.00%          */
   monitoring.bdu = ((thTicks[0])/total_ticks);
   monitoring.sdu = ((thTicks[1])/total_ticks);
   monitoring.can = ((thTicks[2])/total_ticks);
   monitoring.knock = ((thTicks[3])/total_ticks);
   monitoring.sensors = ((thTicks[4])/total_ticks);
   monitoring.monitor = ((thTicks[5])/total_ticks);
   monitoring.idle = (((thTicks[6])/total_ticks));
   monitoring.irq = ((irq_ticks)/total_ticks);
  }
  return 0;
}

Re: Interrupt hooks

Posted: Thu Jun 26, 2014 8:15 pm
by mobyfab
After some testing is does not work.

"cycles spent in interrupt processing" meant cycles spent entering and exiting interrupts. Not very useful. Also it's only a 8bit counter.

Back to the systick event hook: Is there an easy way to check if the OS is running an interrupt (outside of the systick one obviously)?

edit: trying this

Code: Select all

#define SYSTEM_TICK_EVENT_HOOK() {                                          \
  /* System tick event code here.*/                                         \
  /* SCB_ICSR_RETTOBASE: Only systick ISR is active */ \
  if (SCB_ICSR & (1u << 11)) chThdSelf()->runtime++; else chThdSelf()->irqtime++; \
}

Re: Interrupt hooks

Posted: Thu Jun 26, 2014 8:53 pm
by mobyfab
it seems to be working this way :)

Image Image

1st image is with the serial USB dealing with some load. (it uses serial usb and serial so both are interrupt transfers, not DMA)
2nd image is without any load on the serial thread. We can see the interrupts are down, that makes sense.

Re: Interrupt hooks

Posted: Thu Jun 26, 2014 10:03 pm
by Giovanni
In 2.6 there is no portable way to check for ISR execution, there is a new API in 3.0 for this. If you don't need portable code you can check the current exception in the cortex status register.

Giovanni