]> git.sesse.net Git - bcachefs-tools-debian/blob - linux/shrinker.c
Merge https://github.com/fougner/bcachefs-tools
[bcachefs-tools-debian] / linux / shrinker.c
1
2 #include <stdio.h>
3
4 #include <linux/kthread.h>
5 #include <linux/list.h>
6 #include <linux/mm.h>
7 #include <linux/mutex.h>
8 #include <linux/shrinker.h>
9
10 #include "tools-util.h"
11
12 static LIST_HEAD(shrinker_list);
13 static DEFINE_MUTEX(shrinker_lock);
14
15 int register_shrinker(struct shrinker *shrinker, const char *fmt, ...)
16 {
17         mutex_lock(&shrinker_lock);
18         list_add_tail(&shrinker->list, &shrinker_list);
19         mutex_unlock(&shrinker_lock);
20         return 0;
21 }
22
23 void unregister_shrinker(struct shrinker *shrinker)
24 {
25         mutex_lock(&shrinker_lock);
26         list_del(&shrinker->list);
27         mutex_unlock(&shrinker_lock);
28 }
29
30 struct meminfo {
31         u64             total;
32         u64             available;
33 };
34
35 static u64 parse_meminfo_line(const char *line)
36 {
37         u64 v;
38
39         if (sscanf(line, " %llu kB", &v) < 1)
40                 die("sscanf error");
41         return v << 10;
42 }
43
44 void si_meminfo(struct sysinfo *val)
45 {
46         size_t len, n = 0;
47         char *line = NULL;
48         const char *v;
49         FILE *f;
50
51         memset(val, 0, sizeof(*val));
52         val->mem_unit = 1;
53
54         f = fopen("/proc/meminfo", "r");
55         if (!f)
56                 return;
57
58         while ((len = getline(&line, &n, f)) != -1) {
59                 if ((v = strcmp_prefix(line, "MemTotal:")))
60                         val->totalram = parse_meminfo_line(v);
61
62                 if ((v = strcmp_prefix(line, "MemAvailable:")))
63                         val->freeram = parse_meminfo_line(v);
64         }
65
66         fclose(f);
67         free(line);
68 }
69
70 static void run_shrinkers_allocation_failed(gfp_t gfp_mask)
71 {
72         struct shrinker *shrinker;
73
74         mutex_lock(&shrinker_lock);
75         list_for_each_entry(shrinker, &shrinker_list, list) {
76                 struct shrink_control sc = { .gfp_mask  = gfp_mask, };
77
78                 unsigned long have = shrinker->count_objects(shrinker, &sc);
79
80                 sc.nr_to_scan = have / 8;
81
82                 shrinker->scan_objects(shrinker, &sc);
83         }
84         mutex_unlock(&shrinker_lock);
85 }
86
87 void run_shrinkers(gfp_t gfp_mask, bool allocation_failed)
88 {
89         struct shrinker *shrinker;
90         struct sysinfo info;
91         s64 want_shrink;
92
93         if (!(gfp_mask & GFP_KERNEL))
94                 return;
95
96         /* Fast out if there are no shrinkers to run. */
97         if (list_empty(&shrinker_list))
98                 return;
99
100         if (allocation_failed) {
101                 run_shrinkers_allocation_failed(gfp_mask);
102                 return;
103         }
104
105         si_meminfo(&info);
106
107         if (info.totalram && info.freeram) {
108                 want_shrink = (info.totalram >> 2) - info.freeram;
109
110                 if (want_shrink <= 0)
111                         return;
112         } else {
113                 /* If we weren't able to read /proc/meminfo, we must be pretty
114                  * low: */
115
116                 want_shrink = 8 << 20;
117         }
118
119         mutex_lock(&shrinker_lock);
120         list_for_each_entry(shrinker, &shrinker_list, list) {
121                 struct shrink_control sc = {
122                         .gfp_mask       = gfp_mask,
123                         .nr_to_scan     = want_shrink >> PAGE_SHIFT
124                 };
125
126                 shrinker->scan_objects(shrinker, &sc);
127         }
128         mutex_unlock(&shrinker_lock);
129 }
130
131 static int shrinker_thread(void *arg)
132 {
133         while (!kthread_should_stop()) {
134                 sleep(1);
135                 run_shrinkers(GFP_KERNEL, false);
136         }
137
138         return 0;
139 }
140
141 struct task_struct *shrinker_task;
142
143 __attribute__((constructor(103)))
144 static void shrinker_thread_init(void)
145 {
146         shrinker_task = kthread_run(shrinker_thread, NULL, "shrinkers");
147         BUG_ON(IS_ERR(shrinker_task));
148 }
149
150 __attribute__((destructor(103)))
151 static void shrinker_thread_exit(void)
152 {
153         int ret = kthread_stop(shrinker_task);
154         BUG_ON(ret);
155
156         shrinker_task = NULL;
157 }