loadavg.c 1.6 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/pid_namespace.h>
#include <linux/proc_fs.h>
#include <linux/sched.h>
7
#include <linux/sched/loadavg.h>
8
#include <linux/sched/stat.h>
9 10 11 12 13 14
#include <linux/seq_file.h>
#include <linux/seqlock.h>
#include <linux/time.h>

static int loadavg_proc_show(struct seq_file *m, void *v)
{
15
	unsigned long avnrun[3];
16

17
	get_avenrun(avnrun, FIXED_1/200, 0);
18

19 20 21 22
	seq_printf(m, "%lu.%02lu %lu.%02lu %lu.%02lu %ld/%d %d\n",
		LOAD_INT(avnrun[0]), LOAD_FRAC(avnrun[0]),
		LOAD_INT(avnrun[1]), LOAD_FRAC(avnrun[1]),
		LOAD_INT(avnrun[2]), LOAD_FRAC(avnrun[2]),
23
		nr_running(), nr_threads,
24
		idr_get_cursor(&task_active_pid_ns(current)->idr) - 1);
25 26 27
	return 0;
}

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
static inline void get_aven_stress(u64 *stress, u64 offset)
{
	stress[0] = stress_avg_total[0] + offset;
	stress[1] = stress_avg_total[1] + offset;
	stress[2] = stress_avg_total[2] + offset;
}

static int cpu_stress_proc_show(struct seq_file *m, void *v)
{
	u64 avn_stress[3];

	get_aven_stress(avn_stress, FIXED_1/200);

	seq_printf(m, "%llu.%02llu %llu.%02llu %llu.%02llu\n",
		LOAD_INT(avn_stress[0]), LOAD_FRAC(avn_stress[0]),
		LOAD_INT(avn_stress[1]), LOAD_FRAC(avn_stress[1]),
		LOAD_INT(avn_stress[2]), LOAD_FRAC(avn_stress[2]));

	return 0;
}

49 50
static int __init proc_loadavg_init(void)
{
51
	proc_create_single("loadavg", 0, NULL, loadavg_proc_show);
52 53
	return 0;
}
54
fs_initcall(proc_loadavg_init);
55 56 57 58 59 60 61 62 63 64

static int __init proc_cpu_stress_init(void)
{
	proc_create_single("cpu_stress", 0, NULL, cpu_stress_proc_show);

	/* sched_init is called earlier than init_timers so schedule it here */
	schedule_stress();
	return 0;
}
fs_initcall(proc_cpu_stress_init);