stats.c 3.2 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4
/*
 * /proc/schedstat implementation
 */
5 6 7
#include "sched.h"

/*
8 9 10
 * Current schedstat API version.
 *
 * Bump this up when changing the output format or the meaning of an existing
11 12 13 14 15 16 17 18
 * format, so that tools can adapt (or abort)
 */
#define SCHEDSTAT_VERSION 15

static int show_schedstat(struct seq_file *seq, void *v)
{
	int cpu;

19 20 21 22 23
	if (v == (void *)1) {
		seq_printf(seq, "version %d\n", SCHEDSTAT_VERSION);
		seq_printf(seq, "timestamp %lu\n", jiffies);
	} else {
		struct rq *rq;
24 25 26 27
#ifdef CONFIG_SMP
		struct sched_domain *sd;
		int dcount = 0;
#endif
28 29
		cpu = (unsigned long)(v - 2);
		rq = cpu_rq(cpu);
30 31 32

		/* runqueue-specific stats */
		seq_printf(seq,
R
Rakib Mullick 已提交
33
		    "cpu%d %u 0 %u %u %u %u %llu %llu %lu",
34
		    cpu, rq->yld_count,
R
Rakib Mullick 已提交
35
		    rq->sched_count, rq->sched_goidle,
36 37 38 39 40 41 42 43 44 45 46 47
		    rq->ttwu_count, rq->ttwu_local,
		    rq->rq_cpu_time,
		    rq->rq_sched_info.run_delay, rq->rq_sched_info.pcount);

		seq_printf(seq, "\n");

#ifdef CONFIG_SMP
		/* domain-specific stats */
		rcu_read_lock();
		for_each_domain(cpu, sd) {
			enum cpu_idle_type itype;

48 49
			seq_printf(seq, "domain%d %*pb", dcount++,
				   cpumask_pr_args(sched_domain_span(sd)));
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
			for (itype = CPU_IDLE; itype < CPU_MAX_IDLE_TYPES;
					itype++) {
				seq_printf(seq, " %u %u %u %u %u %u %u %u",
				    sd->lb_count[itype],
				    sd->lb_balanced[itype],
				    sd->lb_failed[itype],
				    sd->lb_imbalance[itype],
				    sd->lb_gained[itype],
				    sd->lb_hot_gained[itype],
				    sd->lb_nobusyq[itype],
				    sd->lb_nobusyg[itype]);
			}
			seq_printf(seq,
				   " %u %u %u %u %u %u %u %u %u %u %u %u\n",
			    sd->alb_count, sd->alb_failed, sd->alb_pushed,
			    sd->sbe_count, sd->sbe_balanced, sd->sbe_pushed,
			    sd->sbf_count, sd->sbf_balanced, sd->sbf_pushed,
			    sd->ttwu_wake_remote, sd->ttwu_move_affine,
			    sd->ttwu_move_balance);
		}
		rcu_read_unlock();
#endif
	}
	return 0;
}

76 77 78 79
/*
 * This itererator needs some explanation.
 * It returns 1 for the header position.
 * This means 2 is cpu 0.
80 81
 * In a hotplugged system some CPUs, including cpu 0, may be missing so we have
 * to use cpumask_* to iterate over the CPUs.
82 83
 */
static void *schedstat_start(struct seq_file *file, loff_t *offset)
84
{
85
	unsigned long n = *offset;
86

87 88 89 90 91 92 93 94 95 96 97 98 99 100
	if (n == 0)
		return (void *) 1;

	n--;

	if (n > 0)
		n = cpumask_next(n - 1, cpu_online_mask);
	else
		n = cpumask_first(cpu_online_mask);

	*offset = n + 1;

	if (n < nr_cpu_ids)
		return (void *)(unsigned long)(n + 2);
101

102 103 104 105 106 107
	return NULL;
}

static void *schedstat_next(struct seq_file *file, void *data, loff_t *offset)
{
	(*offset)++;
108

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
	return schedstat_start(file, offset);
}

static void schedstat_stop(struct seq_file *file, void *data)
{
}

static const struct seq_operations schedstat_sops = {
	.start = schedstat_start,
	.next  = schedstat_next,
	.stop  = schedstat_stop,
	.show  = show_schedstat,
};

static int schedstat_open(struct inode *inode, struct file *file)
{
	return seq_open(file, &schedstat_sops);
126 127 128 129 130 131
}

static const struct file_operations proc_schedstat_operations = {
	.open    = schedstat_open,
	.read    = seq_read,
	.llseek  = seq_lseek,
A
Al Viro 已提交
132
	.release = seq_release,
133 134 135 136 137
};

static int __init proc_schedstat_init(void)
{
	proc_create("schedstat", 0, NULL, &proc_schedstat_operations);
138

139 140
	return 0;
}
141
subsys_initcall(proc_schedstat_init);