]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - linux/shrinker.c
Update bcachefs sources to 71a5b27e017d bcachefs: Make backpointer fsck wb flush...
[bcachefs-tools-debian] / linux / shrinker.c
index 25cdfbb64a2c7d9031eec07ac1873b8c150f1860..dae3d29337349821b1257b5f39659949570b4b6d 100644 (file)
@@ -1,6 +1,8 @@
 
 #include <stdio.h>
+#include <unistd.h>
 
+#include <linux/kthread.h>
 #include <linux/list.h>
 #include <linux/mm.h>
 #include <linux/mutex.h>
 static LIST_HEAD(shrinker_list);
 static DEFINE_MUTEX(shrinker_lock);
 
-int register_shrinker(struct shrinker *shrinker, const char *fmt, ...)
+struct shrinker *shrinker_alloc(unsigned int flags, const char *fmt, ...)
+{
+       return calloc(sizeof(struct shrinker), 1);
+}
+
+int shrinker_register(struct shrinker *shrinker)
 {
        mutex_lock(&shrinker_lock);
        list_add_tail(&shrinker->list, &shrinker_list);
@@ -31,38 +38,14 @@ struct meminfo {
        u64             available;
 };
 
-static u64 parse_meminfo_line(const char *line)
-{
-       u64 v;
-
-       if (sscanf(line, " %llu kB", &v) < 1)
-               die("sscanf error");
-       return v << 10;
-}
-
 void si_meminfo(struct sysinfo *val)
 {
-       size_t len, n = 0;
-       char *line = NULL;
-       const char *v;
-       FILE *f;
-
+       long page_size = sysconf(_SC_PAGESIZE);
        memset(val, 0, sizeof(*val));
+       val->mem_unit = 1;
 
-       f = fopen("/proc/meminfo", "r");
-       if (!f)
-               return;
-
-       while ((len = getline(&line, &n, f)) != -1) {
-               if ((v = strcmp_prefix(line, "MemTotal:")))
-                       val->totalram = parse_meminfo_line(v);
-
-               if ((v = strcmp_prefix(line, "MemAvailable:")))
-                       val->freeram = parse_meminfo_line(v);
-       }
-
-       fclose(f);
-       free(line);
+       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)
@@ -86,8 +69,12 @@ void run_shrinkers(gfp_t gfp_mask, bool allocation_failed)
 {
        struct shrinker *shrinker;
        struct sysinfo info;
+       struct mallinfo2 malloc_info = mallinfo2();
        s64 want_shrink;
 
+       if (!(gfp_mask & GFP_KERNEL))
+               return;
+
        /* Fast out if there are no shrinkers to run. */
        if (list_empty(&shrinker_list))
                return;
@@ -99,16 +86,15 @@ void run_shrinkers(gfp_t gfp_mask, bool allocation_failed)
 
        si_meminfo(&info);
 
-       if (info.totalram && info.freeram) {
-               want_shrink = (info.totalram >> 2) - info.freeram;
+       if (info.totalram && info.totalram >> 4 < info.freeram) {
+               /* freeram goes up when system swaps, use malloced data instead */
+               want_shrink = -malloc_info.arena + (info.totalram / 10 * 8);
 
                if (want_shrink <= 0)
                        return;
        } else {
-               /* If we weren't able to read /proc/meminfo, we must be pretty
-                * low: */
-
-               want_shrink = 8 << 20;
+               /* We want to play nice with other apps keep 6% avaliable, free 3% */
+               want_shrink = (info.totalram >> 5);
        }
 
        mutex_lock(&shrinker_lock);
@@ -122,3 +108,45 @@ void run_shrinkers(gfp_t gfp_mask, bool allocation_failed)
        }
        mutex_unlock(&shrinker_lock);
 }
+
+static int shrinker_thread(void *arg)
+{
+       while (!kthread_should_stop()) {
+               struct timespec to;
+               int v;
+
+               clock_gettime(CLOCK_MONOTONIC, &to);
+               to.tv_sec += 1;
+               __set_current_state(TASK_INTERRUPTIBLE);
+               errno = 0;
+               while ((v = READ_ONCE(current->state)) != TASK_RUNNING &&
+                      errno != ETIMEDOUT)
+                       futex(&current->state, FUTEX_WAIT_BITSET|FUTEX_PRIVATE_FLAG,
+                             v, &to, NULL, (uint32_t)~0);
+               if (kthread_should_stop())
+                       break;
+               if (v != TASK_RUNNING)
+                       __set_current_state(TASK_RUNNING);
+               run_shrinkers(GFP_KERNEL, false);
+       }
+
+       return 0;
+}
+
+struct task_struct *shrinker_task;
+
+__attribute__((constructor(103)))
+static void shrinker_thread_init(void)
+{
+       shrinker_task = kthread_run(shrinker_thread, NULL, "shrinkers");
+       BUG_ON(IS_ERR(shrinker_task));
+}
+
+__attribute__((destructor(103)))
+static void shrinker_thread_exit(void)
+{
+       int ret = kthread_stop(shrinker_task);
+       BUG_ON(ret);
+
+       shrinker_task = NULL;
+}