diff options
author | Rasmus Villemoes <rv@rasmusvillemoes.dk> | 2015-08-18 11:55:34 +0200 |
---|---|---|
committer | Mike Frysinger <vapier@gentoo.org> | 2015-09-12 21:09:59 -0400 |
commit | 0ce657c576bf1b2436c4e14a002eaf461897d82c (patch) | |
tree | a2fa54f90f91e449ef089921d5c2dfe7f752df7c /sysdeps/unix/sysv/linux/include | |
parent | b482d0364ed55c171708cd95922a896f002b6043 (diff) | |
download | glibc-0ce657c576bf1b2436c4e14a002eaf461897d82c.tar.gz |
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.
Diffstat (limited to 'sysdeps/unix/sysv/linux/include')
-rw-r--r-- | sysdeps/unix/sysv/linux/include/sys/sysinfo.h | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/sysdeps/unix/sysv/linux/include/sys/sysinfo.h b/sysdeps/unix/sysv/linux/include/sys/sysinfo.h new file mode 100644 index 0000000000..45b7126b57 --- /dev/null +++ b/sysdeps/unix/sysv/linux/include/sys/sysinfo.h @@ -0,0 +1,26 @@ +/* Internal declarations for sys/sysinfo.h. + Copyright (C) 2015 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <http://www.gnu.org/licenses/>. */ + +#ifndef _INCLUDE_SYS_SYSINFO_H +#define _INCLUDE_SYS_SYSINFO_H 1 + +#include_next <sys/sysinfo.h> + +extern __typeof (sysinfo) __sysinfo __THROW; + +#endif /* sys/sysinfo.h */ |