Linux block layer
 help / color / mirror / Atom feed
* Re: [PATCH 5/7] sched: loadavg: make calc_load_n() public
       [not found] ` <20180507210135.1823-6-hannes@cmpxchg.org>
@ 2018-05-09  9:49   ` Peter Zijlstra
  0 siblings, 0 replies; 20+ messages in thread
From: Peter Zijlstra @ 2018-05-09  9:49 UTC (permalink / raw)
  To: Johannes Weiner
  Cc: linux-kernel, linux-mm, linux-block, cgroups, Ingo Molnar,
	Andrew Morton, Tejun Heo, Balbir Singh, Mike Galbraith,
	Oliver Yang, Shakeel Butt, xxx xxx, Taras Kondratiuk,
	Daniel Walker, Vinayak Menon, Ruslan Ruslichenko, kernel-team

On Mon, May 07, 2018 at 05:01:33PM -0400, Johannes Weiner wrote:
> +static inline unsigned long
> +fixed_power_int(unsigned long x, unsigned int frac_bits, unsigned int n)
> +{
> +	unsigned long result = 1UL << frac_bits;
> +
> +	if (n) {
> +		for (;;) {
> +			if (n & 1) {
> +				result *= x;
> +				result += 1UL << (frac_bits - 1);
> +				result >>= frac_bits;
> +			}
> +			n >>= 1;
> +			if (!n)
> +				break;
> +			x *= x;
> +			x += 1UL << (frac_bits - 1);
> +			x >>= frac_bits;
> +		}
> +	}
> +
> +	return result;
> +}

No real objection; but that does look a wee bit fat for an inline I
suppose.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 6/7] psi: pressure stall information for CPU, memory, and IO
       [not found] ` <20180507210135.1823-7-hannes@cmpxchg.org>
@ 2018-05-09  9:59   ` Peter Zijlstra
  2018-05-09 10:04   ` Peter Zijlstra
                     ` (8 subsequent siblings)
  9 siblings, 0 replies; 20+ messages in thread
From: Peter Zijlstra @ 2018-05-09  9:59 UTC (permalink / raw)
  To: Johannes Weiner
  Cc: linux-kernel, linux-mm, linux-block, cgroups, Ingo Molnar,
	Andrew Morton, Tejun Heo, Balbir Singh, Mike Galbraith,
	Oliver Yang, Shakeel Butt, xxx xxx, Taras Kondratiuk,
	Daniel Walker, Vinayak Menon, Ruslan Ruslichenko, kernel-team

On Mon, May 07, 2018 at 05:01:34PM -0400, Johannes Weiner wrote:
> diff --git a/include/linux/psi_types.h b/include/linux/psi_types.h
> new file mode 100644
> index 000000000000..b22b0ffc729d
> --- /dev/null
> +++ b/include/linux/psi_types.h
> @@ -0,0 +1,84 @@
> +#ifndef _LINUX_PSI_TYPES_H
> +#define _LINUX_PSI_TYPES_H
> +
> +#include <linux/types.h>
> +
> +#ifdef CONFIG_PSI
> +
> +/* Tracked task states */
> +enum psi_task_count {
> +	NR_RUNNING,
> +	NR_IOWAIT,
> +	NR_MEMSTALL,
> +	NR_PSI_TASK_COUNTS,
> +};
> +
> +/* Task state bitmasks */
> +#define TSK_RUNNING	(1 << NR_RUNNING)
> +#define TSK_IOWAIT	(1 << NR_IOWAIT)
> +#define TSK_MEMSTALL	(1 << NR_MEMSTALL)
> +
> +/* Resources that workloads could be stalled on */
> +enum psi_res {
> +	PSI_CPU,
> +	PSI_MEM,
> +	PSI_IO,
> +	NR_PSI_RESOURCES,
> +};
> +
> +/* Pressure states for a group of tasks */
> +enum psi_state {
> +	PSI_NONE,		/* No stalled tasks */
> +	PSI_SOME,		/* Stalled tasks & working tasks */
> +	PSI_FULL,		/* Stalled tasks & no working tasks */
> +	NR_PSI_STATES,
> +};
> +
> +struct psi_resource {
> +	/* Current pressure state for this resource */
> +	enum psi_state state;
> +
> +	/* Start of current state (cpu_clock) */
> +	u64 state_start;
> +
> +	/* Time sampling buckets for pressure states (ns) */
> +	u64 times[NR_PSI_STATES - 1];

Fails to explain why no FULL.

> +};
> +
> +struct psi_group_cpu {
> +	/* States of the tasks belonging to this group */
> +	unsigned int tasks[NR_PSI_TASK_COUNTS];
> +

AFAICT there's a hole here, that would fit the @nonidle member. Which
also avoids the later hole generated by it.

> +	/* Per-resource pressure tracking in this group */
> +	struct psi_resource res[NR_PSI_RESOURCES];
> +
> +	/* There are runnable or D-state tasks */
> +	bool nonidle;

Mandatory complaint about using _Bool in composites goes here.

> +	/* Start of current non-idle state (cpu_clock) */
> +	u64 nonidle_start;
> +
> +	/* Time sampling bucket for non-idle state (ns) */
> +	u64 nonidle_time;
> +};
> +
> +struct psi_group {
> +	struct psi_group_cpu *cpus;
> +
> +	struct delayed_work clock_work;
> +	unsigned long period_expires;
> +
> +	u64 some[NR_PSI_RESOURCES];
> +	u64 full[NR_PSI_RESOURCES];
> +
> +	unsigned long avg_some[NR_PSI_RESOURCES][3];
> +	unsigned long avg_full[NR_PSI_RESOURCES][3];
> +};

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 6/7] psi: pressure stall information for CPU, memory, and IO
       [not found] ` <20180507210135.1823-7-hannes@cmpxchg.org>
  2018-05-09  9:59   ` [PATCH 6/7] psi: pressure stall information for CPU, memory, and IO Peter Zijlstra
@ 2018-05-09 10:04   ` Peter Zijlstra
  2018-05-09 10:05   ` Peter Zijlstra
                     ` (7 subsequent siblings)
  9 siblings, 0 replies; 20+ messages in thread
From: Peter Zijlstra @ 2018-05-09 10:04 UTC (permalink / raw)
  To: Johannes Weiner
  Cc: linux-kernel, linux-mm, linux-block, cgroups, Ingo Molnar,
	Andrew Morton, Tejun Heo, Balbir Singh, Mike Galbraith,
	Oliver Yang, Shakeel Butt, xxx xxx, Taras Kondratiuk,
	Daniel Walker, Vinayak Menon, Ruslan Ruslichenko, kernel-team

On Mon, May 07, 2018 at 05:01:34PM -0400, Johannes Weiner wrote:
> +static void psi_clock(struct work_struct *work)
> +{
> +	u64 some[NR_PSI_RESOURCES] = { 0, };
> +	u64 full[NR_PSI_RESOURCES] = { 0, };
> +	unsigned long nonidle_total = 0;
> +	unsigned long missed_periods;
> +	struct delayed_work *dwork;
> +	struct psi_group *group;
> +	unsigned long expires;
> +	int cpu;
> +	int r;
> +
> +	dwork = to_delayed_work(work);
> +	group = container_of(dwork, struct psi_group, clock_work);
> +
> +	/*
> +	 * Calculate the sampling period. The clock might have been
> +	 * stopped for a while.
> +	 */
> +	expires = group->period_expires;
> +	missed_periods = (jiffies - expires) / MY_LOAD_FREQ;
> +	group->period_expires = expires + ((1 + missed_periods) * MY_LOAD_FREQ);
> +
> +	/*
> +	 * Aggregate the per-cpu state into a global state. Each CPU
> +	 * is weighted by its non-idle time in the sampling period.
> +	 */
> +	for_each_online_cpu(cpu) {

Typically when using online CPU state, you also need hotplug notifiers
to deal with changes in the online set.

You also typically need something like cpus_read_lock() around an
iteration of online CPUs, to avoid the set changing while you're poking
at them.

The lack for neither is evident or explained.

> +		struct psi_group_cpu *groupc = per_cpu_ptr(group->cpus, cpu);
> +		unsigned long nonidle;
> +
> +		nonidle = nsecs_to_jiffies(groupc->nonidle_time);
> +		groupc->nonidle_time = 0;
> +		nonidle_total += nonidle;
> +
> +		for (r = 0; r < NR_PSI_RESOURCES; r++) {
> +			struct psi_resource *res = &groupc->res[r];
> +
> +			some[r] += (res->times[0] + res->times[1]) * nonidle;
> +			full[r] += res->times[1] * nonidle;
> +
> +			/* It's racy, but we can tolerate some error */
> +			res->times[0] = 0;
> +			res->times[1] = 0;
> +		}
> +	}
> +
> +	for (r = 0; r < NR_PSI_RESOURCES; r++) {
> +		/* Finish the weighted aggregation */
> +		some[r] /= max(nonidle_total, 1UL);
> +		full[r] /= max(nonidle_total, 1UL);
> +
> +		/* Accumulate stall time */
> +		group->some[r] += some[r];
> +		group->full[r] += full[r];
> +
> +		/* Calculate recent pressure averages */
> +		calc_avgs(group->avg_some[r], some[r], missed_periods);
> +		calc_avgs(group->avg_full[r], full[r], missed_periods);
> +	}
> +
> +	/* Keep the clock ticking only when there is action */
> +	if (nonidle_total)
> +		schedule_delayed_work(dwork, MY_LOAD_FREQ);
> +}

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 6/7] psi: pressure stall information for CPU, memory, and IO
       [not found] ` <20180507210135.1823-7-hannes@cmpxchg.org>
  2018-05-09  9:59   ` [PATCH 6/7] psi: pressure stall information for CPU, memory, and IO Peter Zijlstra
  2018-05-09 10:04   ` Peter Zijlstra
@ 2018-05-09 10:05   ` Peter Zijlstra
  2018-05-09 10:14   ` Peter Zijlstra
                     ` (6 subsequent siblings)
  9 siblings, 0 replies; 20+ messages in thread
From: Peter Zijlstra @ 2018-05-09 10:05 UTC (permalink / raw)
  To: Johannes Weiner
  Cc: linux-kernel, linux-mm, linux-block, cgroups, Ingo Molnar,
	Andrew Morton, Tejun Heo, Balbir Singh, Mike Galbraith,
	Oliver Yang, Shakeel Butt, xxx xxx, Taras Kondratiuk,
	Daniel Walker, Vinayak Menon, Ruslan Ruslichenko, kernel-team

On Mon, May 07, 2018 at 05:01:34PM -0400, Johannes Weiner wrote:
> +	u64 some[NR_PSI_RESOURCES] = { 0, };
> +	u64 full[NR_PSI_RESOURCES] = { 0, };

> +		some[r] /= max(nonidle_total, 1UL);
> +		full[r] /= max(nonidle_total, 1UL);

That's a bare 64bit divide.. that typically failed to build on 32bit
archs.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 6/7] psi: pressure stall information for CPU, memory, and IO
       [not found] ` <20180507210135.1823-7-hannes@cmpxchg.org>
                     ` (2 preceding siblings ...)
  2018-05-09 10:05   ` Peter Zijlstra
@ 2018-05-09 10:14   ` Peter Zijlstra
  2018-05-09 10:21   ` Peter Zijlstra
                     ` (5 subsequent siblings)
  9 siblings, 0 replies; 20+ messages in thread
From: Peter Zijlstra @ 2018-05-09 10:14 UTC (permalink / raw)
  To: Johannes Weiner
  Cc: linux-kernel, linux-mm, linux-block, cgroups, Ingo Molnar,
	Andrew Morton, Tejun Heo, Balbir Singh, Mike Galbraith,
	Oliver Yang, Shakeel Butt, xxx xxx, Taras Kondratiuk,
	Daniel Walker, Vinayak Menon, Ruslan Ruslichenko, kernel-team

On Mon, May 07, 2018 at 05:01:34PM -0400, Johannes Weiner wrote:
> diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
> index 15750c222ca2..1658477466d5 100644
> --- a/kernel/sched/sched.h
> +++ b/kernel/sched/sched.h

> @@ -919,6 +921,8 @@ DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
>  #define cpu_curr(cpu)		(cpu_rq(cpu)->curr)
>  #define raw_rq()		raw_cpu_ptr(&runqueues)
>  
> +extern void update_rq_clock(struct rq *rq);
> +
>  static inline u64 __rq_clock_broken(struct rq *rq)
>  {
>  	return READ_ONCE(rq->clock);
> @@ -1037,6 +1041,86 @@ static inline void rq_repin_lock(struct rq *rq, struct rq_flags *rf)
>  #endif
>  }
>  
> +struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf)
> +	__acquires(rq->lock);
> +
> +struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf)
> +	__acquires(p->pi_lock)
> +	__acquires(rq->lock);
> +
> +static inline void __task_rq_unlock(struct rq *rq, struct rq_flags *rf)
> +	__releases(rq->lock)
> +{
> +	rq_unpin_lock(rq, rf);
> +	raw_spin_unlock(&rq->lock);
> +}
> +
> +static inline void
> +task_rq_unlock(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
> +	__releases(rq->lock)
> +	__releases(p->pi_lock)
> +{
> +	rq_unpin_lock(rq, rf);
> +	raw_spin_unlock(&rq->lock);
> +	raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags);
> +}
> +
> +static inline void
> +rq_lock_irqsave(struct rq *rq, struct rq_flags *rf)
> +	__acquires(rq->lock)
> +{
> +	raw_spin_lock_irqsave(&rq->lock, rf->flags);
> +	rq_pin_lock(rq, rf);
> +}
> +
> +static inline void
> +rq_lock_irq(struct rq *rq, struct rq_flags *rf)
> +	__acquires(rq->lock)
> +{
> +	raw_spin_lock_irq(&rq->lock);
> +	rq_pin_lock(rq, rf);
> +}
> +
> +static inline void
> +rq_lock(struct rq *rq, struct rq_flags *rf)
> +	__acquires(rq->lock)
> +{
> +	raw_spin_lock(&rq->lock);
> +	rq_pin_lock(rq, rf);
> +}
> +
> +static inline void
> +rq_relock(struct rq *rq, struct rq_flags *rf)
> +	__acquires(rq->lock)
> +{
> +	raw_spin_lock(&rq->lock);
> +	rq_repin_lock(rq, rf);
> +}
> +
> +static inline void
> +rq_unlock_irqrestore(struct rq *rq, struct rq_flags *rf)
> +	__releases(rq->lock)
> +{
> +	rq_unpin_lock(rq, rf);
> +	raw_spin_unlock_irqrestore(&rq->lock, rf->flags);
> +}
> +
> +static inline void
> +rq_unlock_irq(struct rq *rq, struct rq_flags *rf)
> +	__releases(rq->lock)
> +{
> +	rq_unpin_lock(rq, rf);
> +	raw_spin_unlock_irq(&rq->lock);
> +}
> +
> +static inline void
> +rq_unlock(struct rq *rq, struct rq_flags *rf)
> +	__releases(rq->lock)
> +{
> +	rq_unpin_lock(rq, rf);
> +	raw_spin_unlock(&rq->lock);
> +}
> +
>  #ifdef CONFIG_NUMA
>  enum numa_topology_type {
>  	NUMA_DIRECT,
> @@ -1670,8 +1754,6 @@ static inline void sub_nr_running(struct rq *rq, unsigned count)
>  	sched_update_tick_dependency(rq);
>  }
>  
> -extern void update_rq_clock(struct rq *rq);
> -
>  extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
>  extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
>  
> @@ -1752,86 +1834,6 @@ static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) { }
>  static inline void sched_avg_update(struct rq *rq) { }
>  #endif
>  
> -struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf)
> -	__acquires(rq->lock);
> -
> -struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf)
> -	__acquires(p->pi_lock)
> -	__acquires(rq->lock);
> -
> -static inline void __task_rq_unlock(struct rq *rq, struct rq_flags *rf)
> -	__releases(rq->lock)
> -{
> -	rq_unpin_lock(rq, rf);
> -	raw_spin_unlock(&rq->lock);
> -}
> -
> -static inline void
> -task_rq_unlock(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
> -	__releases(rq->lock)
> -	__releases(p->pi_lock)
> -{
> -	rq_unpin_lock(rq, rf);
> -	raw_spin_unlock(&rq->lock);
> -	raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags);
> -}
> -
> -static inline void
> -rq_lock_irqsave(struct rq *rq, struct rq_flags *rf)
> -	__acquires(rq->lock)
> -{
> -	raw_spin_lock_irqsave(&rq->lock, rf->flags);
> -	rq_pin_lock(rq, rf);
> -}
> -
> -static inline void
> -rq_lock_irq(struct rq *rq, struct rq_flags *rf)
> -	__acquires(rq->lock)
> -{
> -	raw_spin_lock_irq(&rq->lock);
> -	rq_pin_lock(rq, rf);
> -}
> -
> -static inline void
> -rq_lock(struct rq *rq, struct rq_flags *rf)
> -	__acquires(rq->lock)
> -{
> -	raw_spin_lock(&rq->lock);
> -	rq_pin_lock(rq, rf);
> -}
> -
> -static inline void
> -rq_relock(struct rq *rq, struct rq_flags *rf)
> -	__acquires(rq->lock)
> -{
> -	raw_spin_lock(&rq->lock);
> -	rq_repin_lock(rq, rf);
> -}
> -
> -static inline void
> -rq_unlock_irqrestore(struct rq *rq, struct rq_flags *rf)
> -	__releases(rq->lock)
> -{
> -	rq_unpin_lock(rq, rf);
> -	raw_spin_unlock_irqrestore(&rq->lock, rf->flags);
> -}
> -
> -static inline void
> -rq_unlock_irq(struct rq *rq, struct rq_flags *rf)
> -	__releases(rq->lock)
> -{
> -	rq_unpin_lock(rq, rf);
> -	raw_spin_unlock_irq(&rq->lock);
> -}
> -
> -static inline void
> -rq_unlock(struct rq *rq, struct rq_flags *rf)
> -	__releases(rq->lock)
> -{
> -	rq_unpin_lock(rq, rf);
> -	raw_spin_unlock(&rq->lock);
> -}
> -
>  #ifdef CONFIG_SMP
>  #ifdef CONFIG_PREEMPT
>  


What's all this churn about?

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 6/7] psi: pressure stall information for CPU, memory, and IO
       [not found] ` <20180507210135.1823-7-hannes@cmpxchg.org>
                     ` (3 preceding siblings ...)
  2018-05-09 10:14   ` Peter Zijlstra
@ 2018-05-09 10:21   ` Peter Zijlstra
  2018-05-09 10:26   ` Peter Zijlstra
                     ` (4 subsequent siblings)
  9 siblings, 0 replies; 20+ messages in thread
From: Peter Zijlstra @ 2018-05-09 10:21 UTC (permalink / raw)
  To: Johannes Weiner
  Cc: linux-kernel, linux-mm, linux-block, cgroups, Ingo Molnar,
	Andrew Morton, Tejun Heo, Balbir Singh, Mike Galbraith,
	Oliver Yang, Shakeel Butt, xxx xxx, Taras Kondratiuk,
	Daniel Walker, Vinayak Menon, Ruslan Ruslichenko, kernel-team

On Mon, May 07, 2018 at 05:01:34PM -0400, Johannes Weiner wrote:
> +/**
> + * psi_memstall_enter - mark the beginning of a memory stall section
> + * @flags: flags to handle nested sections
> + *
> + * Marks the calling task as being stalled due to a lack of memory,
> + * such as waiting for a refault or performing reclaim.
> + */
> +void psi_memstall_enter(unsigned long *flags)
> +{
> +	struct rq_flags rf;
> +	struct rq *rq;
> +
> +	*flags = current->flags & PF_MEMSTALL;
> +	if (*flags)
> +		return;
> +	/*
> +	 * PF_MEMSTALL setting & accounting needs to be atomic wrt
> +	 * changes to the task's scheduling state, otherwise we can
> +	 * race with CPU migration.
> +	 */
> +	local_irq_disable();
> +	rq = this_rq();
> +	raw_spin_lock(&rq->lock);
> +	rq_pin_lock(rq, &rf);

Given that churn in sched.h, you seen rq_lock() and friends.

Either write this like:

	local_irq_disable();
	rq = this_rq();
	rq_lock(rq, &rf);

Or instroduce "rq = this_rq_lock_irq()", which we could also use in
do_sched_yield().

> +	update_rq_clock(rq);
> +
> +	current->flags |= PF_MEMSTALL;
> +	psi_task_change(current, rq_clock(rq), 0, TSK_MEMSTALL);
> +
> +	rq_unpin_lock(rq, &rf);
> +	raw_spin_unlock(&rq->lock);
> +	local_irq_enable();

That's called rq_unlock_irq().

> +}
> +
> +/**
> + * psi_memstall_leave - mark the end of an memory stall section
> + * @flags: flags to handle nested memdelay sections
> + *
> + * Marks the calling task as no longer stalled due to lack of memory.
> + */
> +void psi_memstall_leave(unsigned long *flags)
> +{
> +	struct rq_flags rf;
> +	struct rq *rq;
> +
> +	if (*flags)
> +		return;
> +	/*
> +	 * PF_MEMSTALL clearing & accounting needs to be atomic wrt
> +	 * changes to the task's scheduling state, otherwise we could
> +	 * race with CPU migration.
> +	 */
> +	local_irq_disable();
> +	rq = this_rq();
> +	raw_spin_lock(&rq->lock);
> +	rq_pin_lock(rq, &rf);
> +
> +	update_rq_clock(rq);
> +
> +	current->flags &= ~PF_MEMSTALL;
> +	psi_task_change(current, rq_clock(rq), TSK_MEMSTALL, 0);
> +
> +	rq_unpin_lock(rq, &rf);
> +	raw_spin_unlock(&rq->lock);
> +	local_irq_enable();
> +}

Idem.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 6/7] psi: pressure stall information for CPU, memory, and IO
       [not found] ` <20180507210135.1823-7-hannes@cmpxchg.org>
                     ` (4 preceding siblings ...)
  2018-05-09 10:21   ` Peter Zijlstra
@ 2018-05-09 10:26   ` Peter Zijlstra
  2018-05-09 10:46   ` Peter Zijlstra
                     ` (3 subsequent siblings)
  9 siblings, 0 replies; 20+ messages in thread
From: Peter Zijlstra @ 2018-05-09 10:26 UTC (permalink / raw)
  To: Johannes Weiner
  Cc: linux-kernel, linux-mm, linux-block, cgroups, Ingo Molnar,
	Andrew Morton, Tejun Heo, Balbir Singh, Mike Galbraith,
	Oliver Yang, Shakeel Butt, xxx xxx, Taras Kondratiuk,
	Daniel Walker, Vinayak Menon, Ruslan Ruslichenko, kernel-team

On Mon, May 07, 2018 at 05:01:34PM -0400, Johannes Weiner wrote:
> +static void psi_clock(struct work_struct *work)
> +{

> +	dwork = to_delayed_work(work);
> +	group = container_of(dwork, struct psi_group, clock_work);
> +

> +
> +	/* Keep the clock ticking only when there is action */
> +	if (nonidle_total)
> +		schedule_delayed_work(dwork, MY_LOAD_FREQ);
> +}

Note that this doesn't generate a stable frequency for the callback.
The (nondeterministic) time spend doing the actual work is added to each
period, this gives an unconditional downward bias to the frequency, but
also makes it very unstable.

You want explicit management of timer->expires, and add MY_LOAD_FREQ
(which is a misnomer) to it and not reset it based on jiffies.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 6/7] psi: pressure stall information for CPU, memory, and IO
       [not found] ` <20180507210135.1823-7-hannes@cmpxchg.org>
                     ` (5 preceding siblings ...)
  2018-05-09 10:26   ` Peter Zijlstra
@ 2018-05-09 10:46   ` Peter Zijlstra
  2018-05-09 11:38     ` Peter Zijlstra
  2018-05-09 10:55   ` Peter Zijlstra
                     ` (2 subsequent siblings)
  9 siblings, 1 reply; 20+ messages in thread
From: Peter Zijlstra @ 2018-05-09 10:46 UTC (permalink / raw)
  To: Johannes Weiner
  Cc: linux-kernel, linux-mm, linux-block, cgroups, Ingo Molnar,
	Andrew Morton, Tejun Heo, Balbir Singh, Mike Galbraith,
	Oliver Yang, Shakeel Butt, xxx xxx, Taras Kondratiuk,
	Daniel Walker, Vinayak Menon, Ruslan Ruslichenko, kernel-team

On Mon, May 07, 2018 at 05:01:34PM -0400, Johannes Weiner wrote:

> @@ -2038,6 +2038,7 @@ try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
>  	cpu = select_task_rq(p, p->wake_cpu, SD_BALANCE_WAKE, wake_flags);
>  	if (task_cpu(p) != cpu) {
>  		wake_flags |= WF_MIGRATED;
> +		psi_ttwu_dequeue(p);
>  		set_task_cpu(p, cpu);
>  	}
>  

> +static inline void psi_ttwu_dequeue(struct task_struct *p)
> +{
> +	/*
> +	 * Is the task being migrated during a wakeup? Make sure to
> +	 * deregister its sleep-persistent psi states from the old
> +	 * queue, and let psi_enqueue() know it has to requeue.
> +	 */
> +	if (unlikely(p->in_iowait || (p->flags & PF_MEMSTALL))) {
> +		struct rq_flags rf;
> +		struct rq *rq;
> +		int clear = 0;
> +
> +		if (p->in_iowait)
> +			clear |= TSK_IOWAIT;
> +		if (p->flags & PF_MEMSTALL)
> +			clear |= TSK_MEMSTALL;
> +
> +		rq = __task_rq_lock(p, &rf);
> +		update_rq_clock(rq);
> +		psi_task_change(p, rq_clock(rq), clear, 0);
> +		p->sched_psi_wake_requeue = 1;
> +		__task_rq_unlock(rq, &rf);
> +	}
> +}

Yeah, no... not happening.

We spend a lot of time to never touch the old rq->lock on wakeups. Mason
was the one pushing for that, so he should very well know this.

The one cross-cpu atomic (iowait) is already a problem (the whole iowait
accounting being useless makes it even worse), adding significant remote
prodding is just really bad.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 6/7] psi: pressure stall information for CPU, memory, and IO
       [not found] ` <20180507210135.1823-7-hannes@cmpxchg.org>
                     ` (6 preceding siblings ...)
  2018-05-09 10:46   ` Peter Zijlstra
@ 2018-05-09 10:55   ` Peter Zijlstra
  2018-05-09 11:03   ` Vinayak Menon
  2018-06-07  0:46   ` Suren Baghdasaryan
  9 siblings, 0 replies; 20+ messages in thread
From: Peter Zijlstra @ 2018-05-09 10:55 UTC (permalink / raw)
  To: Johannes Weiner
  Cc: linux-kernel, linux-mm, linux-block, cgroups, Ingo Molnar,
	Andrew Morton, Tejun Heo, Balbir Singh, Mike Galbraith,
	Oliver Yang, Shakeel Butt, xxx xxx, Taras Kondratiuk,
	Daniel Walker, Vinayak Menon, Ruslan Ruslichenko, kernel-team

On Mon, May 07, 2018 at 05:01:34PM -0400, Johannes Weiner wrote:

> @@ -28,10 +28,14 @@ static inline int sched_info_on(void)
>  	return 1;
>  #elif defined(CONFIG_TASK_DELAY_ACCT)
>  	extern int delayacct_on;
> -	return delayacct_on;
> -#else
> -	return 0;
> +	if (delayacct_on)
> +		return 1;
> +#elif defined(CONFIG_PSI)
> +	extern int psi_disabled;
> +	if (!psi_disabled)
> +		return 1;
>  #endif
> +	return 0;
>  }

> diff --git a/kernel/sched/stats.h b/kernel/sched/stats.h
> index 8aea199a39b4..cb4a68bcf37a 100644
> --- a/kernel/sched/stats.h
> +++ b/kernel/sched/stats.h
> @@ -55,12 +55,90 @@ static inline void rq_sched_info_depart  (struct rq *rq, unsigned long long delt
>  # define   schedstat_val_or_zero(var)	0
>  #endif /* CONFIG_SCHEDSTATS */
>  
> +#ifdef CONFIG_PSI
> +/*
> + * PSI tracks state that persists across sleeps, such as iowaits and
> + * memory stalls. As a result, it has to distinguish between sleeps,
> + * where a task's runnable state changes, and requeues, where a task
> + * and its state are being moved between CPUs and runqueues.
> + */
> +static inline void psi_enqueue(struct task_struct *p, u64 now)
> +{
> +	int clear = 0, set = TSK_RUNNING;
> +
> +	if (p->state == TASK_RUNNING || p->sched_psi_wake_requeue) {
> +		if (p->flags & PF_MEMSTALL)
> +			set |= TSK_MEMSTALL;
> +		p->sched_psi_wake_requeue = 0;
> +	} else {
> +		if (p->in_iowait)
> +			clear |= TSK_IOWAIT;
> +	}
> +
> +	psi_task_change(p, now, clear, set);
> +}
> +static inline void psi_dequeue(struct task_struct *p, u64 now)
> +{
> +	int clear = TSK_RUNNING, set = 0;
> +
> +	if (p->state == TASK_RUNNING) {
> +		if (p->flags & PF_MEMSTALL)
> +			clear |= TSK_MEMSTALL;
> +	} else {
> +		if (p->in_iowait)
> +			set |= TSK_IOWAIT;
> +	}
> +
> +	psi_task_change(p, now, clear, set);
> +}
> +static inline void psi_ttwu_dequeue(struct task_struct *p)
> +{
> +	/*
> +	 * Is the task being migrated during a wakeup? Make sure to
> +	 * deregister its sleep-persistent psi states from the old
> +	 * queue, and let psi_enqueue() know it has to requeue.
> +	 */
> +	if (unlikely(p->in_iowait || (p->flags & PF_MEMSTALL))) {
> +		struct rq_flags rf;
> +		struct rq *rq;
> +		int clear = 0;
> +
> +		if (p->in_iowait)
> +			clear |= TSK_IOWAIT;
> +		if (p->flags & PF_MEMSTALL)
> +			clear |= TSK_MEMSTALL;
> +
> +		rq = __task_rq_lock(p, &rf);
> +		update_rq_clock(rq);
> +		psi_task_change(p, rq_clock(rq), clear, 0);
> +		p->sched_psi_wake_requeue = 1;
> +		__task_rq_unlock(rq, &rf);
> +	}
> +}

That all seems to be missing psi_disabled tests.. Yes I know it's
burried down in psi_task_change() somewhere, but that's really (too)
late.

(also, you seem to be conserving whitespace; typically we have an empty
lines between functions)

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 6/7] psi: pressure stall information for CPU, memory, and IO
       [not found] ` <20180507210135.1823-7-hannes@cmpxchg.org>
                     ` (7 preceding siblings ...)
  2018-05-09 10:55   ` Peter Zijlstra
@ 2018-05-09 11:03   ` Vinayak Menon
  2018-05-23 13:17     ` Johannes Weiner
  2018-06-07  0:46   ` Suren Baghdasaryan
  9 siblings, 1 reply; 20+ messages in thread
From: Vinayak Menon @ 2018-05-09 11:03 UTC (permalink / raw)
  To: Johannes Weiner, linux-kernel, linux-mm, linux-block, cgroups
  Cc: Ingo Molnar, Peter Zijlstra, Andrew Morton, Tejun Heo,
	Balbir Singh, Mike Galbraith, Oliver Yang, Shakeel Butt, xxx xxx,
	Taras Kondratiuk, Daniel Walker, Ruslan Ruslichenko, kernel-team


On 5/8/2018 2:31 AM, Johannes Weiner wrote:
> +static void psi_group_update(struct psi_group *group, int cpu, u64 now,
> +			     unsigned int clear, unsigned int set)
> +{
> +	enum psi_state state = PSI_NONE;
> +	struct psi_group_cpu *groupc;
> +	unsigned int *tasks;
> +	unsigned int to, bo;
> +
> +	groupc = per_cpu_ptr(group->cpus, cpu);
> +	tasks = groupc->tasks;
> +
> +	/* Update task counts according to the set/clear bitmasks */
> +	for (to = 0; (bo = ffs(clear)); to += bo, clear >>= bo) {
> +		int idx = to + (bo - 1);
> +
> +		if (tasks[idx] == 0 && !psi_bug) {
> +			printk_deferred(KERN_ERR "psi: task underflow! cpu=%d idx=%d tasks=[%u %u %u %u]\n",
> +					cpu, idx, tasks[0], tasks[1],
> +					tasks[2], tasks[3]);
> +			psi_bug = 1;
> +		}
> +		tasks[idx]--;
> +	}
> +	for (to = 0; (bo = ffs(set)); to += bo, set >>= bo)
> +		tasks[to + (bo - 1)]++;
> +
> +	/* Time in which tasks wait for the CPU */
> +	state = PSI_NONE;
> +	if (tasks[NR_RUNNING] > 1)
> +		state = PSI_SOME;
> +	time_state(&groupc->res[PSI_CPU], state, now);
> +
> +	/* Time in which tasks wait for memory */
> +	state = PSI_NONE;
> +	if (tasks[NR_MEMSTALL]) {
> +		if (!tasks[NR_RUNNING] ||
> +		    (cpu_curr(cpu)->flags & PF_MEMSTALL))
> +			state = PSI_FULL;
> +		else
> +			state = PSI_SOME;
> +	}
> +	time_state(&groupc->res[PSI_MEM], state, now);
> +
> +	/* Time in which tasks wait for IO */
> +	state = PSI_NONE;
> +	if (tasks[NR_IOWAIT]) {
> +		if (!tasks[NR_RUNNING])
> +			state = PSI_FULL;
> +		else
> +			state = PSI_SOME;
> +	}
> +	time_state(&groupc->res[PSI_IO], state, now);
> +
> +	/* Time in which tasks are non-idle, to weigh the CPU in summaries */
> +	if (groupc->nonidle)
> +		groupc->nonidle_time += now - groupc->nonidle_start;
> +	groupc->nonidle = tasks[NR_RUNNING] ||
> +		tasks[NR_IOWAIT] || tasks[NR_MEMSTALL];
> +	if (groupc->nonidle)
> +		groupc->nonidle_start = now;
> +
> +	/* Kick the stats aggregation worker if it's gone to sleep */
> +	if (!delayed_work_pending(&group->clock_work))

This causes a crash when the work is scheduled before system_wq is up. In my case when the first
schedule was called from kthreadd. And I had to do this to make it work.
if (keventd_up() && !delayed_work_pending(&group->clock_work))

> +		schedule_delayed_work(&group->clock_work, MY_LOAD_FREQ);
> +}
> +
> +void psi_task_change(struct task_struct *task, u64 now, int clear, int set)
> +{
> +	struct cgroup *cgroup, *parent;

unused variables

Thanks,
Vinayak

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 7/7] psi: cgroup support
       [not found] ` <20180507210135.1823-8-hannes@cmpxchg.org>
@ 2018-05-09 11:07   ` Peter Zijlstra
  0 siblings, 0 replies; 20+ messages in thread
From: Peter Zijlstra @ 2018-05-09 11:07 UTC (permalink / raw)
  To: Johannes Weiner
  Cc: linux-kernel, linux-mm, linux-block, cgroups, Ingo Molnar,
	Andrew Morton, Tejun Heo, Balbir Singh, Mike Galbraith,
	Oliver Yang, Shakeel Butt, xxx xxx, Taras Kondratiuk,
	Daniel Walker, Vinayak Menon, Ruslan Ruslichenko, kernel-team

On Mon, May 07, 2018 at 05:01:35PM -0400, Johannes Weiner wrote:
> --- a/kernel/sched/psi.c
> +++ b/kernel/sched/psi.c
> @@ -260,6 +260,18 @@ void psi_task_change(struct task_struct *task, u64 now, int clear, int set)
>  	task->psi_flags |= set;
>  
>  	psi_group_update(&psi_system, cpu, now, clear, set);
> +
> +#ifdef CONFIG_CGROUPS
> +       cgroup = task->cgroups->dfl_cgrp;
> +       while (cgroup && (parent = cgroup_parent(cgroup))) {
> +               struct psi_group *group;
> +
> +               group = cgroup_psi(cgroup);
> +               psi_group_update(group, cpu, now, clear, set);
> +
> +               cgroup = parent;
> +       }
> +#endif
>  }

TJ fixed needing that for stats at some point, why can't you do the
same?

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 6/7] psi: pressure stall information for CPU, memory, and IO
  2018-05-09 10:46   ` Peter Zijlstra
@ 2018-05-09 11:38     ` Peter Zijlstra
  0 siblings, 0 replies; 20+ messages in thread
From: Peter Zijlstra @ 2018-05-09 11:38 UTC (permalink / raw)
  To: Johannes Weiner
  Cc: linux-kernel, linux-mm, linux-block, cgroups, Ingo Molnar,
	Andrew Morton, Tejun Heo, Balbir Singh, Mike Galbraith,
	Oliver Yang, Shakeel Butt, xxx xxx, Taras Kondratiuk,
	Daniel Walker, Vinayak Menon, Ruslan Ruslichenko, kernel-team

On Wed, May 09, 2018 at 12:46:18PM +0200, Peter Zijlstra wrote:
> On Mon, May 07, 2018 at 05:01:34PM -0400, Johannes Weiner wrote:
> 
> > @@ -2038,6 +2038,7 @@ try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
> >  	cpu = select_task_rq(p, p->wake_cpu, SD_BALANCE_WAKE, wake_flags);
> >  	if (task_cpu(p) != cpu) {
> >  		wake_flags |= WF_MIGRATED;
> > +		psi_ttwu_dequeue(p);
> >  		set_task_cpu(p, cpu);
> >  	}
> >  
> 
> > +static inline void psi_ttwu_dequeue(struct task_struct *p)
> > +{
> > +	/*
> > +	 * Is the task being migrated during a wakeup? Make sure to
> > +	 * deregister its sleep-persistent psi states from the old
> > +	 * queue, and let psi_enqueue() know it has to requeue.
> > +	 */
> > +	if (unlikely(p->in_iowait || (p->flags & PF_MEMSTALL))) {
> > +		struct rq_flags rf;
> > +		struct rq *rq;
> > +		int clear = 0;
> > +
> > +		if (p->in_iowait)
> > +			clear |= TSK_IOWAIT;
> > +		if (p->flags & PF_MEMSTALL)
> > +			clear |= TSK_MEMSTALL;
> > +
> > +		rq = __task_rq_lock(p, &rf);
> > +		update_rq_clock(rq);
> > +		psi_task_change(p, rq_clock(rq), clear, 0);
> > +		p->sched_psi_wake_requeue = 1;
> > +		__task_rq_unlock(rq, &rf);
> > +	}
> > +}
> 
> Yeah, no... not happening.
> 
> We spend a lot of time to never touch the old rq->lock on wakeups. Mason
> was the one pushing for that, so he should very well know this.
> 
> The one cross-cpu atomic (iowait) is already a problem (the whole iowait
> accounting being useless makes it even worse), adding significant remote
> prodding is just really bad.

Also, since all you need is the global number, I don't think you
actually need any of this. See what we do for nr_uninterruptible.

In general I think you want to (re)read loadavg.c some more, and maybe
reuse a bit more of that.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 0/7] psi: pressure stall information for CPU, memory, and IO
       [not found] <20180507210135.1823-1-hannes@cmpxchg.org>
                   ` (2 preceding siblings ...)
       [not found] ` <20180507210135.1823-8-hannes@cmpxchg.org>
@ 2018-05-14 15:39 ` Christopher Lameter
  2018-05-14 17:35   ` Bart Van Assche
  2018-05-14 18:55   ` Johannes Weiner
       [not found] ` <CAJuCfpF4q+1aSg4WQn_p-1-zEDhh-iqST6dc1DkxnDofSPBKGw@mail.gmail.com>
  4 siblings, 2 replies; 20+ messages in thread
From: Christopher Lameter @ 2018-05-14 15:39 UTC (permalink / raw)
  To: Johannes Weiner
  Cc: linux-kernel, linux-mm, linux-block, cgroups, Ingo Molnar,
	Peter Zijlstra, Andrew Morton, Tejun Heo, Balbir Singh,
	Mike Galbraith, Oliver Yang, Shakeel Butt, xxx xxx,
	Taras Kondratiuk, Daniel Walker, Vinayak Menon,
	Ruslan Ruslichenko, kernel-team

On Mon, 7 May 2018, Johannes Weiner wrote:

> What to make of this number? If CPU utilization is at 100% and CPU
> pressure is 0, it means the system is perfectly utilized, with one
> runnable thread per CPU and nobody waiting. At two or more runnable
> tasks per CPU, the system is 100% overcommitted and the pressure
> average will indicate as much. From a utilization perspective this is
> a great state of course: no CPU cycles are being wasted, even when 50%
> of the threads were to go idle (and most workloads do vary). From the
> perspective of the individual job it's not great, however, and they
> might do better with more resources. Depending on what your priority
> is, an elevated "some" number may or may not require action.

This looks awfully similar to loadavg. Problem is that loadavg gets
screwed up by tasks blocked waiting for I/O. Isnt there some way to fix
loadavg instead?

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 0/7] psi: pressure stall information for CPU, memory, and IO
  2018-05-14 15:39 ` [PATCH 0/7] psi: pressure stall information for CPU, memory, and IO Christopher Lameter
@ 2018-05-14 17:35   ` Bart Van Assche
  2018-05-14 18:55   ` Johannes Weiner
  1 sibling, 0 replies; 20+ messages in thread
From: Bart Van Assche @ 2018-05-14 17:35 UTC (permalink / raw)
  To: Christopher Lameter, Johannes Weiner
  Cc: linux-kernel, linux-mm, linux-block, cgroups, Ingo Molnar,
	Peter Zijlstra, Andrew Morton, Tejun Heo, Balbir Singh,
	Mike Galbraith, Oliver Yang, Shakeel Butt, xxx xxx,
	Taras Kondratiuk, Daniel Walker, Vinayak Menon,
	Ruslan Ruslichenko, kernel-team

On 05/14/18 08:39, Christopher Lameter wrote:
> On Mon, 7 May 2018, Johannes Weiner wrote:
>> What to make of this number? If CPU utilization is at 100% and CPU
>> pressure is 0, it means the system is perfectly utilized, with one
>> runnable thread per CPU and nobody waiting. At two or more runnable
>> tasks per CPU, the system is 100% overcommitted and the pressure
>> average will indicate as much. From a utilization perspective this is
>> a great state of course: no CPU cycles are being wasted, even when 50%
>> of the threads were to go idle (and most workloads do vary). From the
>> perspective of the individual job it's not great, however, and they
>> might do better with more resources. Depending on what your priority
>> is, an elevated "some" number may or may not require action.
> 
> This looks awfully similar to loadavg. Problem is that loadavg gets
> screwed up by tasks blocked waiting for I/O. Isnt there some way to fix
> loadavg instead?

The following article explains why it probably made sense in 1993 to 
include TASK_UNINTERRUPTIBLE in loadavg and also why this no longer 
makes sense today:

http://www.brendangregg.com/blog/2017-08-08/linux-load-averages.html

Bart.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 0/7] psi: pressure stall information for CPU, memory, and IO
  2018-05-14 15:39 ` [PATCH 0/7] psi: pressure stall information for CPU, memory, and IO Christopher Lameter
  2018-05-14 17:35   ` Bart Van Assche
@ 2018-05-14 18:55   ` Johannes Weiner
  2018-05-14 20:15     ` Christopher Lameter
  1 sibling, 1 reply; 20+ messages in thread
From: Johannes Weiner @ 2018-05-14 18:55 UTC (permalink / raw)
  To: Christopher Lameter
  Cc: linux-kernel, linux-mm, linux-block, cgroups, Ingo Molnar,
	Peter Zijlstra, Andrew Morton, Tejun Heo, Balbir Singh,
	Mike Galbraith, Oliver Yang, Shakeel Butt, xxx xxx,
	Taras Kondratiuk, Daniel Walker, Vinayak Menon,
	Ruslan Ruslichenko, kernel-team

On Mon, May 14, 2018 at 03:39:33PM +0000, Christopher Lameter wrote:
> On Mon, 7 May 2018, Johannes Weiner wrote:
> 
> > What to make of this number? If CPU utilization is at 100% and CPU
> > pressure is 0, it means the system is perfectly utilized, with one
> > runnable thread per CPU and nobody waiting. At two or more runnable
> > tasks per CPU, the system is 100% overcommitted and the pressure
> > average will indicate as much. From a utilization perspective this is
> > a great state of course: no CPU cycles are being wasted, even when 50%
> > of the threads were to go idle (and most workloads do vary). From the
> > perspective of the individual job it's not great, however, and they
> > might do better with more resources. Depending on what your priority
> > is, an elevated "some" number may or may not require action.
> 
> This looks awfully similar to loadavg. Problem is that loadavg gets
> screwed up by tasks blocked waiting for I/O. Isnt there some way to fix
> loadavg instead?

Counting iowaiting tasks is one thing, but there are a few more things
that make it hard to use for telling the impact of CPU competition:

- It's not normalized to available CPU count. The loadavg in isolation
  doesn't mean anything, and you have to know the number of CPUs and
  any CPU bindings / restrictions in effect, which presents at least
  some difficulty when monitoring a big heterogeneous fleet.

- The way it's sampled makes it impossible to use for latencies. You
  could be mostly idle but periodically have herds of tasks competing
  for the CPU for short, low-latency operations. Even if we changed
  this in the implementation, you're still stuck with the interface
  that has...

- ...a short-term load window of 1m. This is generally fairly coarse
  for something that can be loaded and unloaded as abruptly as the CPU

I'm trying to fix these with a portable way of aggregating multi-cpu
states, as well as tracking the true time spent in a state instead of
sampling it. Plus a smaller short-term window of 10s, but that's
almost irrelevant because I'm exporting the absolute state time clock
so you can calculate your own averages over any time window you want.

Since I'm using the same model and infrastructure for memory and IO
load as well, IMO it makes more sense to present them in a coherent
interface instead of trying to retrofit and change the loadavg file,
which might not even be possible.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 0/7] psi: pressure stall information for CPU, memory, and IO
  2018-05-14 18:55   ` Johannes Weiner
@ 2018-05-14 20:15     ` Christopher Lameter
  0 siblings, 0 replies; 20+ messages in thread
From: Christopher Lameter @ 2018-05-14 20:15 UTC (permalink / raw)
  To: Johannes Weiner
  Cc: linux-kernel, linux-mm, linux-block, cgroups, Ingo Molnar,
	Peter Zijlstra, Andrew Morton, Tejun Heo, Balbir Singh,
	Mike Galbraith, Oliver Yang, Shakeel Butt, xxx xxx,
	Taras Kondratiuk, Daniel Walker, Vinayak Menon,
	Ruslan Ruslichenko, kernel-team

On Mon, 14 May 2018, Johannes Weiner wrote:

> Since I'm using the same model and infrastructure for memory and IO
> load as well, IMO it makes more sense to present them in a coherent
> interface instead of trying to retrofit and change the loadavg file,
> which might not even be possible.

Well I keep looking at the loadavg output from numerous tools and then in
my mind I divide by the number of processors, guess if any of the threads
would be doing I/O and if I cannot figure that out groan and run "vmstat"
for awhile to figure that out.

Lets have some numbers there that make more sense please.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 6/7] psi: pressure stall information for CPU, memory, and IO
  2018-05-09 11:03   ` Vinayak Menon
@ 2018-05-23 13:17     ` Johannes Weiner
  0 siblings, 0 replies; 20+ messages in thread
From: Johannes Weiner @ 2018-05-23 13:17 UTC (permalink / raw)
  To: Vinayak Menon
  Cc: linux-kernel, linux-mm, linux-block, cgroups, Ingo Molnar,
	Peter Zijlstra, Andrew Morton, Tejun Heo, Balbir Singh,
	Mike Galbraith, Oliver Yang, Shakeel Butt, xxx xxx,
	Taras Kondratiuk, Daniel Walker, Ruslan Ruslichenko, kernel-team

On Wed, May 09, 2018 at 04:33:24PM +0530, Vinayak Menon wrote:
> On 5/8/2018 2:31 AM, Johannes Weiner wrote:
> > +	/* Kick the stats aggregation worker if it's gone to sleep */
> > +	if (!delayed_work_pending(&group->clock_work))
> 
> This causes a crash when the work is scheduled before system_wq is up. In my case when the first
> schedule was called from kthreadd. And I had to do this to make it work.
> if (keventd_up() && !delayed_work_pending(&group->clock_work))
>
> > +		schedule_delayed_work(&group->clock_work, MY_LOAD_FREQ);

I was trying to figure out how this is possible, and it didn't make
sense because we do initialize the system_wq way before kthreadd.

Did you by any chance backport this to a pre-4.10 kernel which does
not have 3347fa092821 ("workqueue: make workqueue available early
during boot") yet?

> > +void psi_task_change(struct task_struct *task, u64 now, int clear, int set)
> > +{
> > +	struct cgroup *cgroup, *parent;
> 
> unused variables

They're used in the next patch, I'll fix that up.

Thanks

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 0/7] psi: pressure stall information for CPU, memory, and IO
       [not found] ` <CAJuCfpF4q+1aSg4WQn_p-1-zEDhh-iqST6dc1DkxnDofSPBKGw@mail.gmail.com>
@ 2018-05-29 18:16   ` Johannes Weiner
  2018-05-30 23:32     ` Suren Baghdasaryan
  0 siblings, 1 reply; 20+ messages in thread
From: Johannes Weiner @ 2018-05-29 18:16 UTC (permalink / raw)
  To: Suren Baghdasaryan
  Cc: linux-kernel, linux-mm, linux-block, cgroups, Ingo Molnar,
	Peter Zijlstra, Andrew Morton, Tejun Heo, Balbir Singh,
	Mike Galbraith, Oliver Yang, Shakeel Butt, xxx xxx,
	Taras Kondratiuk, Daniel Walker, Vinayak Menon,
	Ruslan Ruslichenko, kernel-team

Hi Suren,

On Fri, May 25, 2018 at 05:29:30PM -0700, Suren Baghdasaryan wrote:
> Hi Johannes,
> I tried your previous memdelay patches before this new set was posted
> and results were promising for predicting when Android system is close
> to OOM. I'm definitely going to try this one after I backport it to
> 4.9.

I'm happy to hear that!

> Would it make sense to split CONFIG_PSI into CONFIG_PSI_CPU,
> CONFIG_PSI_MEM and CONFIG_PSI_IO since one might need only specific
> subset of this feature?

Yes, that should be doable. I'll split them out in the next version.

> > The total= value gives the absolute stall time in microseconds. This
> > allows detecting latency spikes that might be too short to sway the
> > running averages. It also allows custom time averaging in case the
> > 10s/1m/5m windows aren't adequate for the usecase (or are too coarse
> > with future hardware).
> 
> Any reasons these specific windows were chosen (empirical
> data/historical reasons)? I'm worried that with the smallest window
> being 10s the signal might be too inert to detect fast memory pressure
> buildup before OOM kill happens. I'll have to experiment with that
> first, however if you have some insights into this already please
> share them.

They were chosen empirically. We started out with the loadavg window
sizes, but had to reduce them for exactly the reason you mention -
they're way too coarse to detect acute pressure buildup.

10s has been working well for us. We could make it smaller, but there
is some worry that we don't have enough samples then and the average
becomes too erratic - whereas monitoring total= directly would allow
you to detect accute spikes and handle this erraticness explicitly.

Let me know how it works out in your tests.

Thanks for your feedback.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 0/7] psi: pressure stall information for CPU, memory, and IO
  2018-05-29 18:16   ` Johannes Weiner
@ 2018-05-30 23:32     ` Suren Baghdasaryan
  0 siblings, 0 replies; 20+ messages in thread
From: Suren Baghdasaryan @ 2018-05-30 23:32 UTC (permalink / raw)
  To: Johannes Weiner
  Cc: linux-kernel, linux-mm, linux-block, cgroups, Ingo Molnar,
	Peter Zijlstra, Andrew Morton, Tejun Heo, Balbir Singh,
	Mike Galbraith, Oliver Yang, Shakeel Butt, xxx xxx,
	Taras Kondratiuk, Daniel Walker, Vinayak Menon,
	Ruslan Ruslichenko, kernel-team

On Tue, May 29, 2018 at 11:16 AM, Johannes Weiner <hannes@cmpxchg.org> wrote:
> Hi Suren,
>
> On Fri, May 25, 2018 at 05:29:30PM -0700, Suren Baghdasaryan wrote:
>> Hi Johannes,
>> I tried your previous memdelay patches before this new set was posted
>> and results were promising for predicting when Android system is close
>> to OOM. I'm definitely going to try this one after I backport it to
>> 4.9.
>
> I'm happy to hear that!
>
>> Would it make sense to split CONFIG_PSI into CONFIG_PSI_CPU,
>> CONFIG_PSI_MEM and CONFIG_PSI_IO since one might need only specific
>> subset of this feature?
>
> Yes, that should be doable. I'll split them out in the next version.
>
>> > The total= value gives the absolute stall time in microseconds. This
>> > allows detecting latency spikes that might be too short to sway the
>> > running averages. It also allows custom time averaging in case the
>> > 10s/1m/5m windows aren't adequate for the usecase (or are too coarse
>> > with future hardware).
>>
>> Any reasons these specific windows were chosen (empirical
>> data/historical reasons)? I'm worried that with the smallest window
>> being 10s the signal might be too inert to detect fast memory pressure
>> buildup before OOM kill happens. I'll have to experiment with that
>> first, however if you have some insights into this already please
>> share them.
>
> They were chosen empirically. We started out with the loadavg window
> sizes, but had to reduce them for exactly the reason you mention -
> they're way too coarse to detect acute pressure buildup.
>
> 10s has been working well for us. We could make it smaller, but there
> is some worry that we don't have enough samples then and the average
> becomes too erratic - whereas monitoring total= directly would allow
> you to detect accute spikes and handle this erraticness explicitly.

Unfortunately total= field is now updated only at 2sec intervals which
might be too late to react to mounting memory pressure. With previous
memdelay patchset md->aggregate which is reported as "total" was
calculated directly from inside memdelay_task_change, so it was always
up-to-date. Now group->some and group->full are updated from inside
psi_clock with up to 2sec delay. This prevents us from detecting these
acute pressure spikes immediately. I understand why you moved these
calculations out of the hot path but maybe we could keep updating
"total" inside psi_group_update? This would allow for custom averaging
and eliminate this delay for detecting spikes in the pressure signal.
More conceptually I would love to have a way to monitor the averages
at a slow rate and when they rise and cross some threshold to increase
the monitoring rate and react quickly in case they shoot up. Current
2sec delay poses a problem for doing that.

>
> Let me know how it works out in your tests.

I've done the backporting to 4.9 and running the tests but the 2sec
delay is problematic for getting a detailed look at the signal and its
usefulness. Thinking about workarounds if only for data collection but
don't want to deviate too much from your baseline. Would love to hear
from you if a good compromise can be reached here.

>
> Thanks for your feedback.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 6/7] psi: pressure stall information for CPU, memory, and IO
       [not found] ` <20180507210135.1823-7-hannes@cmpxchg.org>
                     ` (8 preceding siblings ...)
  2018-05-09 11:03   ` Vinayak Menon
@ 2018-06-07  0:46   ` Suren Baghdasaryan
  9 siblings, 0 replies; 20+ messages in thread
From: Suren Baghdasaryan @ 2018-06-07  0:46 UTC (permalink / raw)
  To: Johannes Weiner
  Cc: linux-kernel, linux-mm, linux-block, cgroups, Ingo Molnar,
	Peter Zijlstra, Andrew Morton, Tejun Heo, Balbir Singh,
	Mike Galbraith, Oliver Yang, Shakeel Butt, xxx xxx,
	Taras Kondratiuk, Daniel Walker, Vinayak Menon,
	Ruslan Ruslichenko, kernel-team

Hi Johannes,


On Mon, May 7, 2018 at 2:01 PM, Johannes Weiner <hannes@cmpxchg.org> wrote:
> +static void psi_clock(struct work_struct *work)
> +{
> +       u64 some[NR_PSI_RESOURCES] = { 0, };
> +       u64 full[NR_PSI_RESOURCES] = { 0, };
> +       unsigned long nonidle_total = 0;
> +       unsigned long missed_periods;
> +       struct delayed_work *dwork;
> +       struct psi_group *group;
> +       unsigned long expires;
> +       int cpu;
> +       int r;
> +
> +       dwork = to_delayed_work(work);
> +       group = container_of(dwork, struct psi_group, clock_work);
> +
> +       /*
> +        * Calculate the sampling period. The clock might have been
> +        * stopped for a while.
> +        */
> +       expires = group->period_expires;
> +       missed_periods = (jiffies - expires) / MY_LOAD_FREQ;
> +       group->period_expires = expires + ((1 + missed_periods) * MY_LOAD_FREQ);
> +
> +       /*
> +        * Aggregate the per-cpu state into a global state. Each CPU
> +        * is weighted by its non-idle time in the sampling period.
> +        */

Would it be possible to move this aggregation code (excluding
calc_avgs()) into a separate function which is called from here as
well as from psi_show() before group->some[] and group->full[] are
reported? This would not affect the performance if the information is
not requested and at the same time would keep at least the "total"
field up-to-date when the data is requested. For calc_avgs() I think
we would have to calculate the change in nonidle_total, group->some[]
and group->full[] fields differently because a call to psi_show() in
the middle of two psi_clock() calls would refresh these fields before
2secs expire, however calculating that change is trivial if we store
previous group->some[], group->full[] and nonidle_total values inside
psi_clock(). This would require new fields in psi_group struct to
store these previous values but the upside is that we would eliminate
the problem with reporting potentially stale data (up to 2sec update
delay) and provide a function one can use to refresh group->some[] and
group->full[] and implement custom averaging.

> +       for_each_online_cpu(cpu) {
> +               struct psi_group_cpu *groupc = per_cpu_ptr(group->cpus, cpu);
> +               unsigned long nonidle;
> +
> +               nonidle = nsecs_to_jiffies(groupc->nonidle_time);
> +               groupc->nonidle_time = 0;
> +               nonidle_total += nonidle;
> +
> +               for (r = 0; r < NR_PSI_RESOURCES; r++) {
> +                       struct psi_resource *res = &groupc->res[r];
> +
> +                       some[r] += (res->times[0] + res->times[1]) * nonidle;
> +                       full[r] += res->times[1] * nonidle;
> +
> +                       /* It's racy, but we can tolerate some error */
> +                       res->times[0] = 0;
> +                       res->times[1] = 0;
> +               }
> +       }
> +
> +       for (r = 0; r < NR_PSI_RESOURCES; r++) {
> +               /* Finish the weighted aggregation */
> +               some[r] /= max(nonidle_total, 1UL);
> +               full[r] /= max(nonidle_total, 1UL);
> +
> +               /* Accumulate stall time */
> +               group->some[r] += some[r];
> +               group->full[r] += full[r];
> +
> +               /* Calculate recent pressure averages */
> +               calc_avgs(group->avg_some[r], some[r], missed_periods);
> +               calc_avgs(group->avg_full[r], full[r], missed_periods);
> +       }
> +
> +       /* Keep the clock ticking only when there is action */
> +       if (nonidle_total)
> +               schedule_delayed_work(dwork, MY_LOAD_FREQ);
> +}
> +

Thanks,
Suren.

^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2018-06-07  0:46 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20180507210135.1823-1-hannes@cmpxchg.org>
     [not found] ` <20180507210135.1823-6-hannes@cmpxchg.org>
2018-05-09  9:49   ` [PATCH 5/7] sched: loadavg: make calc_load_n() public Peter Zijlstra
     [not found] ` <20180507210135.1823-7-hannes@cmpxchg.org>
2018-05-09  9:59   ` [PATCH 6/7] psi: pressure stall information for CPU, memory, and IO Peter Zijlstra
2018-05-09 10:04   ` Peter Zijlstra
2018-05-09 10:05   ` Peter Zijlstra
2018-05-09 10:14   ` Peter Zijlstra
2018-05-09 10:21   ` Peter Zijlstra
2018-05-09 10:26   ` Peter Zijlstra
2018-05-09 10:46   ` Peter Zijlstra
2018-05-09 11:38     ` Peter Zijlstra
2018-05-09 10:55   ` Peter Zijlstra
2018-05-09 11:03   ` Vinayak Menon
2018-05-23 13:17     ` Johannes Weiner
2018-06-07  0:46   ` Suren Baghdasaryan
     [not found] ` <20180507210135.1823-8-hannes@cmpxchg.org>
2018-05-09 11:07   ` [PATCH 7/7] psi: cgroup support Peter Zijlstra
2018-05-14 15:39 ` [PATCH 0/7] psi: pressure stall information for CPU, memory, and IO Christopher Lameter
2018-05-14 17:35   ` Bart Van Assche
2018-05-14 18:55   ` Johannes Weiner
2018-05-14 20:15     ` Christopher Lameter
     [not found] ` <CAJuCfpF4q+1aSg4WQn_p-1-zEDhh-iqST6dc1DkxnDofSPBKGw@mail.gmail.com>
2018-05-29 18:16   ` Johannes Weiner
2018-05-30 23:32     ` Suren Baghdasaryan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox