Measuring CPU Usage

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: utzig, lbednarz, tfAteba, barthess, RoccoMarco

musiclover
Posts: 4
Joined: Tue May 09, 2017 2:02 am
Has thanked: 5 times
Been thanked: 2 times

Re: Measuring CPU Usage

Postby musiclover » Sun May 14, 2017 3:45 am

Hi Giovanni,
I wrote a function to measure the average usage of CPU. The code is below. Is this correct? Thanks a lot!

Code: Select all

static void CPU_load(BaseSequentialStream *chp) {

  thread_t *tp;

  uint64_t sum=0;
  uint16_t tmp1, tmp2;

  tp = chRegFirstThread();
  do {
    sum += tp->p_stats.cumulative;
    tp = chRegNextThread(tp);
  } while (tp != NULL);
  sum += ch.kernel_stats.m_crit_thd.cumulative;
  sum += ch.kernel_stats.m_crit_isr.cumulative;

  tp = chRegFirstThread();
  do {
    tmp1 = (uint16_t)(tp->p_stats.cumulative*10000/sum);
    chprintf(chp, "%12s %u.%u%%\r\n", tp->p_name, tmp1/100, tmp1%100);
    tp = chRegNextThread(tp);
  } while (tp != NULL);

  tmp1 = (uint16_t)(ch.kernel_stats.m_crit_thd.cumulative*10000/sum);
  tmp2 = (uint16_t)(ch.kernel_stats.m_crit_isr.cumulative*10000/sum);

  chprintf(chp, "thd:%u.%u%%   isr:%u.%u%%\r\n",
      tmp1/100, tmp1%100,tmp2/100, tmp2%100);
  chprintf(chp, "\r\n");
}

User avatar
Giovanni
Site Admin
Posts: 12098
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 549 times
Been thanked: 469 times
Contact:

Re: Measuring CPU Usage

Postby Giovanni » Sun May 14, 2017 7:04 am

Hi,

It looks OK, it is what I meant. Note that statistics make the operations a bit slower and use more RAM because handling of counters.

Giovanni

musiclover
Posts: 4
Joined: Tue May 09, 2017 2:02 am
Has thanked: 5 times
Been thanked: 2 times

Re: Measuring CPU Usage

Postby musiclover » Sun May 14, 2017 9:27 am

Thanks Giovanni and thanks for remind. That code will only be used for testing, and when testing is finished, I'll disable the code and change CH_DBG_STATISTICS to FALSE. :D

sntnjrg
Posts: 28
Joined: Mon Jul 22, 2019 2:19 pm
Has thanked: 8 times

Re: Measuring CPU Usage

Postby sntnjrg » Wed Sep 04, 2019 11:11 am

Hi,

Why is it multiplied by 10000?

Thank you.

sntnjrg
Posts: 28
Joined: Mon Jul 22, 2019 2:19 pm
Has thanked: 8 times

Re: Measuring CPU Usage

Postby sntnjrg » Wed Sep 04, 2019 3:03 pm

Hi,

I understand the reason for the multiplication by 10000.

When I run the code, I have noticed that the sum of all the percentages of the threads is never the 100%.

When you do the following:

Code: Select all

tmp1 = (uint16_t)(tp->p_stats.cumulative*10000/sum);

Hasn't the value of the cumulative field changed from the one previously added?

What other approach would you recommend to obtain the CPU usage percentage of each thread?

Thank you.

User avatar
Giovanni
Site Admin
Posts: 12098
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 549 times
Been thanked: 469 times
Contact:

Re: Measuring CPU Usage

Postby Giovanni » Wed Sep 04, 2019 4:03 pm

The system provides only the time spent by each thread as an absolute value (number of clock cycles), converting that in percentage could be subject to rounding errors.

Giovanni

sntnjrg
Posts: 28
Joined: Mon Jul 22, 2019 2:19 pm
Has thanked: 8 times

Re: Measuring CPU Usage

Postby sntnjrg » Thu Sep 05, 2019 3:16 pm

Thank you Giovanni for the help and musiclover for the function.


Return to “General Support”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest