From patchwork Tue Mar 3 12:14:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 11417853 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A7AFC92A for ; Tue, 3 Mar 2020 12:15:30 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 635E520866 for ; Tue, 3 Mar 2020 12:15:30 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="BKwnX3qH" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 635E520866 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amazon.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id A766E6B0007; Tue, 3 Mar 2020 07:15:29 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id A276F6B0008; Tue, 3 Mar 2020 07:15:29 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 915256B000A; Tue, 3 Mar 2020 07:15:29 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0072.hostedemail.com [216.40.44.72]) by kanga.kvack.org (Postfix) with ESMTP id 7AD956B0007 for ; Tue, 3 Mar 2020 07:15:29 -0500 (EST) Received: from smtpin18.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 2D115181AC9B6 for ; Tue, 3 Mar 2020 12:15:29 +0000 (UTC) X-FDA: 76553946378.18.pull26_6a03e0c06bf5a X-Spam-Summary: 1,0,0,,d41d8cd98f00b204,prvs=3245da543=sjpark@amazon.com,,RULES_HIT:30001:30003:30004:30054:30064:30070,0,RBL:207.171.184.25:@amazon.com:.lbl8.mailshell.net-66.10.201.10 62.18.0.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:1,LUA_SUMMARY:none X-HE-Tag: pull26_6a03e0c06bf5a X-Filterd-Recvd-Size: 9533 Received: from smtp-fw-9101.amazon.com (smtp-fw-9101.amazon.com [207.171.184.25]) by imf31.hostedemail.com (Postfix) with ESMTP for ; Tue, 3 Mar 2020 12:15:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1583237729; x=1614773729; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=XU7MCEqVBpw+dGJYEvQn0yHY813ydWFHwe8qEM26yTk=; b=BKwnX3qHZnQDNWsmtgvX7WMvJJKMSb5XssH599aha5kdnKzTj2d8EXCb QqHuSPqK+vz5QnR95EUGFlYQDcH5+nMcOXqiM2TPKPb8VAbfoNDohwIt0 wYeWPmDYzViACQPz7T9Rg9mRQ22lQCtbVazf3b57RPNAUmQTN7+9qZ2GB E=; IronPort-SDR: W/BJk7aspZ2vT4gMjxsOkUsnBxJMGdTygkEnCXCB0oJuN/WVZkBb014pamLvNShviJS+eYtrtU mFtrlFHje38w== X-IronPort-AV: E=Sophos;i="5.70,511,1574121600"; d="scan'208";a="20475725" Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-2b-81e76b79.us-west-2.amazon.com) ([10.47.23.38]) by smtp-border-fw-out-9101.sea19.amazon.com with ESMTP; 03 Mar 2020 12:15:27 +0000 Received: from EX13MTAUEA002.ant.amazon.com (pdx4-ws-svc-p6-lb7-vlan3.pdx.amazon.com [10.170.41.166]) by email-inbound-relay-2b-81e76b79.us-west-2.amazon.com (Postfix) with ESMTPS id 70916A2743; Tue, 3 Mar 2020 12:15:25 +0000 (UTC) Received: from EX13D31EUA001.ant.amazon.com (10.43.165.15) by EX13MTAUEA002.ant.amazon.com (10.43.61.77) with Microsoft SMTP Server (TLS) id 15.0.1236.3; Tue, 3 Mar 2020 12:15:25 +0000 Received: from u886c93fd17d25d.ant.amazon.com (10.43.160.16) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Tue, 3 Mar 2020 12:15:13 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [RFC v4 2/7] mm/damon: Account age of target regions Date: Tue, 3 Mar 2020 13:14:01 +0100 Message-ID: <20200303121406.20954-3-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200303121406.20954-1-sjpark@amazon.com> References: <20200303121406.20954-1-sjpark@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.43.160.16] X-ClientProxiedBy: EX13D22UWB002.ant.amazon.com (10.43.161.28) To EX13D31EUA001.ant.amazon.com (10.43.165.15) X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: SeongJae Park DAMON can be used as a primitive for data access pattern awared memory maangement optimizations. However, users who want such optimizations should run DAMON, read the monitoring results, analyze it, plan a new memory management scheme, and apply the new scheme by themselves. It would not be too hard, but still require some level of efforts. For complicated optimizations, this effort is inevitable. That said, in many cases, users would simply want to apply an actions to a memory region of a specific size having a specific access frequency for a specific time. For example, "page out a memory region larger than 100 MiB but having a low access frequency more than 10 minutes", or "Use THP for a memory region larger than 2 MiB having a high access frequency for more than 2 seconds". For such optimizations, users will need to first account the age of each region themselves. To reduce such efforts, this commit implements a simple age account of each region in DAMON. For each aggregation step, DAMON compares the access frequency and start/end address of each region with those from last aggregation and reset the age of the region if the change is significant. Else, the age is incremented. Signed-off-by: SeongJae Park --- include/linux/damon.h | 5 +++ mm/damon.c | 80 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 80 insertions(+), 5 deletions(-) diff --git a/include/linux/damon.h b/include/linux/damon.h index 78785cb88d42..50fbe308590e 100644 --- a/include/linux/damon.h +++ b/include/linux/damon.h @@ -22,6 +22,11 @@ struct damon_region { unsigned long sampling_addr; unsigned int nr_accesses; struct list_head list; + + unsigned int age; + unsigned long last_vm_start; + unsigned long last_vm_end; + unsigned int last_nr_accesses; }; /* Represents a monitoring target task */ diff --git a/mm/damon.c b/mm/damon.c index ff150ae7532a..c292ddd36c86 100644 --- a/mm/damon.c +++ b/mm/damon.c @@ -87,6 +87,10 @@ static struct damon_region *damon_new_region(struct damon_ctx *ctx, ret->sampling_addr = damon_rand(ctx, vm_start, vm_end); INIT_LIST_HEAD(&ret->list); + ret->age = 0; + ret->last_vm_start = vm_start; + ret->last_vm_end = vm_end; + return ret; } @@ -600,11 +604,44 @@ static void kdamond_flush_aggregated(struct damon_ctx *c) damon_write_rbuf(c, &r->vm_end, sizeof(r->vm_end)); damon_write_rbuf(c, &r->nr_accesses, sizeof(r->nr_accesses)); + r->last_nr_accesses = r->nr_accesses; r->nr_accesses = 0; } } } +#define diff_of(a, b) (a > b ? a - b : b - a) + +/* + * Increase or reset the age of the given monitoring target region + * + * If the area or '->nr_accesses' has changed significantly, reset the '->age'. + * Else, increase the age. + */ +static void damon_do_count_age(struct damon_region *r, unsigned int threshold) +{ + unsigned long sz_threshold = (r->vm_end - r->vm_start) / 5; + + if (diff_of(r->vm_start, r->last_vm_start) + + diff_of(r->vm_end, r->last_vm_end) > sz_threshold) + r->age = 0; + else if (diff_of(r->nr_accesses, r->last_nr_accesses) > threshold) + r->age = 0; + else + r->age++; +} + +static void kdamond_count_age(struct damon_ctx *c, unsigned int threshold) +{ + struct damon_task *t; + struct damon_region *r; + + damon_for_each_task(c, t) { + damon_for_each_region(r, t) + damon_do_count_age(r, threshold); + } +} + #define sz_damon_region(r) (r->vm_end - r->vm_start) /* @@ -613,15 +650,15 @@ static void kdamond_flush_aggregated(struct damon_ctx *c) static void damon_merge_two_regions(struct damon_region *l, struct damon_region *r) { - l->nr_accesses = (l->nr_accesses * sz_damon_region(l) + - r->nr_accesses * sz_damon_region(r)) / - (sz_damon_region(l) + sz_damon_region(r)); + unsigned long sz_l = sz_damon_region(l), sz_r = sz_damon_region(r); + + l->nr_accesses = (l->nr_accesses * sz_l + r->nr_accesses * sz_r) / + (sz_l + sz_r); + l->age = (l->age * sz_l + r->age * sz_r) / (sz_l + sz_r); l->vm_end = r->vm_end; damon_destroy_region(r); } -#define diff_of(a, b) (a > b ? a - b : b - a) - /* * Merge adjacent regions having similar access frequencies * @@ -631,17 +668,43 @@ static void damon_merge_two_regions(struct damon_region *l, static void damon_merge_regions_of(struct damon_task *t, unsigned int thres) { struct damon_region *r, *prev = NULL, *next; + unsigned long sz_subregion, last_last_vm = 0; + unsigned long sz_biggest = 0; /* size of the biggest subregion */ + struct region last_biggest; /* last region of the biggest sub */ damon_for_each_region_safe(r, next, t) { if (!prev || prev->vm_end != r->vm_start) goto next; if (diff_of(prev->nr_accesses, r->nr_accesses) > thres) goto next; + if (!sz_biggest) { + sz_biggest = sz_damon_region(prev); + last_biggest.start = prev->last_vm_start; + last_biggest.end = prev->last_vm_end; + } + if (last_last_vm != r->last_vm_start) + sz_subregion = 0; + sz_subregion += sz_damon_region(r); + last_last_vm = r->last_vm_start; + if (sz_subregion > sz_biggest) { + sz_biggest = sz_subregion; + last_biggest.start = r->last_vm_start; + last_biggest.end = r->last_vm_end; + } damon_merge_two_regions(prev, r); continue; next: + if (sz_biggest) { + sz_biggest = 0; + prev->last_vm_start = last_biggest.start; + prev->last_vm_end = last_biggest.end; + } prev = r; } + if (sz_biggest) { + prev->last_vm_start = last_biggest.start; + prev->last_vm_end = last_biggest.end; + } } /* @@ -674,6 +737,12 @@ static void damon_split_region_at(struct damon_ctx *ctx, struct damon_region *new; new = damon_new_region(ctx, r->vm_start + sz_r, r->vm_end); + new->age = r->age; + new->last_vm_start = r->vm_start; + new->last_nr_accesses = r->last_nr_accesses; + + r->last_vm_start = r->vm_start; + r->last_vm_end = r->vm_end; r->vm_end = new->vm_start; damon_add_region(new, r, damon_next_region(r)); @@ -865,6 +934,7 @@ static int kdamond_fn(void *data) if (kdamond_aggregate_interval_passed(ctx)) { kdamond_merge_regions(ctx, max_nr_accesses / 10); + kdamond_count_age(ctx, max_nr_accesses / 10); if (ctx->aggregate_cb) ctx->aggregate_cb(ctx); kdamond_flush_aggregated(ctx);