projects/linux·blob master · refs

174 lines · 3.4 KiB

1// SPDX-License-Identifier: GPL-2.0
2/*
3 * proactive reclamation: monitor access pattern of a given process, find
4 * regions that seems not accessed, and proactively page out the regions.
5 */
6
7#define pr_fmt(fmt) "damon_sample_prcl: " fmt
8
9#include <linux/damon.h>
10#include <linux/init.h>
11#include <linux/kernel.h>
12#include <linux/module.h>
13
14#ifdef MODULE_PARAM_PREFIX
15#undef MODULE_PARAM_PREFIX
16#endif
17#define MODULE_PARAM_PREFIX "damon_sample_prcl."
18
19static int target_pid __read_mostly;
20module_param(target_pid, int, 0600);
21
22static int damon_sample_prcl_enable_store(
23 const char *val, const struct kernel_param *kp);
24
25static const struct kernel_param_ops enabled_param_ops = {
26 .set = damon_sample_prcl_enable_store,
27 .get = param_get_bool,
28};
29
30static bool enabled __read_mostly;
31module_param_cb(enabled, &enabled_param_ops, &enabled, 0600);
32MODULE_PARM_DESC(enabled, "Enable or disable DAMON_SAMPLE_PRCL");
33
34static struct damon_ctx *ctx;
35static struct pid *target_pidp;
36
37static int damon_sample_prcl_repeat_call_fn(void *data)
38{
39 struct damon_ctx *c = data;
40 struct damon_target *t;
41
42 damon_for_each_target(t, c) {
43 struct damon_region *r;
44 unsigned long wss = 0;
45
46 damon_for_each_region(r, t) {
47 if (r->nr_accesses > 0)
48 wss += r->ar.end - r->ar.start;
49 }
50 pr_info("wss: %lu\n", wss);
51 }
52 return 0;
53}
54
55static struct damon_call_control repeat_call_control = {
56 .fn = damon_sample_prcl_repeat_call_fn,
57 .repeat = true,
58};
59
60static int damon_sample_prcl_start(void)
61{
62 struct damon_target *target;
63 struct damos *scheme;
64 int err;
65
66 pr_info("start\n");
67
68 ctx = damon_new_ctx();
69 if (!ctx)
70 return -ENOMEM;
71 if (damon_select_ops(ctx, DAMON_OPS_VADDR)) {
72 damon_destroy_ctx(ctx);
73 return -EINVAL;
74 }
75
76 target = damon_new_target();
77 if (!target) {
78 damon_destroy_ctx(ctx);
79 return -ENOMEM;
80 }
81 damon_add_target(ctx, target);
82 target_pidp = find_get_pid(target_pid);
83 if (!target_pidp) {
84 damon_destroy_ctx(ctx);
85 return -EINVAL;
86 }
87 target->pid = target_pidp;
88
89 scheme = damon_new_scheme(
90 &(struct damos_access_pattern) {
91 .min_sz_region = PAGE_SIZE,
92 .max_sz_region = ULONG_MAX,
93 .min_nr_accesses = 0,
94 .max_nr_accesses = 0,
95 .min_age_region = 50,
96 .max_age_region = UINT_MAX},
97 DAMOS_PAGEOUT,
98 0,
99 &(struct damos_quota){},
100 &(struct damos_watermarks){},
101 NUMA_NO_NODE);
102 if (!scheme) {
103 damon_destroy_ctx(ctx);
104 return -ENOMEM;
105 }
106 damon_set_schemes(ctx, &scheme, 1);
107
108 err = damon_start(&ctx, 1, true);
109 if (err) {
110 damon_destroy_ctx(ctx);
111 return err;
112 }
113
114 repeat_call_control.data = ctx;
115 err = damon_call(ctx, &repeat_call_control);
116 if (err)
117 damon_destroy_ctx(ctx);
118 return err;
119}
120
121static void damon_sample_prcl_stop(void)
122{
123 pr_info("stop\n");
124 if (ctx) {
125 damon_stop(&ctx, 1);
126 damon_destroy_ctx(ctx);
127 }
128}
129
130static int damon_sample_prcl_enable_store(
131 const char *val, const struct kernel_param *kp)
132{
133 bool is_enabled = enabled;
134 int err;
135
136 err = kstrtobool(val, &enabled);
137 if (err)
138 return err;
139
140 if (enabled == is_enabled)
141 return 0;
142
143 if (!damon_initialized())
144 return 0;
145
146 if (enabled) {
147 err = damon_sample_prcl_start();
148 if (err)
149 enabled = false;
150 return err;
151 }
152 damon_sample_prcl_stop();
153 return 0;
154}
155
156static int __init damon_sample_prcl_init(void)
157{
158 int err = 0;
159
160 if (!damon_initialized()) {
161 if (enabled)
162 enabled = false;
163 return -ENOMEM;
164 }
165
166 if (enabled) {
167 err = damon_sample_prcl_start();
168 if (err)
169 enabled = false;
170 }
171 return 0;
172}
173
174module_init(damon_sample_prcl_init);