mm/damon/core: support multiple metrics for quota goal
authorSeongJae Park <sj@kernel.org>
Mon, 19 Feb 2024 19:44:23 +0000 (11:44 -0800)
committerAndrew Morton <akpm@linux-foundation.org>
Sat, 24 Feb 2024 01:48:28 +0000 (17:48 -0800)
DAMOS quota auto-tuning asks users to assess the current tuned quota and
provide the feedback in a manual and repeated way.  It allows users
generate the feedback from a source that the kernel cannot access, and
writing a script or a function for doing the manual and repeated feeding
is not a big deal.  However, additional works are additional works, and it
could be more efficient if DAMOS could do the fetch itself, especially in
case of DAMON sysfs interface use case, since it can avoid the context
switches between the user-space and the kernel-space, though the overhead
would be only trivial in most cases.  Also in many cases, feedbacks could
be made from kernel-accessible sources, such as PSI, CPU usage, etc.  Make
the quota goal to support multiple types of metrics including such ones.

Link: https://lkml.kernel.org/r/20240219194431.159606-13-sj@kernel.org
Signed-off-by: SeongJae Park <sj@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
include/linux/damon.h
mm/damon/core.c
mm/damon/sysfs-schemes.c

index de0cdc7f96d2d9804e6f7f969c36692d7bde8fff..5a06993d84794168ae7d425bcb98172685490a49 100644 (file)
@@ -126,18 +126,37 @@ enum damos_action {
        NR_DAMOS_ACTIONS,
 };
 
+/**
+ * enum damos_quota_goal_metric - Represents the metric to be used as the goal
+ *
+ * @DAMOS_QUOTA_USER_INPUT:    User-input value.
+ * @NR_DAMOS_QUOTA_GOAL_METRICS:       Number of DAMOS quota goal metrics.
+ *
+ * Metrics equal to larger than @NR_DAMOS_QUOTA_GOAL_METRICS are unsupported.
+ */
+enum damos_quota_goal_metric {
+       DAMOS_QUOTA_USER_INPUT,
+       NR_DAMOS_QUOTA_GOAL_METRICS,
+};
+
 /**
  * struct damos_quota_goal - DAMOS scheme quota auto-tuning goal.
- * @target_value:      Target value to achieve with the tuning.
- * @current_value:     Current value that achieving with the tuning.
+ * @metric:            Metric to be used for representing the goal.
+ * @target_value:      Target value of @metric to achieve with the tuning.
+ * @current_value:     Current value of @metric.
  * @list:              List head for siblings.
  *
  * Data structure for getting the current score of the quota tuning goal.  The
  * score is calculated by how close @current_value and @target_value are.  Then
  * the score is entered to DAMON's internal feedback loop mechanism to get the
  * auto-tuned quota.
+ *
+ * If @metric is DAMOS_QUOTA_USER_INPUT, @current_value should be manually
+ * entered by the user, probably inside the kdamond callbacks.  Otherwise,
+ * DAMON sets @current_value with self-measured value of @metric.
  */
 struct damos_quota_goal {
+       enum damos_quota_goal_metric metric;
        unsigned long target_value;
        unsigned long current_value;
        struct list_head list;
@@ -689,7 +708,8 @@ void damos_add_filter(struct damos *s, struct damos_filter *f);
 void damos_destroy_filter(struct damos_filter *f);
 
 struct damos_quota_goal *damos_new_quota_goal(
-               unsigned long target_value, unsigned long current_value);
+               enum damos_quota_goal_metric metric,
+               unsigned long target_value);
 void damos_add_quota_goal(struct damos_quota *q, struct damos_quota_goal *g);
 void damos_destroy_quota_goal(struct damos_quota_goal *goal);
 
index 907f467fc8c007a8e801a302e740fcf8c4d7118e..973423166ee28f86ba4c3b29418ebc1f498b8edf 100644 (file)
@@ -300,15 +300,16 @@ void damos_destroy_filter(struct damos_filter *f)
 }
 
 struct damos_quota_goal *damos_new_quota_goal(
-               unsigned long target_value, unsigned long current_value)
+               enum damos_quota_goal_metric metric,
+               unsigned long target_value)
 {
        struct damos_quota_goal *goal;
 
        goal = kmalloc(sizeof(*goal), GFP_KERNEL);
        if (!goal)
                return NULL;
+       goal->metric = metric;
        goal->target_value = target_value;
-       goal->current_value = current_value;
        INIT_LIST_HEAD(&goal->list);
        return goal;
 }
@@ -1124,16 +1125,31 @@ static unsigned long damon_feed_loop_next_input(unsigned long last_input,
        return min_input;
 }
 
+static void damos_set_quota_goal_current_value(struct damos_quota_goal *goal)
+{
+       u64 now_psi_total;
+
+       switch (goal->metric) {
+       case DAMOS_QUOTA_USER_INPUT:
+               /* User should already set goal->current_value */
+               break;
+       default:
+               break;
+       }
+}
+
 /* Return the highest score since it makes schemes least aggressive */
 static unsigned long damos_quota_score(struct damos_quota *quota)
 {
        struct damos_quota_goal *goal;
        unsigned long highest_score = 0;
 
-       damos_for_each_quota_goal(goal, quota)
+       damos_for_each_quota_goal(goal, quota) {
+               damos_set_quota_goal_current_value(goal);
                highest_score = max(highest_score,
                                goal->current_value * 10000 /
                                goal->target_value);
+       }
 
        return highest_score;
 }
index 50218a7bfa0a681d8cee753ad42581602da66566..7a8a39f2679b7329f501ef6ffb4adc81c06a38e1 100644 (file)
@@ -1899,10 +1899,11 @@ static int damos_sysfs_set_quota_score(
                if (!sysfs_goal->target_value)
                        continue;
 
-               goal = damos_new_quota_goal(sysfs_goal->target_value,
-                               sysfs_goal->current_value);
+               goal = damos_new_quota_goal(DAMOS_QUOTA_USER_INPUT,
+                               sysfs_goal->target_value);
                if (!goal)
                        return -ENOMEM;
+               goal->current_value = sysfs_goal->current_value;
                damos_add_quota_goal(quota, goal);
        }
        return 0;