Re: Avoid offline cpu in automatic frequency scheduling

2020-05-28 Thread Ted Unangst
On 2020-05-28, Solene Rapenne wrote:
> > > In the current case, if you have offline cpu (because of hw.smt=0),
> > > the total will still sum all the idle cpu and it's unlikely that
> > > the total threshold is ever reached before the per cpu one.
> > > 
> > > so, this diff skip offline cpus in the loop (robert@ gave me the big
> > > clue to use cpu_is_online in the loop)  

This looks good.

> 
> It's a lot of magic numbers without explanation here. I'm curious if
> this could be improved. I guess that changing them would tip the
> balance between responsiveness and battery saving.

Did I do that? You have it about right, although there was never much science
behind the algorithm or numbers. It was something that seemed to work okay
without much complexity.



Re: Avoid offline cpu in automatic frequency scheduling

2020-05-28 Thread Solene Rapenne
On Thu, 28 May 2020 11:43:07 -0400
Bryan Steele :

> On Thu, May 28, 2020 at 04:29:19PM +0200, Solene Rapenne wrote:
> > the macro CPU_INFO_FOREACH loop over every CPU but the frequency
> > algorithm will raise frequency if one cpu usage goes over a
> > threshold but also if the sum of cpu usage goes over another
> > threshold.
> > 
> > In the current case, if you have offline cpu (because of hw.smt=0),
> > the total will still sum all the idle cpu and it's unlikely that
> > the total threshold is ever reached before the per cpu one.
> > 
> > so, this diff skip offline cpus in the loop (robert@ gave me the big
> > clue to use cpu_is_online in the loop)  
> 
> Thanks for finding this solene! Nice work.
> 
> I've been aware of some issues on AMD with the automatic frequency
> scaling and haven't been able to narrow them down. This may not solve
> all of them, but does appear to improve things on Ryzen CPUs which
> have many cores/threads and no BIOS knob to disable SMT.
> 
> Assuming others agree with the approach..
> 
> ok brynet@

I think this should help CPU with a lot of cores. If I got the
algorithm right this is doing this:

if 1 cpu has idle < 25% then scale up
OR
if sum cpu idle < 33% then scale up

when it scales up, it will bump a ticker to 5 and set perf to 100% and
check again in the next 100ms. Everytime the loop doesn't need to scale
up (but is still at 100% freq) it decrement the ticket. Once the ticket
is 0 or less, then the frequency goes to minimum.

with the current state, if you have offline cpu, you can never go under
50% of the total idle. So the only way to scale up is to have 1 CPU
with less than 25% idle.

It's a lot of magic numbers without explanation here. I'm curious if
this could be improved. I guess that changing them would tip the
balance between responsiveness and battery saving.



Re: Avoid offline cpu in automatic frequency scheduling

2020-05-28 Thread Bryan Steele
On Thu, May 28, 2020 at 04:29:19PM +0200, Solene Rapenne wrote:
> the macro CPU_INFO_FOREACH loop over every CPU but the frequency
> algorithm will raise frequency if one cpu usage goes over a threshold
> but also if the sum of cpu usage goes over another threshold.
> 
> In the current case, if you have offline cpu (because of hw.smt=0), the
> total will still sum all the idle cpu and it's unlikely that the total
> threshold is ever reached before the per cpu one.
> 
> so, this diff skip offline cpus in the loop (robert@ gave me the big
> clue to use cpu_is_online in the loop)

Thanks for finding this solene! Nice work.

I've been aware of some issues on AMD with the automatic frequency
scaling and haven't been able to narrow them down. This may not solve
all of them, but does appear to improve things on Ryzen CPUs which have
many cores/threads and no BIOS knob to disable SMT.

Assuming others agree with the approach..

ok brynet@

> Index: sched_bsd.c
> ===
> RCS file: /cvs/src/sys/kern/sched_bsd.c,v
> retrieving revision 1.62
> diff -u -p -r1.62 sched_bsd.c
> --- sched_bsd.c   30 Jan 2020 08:51:27 -  1.62
> +++ sched_bsd.c   28 May 2020 14:21:25 -
> @@ -576,6 +576,8 @@ setperf_auto(void *v)
>   j = 0;
>   speedup = 0;
>   CPU_INFO_FOREACH(cii, ci) {
> + if (!cpu_is_online(ci))
> + continue;
>   total = 0;
>   for (i = 0; i < CPUSTATES; i++) {
>   total += ci->ci_schedstate.spc_cp_time[i];
> 
> 



Re: Avoid offline cpu in automatic frequency scheduling

2020-05-28 Thread Solene Rapenne
On Thu, 28 May 2020 16:29:19 +0200
Solene Rapenne :

> so, this diff skip offline cpus in the loop (robert@ gave me the big
> clue to use cpu_is_online in the loop)

apologies, it was claudio@ but robert@ helped too.