Skip to content

Commit 5b3acc8

Browse files
author
Alexey Dobriyan
committed
proc: switch /proc/loadavg to seq_file
and move it to fs/proc/loadavg.c while I'm at it. Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
1 parent 6c2f91e commit 5b3acc8

3 files changed

Lines changed: 52 additions & 27 deletions

File tree

‎fs/proc/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ proc-$(CONFIG_MMU) := mmu.o task_mmu.o
99

1010
proc-y += inode.o root.o base.o generic.o array.o \
1111
proc_tty.o proc_misc.o
12-
12+
proc-y += loadavg.o
1313
proc-$(CONFIG_PROC_SYSCTL) += proc_sysctl.o
1414
proc-$(CONFIG_NET) += proc_net.o
1515
proc-$(CONFIG_PROC_KCORE) += kcore.o

‎fs/proc/loadavg.c‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <linux/fs.h>
2+
#include <linux/init.h>
3+
#include <linux/pid_namespace.h>
4+
#include <linux/proc_fs.h>
5+
#include <linux/sched.h>
6+
#include <linux/seq_file.h>
7+
#include <linux/seqlock.h>
8+
#include <linux/time.h>
9+
10+
#define LOAD_INT(x) ((x) >> FSHIFT)
11+
#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
12+
13+
static int loadavg_proc_show(struct seq_file *m, void *v)
14+
{
15+
int a, b, c;
16+
unsigned long seq;
17+
18+
do {
19+
seq = read_seqbegin(&xtime_lock);
20+
a = avenrun[0] + (FIXED_1/200);
21+
b = avenrun[1] + (FIXED_1/200);
22+
c = avenrun[2] + (FIXED_1/200);
23+
} while (read_seqretry(&xtime_lock, seq));
24+
25+
seq_printf(m, "%d.%02d %d.%02d %d.%02d %ld/%d %d\n",
26+
LOAD_INT(a), LOAD_FRAC(a),
27+
LOAD_INT(b), LOAD_FRAC(b),
28+
LOAD_INT(c), LOAD_FRAC(c),
29+
nr_running(), nr_threads,
30+
task_active_pid_ns(current)->last_pid);
31+
return 0;
32+
}
33+
34+
static int loadavg_proc_open(struct inode *inode, struct file *file)
35+
{
36+
return single_open(file, loadavg_proc_show, NULL);
37+
}
38+
39+
static const struct file_operations loadavg_proc_fops = {
40+
.open = loadavg_proc_open,
41+
.read = seq_read,
42+
.llseek = seq_lseek,
43+
.release = single_release,
44+
};
45+
46+
static int __init proc_loadavg_init(void)
47+
{
48+
proc_create("loadavg", 0, NULL, &loadavg_proc_fops);
49+
return 0;
50+
}
51+
module_init(proc_loadavg_init);

‎fs/proc/proc_misc.c‎

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@
5757
#include <asm/div64.h>
5858
#include "internal.h"
5959

60-
#define LOAD_INT(x) ((x) >> FSHIFT)
61-
#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
6260
/*
6361
* Warning: stuff below (imported functions) assumes that its output will fit
6462
* into one page. For some of those functions it may be wrong. Moreover, we
@@ -80,29 +78,6 @@ static int proc_calc_metrics(char *page, char **start, off_t off,
8078
return len;
8179
}
8280

83-
static int loadavg_read_proc(char *page, char **start, off_t off,
84-
int count, int *eof, void *data)
85-
{
86-
int a, b, c;
87-
int len;
88-
unsigned long seq;
89-
90-
do {
91-
seq = read_seqbegin(&xtime_lock);
92-
a = avenrun[0] + (FIXED_1/200);
93-
b = avenrun[1] + (FIXED_1/200);
94-
c = avenrun[2] + (FIXED_1/200);
95-
} while (read_seqretry(&xtime_lock, seq));
96-
97-
len = sprintf(page,"%d.%02d %d.%02d %d.%02d %ld/%d %d\n",
98-
LOAD_INT(a), LOAD_FRAC(a),
99-
LOAD_INT(b), LOAD_FRAC(b),
100-
LOAD_INT(c), LOAD_FRAC(c),
101-
nr_running(), nr_threads,
102-
task_active_pid_ns(current)->last_pid);
103-
return proc_calc_metrics(page, start, off, count, eof, len);
104-
}
105-
10681
static int uptime_read_proc(char *page, char **start, off_t off,
10782
int count, int *eof, void *data)
10883
{
@@ -861,7 +836,6 @@ void __init proc_misc_init(void)
861836
char *name;
862837
int (*read_proc)(char*,char**,off_t,int,int*,void*);
863838
} *p, simple_ones[] = {
864-
{"loadavg", loadavg_read_proc},
865839
{"uptime", uptime_read_proc},
866840
{"meminfo", meminfo_read_proc},
867841
{"version", version_read_proc},

0 commit comments

Comments
 (0)