|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * proactive reclamation: monitor access pattern of a given process, find |
| 4 | + * regiosn 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 | +static int target_pid __read_mostly; |
| 15 | +module_param(target_pid, int, 0600); |
| 16 | + |
| 17 | +static int damon_sample_prcl_enable_store( |
| 18 | + const char *val, const struct kernel_param *kp); |
| 19 | + |
| 20 | +static const struct kernel_param_ops enable_param_ops = { |
| 21 | + .set = damon_sample_prcl_enable_store, |
| 22 | + .get = param_get_bool, |
| 23 | +}; |
| 24 | + |
| 25 | +static bool enable __read_mostly; |
| 26 | +module_param_cb(enable, &enable_param_ops, &enable, 0600); |
| 27 | +MODULE_PARM_DESC(enable, "Enable of disable DAMON_SAMPLE_WSSE"); |
| 28 | + |
| 29 | +static struct damon_ctx *ctx; |
| 30 | +static struct pid *target_pidp; |
| 31 | + |
| 32 | +static int damon_sample_prcl_after_aggregate(struct damon_ctx *c) |
| 33 | +{ |
| 34 | + struct damon_target *t; |
| 35 | + |
| 36 | + damon_for_each_target(t, c) { |
| 37 | + struct damon_region *r; |
| 38 | + unsigned long wss = 0; |
| 39 | + |
| 40 | + damon_for_each_region(r, t) { |
| 41 | + if (r->nr_accesses > 0) |
| 42 | + wss += r->ar.end - r->ar.start; |
| 43 | + } |
| 44 | + pr_info("wss: %lu\n", wss); |
| 45 | + } |
| 46 | + return 0; |
| 47 | +} |
| 48 | + |
| 49 | +static int damon_sample_prcl_start(void) |
| 50 | +{ |
| 51 | + struct damon_target *target; |
| 52 | + |
| 53 | + pr_info("start\n"); |
| 54 | + |
| 55 | + ctx = damon_new_ctx(); |
| 56 | + if (!ctx) |
| 57 | + return -ENOMEM; |
| 58 | + if (damon_select_ops(ctx, DAMON_OPS_VADDR)) { |
| 59 | + damon_destroy_ctx(ctx); |
| 60 | + return -EINVAL; |
| 61 | + } |
| 62 | + |
| 63 | + target = damon_new_target(); |
| 64 | + if (!target) { |
| 65 | + damon_destroy_ctx(ctx); |
| 66 | + return -ENOMEM; |
| 67 | + } |
| 68 | + damon_add_target(ctx, target); |
| 69 | + target_pidp = find_get_pid(target_pid); |
| 70 | + if (!target_pidp) { |
| 71 | + damon_destroy_ctx(ctx); |
| 72 | + return -EINVAL; |
| 73 | + } |
| 74 | + target->pid = target_pidp; |
| 75 | + |
| 76 | + ctx->callback.after_aggregation = damon_sample_prcl_after_aggregate; |
| 77 | + |
| 78 | + return damon_start(&ctx, 1, true); |
| 79 | +} |
| 80 | + |
| 81 | +static void damon_sample_prcl_stop(void) |
| 82 | +{ |
| 83 | + pr_info("stop\n"); |
| 84 | + if (ctx) { |
| 85 | + damon_stop(&ctx, 1); |
| 86 | + damon_destroy_ctx(ctx); |
| 87 | + } |
| 88 | + if (target_pidp) |
| 89 | + put_pid(target_pidp); |
| 90 | +} |
| 91 | + |
| 92 | +static int damon_sample_prcl_enable_store( |
| 93 | + const char *val, const struct kernel_param *kp) |
| 94 | +{ |
| 95 | + bool enabled = enable; |
| 96 | + int err; |
| 97 | + |
| 98 | + err = kstrtobool(val, &enable); |
| 99 | + if (err) |
| 100 | + return err; |
| 101 | + |
| 102 | + if (enable == enabled) |
| 103 | + return 0; |
| 104 | + |
| 105 | + if (enable) |
| 106 | + return damon_sample_prcl_start(); |
| 107 | + damon_sample_prcl_stop(); |
| 108 | + return 0; |
| 109 | +} |
| 110 | + |
| 111 | +static int __init damon_sample_prcl_init(void) |
| 112 | +{ |
| 113 | + return 0; |
| 114 | +} |
| 115 | + |
| 116 | +module_init(damon_sample_prcl_init); |
0 commit comments