Skip to contentSkip to conversation

mm/damon: simplify scheme create in lru_sort.c

PATCH Superseded Xin Hao 2 participants 3 messages Sep 14, 2022 – Sep 15, 2022

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Xin Hao

author
#0 lore raw

PATCH v2 1/1 mm/damon: simplify scheme create in lru_sort.c

In damon_lru_sort_new_hot_scheme() and damon_lru_sort_new_cold_scheme(), they have so much in common, so we can combine them into a single function, and we just need to distinguish their differences.

---
 mm/damon/lru_sort.c | 57 ++++++++++++++++++++++-----------------------
 1 file changed, 28 insertions(+), 29 deletions(-)
mm/damon/lru_sort.c +28 -29
index 07a0908963fd..2eac907e866d 100644
@@ -135,17 +135,40 @@ DEFINE_DAMON_MODULES_DAMOS_STATS_PARAMS(damon_lru_sort_cold_stat,
 static struct damon_ctx *ctx;
 static struct damon_target *target;
 
-static struct damos *damon_lru_sort_new_scheme(
-		struct damos_access_pattern *pattern, enum damos_action action)
+static struct damos *damon_lru_sort_new_scheme(unsigned int thres,
+					       enum damos_action action)
 {
+	struct damos_access_pattern pattern = {
+		/* Find regions having PAGE_SIZE or larger size */
+		.min_sz_region = PAGE_SIZE,
+		.max_sz_region = ULONG_MAX,
+		/* and accessed for more than the threshold */
+		.min_nr_accesses = 0,
+		.max_nr_accesses = 0,
+		/* no matter its age */
+		.min_age_region = 0,
+		.max_age_region = UINT_MAX,
+	};
 	struct damos_quota quota = damon_lru_sort_quota;
 
 	/* Use half of total quota for hot/cold pages sorting */
 	quota.ms = quota.ms / 2;
 
+	switch (action) {
+	case DAMOS_LRU_PRIO:
+		pattern.min_nr_accesses = thres;
+		pattern.max_nr_accesses = UINT_MAX;
+		break;
+	case DAMOS_LRU_DEPRIO:
+		pattern.min_age_region = thres;
+		break;
+	default:
+		return NULL;
+	}
+
 	return damon_new_scheme(
 			/* find the pattern, and */
-			pattern,
+			&pattern,
 			/* (de)prioritize on LRU-lists */
 			action,
 			/* under the quota. */
@@ -157,37 +180,13 @@ static struct damos *damon_lru_sort_new_scheme(
 /* Create a DAMON-based operation scheme for hot memory regions */
 static struct damos *damon_lru_sort_new_hot_scheme(unsigned int hot_thres)
 {
-	struct damos_access_pattern pattern = {
-		/* Find regions having PAGE_SIZE or larger size */
-		.min_sz_region = PAGE_SIZE,
-		.max_sz_region = ULONG_MAX,
-		/* and accessed for more than the threshold */
-		.min_nr_accesses = hot_thres,
-		.max_nr_accesses = UINT_MAX,
-		/* no matter its age */
-		.min_age_region = 0,
-		.max_age_region = UINT_MAX,
-	};
-
-	return damon_lru_sort_new_scheme(&pattern, DAMOS_LRU_PRIO);
+	return damon_lru_sort_new_scheme(hot_thres, DAMOS_LRU_PRIO);
 }
 
 /* Create a DAMON-based operation scheme for cold memory regions */
 static struct damos *damon_lru_sort_new_cold_scheme(unsigned int cold_thres)
 {
-	struct damos_access_pattern pattern = {
-		/* Find regions having PAGE_SIZE or larger size */
-		.min_sz_region = PAGE_SIZE,
-		.max_sz_region = ULONG_MAX,
-		/* and not accessed at all */
-		.min_nr_accesses = 0,
-		.max_nr_accesses = 0,
-		/* for min_age or more micro-seconds */
-		.min_age_region = cold_thres,
-		.max_age_region = UINT_MAX,
-	};
-
-	return damon_lru_sort_new_scheme(&pattern, DAMOS_LRU_DEPRIO);
+	return damon_lru_sort_new_scheme(cold_thres, DAMOS_LRU_DEPRIO);
 }
 
 static int damon_lru_sort_apply_parameters(void)
Signature · 1 line
2.31.0
maintainer
#1 lore raw

Hi Xin,

Quoted 3 lines · On Wed, 14 Sep 2022 19:38:59 +0800 Xin Hao <[email protected]> wrote:

In damon_lru_sort_new_hot_scheme() and damon_lru_sort_new_cold_scheme(), they have so much in common, so we can combine them into a single function, and we just need to distinguish their differences.

Thank you again for patiently waiting for my changes and reworking on this!

Quoted 14 lines
---
 mm/damon/lru_sort.c | 57 ++++++++++++++++++++++-----------------------
 1 file changed, 28 insertions(+), 29 deletions(-)
mm/damon/lru_sort.c +0 -0
index 07a0908963fd..2eac907e866d 100644
@@ -135,17 +135,40 @@ DEFINE_DAMON_MODULES_DAMOS_STATS_PARAMS(damon_lru_sort_cold_stat,
 static struct damon_ctx *ctx;
 static struct damon_target *target;
 

mm/damon/lru_sort.c DEFINE_DAMON_MODULES_DAMOS_STATS_PARAMS(damon_lru_sort_cold_stat, · line 145

Quoted 10 lines
mm/damon/lru_sort.c +7 -2
-static struct damos *damon_lru_sort_new_scheme(
-		struct damos_access_pattern *pattern, enum damos_action action)
+static struct damos *damon_lru_sort_new_scheme(unsigned int thres,
+					       enum damos_action action)
 {
+	struct damos_access_pattern pattern = {
+		/* Find regions having PAGE_SIZE or larger size */
+		.min_sz_region = PAGE_SIZE,
+		.max_sz_region = ULONG_MAX,
+		/* and accessed for more than the threshold */

This comment would be better to be written again?

mm/damon/lru_sort.c DEFINE_DAMON_MODULES_DAMOS_STATS_PARAMS(damon_lru_sort_cold_stat, · line 147

Quoted 2 lines
mm/damon/lru_sort.c +2 -0
+		.min_nr_accesses = 0,
+		.max_nr_accesses = 0,

If we're gonna set above two fields anyway later, we could simply remove above three lines.

Quoted 11 lines
mm/damon/lru_sort.c +6 -0
+		/* no matter its age */
+		.min_age_region = 0,
+		.max_age_region = UINT_MAX,
+	};
 	struct damos_quota quota = damon_lru_sort_quota;
 
 	/* Use half of total quota for hot/cold pages sorting */
 	quota.ms = quota.ms / 2;
 
+	switch (action) {
+	case DAMOS_LRU_PRIO:

mm/damon/lru_sort.c DEFINE_DAMON_MODULES_DAMOS_STATS_PARAMS(damon_lru_sort_cold_stat, · line 168

Quoted 10 lines
mm/damon/lru_sort.c +10 -0
+		pattern.min_nr_accesses = thres;
+		pattern.max_nr_accesses = UINT_MAX;
+		break;
+	case DAMOS_LRU_DEPRIO:
+		pattern.min_age_region = thres;
+		break;
+	default:
+		return NULL;
+	}
+

This switch-case makes me wondering if the 'default' case really possible case. I think it would be clearer to set these from caller.

IMHO, it might be clearer and shorter to define a static global 'struct damos_access_pattern' stub variable, and make the damon_lru_sort_new_{hot,cold}_scheme() copies it to their local variable, update the relevant fields, and pass that to 'damon_new_scheme()'. What do you think?

Quoted 15 lines
mm/damon/lru_sort.c +1 -5
 	return damon_new_scheme(
 			/* find the pattern, and */
-			pattern,
+			&pattern,
 			/* (de)prioritize on LRU-lists */
 			action,
 			/* under the quota. */
@@ -157,37 +180,13 @@ static struct damos *damon_lru_sort_new_scheme(
 /* Create a DAMON-based operation scheme for hot memory regions */
 static struct damos *damon_lru_sort_new_hot_scheme(unsigned int hot_thres)
 {
-	struct damos_access_pattern pattern = {
-		/* Find regions having PAGE_SIZE or larger size */
-		.min_sz_region = PAGE_SIZE,
-		.max_sz_region = ULONG_MAX,

mm/damon/lru_sort.c static struct damos *damon_lru_sort_new_scheme( · line 183

Quoted 10 lines
mm/damon/lru_sort.c +1 -9
-		/* and accessed for more than the threshold */
-		.min_nr_accesses = hot_thres,
-		.max_nr_accesses = UINT_MAX,
-		/* no matter its age */
-		.min_age_region = 0,
-		.max_age_region = UINT_MAX,
-	};
-
-	return damon_lru_sort_new_scheme(&pattern, DAMOS_LRU_PRIO);
+	return damon_lru_sort_new_scheme(hot_thres, DAMOS_LRU_PRIO);

If we follow what I suggested above, we could make this like below:

	struct damos_access_pattern pattern = damon_lru_sort_stub_access_pattern;

	pattern.min_nr_accesses = hot_thres;
	return damon_lru_sort_new_scheme(&pattern, DAMOS_LRU_PRIO);
Quoted 9 lines
mm/damon/lru_sort.c +0 -4
 }
 
 /* Create a DAMON-based operation scheme for cold memory regions */
 static struct damos *damon_lru_sort_new_cold_scheme(unsigned int cold_thres)
 {
-	struct damos_access_pattern pattern = {
-		/* Find regions having PAGE_SIZE or larger size */
-		.min_sz_region = PAGE_SIZE,
-		.max_sz_region = ULONG_MAX,

mm/damon/lru_sort.c static struct damos *damon_lru_sort_new_scheme( · line 189

Quoted 10 lines
mm/damon/lru_sort.c +1 -9
-		/* and not accessed at all */
-		.min_nr_accesses = 0,
-		.max_nr_accesses = 0,
-		/* for min_age or more micro-seconds */
-		.min_age_region = cold_thres,
-		.max_age_region = UINT_MAX,
-	};
-
-	return damon_lru_sort_new_scheme(&pattern, DAMOS_LRU_DEPRIO);
+	return damon_lru_sort_new_scheme(cold_thres, DAMOS_LRU_DEPRIO);

And similarly here.

Quoted 7 lines
mm/damon/lru_sort.c +0 -0
 }
 
 static int damon_lru_sort_apply_parameters(void)
Signature · 1 line
2.31.0

Thanks,
SJ

author
#2 lore raw
Quoted 96 lines · SeongJae Park

Hi Xin,

Quoted 3 lines · On Wed, 14 Sep 2022 19:38:59 +0800 Xin Hao <[email protected]> wrote:

In damon_lru_sort_new_hot_scheme() and damon_lru_sort_new_cold_scheme(), they have so much in common, so we can combine them into a single function, and we just need to distinguish their differences.

Thank you again for patiently waiting for my changes and reworking on this!

Quoted 13 lines
---
  mm/damon/lru_sort.c | 57 ++++++++++++++++++++++-----------------------
  1 file changed, 28 insertions(+), 29 deletions(-)
mm/damon/lru_sort.c +0 -0
index 07a0908963fd..2eac907e866d 100644
@@ -135,17 +135,40 @@ DEFINE_DAMON_MODULES_DAMOS_STATS_PARAMS(damon_lru_sort_cold_stat,
  static struct damon_ctx *ctx;
  static struct damon_target *target;
 

mm/damon/lru_sort.c DEFINE_DAMON_MODULES_DAMOS_STATS_PARAMS(damon_lru_sort_cold_stat, · line 145

Quoted 10 lines
mm/damon/lru_sort.c +7 -2
-static struct damos *damon_lru_sort_new_scheme(
-		struct damos_access_pattern *pattern, enum damos_action action)
+static struct damos *damon_lru_sort_new_scheme(unsigned int thres,
+					       enum damos_action action)
  {
+	struct damos_access_pattern pattern = {
+		/* Find regions having PAGE_SIZE or larger size */
+		.min_sz_region = PAGE_SIZE,
+		.max_sz_region = ULONG_MAX,
+		/* and accessed for more than the threshold */

This comment would be better to be written again?

mm/damon/lru_sort.c DEFINE_DAMON_MODULES_DAMOS_STATS_PARAMS(damon_lru_sort_cold_stat, · line 147

Quoted 2 lines
mm/damon/lru_sort.c +2 -0
+		.min_nr_accesses = 0,
+		.max_nr_accesses = 0,

If we're gonna set above two fields anyway later, we could simply remove above three lines.

Quoted 11 lines
mm/damon/lru_sort.c +6 -0
+		/* no matter its age */
+		.min_age_region = 0,
+		.max_age_region = UINT_MAX,
+	};
  	struct damos_quota quota = damon_lru_sort_quota;
 
  	/* Use half of total quota for hot/cold pages sorting */
  	quota.ms = quota.ms / 2;
 
+	switch (action) {
+	case DAMOS_LRU_PRIO:

mm/damon/lru_sort.c DEFINE_DAMON_MODULES_DAMOS_STATS_PARAMS(damon_lru_sort_cold_stat, · line 168

Quoted 10 lines
mm/damon/lru_sort.c +10 -0
+		pattern.min_nr_accesses = thres;
+		pattern.max_nr_accesses = UINT_MAX;
+		break;
+	case DAMOS_LRU_DEPRIO:
+		pattern.min_age_region = thres;
+		break;
+	default:
+		return NULL;
+	}
+

This switch-case makes me wondering if the 'default' case really possible case. I think it would be clearer to set these from caller.

IMHO, it might be clearer and shorter to define a static global 'struct damos_access_pattern' stub variable, and make the damon_lru_sort_new_{hot,cold}_scheme() copies it to their local variable, update the relevant fields, and pass that to 'damon_new_scheme()'. What do you think?

Quoted 15 lines
mm/damon/lru_sort.c +1 -5
  	return damon_new_scheme(
  			/* find the pattern, and */
-			pattern,
+			&pattern,
  			/* (de)prioritize on LRU-lists */
  			action,
  			/* under the quota. */
@@ -157,37 +180,13 @@ static struct damos *damon_lru_sort_new_scheme(
  /* Create a DAMON-based operation scheme for hot memory regions */
  static struct damos *damon_lru_sort_new_hot_scheme(unsigned int hot_thres)
  {
-	struct damos_access_pattern pattern = {
-		/* Find regions having PAGE_SIZE or larger size */
-		.min_sz_region = PAGE_SIZE,
-		.max_sz_region = ULONG_MAX,

mm/damon/lru_sort.c static struct damos *damon_lru_sort_new_scheme( · line 183

Quoted 10 lines
mm/damon/lru_sort.c +1 -9
-		/* and accessed for more than the threshold */
-		.min_nr_accesses = hot_thres,
-		.max_nr_accesses = UINT_MAX,
-		/* no matter its age */
-		.min_age_region = 0,
-		.max_age_region = UINT_MAX,
-	};
-
-	return damon_lru_sort_new_scheme(&pattern, DAMOS_LRU_PRIO);
+	return damon_lru_sort_new_scheme(hot_thres, DAMOS_LRU_PRIO);

If we follow what I suggested above, we could make this like below:

Quoted 4 lines
	struct damos_access_pattern pattern = damon_lru_sort_stub_access_pattern;

	pattern.min_nr_accesses = hot_thres;
	return damon_lru_sort_new_scheme(&pattern, DAMOS_LRU_PRIO);

Agree totally,  i will fix it in the next patch.

Quoted 31 lines
Quoted 9 lines
mm/damon/lru_sort.c +0 -4
  }
 
  /* Create a DAMON-based operation scheme for cold memory regions */
  static struct damos *damon_lru_sort_new_cold_scheme(unsigned int cold_thres)
  {
-	struct damos_access_pattern pattern = {
-		/* Find regions having PAGE_SIZE or larger size */
-		.min_sz_region = PAGE_SIZE,
-		.max_sz_region = ULONG_MAX,

mm/damon/lru_sort.c static struct damos *damon_lru_sort_new_scheme( · line 189

Quoted 10 lines
mm/damon/lru_sort.c +1 -9
-		/* and not accessed at all */
-		.min_nr_accesses = 0,
-		.max_nr_accesses = 0,
-		/* for min_age or more micro-seconds */
-		.min_age_region = cold_thres,
-		.max_age_region = UINT_MAX,
-	};
-
-	return damon_lru_sort_new_scheme(&pattern, DAMOS_LRU_DEPRIO);
+	return damon_lru_sort_new_scheme(cold_thres, DAMOS_LRU_DEPRIO);

And similarly here.

Quoted 7 lines
mm/damon/lru_sort.c +0 -0
  }
 
  static int damon_lru_sort_apply_parameters(void)
Signature · 1 line
2.31.0

Thanks,
SJ