]> git.sesse.net Git - bcachefs-tools-debian/commitdiff
bcachefs-tools: Use sysinfo(2) directly to implement si_meminfo()
authorChris Webb <chris@arachsys.com>
Sat, 9 Dec 2023 12:49:12 +0000 (12:49 +0000)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 10 Dec 2023 04:26:29 +0000 (23:26 -0500)
Use a single sysinfo(2) call to fill out struct sysinfo instead of
multiple libc sysconf(3) requests, which will only make sysinfo(2) calls
internally anyway. This also enables us to access other struct sysinfo
fields, not just the three filled-out previously.

As we provide our own definition of struct sysinfo in include/linux/mm.h
to match the kernel, which is not guaranteed to align with the definition
libc provides in <sys/sysinfo.h>, use syscall(SYS_sysinfo, ...) directly
instead of the libc wrapper.

Signed-off-by: Chris Webb <chris@arachsys.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
include/linux/mm.h
linux/shrinker.c

index 4bf80ba30050b85ee5cb3659bd4d01592a1c7bb5..744a14ce41887b278809d65fa685e809b26f41db 100644 (file)
@@ -2,6 +2,7 @@
 #ifndef _TOOLS_LINUX_MM_H
 #define _TOOLS_LINUX_MM_H
 
+#include <sys/syscall.h>
 #include <linux/types.h>
 
 struct sysinfo {
@@ -20,6 +21,11 @@ struct sysinfo {
        __u32 mem_unit;                 /* Memory unit size in bytes */
 };
 
-extern void si_meminfo(struct sysinfo * val);
+
+
+static inline void si_meminfo(struct sysinfo *val)
+{
+       BUG_ON(syscall(SYS_sysinfo, val));
+}
 
 #endif /* _TOOLS_LINUX_MM_H */
index 91f633b57927a2e80879faa5348b869a4b7ef4a4..7658fb7e71d427844a2aeb811033a42143392db7 100644 (file)
@@ -33,21 +33,6 @@ void unregister_shrinker(struct shrinker *shrinker)
        mutex_unlock(&shrinker_lock);
 }
 
-struct meminfo {
-       u64             total;
-       u64             available;
-};
-
-void si_meminfo(struct sysinfo *val)
-{
-       long page_size = sysconf(_SC_PAGESIZE);
-       memset(val, 0, sizeof(*val));
-       val->mem_unit = 1;
-
-       val->totalram = sysconf(_SC_PHYS_PAGES) * page_size;
-       val->freeram  = sysconf(_SC_AVPHYS_PAGES) * page_size;
-}
-
 static void run_shrinkers_allocation_failed(gfp_t gfp_mask)
 {
        struct shrinker *shrinker;