Re: [PATCH] mm/vmstat: spread vmstat_update requeue across the stat interval

From: Johannes Weiner

Date: Wed Apr 01 2026 - 11:00:55 EST


On Wed, Apr 01, 2026 at 07:39:28AM -0700, Breno Leitao wrote:
> Hello Johannes,
>
> On Wed, Apr 01, 2026 at 10:25:35AM -0400, Johannes Weiner wrote:
> > On Wed, Apr 01, 2026 at 06:57:50AM -0700, Breno Leitao wrote:
> > > +static unsigned long vmstat_spread_delay(void)
> > > +{
> > > + unsigned long interval = sysctl_stat_interval;
> > > + unsigned int nr_cpus = num_online_cpus();
> > > +
> > > + if (nr_cpus <= 1)
> > > + return round_jiffies_relative(interval);
> > > +
> > > + /*
> > > + * Spread per-cpu vmstat work evenly across the interval. Don't
> > > + * use round_jiffies_relative() here -- it would snap every CPU
> > > + * back to the same second boundary, defeating the spread.
> > > + */
> > > + return interval + (interval * (smp_processor_id() % nr_cpus)) / nr_cpus;
> >
> > smp_processor_id() <= nr_cpus, so
> >
> > return interval + interval*cpu/nr_cpus
> >
> > should be equivalent, no?
>
> nr_cpus is the number of online CPUs, while smp_processor_id() is the
> CPU id.
>
> If you offline a CPU, then smp_processor_id() might be bigger than
> num_online_cpus()
>
> My goal was to linearly shift the timer and avoid creating gaps when
> removing certain CPUs.

Ah makes sense. Plus you'd spill into the next interval otherwise.