Idle thread stats handling changed in 21.11?

Discussions and support about ChibiOS/RT, the free embedded RTOS.
steved
Posts: 825
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 135 times

Idle thread stats handling changed in 21.11?

Postby steved » Mon Mar 20, 2023 7:09 pm

I have some debug code which traverses the thread list using chRegFirstThread() and chRegNextThread() to calculate percentage CPU usage for each thread. This works fine in 20.3.4.

The same routine moved to 21.11.3, with appropriate changes to variables, gets values of zero for the timings tp->stats.cumulative, and also the context switching count tp->stats.n. All other threads yield what look to be reasonable results.
The address of the idle thread as give by the travering pointer matches that from chSysGetIdleThreadX(); thread priority is 1, and the name is correct.
My assumption is that the idle thread is treated the same as all others, in that context switches go through chSysSwitch(ntp, otp), which calls __stats_ctxswc(thread_t *ntp, thread_t *otp) - or is the idle thread different in some key way?

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

Re: Idle thread stats handling changed in 21.11?

Postby Giovanni » Mon Mar 20, 2023 8:48 pm

Hi,

It is a normal thread, it is just created in a different way.

Screenshot from 2023-03-20 20-47-31.png


A quick test seems to agree with your finding, numbers are off, I will look into this.

Giovanni

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

Re: Idle thread stats handling changed in 21.11?

Postby Giovanni » Tue Mar 21, 2023 11:37 am

Hi,

I committed tentative fixes for 2 distinct problems:

1) ARM Cortex-M (alternate) port had a bug that caused the "0" in the idle thread (but also made other threads values invalid).

2) A bug in the RT kernel caused a very large value for the main thread.

Changes committed to trunk and 21.11.x stable.

Giovanni

steved
Posts: 825
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 135 times

Re: Idle thread stats handling changed in 21.11?

Postby steved » Wed Mar 22, 2023 10:38 am

That's a lot better.
The per-thread statistics now make a lot more sense.
The context switch counts also look accurate - for example I get 262546 as the value from currcore->kernel_stats.n_ctxswc, and 260576 from having added up tp->stats.n across all threads; no locking and code execution times mean that a difference is expected, and pretty stable around 1960-1970 counts.

However, not convinced on the kernel stats; they are giving some odd values which I've not properly investigated yet.

Are the following times already included in the thread times (Looks as if they are):
currcore->kernel_stats.m_crit_thd.cumulative;
currcore->kernel_stats.m_crit_isr.cumulative;

At present, critical section time is improbably low (approaching zero). Number of interrupts is improbably low (<50). Total ISR time is excessively high (much greater than total execution time)

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

Re: Idle thread stats handling changed in 21.11?

Postby Giovanni » Wed Mar 22, 2023 12:55 pm

Hi,

Number of interrupts can be low in tick-less mode, there is no periodic tick.

Critical sections must be read carefully, if there is a blocking API inside a critical section then the "unlock" is performed by the woken thread, the code path can be not obvious.

Said that, let me know if you notice anomalies.

Giovanni

steved
Posts: 825
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 135 times

Re: Idle thread stats handling changed in 21.11?

Postby steved » Wed Mar 22, 2023 5:03 pm

Looked into this a bit more, and concluded that the strange values I'm getting for kernel stats are somehow down to a mix of uint32_t and uint64_t variables. Possibly a contribution from chprintf(); certainly I've noted that unsigned long variables are saved into a signed long internally.

User avatar
FXCoder
Posts: 384
Joined: Sun Jun 12, 2016 4:10 am
Location: Sydney, Australia
Has thanked: 180 times
Been thanked: 130 times

Re: Idle thread stats handling changed in 21.11?

Postby FXCoder » Thu Mar 23, 2023 1:54 pm

Hi Steve,
How are things?

Yeah... chprintf and 64 bit are not good friends.

Cheers
--
Bob

steved
Posts: 825
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 135 times

Re: Idle thread stats handling changed in 21.11?

Postby steved » Thu Mar 23, 2023 2:58 pm

Hi Bob; still keeping going, thanks. Although not much to do with Chibi the past couple of years. Existing projects all stable, and ST's shortages discouraged new projects. Might have used the RP2040 instead if they'd made it easy to port the libraries, but probably that ship has sailed unless I find an application that needs PIO.

Anyway, got the residual bits sorted; not so much a problem with chprintf() (apart from the signed/unsigned long detail) as matching variable types with the formatting strings as for printf() (although IIRC gcc now picks up errors if you use printf()). Life was simple when Chibi only needed to support 32-bit integers...


Return to “ChibiOS/RT”

Who is online

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