1 // SPDX-License-Identifier: GPL-2.0
8 #define pr_fmt(fmt) "damon: " fmt
10 #include <linux/damon.h>
11 #include <linux/delay.h>
12 #include <linux/kthread.h>
14 #include <linux/random.h>
15 #include <linux/slab.h>
16 #include <linux/string.h>
18 #define CREATE_TRACE_POINTS
19 #include <trace/events/damon.h>
21 #ifdef CONFIG_DAMON_KUNIT_TEST
22 #undef DAMON_MIN_REGION
23 #define DAMON_MIN_REGION 1
26 /* Get a random number in [l, r) */
27 #define damon_rand(l, r) (l + prandom_u32_max(r - l))
29 static DEFINE_MUTEX(damon_lock);
30 static int nr_running_ctxs;
33 * Construct a damon_region struct
35 * Returns the pointer to the new struct if success, or NULL otherwise
37 struct damon_region *damon_new_region(unsigned long start, unsigned long end)
39 struct damon_region *region;
41 region = kmalloc(sizeof(*region), GFP_KERNEL);
45 region->ar.start = start;
47 region->nr_accesses = 0;
48 INIT_LIST_HEAD(®ion->list);
51 region->last_nr_accesses = 0;
57 * Add a region between two other regions
59 inline void damon_insert_region(struct damon_region *r,
60 struct damon_region *prev, struct damon_region *next,
61 struct damon_target *t)
63 __list_add(&r->list, &prev->list, &next->list);
67 void damon_add_region(struct damon_region *r, struct damon_target *t)
69 list_add_tail(&r->list, &t->regions_list);
73 static void damon_del_region(struct damon_region *r, struct damon_target *t)
79 static void damon_free_region(struct damon_region *r)
84 void damon_destroy_region(struct damon_region *r, struct damon_target *t)
86 damon_del_region(r, t);
90 struct damos *damon_new_scheme(
91 unsigned long min_sz_region, unsigned long max_sz_region,
92 unsigned int min_nr_accesses, unsigned int max_nr_accesses,
93 unsigned int min_age_region, unsigned int max_age_region,
94 enum damos_action action, struct damos_quota *quota,
95 struct damos_watermarks *wmarks)
99 scheme = kmalloc(sizeof(*scheme), GFP_KERNEL);
102 scheme->min_sz_region = min_sz_region;
103 scheme->max_sz_region = max_sz_region;
104 scheme->min_nr_accesses = min_nr_accesses;
105 scheme->max_nr_accesses = max_nr_accesses;
106 scheme->min_age_region = min_age_region;
107 scheme->max_age_region = max_age_region;
108 scheme->action = action;
109 scheme->stat_count = 0;
111 INIT_LIST_HEAD(&scheme->list);
113 scheme->quota.ms = quota->ms;
114 scheme->quota.sz = quota->sz;
115 scheme->quota.reset_interval = quota->reset_interval;
116 scheme->quota.weight_sz = quota->weight_sz;
117 scheme->quota.weight_nr_accesses = quota->weight_nr_accesses;
118 scheme->quota.weight_age = quota->weight_age;
119 scheme->quota.total_charged_sz = 0;
120 scheme->quota.total_charged_ns = 0;
121 scheme->quota.esz = 0;
122 scheme->quota.charged_sz = 0;
123 scheme->quota.charged_from = 0;
124 scheme->quota.charge_target_from = NULL;
125 scheme->quota.charge_addr_from = 0;
127 scheme->wmarks.metric = wmarks->metric;
128 scheme->wmarks.interval = wmarks->interval;
129 scheme->wmarks.high = wmarks->high;
130 scheme->wmarks.mid = wmarks->mid;
131 scheme->wmarks.low = wmarks->low;
132 scheme->wmarks.activated = true;
137 void damon_add_scheme(struct damon_ctx *ctx, struct damos *s)
139 list_add_tail(&s->list, &ctx->schemes);
142 static void damon_del_scheme(struct damos *s)
147 static void damon_free_scheme(struct damos *s)
152 void damon_destroy_scheme(struct damos *s)
155 damon_free_scheme(s);
159 * Construct a damon_target struct
161 * Returns the pointer to the new struct if success, or NULL otherwise
163 struct damon_target *damon_new_target(unsigned long id)
165 struct damon_target *t;
167 t = kmalloc(sizeof(*t), GFP_KERNEL);
173 INIT_LIST_HEAD(&t->regions_list);
178 void damon_add_target(struct damon_ctx *ctx, struct damon_target *t)
180 list_add_tail(&t->list, &ctx->adaptive_targets);
183 bool damon_targets_empty(struct damon_ctx *ctx)
185 return list_empty(&ctx->adaptive_targets);
188 static void damon_del_target(struct damon_target *t)
193 void damon_free_target(struct damon_target *t)
195 struct damon_region *r, *next;
197 damon_for_each_region_safe(r, next, t)
198 damon_free_region(r);
202 void damon_destroy_target(struct damon_target *t)
205 damon_free_target(t);
208 unsigned int damon_nr_regions(struct damon_target *t)
210 return t->nr_regions;
213 struct damon_ctx *damon_new_ctx(void)
215 struct damon_ctx *ctx;
217 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
221 ctx->sample_interval = 5 * 1000;
222 ctx->aggr_interval = 100 * 1000;
223 ctx->primitive_update_interval = 60 * 1000 * 1000;
225 ktime_get_coarse_ts64(&ctx->last_aggregation);
226 ctx->last_primitive_update = ctx->last_aggregation;
228 mutex_init(&ctx->kdamond_lock);
230 ctx->min_nr_regions = 10;
231 ctx->max_nr_regions = 1000;
233 INIT_LIST_HEAD(&ctx->adaptive_targets);
234 INIT_LIST_HEAD(&ctx->schemes);
239 static void damon_destroy_targets(struct damon_ctx *ctx)
241 struct damon_target *t, *next_t;
243 if (ctx->primitive.cleanup) {
244 ctx->primitive.cleanup(ctx);
248 damon_for_each_target_safe(t, next_t, ctx)
249 damon_destroy_target(t);
252 void damon_destroy_ctx(struct damon_ctx *ctx)
254 struct damos *s, *next_s;
256 damon_destroy_targets(ctx);
258 damon_for_each_scheme_safe(s, next_s, ctx)
259 damon_destroy_scheme(s);
265 * damon_set_targets() - Set monitoring targets.
266 * @ctx: monitoring context
267 * @ids: array of target ids
268 * @nr_ids: number of entries in @ids
270 * This function should not be called while the kdamond is running.
272 * Return: 0 on success, negative error code otherwise.
274 int damon_set_targets(struct damon_ctx *ctx,
275 unsigned long *ids, ssize_t nr_ids)
278 struct damon_target *t, *next;
280 damon_destroy_targets(ctx);
282 for (i = 0; i < nr_ids; i++) {
283 t = damon_new_target(ids[i]);
285 /* The caller should do cleanup of the ids itself */
286 damon_for_each_target_safe(t, next, ctx)
287 damon_destroy_target(t);
290 damon_add_target(ctx, t);
297 * damon_set_attrs() - Set attributes for the monitoring.
298 * @ctx: monitoring context
299 * @sample_int: time interval between samplings
300 * @aggr_int: time interval between aggregations
301 * @primitive_upd_int: time interval between monitoring primitive updates
302 * @min_nr_reg: minimal number of regions
303 * @max_nr_reg: maximum number of regions
305 * This function should not be called while the kdamond is running.
306 * Every time interval is in micro-seconds.
308 * Return: 0 on success, negative error code otherwise.
310 int damon_set_attrs(struct damon_ctx *ctx, unsigned long sample_int,
311 unsigned long aggr_int, unsigned long primitive_upd_int,
312 unsigned long min_nr_reg, unsigned long max_nr_reg)
316 if (min_nr_reg > max_nr_reg)
319 ctx->sample_interval = sample_int;
320 ctx->aggr_interval = aggr_int;
321 ctx->primitive_update_interval = primitive_upd_int;
322 ctx->min_nr_regions = min_nr_reg;
323 ctx->max_nr_regions = max_nr_reg;
329 * damon_set_schemes() - Set data access monitoring based operation schemes.
330 * @ctx: monitoring context
331 * @schemes: array of the schemes
332 * @nr_schemes: number of entries in @schemes
334 * This function should not be called while the kdamond of the context is
337 * Return: 0 if success, or negative error code otherwise.
339 int damon_set_schemes(struct damon_ctx *ctx, struct damos **schemes,
342 struct damos *s, *next;
345 damon_for_each_scheme_safe(s, next, ctx)
346 damon_destroy_scheme(s);
347 for (i = 0; i < nr_schemes; i++)
348 damon_add_scheme(ctx, schemes[i]);
353 * damon_nr_running_ctxs() - Return number of currently running contexts.
355 int damon_nr_running_ctxs(void)
359 mutex_lock(&damon_lock);
360 nr_ctxs = nr_running_ctxs;
361 mutex_unlock(&damon_lock);
366 /* Returns the size upper limit for each monitoring region */
367 static unsigned long damon_region_sz_limit(struct damon_ctx *ctx)
369 struct damon_target *t;
370 struct damon_region *r;
371 unsigned long sz = 0;
373 damon_for_each_target(t, ctx) {
374 damon_for_each_region(r, t)
375 sz += r->ar.end - r->ar.start;
378 if (ctx->min_nr_regions)
379 sz /= ctx->min_nr_regions;
380 if (sz < DAMON_MIN_REGION)
381 sz = DAMON_MIN_REGION;
386 static int kdamond_fn(void *data);
389 * __damon_start() - Starts monitoring with given context.
390 * @ctx: monitoring context
392 * This function should be called while damon_lock is hold.
394 * Return: 0 on success, negative error code otherwise.
396 static int __damon_start(struct damon_ctx *ctx)
400 mutex_lock(&ctx->kdamond_lock);
403 ctx->kdamond = kthread_run(kdamond_fn, ctx, "kdamond.%d",
405 if (IS_ERR(ctx->kdamond)) {
406 err = PTR_ERR(ctx->kdamond);
410 mutex_unlock(&ctx->kdamond_lock);
416 * damon_start() - Starts the monitorings for a given group of contexts.
417 * @ctxs: an array of the pointers for contexts to start monitoring
418 * @nr_ctxs: size of @ctxs
420 * This function starts a group of monitoring threads for a group of monitoring
421 * contexts. One thread per each context is created and run in parallel. The
422 * caller should handle synchronization between the threads by itself. If a
423 * group of threads that created by other 'damon_start()' call is currently
424 * running, this function does nothing but returns -EBUSY.
426 * Return: 0 on success, negative error code otherwise.
428 int damon_start(struct damon_ctx **ctxs, int nr_ctxs)
433 mutex_lock(&damon_lock);
434 if (nr_running_ctxs) {
435 mutex_unlock(&damon_lock);
439 for (i = 0; i < nr_ctxs; i++) {
440 err = __damon_start(ctxs[i]);
445 mutex_unlock(&damon_lock);
451 * __damon_stop() - Stops monitoring of given context.
452 * @ctx: monitoring context
454 * Return: 0 on success, negative error code otherwise.
456 static int __damon_stop(struct damon_ctx *ctx)
458 struct task_struct *tsk;
460 mutex_lock(&ctx->kdamond_lock);
463 get_task_struct(tsk);
464 mutex_unlock(&ctx->kdamond_lock);
466 put_task_struct(tsk);
469 mutex_unlock(&ctx->kdamond_lock);
475 * damon_stop() - Stops the monitorings for a given group of contexts.
476 * @ctxs: an array of the pointers for contexts to stop monitoring
477 * @nr_ctxs: size of @ctxs
479 * Return: 0 on success, negative error code otherwise.
481 int damon_stop(struct damon_ctx **ctxs, int nr_ctxs)
485 for (i = 0; i < nr_ctxs; i++) {
486 /* nr_running_ctxs is decremented in kdamond_fn */
487 err = __damon_stop(ctxs[i]);
496 * damon_check_reset_time_interval() - Check if a time interval is elapsed.
497 * @baseline: the time to check whether the interval has elapsed since
498 * @interval: the time interval (microseconds)
500 * See whether the given time interval has passed since the given baseline
501 * time. If so, it also updates the baseline to current time for next check.
503 * Return: true if the time interval has passed, or false otherwise.
505 static bool damon_check_reset_time_interval(struct timespec64 *baseline,
506 unsigned long interval)
508 struct timespec64 now;
510 ktime_get_coarse_ts64(&now);
511 if ((timespec64_to_ns(&now) - timespec64_to_ns(baseline)) <
519 * Check whether it is time to flush the aggregated information
521 static bool kdamond_aggregate_interval_passed(struct damon_ctx *ctx)
523 return damon_check_reset_time_interval(&ctx->last_aggregation,
528 * Reset the aggregated monitoring results ('nr_accesses' of each region).
530 static void kdamond_reset_aggregated(struct damon_ctx *c)
532 struct damon_target *t;
534 damon_for_each_target(t, c) {
535 struct damon_region *r;
537 damon_for_each_region(r, t) {
538 trace_damon_aggregated(t, r, damon_nr_regions(t));
539 r->last_nr_accesses = r->nr_accesses;
545 static void damon_split_region_at(struct damon_ctx *ctx,
546 struct damon_target *t, struct damon_region *r,
549 static bool __damos_valid_target(struct damon_region *r, struct damos *s)
553 sz = r->ar.end - r->ar.start;
554 return s->min_sz_region <= sz && sz <= s->max_sz_region &&
555 s->min_nr_accesses <= r->nr_accesses &&
556 r->nr_accesses <= s->max_nr_accesses &&
557 s->min_age_region <= r->age && r->age <= s->max_age_region;
560 static bool damos_valid_target(struct damon_ctx *c, struct damon_target *t,
561 struct damon_region *r, struct damos *s)
563 bool ret = __damos_valid_target(r, s);
565 if (!ret || !s->quota.esz || !c->primitive.get_scheme_score)
568 return c->primitive.get_scheme_score(c, t, r, s) >= s->quota.min_score;
571 static void damon_do_apply_schemes(struct damon_ctx *c,
572 struct damon_target *t,
573 struct damon_region *r)
577 damon_for_each_scheme(s, c) {
578 struct damos_quota *quota = &s->quota;
579 unsigned long sz = r->ar.end - r->ar.start;
580 struct timespec64 begin, end;
582 if (!s->wmarks.activated)
585 /* Check the quota */
586 if (quota->esz && quota->charged_sz >= quota->esz)
589 /* Skip previously charged regions */
590 if (quota->charge_target_from) {
591 if (t != quota->charge_target_from)
593 if (r == damon_last_region(t)) {
594 quota->charge_target_from = NULL;
595 quota->charge_addr_from = 0;
598 if (quota->charge_addr_from &&
599 r->ar.end <= quota->charge_addr_from)
602 if (quota->charge_addr_from && r->ar.start <
603 quota->charge_addr_from) {
604 sz = ALIGN_DOWN(quota->charge_addr_from -
605 r->ar.start, DAMON_MIN_REGION);
607 if (r->ar.end - r->ar.start <=
610 sz = DAMON_MIN_REGION;
612 damon_split_region_at(c, t, r, sz);
613 r = damon_next_region(r);
614 sz = r->ar.end - r->ar.start;
616 quota->charge_target_from = NULL;
617 quota->charge_addr_from = 0;
620 if (!damos_valid_target(c, t, r, s))
623 /* Apply the scheme */
624 if (c->primitive.apply_scheme) {
626 quota->charged_sz + sz > quota->esz) {
627 sz = ALIGN_DOWN(quota->esz - quota->charged_sz,
631 damon_split_region_at(c, t, r, sz);
633 ktime_get_coarse_ts64(&begin);
634 c->primitive.apply_scheme(c, t, r, s);
635 ktime_get_coarse_ts64(&end);
636 quota->total_charged_ns += timespec64_to_ns(&end) -
637 timespec64_to_ns(&begin);
638 quota->charged_sz += sz;
639 if (quota->esz && quota->charged_sz >= quota->esz) {
640 quota->charge_target_from = t;
641 quota->charge_addr_from = r->ar.end + 1;
644 if (s->action != DAMOS_STAT)
653 /* Shouldn't be called if quota->ms and quota->sz are zero */
654 static void damos_set_effective_quota(struct damos_quota *quota)
656 unsigned long throughput;
660 quota->esz = quota->sz;
664 if (quota->total_charged_ns)
665 throughput = quota->total_charged_sz * 1000000 /
666 quota->total_charged_ns;
668 throughput = PAGE_SIZE * 1024;
669 esz = throughput * quota->ms;
671 if (quota->sz && quota->sz < esz)
676 static void kdamond_apply_schemes(struct damon_ctx *c)
678 struct damon_target *t;
679 struct damon_region *r, *next_r;
682 damon_for_each_scheme(s, c) {
683 struct damos_quota *quota = &s->quota;
684 unsigned long cumulated_sz;
685 unsigned int score, max_score = 0;
687 if (!s->wmarks.activated)
690 if (!quota->ms && !quota->sz)
693 /* New charge window starts */
694 if (time_after_eq(jiffies, quota->charged_from +
696 quota->reset_interval))) {
697 quota->total_charged_sz += quota->charged_sz;
698 quota->charged_from = jiffies;
699 quota->charged_sz = 0;
700 damos_set_effective_quota(quota);
703 if (!c->primitive.get_scheme_score)
706 /* Fill up the score histogram */
707 memset(quota->histogram, 0, sizeof(quota->histogram));
708 damon_for_each_target(t, c) {
709 damon_for_each_region(r, t) {
710 if (!__damos_valid_target(r, s))
712 score = c->primitive.get_scheme_score(
714 quota->histogram[score] +=
715 r->ar.end - r->ar.start;
716 if (score > max_score)
721 /* Set the min score limit */
722 for (cumulated_sz = 0, score = max_score; ; score--) {
723 cumulated_sz += quota->histogram[score];
724 if (cumulated_sz >= quota->esz || !score)
727 quota->min_score = score;
730 damon_for_each_target(t, c) {
731 damon_for_each_region_safe(r, next_r, t)
732 damon_do_apply_schemes(c, t, r);
736 #define sz_damon_region(r) (r->ar.end - r->ar.start)
739 * Merge two adjacent regions into one region
741 static void damon_merge_two_regions(struct damon_target *t,
742 struct damon_region *l, struct damon_region *r)
744 unsigned long sz_l = sz_damon_region(l), sz_r = sz_damon_region(r);
746 l->nr_accesses = (l->nr_accesses * sz_l + r->nr_accesses * sz_r) /
748 l->age = (l->age * sz_l + r->age * sz_r) / (sz_l + sz_r);
749 l->ar.end = r->ar.end;
750 damon_destroy_region(r, t);
753 #define diff_of(a, b) (a > b ? a - b : b - a)
756 * Merge adjacent regions having similar access frequencies
758 * t target affected by this merge operation
759 * thres '->nr_accesses' diff threshold for the merge
760 * sz_limit size upper limit of each region
762 static void damon_merge_regions_of(struct damon_target *t, unsigned int thres,
763 unsigned long sz_limit)
765 struct damon_region *r, *prev = NULL, *next;
767 damon_for_each_region_safe(r, next, t) {
768 if (diff_of(r->nr_accesses, r->last_nr_accesses) > thres)
773 if (prev && prev->ar.end == r->ar.start &&
774 diff_of(prev->nr_accesses, r->nr_accesses) <= thres &&
775 sz_damon_region(prev) + sz_damon_region(r) <= sz_limit)
776 damon_merge_two_regions(t, prev, r);
783 * Merge adjacent regions having similar access frequencies
785 * threshold '->nr_accesses' diff threshold for the merge
786 * sz_limit size upper limit of each region
788 * This function merges monitoring target regions which are adjacent and their
789 * access frequencies are similar. This is for minimizing the monitoring
790 * overhead under the dynamically changeable access pattern. If a merge was
791 * unnecessarily made, later 'kdamond_split_regions()' will revert it.
793 static void kdamond_merge_regions(struct damon_ctx *c, unsigned int threshold,
794 unsigned long sz_limit)
796 struct damon_target *t;
798 damon_for_each_target(t, c)
799 damon_merge_regions_of(t, threshold, sz_limit);
803 * Split a region in two
805 * r the region to be split
806 * sz_r size of the first sub-region that will be made
808 static void damon_split_region_at(struct damon_ctx *ctx,
809 struct damon_target *t, struct damon_region *r,
812 struct damon_region *new;
814 new = damon_new_region(r->ar.start + sz_r, r->ar.end);
818 r->ar.end = new->ar.start;
821 new->last_nr_accesses = r->last_nr_accesses;
823 damon_insert_region(new, r, damon_next_region(r), t);
826 /* Split every region in the given target into 'nr_subs' regions */
827 static void damon_split_regions_of(struct damon_ctx *ctx,
828 struct damon_target *t, int nr_subs)
830 struct damon_region *r, *next;
831 unsigned long sz_region, sz_sub = 0;
834 damon_for_each_region_safe(r, next, t) {
835 sz_region = r->ar.end - r->ar.start;
837 for (i = 0; i < nr_subs - 1 &&
838 sz_region > 2 * DAMON_MIN_REGION; i++) {
840 * Randomly select size of left sub-region to be at
841 * least 10 percent and at most 90% of original region
843 sz_sub = ALIGN_DOWN(damon_rand(1, 10) *
844 sz_region / 10, DAMON_MIN_REGION);
845 /* Do not allow blank region */
846 if (sz_sub == 0 || sz_sub >= sz_region)
849 damon_split_region_at(ctx, t, r, sz_sub);
856 * Split every target region into randomly-sized small regions
858 * This function splits every target region into random-sized small regions if
859 * current total number of the regions is equal or smaller than half of the
860 * user-specified maximum number of regions. This is for maximizing the
861 * monitoring accuracy under the dynamically changeable access patterns. If a
862 * split was unnecessarily made, later 'kdamond_merge_regions()' will revert
865 static void kdamond_split_regions(struct damon_ctx *ctx)
867 struct damon_target *t;
868 unsigned int nr_regions = 0;
869 static unsigned int last_nr_regions;
870 int nr_subregions = 2;
872 damon_for_each_target(t, ctx)
873 nr_regions += damon_nr_regions(t);
875 if (nr_regions > ctx->max_nr_regions / 2)
878 /* Maybe the middle of the region has different access frequency */
879 if (last_nr_regions == nr_regions &&
880 nr_regions < ctx->max_nr_regions / 3)
883 damon_for_each_target(t, ctx)
884 damon_split_regions_of(ctx, t, nr_subregions);
886 last_nr_regions = nr_regions;
890 * Check whether it is time to check and apply the target monitoring regions
892 * Returns true if it is.
894 static bool kdamond_need_update_primitive(struct damon_ctx *ctx)
896 return damon_check_reset_time_interval(&ctx->last_primitive_update,
897 ctx->primitive_update_interval);
901 * Check whether current monitoring should be stopped
903 * The monitoring is stopped when either the user requested to stop, or all
904 * monitoring targets are invalid.
906 * Returns true if need to stop current monitoring.
908 static bool kdamond_need_stop(struct damon_ctx *ctx)
910 struct damon_target *t;
912 if (kthread_should_stop())
915 if (!ctx->primitive.target_valid)
918 damon_for_each_target(t, ctx) {
919 if (ctx->primitive.target_valid(t))
926 static unsigned long damos_wmark_metric_value(enum damos_wmark_metric metric)
931 case DAMOS_WMARK_FREE_MEM_RATE:
933 return i.freeram * 1000 / i.totalram;
941 * Returns zero if the scheme is active. Else, returns time to wait for next
942 * watermark check in micro-seconds.
944 static unsigned long damos_wmark_wait_us(struct damos *scheme)
946 unsigned long metric;
948 if (scheme->wmarks.metric == DAMOS_WMARK_NONE)
951 metric = damos_wmark_metric_value(scheme->wmarks.metric);
952 /* higher than high watermark or lower than low watermark */
953 if (metric > scheme->wmarks.high || scheme->wmarks.low > metric) {
954 if (scheme->wmarks.activated)
955 pr_debug("deactivate a scheme (%d) for %s wmark\n",
957 metric > scheme->wmarks.high ?
959 scheme->wmarks.activated = false;
960 return scheme->wmarks.interval;
963 /* inactive and higher than middle watermark */
964 if ((scheme->wmarks.high >= metric && metric >= scheme->wmarks.mid) &&
965 !scheme->wmarks.activated)
966 return scheme->wmarks.interval;
968 if (!scheme->wmarks.activated)
969 pr_debug("activate a scheme (%d)\n", scheme->action);
970 scheme->wmarks.activated = true;
974 static void kdamond_usleep(unsigned long usecs)
976 /* See Documentation/timers/timers-howto.rst for the thresholds */
977 if (usecs > 20 * USEC_PER_MSEC)
978 schedule_timeout_idle(usecs_to_jiffies(usecs));
980 usleep_idle_range(usecs, usecs + 1);
983 /* Returns negative error code if it's not activated but should return */
984 static int kdamond_wait_activation(struct damon_ctx *ctx)
987 unsigned long wait_time;
988 unsigned long min_wait_time = 0;
990 while (!kdamond_need_stop(ctx)) {
991 damon_for_each_scheme(s, ctx) {
992 wait_time = damos_wmark_wait_us(s);
993 if (!min_wait_time || wait_time < min_wait_time)
994 min_wait_time = wait_time;
999 kdamond_usleep(min_wait_time);
1005 * The monitoring daemon that runs as a kernel thread
1007 static int kdamond_fn(void *data)
1009 struct damon_ctx *ctx = (struct damon_ctx *)data;
1010 struct damon_target *t;
1011 struct damon_region *r, *next;
1012 unsigned int max_nr_accesses = 0;
1013 unsigned long sz_limit = 0;
1016 pr_debug("kdamond (%d) starts\n", current->pid);
1018 if (ctx->primitive.init)
1019 ctx->primitive.init(ctx);
1020 if (ctx->callback.before_start && ctx->callback.before_start(ctx))
1023 sz_limit = damon_region_sz_limit(ctx);
1025 while (!kdamond_need_stop(ctx) && !done) {
1026 if (kdamond_wait_activation(ctx))
1029 if (ctx->primitive.prepare_access_checks)
1030 ctx->primitive.prepare_access_checks(ctx);
1031 if (ctx->callback.after_sampling &&
1032 ctx->callback.after_sampling(ctx))
1035 kdamond_usleep(ctx->sample_interval);
1037 if (ctx->primitive.check_accesses)
1038 max_nr_accesses = ctx->primitive.check_accesses(ctx);
1040 if (kdamond_aggregate_interval_passed(ctx)) {
1041 kdamond_merge_regions(ctx,
1042 max_nr_accesses / 10,
1044 if (ctx->callback.after_aggregation &&
1045 ctx->callback.after_aggregation(ctx))
1047 kdamond_apply_schemes(ctx);
1048 kdamond_reset_aggregated(ctx);
1049 kdamond_split_regions(ctx);
1050 if (ctx->primitive.reset_aggregated)
1051 ctx->primitive.reset_aggregated(ctx);
1054 if (kdamond_need_update_primitive(ctx)) {
1055 if (ctx->primitive.update)
1056 ctx->primitive.update(ctx);
1057 sz_limit = damon_region_sz_limit(ctx);
1060 damon_for_each_target(t, ctx) {
1061 damon_for_each_region_safe(r, next, t)
1062 damon_destroy_region(r, t);
1065 if (ctx->callback.before_terminate)
1066 ctx->callback.before_terminate(ctx);
1067 if (ctx->primitive.cleanup)
1068 ctx->primitive.cleanup(ctx);
1070 pr_debug("kdamond (%d) finishes\n", current->pid);
1071 mutex_lock(&ctx->kdamond_lock);
1072 ctx->kdamond = NULL;
1073 mutex_unlock(&ctx->kdamond_lock);
1075 mutex_lock(&damon_lock);
1077 mutex_unlock(&damon_lock);
1082 #include "core-test.h"