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");

}