In a single-CPU microprocessor that supports a multi-tasking scheduler (semi-RTOS, but non-preemptive), there is an idle task that runs when no other tasks are ready.
The idle task can do whatever. Currently, it does nothing and returns to the scheduler which then waits for an event to cause a ready task. While waiting, the scheduler again calls the idle task.
So, in the idle task, to do something to log a metric to derive the percent of idle time, what method comes to mind? There is a periodic clock interrupt, and the idle task or a new metric-gathering task could be in place to make note of clock tics, and so on, while idle.
something like the ratio of clock ticks while idle to tick while busy. Note that the scheduler is non-preemptive; the idle task must yield or do a wait(n) clock ticks.
What's the usual technique?