| 1 | |
| 2 | |
| 3 | * working set size estimation: monitor access pattern of given process and
|
| 4 | * print estimated working set size (total size of regions that showing some
|
| 5 | * access).
|
| 6 |
|
| 7 |
|
| 8 | #define pr_fmt(fmt) "damon_sample_wsse: " fmt
|
| 9 |
|
| 10 | #include <linux/damon.h>
|
| 11 | #include <linux/init.h>
|
| 12 | #include <linux/kernel.h>
|
| 13 | #include <linux/module.h>
|
| 14 |
|
| 15 | #ifdef MODULE_PARAM_PREFIX
|
| 16 | #undef MODULE_PARAM_PREFIX
|
| 17 | #endif
|
| 18 | #define MODULE_PARAM_PREFIX "damon_sample_wsse."
|
| 19 |
|
| 20 | static int target_pid __read_mostly;
|
| 21 | module_param(target_pid, int, 0600);
|
| 22 |
|
| 23 | static int damon_sample_wsse_enable_store(
|
| 24 | const char *val, const struct kernel_param *kp);
|
| 25 |
|
| 26 | static const struct kernel_param_ops enabled_param_ops = {
|
| 27 | .set = damon_sample_wsse_enable_store,
|
| 28 | .get = param_get_bool,
|
| 29 | };
|
| 30 |
|
| 31 | static bool enabled __read_mostly;
|
| 32 | module_param_cb(enabled, &enabled_param_ops, &enabled, 0600);
|
| 33 | MODULE_PARM_DESC(enabled, "Enable or disable DAMON_SAMPLE_WSSE");
|
| 34 |
|
| 35 | static struct damon_ctx *ctx;
|
| 36 | static struct pid *target_pidp;
|
| 37 |
|
| 38 | static int damon_sample_wsse_repeat_call_fn(void *data)
|
| 39 | {
|
| 40 | struct damon_ctx *c = data;
|
| 41 | struct damon_target *t;
|
| 42 |
|
| 43 | damon_for_each_target(t, c) {
|
| 44 | struct damon_region *r;
|
| 45 | unsigned long wss = 0;
|
| 46 |
|
| 47 | damon_for_each_region(r, t) {
|
| 48 | if (r->nr_accesses > 0)
|
| 49 | wss += r->ar.end - r->ar.start;
|
| 50 | }
|
| 51 | pr_info("wss: %lu\n", wss);
|
| 52 | }
|
| 53 | return 0;
|
| 54 | }
|
| 55 |
|
| 56 | static struct damon_call_control repeat_call_control = {
|
| 57 | .fn = damon_sample_wsse_repeat_call_fn,
|
| 58 | .repeat = true,
|
| 59 | };
|
| 60 |
|
| 61 | static int damon_sample_wsse_start(void)
|
| 62 | {
|
| 63 | struct damon_target *target;
|
| 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 | err = damon_start(&ctx, 1, true);
|
| 90 | if (err) {
|
| 91 | damon_destroy_ctx(ctx);
|
| 92 | return err;
|
| 93 | }
|
| 94 | repeat_call_control.data = ctx;
|
| 95 | err = damon_call(ctx, &repeat_call_control);
|
| 96 | if (err)
|
| 97 | damon_destroy_ctx(ctx);
|
| 98 | return err;
|
| 99 | }
|
| 100 |
|
| 101 | static void damon_sample_wsse_stop(void)
|
| 102 | {
|
| 103 | pr_info("stop\n");
|
| 104 | if (ctx) {
|
| 105 | damon_stop(&ctx, 1);
|
| 106 | damon_destroy_ctx(ctx);
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | static int damon_sample_wsse_enable_store(
|
| 111 | const char *val, const struct kernel_param *kp)
|
| 112 | {
|
| 113 | bool is_enabled = enabled;
|
| 114 | int err;
|
| 115 |
|
| 116 | err = kstrtobool(val, &enabled);
|
| 117 | if (err)
|
| 118 | return err;
|
| 119 |
|
| 120 | if (enabled == is_enabled)
|
| 121 | return 0;
|
| 122 |
|
| 123 | if (!damon_initialized())
|
| 124 | return 0;
|
| 125 |
|
| 126 | if (enabled) {
|
| 127 | err = damon_sample_wsse_start();
|
| 128 | if (err)
|
| 129 | enabled = false;
|
| 130 | return err;
|
| 131 | }
|
| 132 | damon_sample_wsse_stop();
|
| 133 | return 0;
|
| 134 | }
|
| 135 |
|
| 136 | static int __init damon_sample_wsse_init(void)
|
| 137 | {
|
| 138 | int err = 0;
|
| 139 |
|
| 140 | if (!damon_initialized()) {
|
| 141 | err = -ENOMEM;
|
| 142 | if (enabled)
|
| 143 | enabled = false;
|
| 144 | }
|
| 145 |
|
| 146 | if (enabled) {
|
| 147 | err = damon_sample_wsse_start();
|
| 148 | if (err)
|
| 149 | enabled = false;
|
| 150 | }
|
| 151 | return err;
|
| 152 | }
|
| 153 |
|
| 154 | module_init(damon_sample_wsse_init);
|