Skip to content

Commit bda7bad

Browse files
hkamezawatorvalds
authored andcommitted
procfs: speed up /proc/pid/stat, statm
Process accounting applications as top, ps visit some files under /proc/<pid>. With seq_put_decimal_ull(), we can optimize /proc/<pid>/stat and /proc/<pid>/statm files. This patch adds - seq_put_decimal_ll() for signed values. - allow delimiter == 0. - convert seq_printf() to seq_put_decimal_ull/ll in /proc/stat, statm. Test result on a system with 2000+ procs. Before patch: [kamezawa@bluextal test]$ top -b -n 1 | wc -l 2223 [kamezawa@bluextal test]$ time top -b -n 1 > /dev/null real 0m0.675s user 0m0.044s sys 0m0.121s [kamezawa@bluextal test]$ time ps -elf > /dev/null real 0m0.236s user 0m0.056s sys 0m0.176s After patch: kamezawa@bluextal ~]$ time top -b -n 1 > /dev/null real 0m0.657s user 0m0.052s sys 0m0.100s [kamezawa@bluextal ~]$ time ps -elf > /dev/null real 0m0.198s user 0m0.050s sys 0m0.145s Considering top, ps tend to scan /proc periodically, this will reduce cpu consumption by top/ps to some extent. [akpm@linux-foundation.org: checkpatch fixes] Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Cc: Alexey Dobriyan <adobriyan@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 1ac101a commit bda7bad

3 files changed

Lines changed: 86 additions & 56 deletions

File tree

‎fs/proc/array.c‎

Lines changed: 64 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -462,59 +462,56 @@ static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
462462
/* convert nsec -> ticks */
463463
start_time = nsec_to_clock_t(start_time);
464464

465-
seq_printf(m, "%d (%s) %c %d %d %d %d %d %u %lu \
466-
%lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
467-
%lu %lu %lu %lu %lu %lu %lu %lu %d %d %u %u %llu %lu %ld %lu %lu %lu\n",
468-
pid_nr_ns(pid, ns),
469-
tcomm,
470-
state,
471-
ppid,
472-
pgid,
473-
sid,
474-
tty_nr,
475-
tty_pgrp,
476-
task->flags,
477-
min_flt,
478-
cmin_flt,
479-
maj_flt,
480-
cmaj_flt,
481-
cputime_to_clock_t(utime),
482-
cputime_to_clock_t(stime),
483-
cputime_to_clock_t(cutime),
484-
cputime_to_clock_t(cstime),
485-
priority,
486-
nice,
487-
num_threads,
488-
start_time,
489-
vsize,
490-
mm ? get_mm_rss(mm) : 0,
491-
rsslim,
492-
mm ? (permitted ? mm->start_code : 1) : 0,
493-
mm ? (permitted ? mm->end_code : 1) : 0,
494-
(permitted && mm) ? mm->start_stack : 0,
495-
esp,
496-
eip,
497-
/* The signal information here is obsolete.
498-
* It must be decimal for Linux 2.0 compatibility.
499-
* Use /proc/#/status for real-time signals.
500-
*/
501-
task->pending.signal.sig[0] & 0x7fffffffUL,
502-
task->blocked.sig[0] & 0x7fffffffUL,
503-
sigign .sig[0] & 0x7fffffffUL,
504-
sigcatch .sig[0] & 0x7fffffffUL,
505-
wchan,
506-
0UL,
507-
0UL,
508-
task->exit_signal,
509-
task_cpu(task),
510-
task->rt_priority,
511-
task->policy,
512-
(unsigned long long)delayacct_blkio_ticks(task),
513-
cputime_to_clock_t(gtime),
514-
cputime_to_clock_t(cgtime),
515-
(mm && permitted) ? mm->start_data : 0,
516-
(mm && permitted) ? mm->end_data : 0,
517-
(mm && permitted) ? mm->start_brk : 0);
465+
seq_printf(m, "%d (%s) %c", pid_nr_ns(pid, ns), tcomm, state);
466+
seq_put_decimal_ll(m, ' ', ppid);
467+
seq_put_decimal_ll(m, ' ', pgid);
468+
seq_put_decimal_ll(m, ' ', sid);
469+
seq_put_decimal_ll(m, ' ', tty_nr);
470+
seq_put_decimal_ll(m, ' ', tty_pgrp);
471+
seq_put_decimal_ull(m, ' ', task->flags);
472+
seq_put_decimal_ull(m, ' ', min_flt);
473+
seq_put_decimal_ull(m, ' ', cmin_flt);
474+
seq_put_decimal_ull(m, ' ', maj_flt);
475+
seq_put_decimal_ull(m, ' ', cmaj_flt);
476+
seq_put_decimal_ull(m, ' ', cputime_to_clock_t(utime));
477+
seq_put_decimal_ull(m, ' ', cputime_to_clock_t(stime));
478+
seq_put_decimal_ll(m, ' ', cputime_to_clock_t(cutime));
479+
seq_put_decimal_ll(m, ' ', cputime_to_clock_t(cstime));
480+
seq_put_decimal_ll(m, ' ', priority);
481+
seq_put_decimal_ll(m, ' ', nice);
482+
seq_put_decimal_ll(m, ' ', num_threads);
483+
seq_put_decimal_ull(m, ' ', 0);
484+
seq_put_decimal_ull(m, ' ', start_time);
485+
seq_put_decimal_ull(m, ' ', vsize);
486+
seq_put_decimal_ll(m, ' ', mm ? get_mm_rss(mm) : 0);
487+
seq_put_decimal_ull(m, ' ', rsslim);
488+
seq_put_decimal_ull(m, ' ', mm ? (permitted ? mm->start_code : 1) : 0);
489+
seq_put_decimal_ull(m, ' ', mm ? (permitted ? mm->end_code : 1) : 0);
490+
seq_put_decimal_ull(m, ' ', (permitted && mm) ? mm->start_stack : 0);
491+
seq_put_decimal_ull(m, ' ', esp);
492+
seq_put_decimal_ull(m, ' ', eip);
493+
/* The signal information here is obsolete.
494+
* It must be decimal for Linux 2.0 compatibility.
495+
* Use /proc/#/status for real-time signals.
496+
*/
497+
seq_put_decimal_ull(m, ' ', task->pending.signal.sig[0] & 0x7fffffffUL);
498+
seq_put_decimal_ull(m, ' ', task->blocked.sig[0] & 0x7fffffffUL);
499+
seq_put_decimal_ull(m, ' ', sigign.sig[0] & 0x7fffffffUL);
500+
seq_put_decimal_ull(m, ' ', sigcatch.sig[0] & 0x7fffffffUL);
501+
seq_put_decimal_ull(m, ' ', wchan);
502+
seq_put_decimal_ull(m, ' ', 0);
503+
seq_put_decimal_ull(m, ' ', 0);
504+
seq_put_decimal_ll(m, ' ', task->exit_signal);
505+
seq_put_decimal_ll(m, ' ', task_cpu(task));
506+
seq_put_decimal_ull(m, ' ', task->rt_priority);
507+
seq_put_decimal_ull(m, ' ', task->policy);
508+
seq_put_decimal_ull(m, ' ', delayacct_blkio_ticks(task));
509+
seq_put_decimal_ull(m, ' ', cputime_to_clock_t(gtime));
510+
seq_put_decimal_ll(m, ' ', cputime_to_clock_t(cgtime));
511+
seq_put_decimal_ull(m, ' ', (mm && permitted) ? mm->start_data : 0);
512+
seq_put_decimal_ull(m, ' ', (mm && permitted) ? mm->end_data : 0);
513+
seq_put_decimal_ull(m, ' ', (mm && permitted) ? mm->start_brk : 0);
514+
seq_putc(m, '\n');
518515
if (mm)
519516
mmput(mm);
520517
return 0;
@@ -542,8 +539,20 @@ int proc_pid_statm(struct seq_file *m, struct pid_namespace *ns,
542539
size = task_statm(mm, &shared, &text, &data, &resident);
543540
mmput(mm);
544541
}
545-
seq_printf(m, "%lu %lu %lu %lu 0 %lu 0\n",
546-
size, resident, shared, text, data);
542+
/*
543+
* For quick read, open code by putting numbers directly
544+
* expected format is
545+
* seq_printf(m, "%lu %lu %lu %lu 0 %lu 0\n",
546+
* size, resident, shared, text, data);
547+
*/
548+
seq_put_decimal_ull(m, 0, size);
549+
seq_put_decimal_ull(m, ' ', resident);
550+
seq_put_decimal_ull(m, ' ', shared);
551+
seq_put_decimal_ull(m, ' ', text);
552+
seq_put_decimal_ull(m, ' ', 0);
553+
seq_put_decimal_ull(m, ' ', text);
554+
seq_put_decimal_ull(m, ' ', 0);
555+
seq_putc(m, '\n');
547556

548557
return 0;
549558
}

‎fs/seq_file.c‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,8 @@ int seq_put_decimal_ull(struct seq_file *m, char delimiter,
659659
if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
660660
goto overflow;
661661

662-
m->buf[m->count++] = delimiter;
662+
if (delimiter)
663+
m->buf[m->count++] = delimiter;
663664

664665
if (num < 10) {
665666
m->buf[m->count++] = num + '0';
@@ -677,6 +678,24 @@ int seq_put_decimal_ull(struct seq_file *m, char delimiter,
677678
}
678679
EXPORT_SYMBOL(seq_put_decimal_ull);
679680

681+
int seq_put_decimal_ll(struct seq_file *m, char delimiter,
682+
long long num)
683+
{
684+
if (num < 0) {
685+
if (m->count + 3 >= m->size) {
686+
m->count = m->size;
687+
return -1;
688+
}
689+
if (delimiter)
690+
m->buf[m->count++] = delimiter;
691+
num = -num;
692+
delimiter = '-';
693+
}
694+
return seq_put_decimal_ull(m, delimiter, num);
695+
696+
}
697+
EXPORT_SYMBOL(seq_put_decimal_ll);
698+
680699
/**
681700
* seq_write - write arbitrary data to buffer
682701
* @seq: seq_file identifying the buffer to which data should be written

‎include/linux/seq_file.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ int seq_open_private(struct file *, const struct seq_operations *, int);
123123
int seq_release_private(struct inode *, struct file *);
124124
int seq_put_decimal_ull(struct seq_file *m, char delimiter,
125125
unsigned long long num);
126+
int seq_put_decimal_ll(struct seq_file *m, char delimiter,
127+
long long num);
126128

127129
#define SEQ_START_TOKEN ((void *)1)
128130
/*

0 commit comments

Comments
 (0)