]> sourceware.org Git - glibc.git/commit
linux/getsysstats.c: use sysinfo() instead of parsing /proc/meminfo
authorRasmus Villemoes <rv@rasmusvillemoes.dk>
Tue, 18 Aug 2015 09:55:34 +0000 (11:55 +0200)
committerMike Frysinger <vapier@gentoo.org>
Sun, 13 Sep 2015 01:09:59 +0000 (21:09 -0400)
commit0ce657c576bf1b2436c4e14a002eaf461897d82c
treea2fa54f90f91e449ef089921d5c2dfe7f752df7c
parentb482d0364ed55c171708cd95922a896f002b6043
linux/getsysstats.c: use sysinfo() instead of parsing /proc/meminfo

Profiling git's test suite, Linus noted [1] that a disproportionately
large amount of time was spent reading /proc/meminfo. This is done by
the glibc functions get_phys_pages and get_avphys_pages, but they only
need the MemTotal and MemFree fields, respectively. That same
information can be obtained with a single syscall, sysinfo, instead of
six: open, fstat, mmap, read, close, munmap. While sysinfo also
provides more than necessary, it does a lot less work than what the
kernel needs to do to provide the entire /proc/meminfo. Both strace -T
and in-app microbenchmarks shows that the sysinfo() approach is
roughly an order of magnitude faster.

sysinfo() is much older than what glibc currently requires, so I don't
think there's any reason to keep the old parsing code. Moreover, this
makes get_[av]phys_pages work even in the absence of /proc.

Linus noted that something as simple as 'bash -c "echo"' would trigger
the reading of /proc/meminfo, but gdb says that many more applications
than just bash are affected:

Starting program: /bin/bash "-c" "echo"

Breakpoint 1, __get_phys_pages () at ../sysdeps/unix/sysv/linux/getsysstats.c:283
283     ../sysdeps/unix/sysv/linux/getsysstats.c: No such file or directory.
(gdb) bt

So it seems that any application that uses qsort on a moderately sized
array will incur this cost (once), which is obviously proportionately
more expensive for lots of short-lived processes (such as the git test
suite).

[1] http://thread.gmane.org/gmane.linux.kernel/2019285

Signed-off-by: Rasmus Villemoes <rv@rasmusvillemoes.dk>
* sysdeps/unix/sysv/linux/getsysstats.c (__get_phys_pages):
Use sysinfo system call instead of parsing /proc/meminfo.
* sysdeps/unix/sysv/linux/getsysstats.c (__get_avphys_pages):
Likewise.
ChangeLog
sysdeps/unix/sysv/linux/getsysstats.c
sysdeps/unix/sysv/linux/include/sys/sysinfo.h [new file with mode: 0644]
sysdeps/unix/sysv/linux/syscalls.list
This page took 0.058364 seconds and 5 git commands to generate.