From patchwork Tue Feb 25 10:22:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 11403175 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 BB99014D5 for ; Tue, 25 Feb 2020 10:23:57 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 88EFD20714 for ; Tue, 25 Feb 2020 10:23:57 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="CvwBMF9x" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 88EFD20714 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 8564D6B0005; Tue, 25 Feb 2020 05:23:56 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 7DE856B0006; Tue, 25 Feb 2020 05:23:56 -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 6A68E6B0007; Tue, 25 Feb 2020 05:23:56 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0120.hostedemail.com [216.40.44.120]) by kanga.kvack.org (Postfix) with ESMTP id 537276B0005 for ; Tue, 25 Feb 2020 05:23:56 -0500 (EST) Received: from smtpin29.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id DD7B7824556B for ; Tue, 25 Feb 2020 10:23:55 +0000 (UTC) X-FDA: 76528263630.29.rifle95_224189337c732 X-Spam-Summary: 1,0,0,,d41d8cd98f00b204,prvs=317a3a273=sjpark@amazon.com,,RULES_HIT:30012:30054:30064,0,RBL:207.171.184.29:@amazon.com:.lbl8.mailshell.net-62.18.0.100 66.10.201.10,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:35,LUA_SUMMARY:none X-HE-Tag: rifle95_224189337c732 X-Filterd-Recvd-Size: 4623 Received: from smtp-fw-9102.amazon.com (smtp-fw-9102.amazon.com [207.171.184.29]) by imf22.hostedemail.com (Postfix) with ESMTP for ; Tue, 25 Feb 2020 10:23:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1582626236; x=1614162236; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=kAdTeVn39NF6MtyWxbSfFrMuEluavNfxQ8uqZzEFpes=; b=CvwBMF9x9ZRNs8j5dpaKJEoOgEFZhZv3nWXc/ZhApUEniaIcEQZJXihu UwUG4pDXRgCN74MsgDgCAyqZHooHNfqXw8tkM+hUTl0bXxz3WP76t0n8k Gsxh8+1UM6EnYk2B1yZq6i9Lfpps+hTmiLl7WN4mQEpsbHX5J7h7fThhz E=; IronPort-SDR: 6wwrgkGYYv8CpR1fY1H7m84KIHG0aC7W/f56Ef/NEvS6zsJ76rwI6GR2ureJsBKf5rUl3zLmHN 0DU74Z+OjKEQ== X-IronPort-AV: E=Sophos;i="5.70,483,1574121600"; d="scan'208";a="27313100" Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-1e-97fdccfd.us-east-1.amazon.com) ([10.47.23.38]) by smtp-border-fw-out-9102.sea19.amazon.com with ESMTP; 25 Feb 2020 10:23:50 +0000 Received: from EX13MTAUEA002.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan3.iad.amazon.com [10.40.159.166]) by email-inbound-relay-1e-97fdccfd.us-east-1.amazon.com (Postfix) with ESMTPS id 2B969A2B80; Tue, 25 Feb 2020 10:23:40 +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, 25 Feb 2020 10:23:40 +0000 Received: from u886c93fd17d25d.ant.amazon.com (10.43.162.53) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Tue, 25 Feb 2020 10:23:28 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [RFC v3 1/7] mm/madvise: Export madvise_common() to mm internal code Date: Tue, 25 Feb 2020 11:22:54 +0100 Message-ID: <20200225102300.23895-2-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200225102300.23895-1-sjpark@amazon.com> References: <20200225102300.23895-1-sjpark@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.43.162.53] X-ClientProxiedBy: EX13D30UWC001.ant.amazon.com (10.43.162.128) 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 This commit exports ``madvise_common()`` to ``mm/`` code for future reuse. Signed-off-by: SeongJae Park --- mm/internal.h | 4 ++++ mm/madvise.c | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/mm/internal.h b/mm/internal.h index 3cf20ab3ca01..dcdfe00e02ff 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -576,4 +576,8 @@ static inline bool is_migrate_highatomic_page(struct page *page) void setup_zone_pageset(struct zone *zone); extern struct page *alloc_new_node_page(struct page *page, unsigned long node); + + +int madvise_common(struct task_struct *task, struct mm_struct *mm, + unsigned long start, size_t len_in, int behavior); #endif /* __MM_INTERNAL_H */ diff --git a/mm/madvise.c b/mm/madvise.c index 0c901de531e4..4fa9dfc770bc 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -1005,7 +1005,7 @@ madvise_behavior_valid(int behavior) * @task could be a zombie leader if it calls sys_exit so accessing mm_struct * via task->mm is prohibited. Please use @mm instead of task->mm. */ -static int madvise_common(struct task_struct *task, struct mm_struct *mm, +int madvise_common(struct task_struct *task, struct mm_struct *mm, unsigned long start, size_t len_in, int behavior) { unsigned long end, tmp; @@ -1103,6 +1103,7 @@ static int madvise_common(struct task_struct *task, struct mm_struct *mm, return error; } +EXPORT_SYMBOL_GPL(madvise_common); /* * The madvise(2) system call. From patchwork Tue Feb 25 10:22:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 11403177 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 72E881395 for ; Tue, 25 Feb 2020 10:24:14 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 3170220714 for ; Tue, 25 Feb 2020 10:24:14 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="keknYfUI" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3170220714 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 549426B0006; Tue, 25 Feb 2020 05:24:13 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 4D30B6B0007; Tue, 25 Feb 2020 05:24:13 -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 373136B0008; Tue, 25 Feb 2020 05:24:13 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0100.hostedemail.com [216.40.44.100]) by kanga.kvack.org (Postfix) with ESMTP id 1EDAD6B0006 for ; Tue, 25 Feb 2020 05:24:13 -0500 (EST) Received: from smtpin30.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id D5E86181AC9CB for ; Tue, 25 Feb 2020 10:24:12 +0000 (UTC) X-FDA: 76528264344.30.snow74_24b19ddf9e253 X-Spam-Summary: 1,0,0,,d41d8cd98f00b204,prvs=317a3a273=sjpark@amazon.com,,RULES_HIT:30001:30003:30004:30054:30064:30070,0,RBL:207.171.184.25:@amazon.com:.lbl8.mailshell.net-62.18.0.100 66.10.201.10,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:24,LUA_SUMMARY:none X-HE-Tag: snow74_24b19ddf9e253 X-Filterd-Recvd-Size: 10361 Received: from smtp-fw-9101.amazon.com (smtp-fw-9101.amazon.com [207.171.184.25]) by imf11.hostedemail.com (Postfix) with ESMTP for ; Tue, 25 Feb 2020 10:24:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1582626252; x=1614162252; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=LQ8vfC/Yy4RDqnLc18+rRGkdm38lbp4dLOl8ELTUwIc=; b=keknYfUINmh01vZw0NTeePYo/JizhGTtywds5sYUiEKoEzgPD0YnGSEA CZbqisGrO0KSrOYxFkiEDWRfdz2fu9TYIDpv1qoyvXNWTNwo9lT/R/ZFY zCguEXA+3lBAZ0w3f1uuaOL+wD3LLBvHWAqc0vpFrV4f/vjHI1G7okO7y w=; IronPort-SDR: UUvdNpAW0JpUG04VUWXzNuspKtYpE7VpsjNAPzcsUvT0pdsz6Zk7T1bTrkYhswoMe7fmZuZ9Nh MnKWtAebI16Q== X-IronPort-AV: E=Sophos;i="5.70,483,1574121600"; d="scan'208";a="18935475" Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-1d-74cf8b49.us-east-1.amazon.com) ([10.47.23.38]) by smtp-border-fw-out-9101.sea19.amazon.com with ESMTP; 25 Feb 2020 10:24:08 +0000 Received: from EX13MTAUEA002.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan2.iad.amazon.com [10.40.159.162]) by email-inbound-relay-1d-74cf8b49.us-east-1.amazon.com (Postfix) with ESMTPS id EEBDBC0974; Tue, 25 Feb 2020 10:23:57 +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, 25 Feb 2020 10:23:57 +0000 Received: from u886c93fd17d25d.ant.amazon.com (10.43.162.53) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Tue, 25 Feb 2020 10:23:45 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [RFC v3 2/7] mm/damon: Account age of target regions Date: Tue, 25 Feb 2020 11:22:55 +0100 Message-ID: <20200225102300.23895-3-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200225102300.23895-1-sjpark@amazon.com> References: <20200225102300.23895-1-sjpark@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.43.162.53] X-ClientProxiedBy: EX13D30UWC001.ant.amazon.com (10.43.162.128) 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 | 109 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 109 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..e08dba2e2717 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,35 +650,90 @@ 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 * * t task that merge operation will make change * thres merge regions having '->nr_accesses' diff smaller than this + * + * Merged new region's '->last_vm_(start|end)' will be those of the biggest + * subregion that merged into the new region. If a region is splitted into two + * segments and now both of the two segments are merged into the new region, + * the size of the subregion is the sum of the two segments. For example: + * + * < A > + * split + * < A >< A > + * merge + * < A > + * + * < A >< A > + * split + * < A >< B > + * merge + * < A >< B > + * + * < A >< B > + * split + * < A >< A >< B >< B > + * merge + * < B > + * + * < A >< B > + * split + * < A >< B >< B > + * merge + * < A > */ 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 +766,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 +963,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); From patchwork Tue Feb 25 10:22:56 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 11403179 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 29DEA14D5 for ; Tue, 25 Feb 2020 10:24:46 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id DDA5D20714 for ; Tue, 25 Feb 2020 10:24:45 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="foyIz9S8" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DDA5D20714 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 1C6816B0007; Tue, 25 Feb 2020 05:24:45 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 150786B0008; Tue, 25 Feb 2020 05:24:45 -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 017D46B000A; Tue, 25 Feb 2020 05:24:44 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0151.hostedemail.com [216.40.44.151]) by kanga.kvack.org (Postfix) with ESMTP id DF6886B0007 for ; Tue, 25 Feb 2020 05:24:44 -0500 (EST) Received: from smtpin09.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 9405A2490 for ; Tue, 25 Feb 2020 10:24:44 +0000 (UTC) X-FDA: 76528265688.09.egg64_29555bc0de731 X-Spam-Summary: 1,0,0,,d41d8cd98f00b204,prvs=317a3a273=sjpark@amazon.com,,RULES_HIT:30004:30012:30034:30054:30064:30070,0,RBL:72.21.198.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:23,LUA_SUMMARY:none X-HE-Tag: egg64_29555bc0de731 X-Filterd-Recvd-Size: 11038 Received: from smtp-fw-4101.amazon.com (smtp-fw-4101.amazon.com [72.21.198.25]) by imf35.hostedemail.com (Postfix) with ESMTP for ; Tue, 25 Feb 2020 10:24:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1582626284; x=1614162284; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=cruTKgEz/zvbs2SVoNSJW8F372P48VGKaxC33GYXoBQ=; b=foyIz9S8DrTmHs9yBQrQGV7/z5nGfoObDKmJJq4CEUyNfN8VzJ1odON7 e07oqlTIZWZ4y4mWLxzNlxAYnPnhrfns2P0buhTOSAUGfxK2ya9YkLQpE DfjChOsPSJdm2TQ5kufRv1LHXyPYEvPmnddds4GC/Qhm7ZZdUVpcVwno7 Q=; IronPort-SDR: skDApPe28e6C99O4I83Nc9x8cxfcwci/1dB5YpuvxzA1OelYtHZr7N/2C1xJW/PeFnKfYapmpW C3WNkJeV6o6Q== X-IronPort-AV: E=Sophos;i="5.70,483,1574121600"; d="scan'208";a="18459914" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-1e-97fdccfd.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-out-4101.iad4.amazon.com with ESMTP; 25 Feb 2020 10:24:44 +0000 Received: from EX13MTAUEA002.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan3.iad.amazon.com [10.40.159.166]) by email-inbound-relay-1e-97fdccfd.us-east-1.amazon.com (Postfix) with ESMTPS id 885E2A2B82; Tue, 25 Feb 2020 10:24:34 +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, 25 Feb 2020 10:24:33 +0000 Received: from u886c93fd17d25d.ant.amazon.com (10.43.162.53) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Tue, 25 Feb 2020 10:24:22 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [RFC v3 3/7] mm/damon: Implement data access monitoring-based operation schemes Date: Tue, 25 Feb 2020 11:22:56 +0100 Message-ID: <20200225102300.23895-4-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200225102300.23895-1-sjpark@amazon.com> References: <20200225102300.23895-1-sjpark@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.43.162.53] X-ClientProxiedBy: EX13D30UWC001.ant.amazon.com (10.43.162.128) 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 In many cases, users might use DAMON for simple data access awared memory management optimizations such as applying an operation scheme 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". To minimize users from spending their time for implementation of such simple data access monitoring-based operation schemes, this commit makes DAMON to handle such schemes directly. With this commit, users can simply specify their desired schemes to DAMON. Each of the schemes is composed with conditions for filtering of the target memory regions and desired memory management action for the target. In specific, the format is:: The filtering conditions are size of memory region, number of accesses to the region monitored by DAMON, and the age of the region. The age of region is incremented periodically but reset when its addresses or access frequency has significanly changed or the action of a scheme has applied. For the action, current implementation supports only a few of madvise() hints, ``MADV_WILLNEED``, ``MADV_COLD``, ``MADV_PAGEOUT``, ``MADV_HUGEPAGE``, and ``MADV_NOHUGEPAGE``. Signed-off-by: SeongJae Park --- include/linux/damon.h | 24 ++++++++ mm/damon.c | 134 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+) diff --git a/include/linux/damon.h b/include/linux/damon.h index 50fbe308590e..8cb2452579ee 100644 --- a/include/linux/damon.h +++ b/include/linux/damon.h @@ -36,6 +36,27 @@ struct damon_task { struct list_head list; }; +/* Data Access Monitoring-based Operation Scheme */ +enum damos_action { + DAMOS_WILLNEED, + DAMOS_COLD, + DAMOS_PAGEOUT, + DAMOS_HUGEPAGE, + DAMOS_NOHUGEPAGE, + DAMOS_ACTION_LEN, +}; + +struct damos { + unsigned int min_sz_region; + unsigned int max_sz_region; + unsigned int min_nr_accesses; + unsigned int max_nr_accesses; + unsigned int min_age_region; + unsigned int max_age_region; + enum damos_action action; + struct list_head list; +}; + struct damon_ctx { unsigned long sample_interval; unsigned long aggr_interval; @@ -58,6 +79,7 @@ struct damon_ctx { struct rnd_state rndseed; struct list_head tasks_list; /* 'damon_task' objects */ + struct list_head schemes_list; /* 'damos' objects */ /* callbacks */ void (*sample_cb)(struct damon_ctx *context); @@ -66,6 +88,8 @@ struct damon_ctx { int damon_set_pids(struct damon_ctx *ctx, unsigned long *pids, ssize_t nr_pids); +int damon_set_schemes(struct damon_ctx *ctx, + struct damos **schemes, ssize_t nr_schemes); int damon_set_recording(struct damon_ctx *ctx, unsigned int rbuf_len, char *rfile_path); int damon_set_attrs(struct damon_ctx *ctx, unsigned long s, unsigned long a, diff --git a/mm/damon.c b/mm/damon.c index e08dba2e2717..a4d6dff60b1f 100644 --- a/mm/damon.c +++ b/mm/damon.c @@ -11,6 +11,7 @@ #define CREATE_TRACE_POINTS +#include #include #include #include @@ -24,6 +25,8 @@ #include #include +#include "internal.h" + #define damon_get_task_struct(t) \ (get_pid_task(find_vpid(t->pid), PIDTYPE_PID)) @@ -45,6 +48,12 @@ #define damon_for_each_task_safe(ctx, t, next) \ list_for_each_entry_safe(t, next, &(ctx)->tasks_list, list) +#define damon_for_each_schemes(ctx, r) \ + list_for_each_entry(r, &(ctx)->schemes_list, list) + +#define damon_for_each_schemes_safe(ctx, s, next) \ + list_for_each_entry_safe(s, next, &(ctx)->schemes_list, list) + #define MAX_RFILE_PATH_LEN 256 /* Get a random number in [l, r) */ @@ -190,6 +199,27 @@ static void damon_destroy_task(struct damon_task *t) damon_free_task(t); } +static void damon_add_scheme(struct damon_ctx *ctx, struct damos *s) +{ + list_add_tail(&s->list, &ctx->schemes_list); +} + +static void damon_del_scheme(struct damos *s) +{ + list_del(&s->list); +} + +static void damon_free_scheme(struct damos *s) +{ + kfree(s); +} + +static void damon_destroy_scheme(struct damos *s) +{ + damon_del_scheme(s); + damon_free_scheme(s); +} + /* * Returns number of monitoring target tasks */ @@ -642,6 +672,87 @@ static void kdamond_count_age(struct damon_ctx *c, unsigned int threshold) } } +static int damos_do_action(struct damon_task *task, struct damon_region *r, + enum damos_action action) +{ + struct task_struct *t; + struct mm_struct *mm; + int madv_action; + int ret = -EINVAL; + + switch (action) { + case DAMOS_WILLNEED: + madv_action = MADV_WILLNEED; + break; + case DAMOS_COLD: + madv_action = MADV_COLD; + break; + case DAMOS_PAGEOUT: + madv_action = MADV_PAGEOUT; + break; + case DAMOS_HUGEPAGE: + madv_action = MADV_HUGEPAGE; + break; + case DAMOS_NOHUGEPAGE: + madv_action = MADV_NOHUGEPAGE; + break; + default: + pr_warn("Wrong action %d\n", action); + goto out; + } + + t = damon_get_task_struct(task); + if (!t) + goto out; + mm = damon_get_mm(task); + if (!mm) + goto put_task_out; + + ret = madvise_common(t, mm, PAGE_ALIGN(r->vm_start), + PAGE_ALIGN(r->vm_end - r->vm_start), madv_action); + + mmput(mm); +put_task_out: + put_task_struct(t); +out: + return ret; +} + +static void damon_do_apply_schemes(struct damon_ctx *c, struct damon_task *t, + struct damon_region *r) +{ + struct damos *s; + unsigned long sz; + + damon_for_each_schemes(c, s) { + sz = r->vm_end - r->vm_start; + if ((s->min_sz_region && sz < s->min_sz_region) || + (s->max_sz_region && s->max_sz_region < sz)) + continue; + if ((s->min_nr_accesses && r->nr_accesses < s->min_nr_accesses) + || (s->max_nr_accesses && + s->max_nr_accesses < r->nr_accesses)) + continue; + if ((s->min_age_region && r->age < s->min_age_region) || + (s->max_age_region && + s->max_age_region < r->age)) + continue; + damos_do_action(t, r, s->action); + r->age = 0; + } +} + +static void kdamond_apply_schemes(struct damon_ctx *c) +{ + struct damon_task *t; + struct damon_region *r; + + damon_for_each_task(c, t) { + damon_for_each_region(r, t) + damon_do_apply_schemes(c, t, r); + } +} + #define sz_damon_region(r) (r->vm_end - r->vm_start) /* @@ -966,6 +1077,7 @@ static int kdamond_fn(void *data) kdamond_count_age(ctx, max_nr_accesses / 10); if (ctx->aggregate_cb) ctx->aggregate_cb(ctx); + kdamond_apply_schemes(ctx); kdamond_flush_aggregated(ctx); kdamond_split_regions(ctx); } @@ -1040,6 +1152,27 @@ int damon_stop(struct damon_ctx *ctx) return damon_turn_kdamond(ctx, false); } +/* + * Set the data access monitoring oriented schemes + * + * NOTE: This function should not be called while the kdamond of the context is + * running. + * + * Returns 0 if success, or negative error code otherwise. + */ +int damon_set_schemes(struct damon_ctx *ctx, struct damos **schemes, + ssize_t nr_schemes) +{ + struct damos *s, *next; + ssize_t i; + + damon_for_each_schemes_safe(ctx, s, next) + damon_destroy_scheme(s); + for (i = 0; i < nr_schemes; i++) + damon_add_scheme(ctx, schemes[i]); + return 0; +} + /* * This function should not be called while the kdamond is running. */ @@ -1485,6 +1618,7 @@ static int __init damon_init_user_ctx(void) prandom_seed_state(&ctx->rndseed, 42); INIT_LIST_HEAD(&ctx->tasks_list); + INIT_LIST_HEAD(&ctx->schemes_list); ctx->sample_cb = NULL; ctx->aggregate_cb = NULL; From patchwork Tue Feb 25 10:22:57 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 11403187 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 B57D91395 for ; Tue, 25 Feb 2020 10:25:37 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 7604320714 for ; Tue, 25 Feb 2020 10:25:37 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="b4ATFEhK" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7604320714 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 B619B6B0003; Tue, 25 Feb 2020 05:25:36 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id B11256B0005; Tue, 25 Feb 2020 05:25:36 -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 A00D26B0008; Tue, 25 Feb 2020 05:25:36 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0194.hostedemail.com [216.40.44.194]) by kanga.kvack.org (Postfix) with ESMTP id 871356B0003 for ; Tue, 25 Feb 2020 05:25:36 -0500 (EST) Received: from smtpin05.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 3E0C5180AD80F for ; Tue, 25 Feb 2020 10:25:36 +0000 (UTC) X-FDA: 76528267872.05.boats25_30cd5afd2e829 X-Spam-Summary: 1,0,0,,d41d8cd98f00b204,prvs=317a3a273=sjpark@amazon.com,,RULES_HIT:30004:30051:30054:30064:30070,0,RBL:52.95.49.90:@amazon.com:.lbl8.mailshell.net-62.18.0.100 64.10.201.10,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:23,LUA_SUMMARY:none X-HE-Tag: boats25_30cd5afd2e829 X-Filterd-Recvd-Size: 9109 Received: from smtp-fw-6002.amazon.com (smtp-fw-6002.amazon.com [52.95.49.90]) by imf32.hostedemail.com (Postfix) with ESMTP for ; Tue, 25 Feb 2020 10:25:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1582626335; x=1614162335; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=QOAAvZCA61DVc3DW/VsdHuEoi43TdaxmLUyV9t/JY3E=; b=b4ATFEhKx6mO2DSCHckclmqEyLO5LFPl1FftkASVrfmpQ6amuNTkZyBD H3LqJqSHBfXNQxwqJ2eQY4ftVcYq4/JFONny+n0hpU4sauc63KxKImVTD g1vCytISscP3H0J8deaC3d+uKPZ9Ah9j6+QQVP4CGJ1TnFgQhEFM6onwx A=; IronPort-SDR: z4r5X4rs+RwojLQn+u/mLVn1OxeUQy0jlgNK5r2VKwkVuuDR0hpdoyJ3c59OEyf9J247677EiK uU5T0Qn2i4pg== X-IronPort-AV: E=Sophos;i="5.70,483,1574121600"; d="scan'208";a="18080229" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-1a-e34f1ddc.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-out-6002.iad6.amazon.com with ESMTP; 25 Feb 2020 10:25:22 +0000 Received: from EX13MTAUEA002.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan2.iad.amazon.com [10.40.159.162]) by email-inbound-relay-1a-e34f1ddc.us-east-1.amazon.com (Postfix) with ESMTPS id A6B87A270F; Tue, 25 Feb 2020 10:25:20 +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, 25 Feb 2020 10:25:20 +0000 Received: from u886c93fd17d25d.ant.amazon.com (10.43.162.53) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Tue, 25 Feb 2020 10:25:08 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [RFC v3 4/7] mm/damon/schemes: Implement a debugfs interface Date: Tue, 25 Feb 2020 11:22:57 +0100 Message-ID: <20200225102300.23895-5-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200225102300.23895-1-sjpark@amazon.com> References: <20200225102300.23895-1-sjpark@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.43.162.53] X-ClientProxiedBy: EX13D30UWC001.ant.amazon.com (10.43.162.128) 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 This commit implements a debugfs interface for the data access monitoring oriented memory management schemes. It is supposed to be used by administrators and/or privileged user space programs. Users can read and update the rules using ``/damon/schemes`` file. The format is:: Signed-off-by: SeongJae Park --- mm/damon.c | 171 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 169 insertions(+), 2 deletions(-) diff --git a/mm/damon.c b/mm/damon.c index a4d6dff60b1f..b286372dbf0e 100644 --- a/mm/damon.c +++ b/mm/damon.c @@ -199,6 +199,29 @@ static void damon_destroy_task(struct damon_task *t) damon_free_task(t); } +static struct damos *damon_new_scheme( + unsigned int min_sz_region, unsigned int max_sz_region, + unsigned int min_nr_accesses, unsigned int max_nr_accesses, + unsigned int min_age_region, unsigned int max_age_region, + enum damos_action action) +{ + struct damos *ret; + + ret = kmalloc(sizeof(struct damos), GFP_KERNEL); + if (!ret) + return NULL; + ret->min_sz_region = min_sz_region; + ret->max_sz_region = max_sz_region; + ret->min_nr_accesses = min_nr_accesses; + ret->max_nr_accesses = max_nr_accesses; + ret->min_age_region = min_age_region; + ret->max_age_region = max_age_region; + ret->action = action; + INIT_LIST_HEAD(&ret->list); + + return ret; +} + static void damon_add_scheme(struct damon_ctx *ctx, struct damos *s) { list_add_tail(&s->list, &ctx->schemes_list); @@ -1329,6 +1352,144 @@ static ssize_t debugfs_monitor_on_write(struct file *file, return ret; } +static ssize_t sprint_schemes(struct damon_ctx *c, char *buf, ssize_t len) +{ + struct damos *s; + int written = 0; + int rc; + + damon_for_each_schemes(c, s) { + rc = snprintf(&buf[written], len - written, + "%u %u %u %u %u %u %d\n", + s->min_sz_region, s->max_sz_region, + s->min_nr_accesses, s->max_nr_accesses, + s->min_age_region, s->max_age_region, + s->action); + if (!rc) + return -ENOMEM; + written += rc; + } + return written; +} + +static ssize_t debugfs_schemes_read(struct file *file, char __user *buf, + size_t count, loff_t *ppos) +{ + struct damon_ctx *ctx = &damon_user_ctx; + char *kbuf; + ssize_t ret; + + kbuf = kmalloc(count, GFP_KERNEL); + if (!kbuf) + return -ENOMEM; + + ret = sprint_schemes(ctx, kbuf, count); + if (ret < 0) + goto out; + ret = simple_read_from_buffer(buf, count, ppos, kbuf, ret); + +out: + kfree(kbuf); + return ret; +} + +static void free_schemes_arr(struct damos **schemes, ssize_t nr_schemes) +{ + ssize_t i; + + for (i = 0; i < nr_schemes; i++) + kfree(schemes[i]); + kfree(schemes); +} + +/* + * Converts a string into an array of struct damos pointers + * + * Returns an array of struct damos pointers that converted if the conversion + * success, or NULL otherwise. + */ +static struct damos **str_to_schemes(const char *str, ssize_t len, + ssize_t *nr_schemes) +{ + struct damos *scheme, **schemes; + const int max_nr_schemes = 256; + int pos = 0, parsed, ret; + unsigned int min_sz, max_sz, min_nr_a, max_nr_a, min_age, max_age; + int action; + + schemes = kmalloc_array(max_nr_schemes, sizeof(struct damos *), + GFP_KERNEL); + if (!schemes) + return NULL; + + *nr_schemes = 0; + while (pos < len && *nr_schemes < max_nr_schemes) { + ret = sscanf(&str[pos], "%u %u %u %u %u %u %d%n", + &min_sz, &max_sz, &min_nr_a, &max_nr_a, + &min_age, &max_age, &action, &parsed); + pos += parsed; + if (ret != 7) + break; + if (action >= DAMOS_ACTION_LEN) { + pr_err("wrong action %d\n", action); + goto fail; + } + + scheme = damon_new_scheme(min_sz, max_sz, min_nr_a, max_nr_a, + min_age, max_age, action); + if (!scheme) + goto fail; + + schemes[*nr_schemes] = scheme; + *nr_schemes += 1; + } + if (!*nr_schemes) + goto fail; + return schemes; +fail: + free_schemes_arr(schemes, *nr_schemes); + return NULL; +} + +static ssize_t debugfs_schemes_write(struct file *file, const char __user *buf, + size_t count, loff_t *ppos) +{ + struct damon_ctx *ctx = &damon_user_ctx; + char *kbuf; + struct damos **schemes; + ssize_t nr_schemes = 0, ret; + + if (*ppos) + return -EINVAL; + + kbuf = kmalloc_array(count, sizeof(char), GFP_KERNEL); + if (!kbuf) + return -ENOMEM; + + ret = simple_write_to_buffer(kbuf, count, ppos, buf, count); + if (ret < 0) + goto out; + + schemes = str_to_schemes(kbuf, ret, &nr_schemes); + + spin_lock(&ctx->kdamond_lock); + if (ctx->kdamond) + goto monitor_running; + + damon_set_schemes(ctx, schemes, nr_schemes); + spin_unlock(&ctx->kdamond_lock); + goto out; + +monitor_running: + spin_unlock(&ctx->kdamond_lock); + pr_err("%s: kdamond is running. Turn it off first.\n", __func__); + ret = -EINVAL; + free_schemes_arr(schemes, nr_schemes); +out: + kfree(kbuf); + return ret; +} + static ssize_t damon_sprint_pids(struct damon_ctx *ctx, char *buf, ssize_t len) { struct damon_task *t; @@ -1559,6 +1720,12 @@ static const struct file_operations pids_fops = { .write = debugfs_pids_write, }; +static const struct file_operations schemes_fops = { + .owner = THIS_MODULE, + .read = debugfs_schemes_read, + .write = debugfs_schemes_write, +}; + static const struct file_operations record_fops = { .owner = THIS_MODULE, .read = debugfs_record_read, @@ -1575,10 +1742,10 @@ static struct dentry *debugfs_root; static int __init debugfs_init(void) { - const char * const file_names[] = {"attrs", "record", + const char * const file_names[] = {"attrs", "record", "schemes", "pids", "monitor_on"}; const struct file_operations *fops[] = {&attrs_fops, &record_fops, - &pids_fops, &monitor_on_fops}; + &schemes_fops, &pids_fops, &monitor_on_fops}; int i; debugfs_root = debugfs_create_dir("damon", NULL); From patchwork Tue Feb 25 10:22:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 11403189 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 2C493159A for ; Tue, 25 Feb 2020 10:25:56 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E408620714 for ; Tue, 25 Feb 2020 10:25:55 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="llAXAobl" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E408620714 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 3BF9E6B0005; Tue, 25 Feb 2020 05:25:55 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 397876B0008; Tue, 25 Feb 2020 05:25:55 -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 2AE156B000A; Tue, 25 Feb 2020 05:25:55 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0117.hostedemail.com [216.40.44.117]) by kanga.kvack.org (Postfix) with ESMTP id 140936B0005 for ; Tue, 25 Feb 2020 05:25:55 -0500 (EST) Received: from smtpin03.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id BA6952496 for ; Tue, 25 Feb 2020 10:25:54 +0000 (UTC) X-FDA: 76528268628.03.skin49_338b0514e533c X-Spam-Summary: 1,0,0,,d41d8cd98f00b204,prvs=317a3a273=sjpark@amazon.com,,RULES_HIT:30004:30054:30064:30070,0,RBL:207.171.184.29:@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:26,LUA_SUMMARY:none X-HE-Tag: skin49_338b0514e533c X-Filterd-Recvd-Size: 4378 Received: from smtp-fw-9102.amazon.com (smtp-fw-9102.amazon.com [207.171.184.29]) by imf46.hostedemail.com (Postfix) with ESMTP for ; Tue, 25 Feb 2020 10:25:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1582626354; x=1614162354; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=SBNXt18fiSDkKKFTHL/6fBU1JbEP1Jnn1oHT/2Qkjhc=; b=llAXAoblaH989jWHXuhP8RENMXwTjEbgCasvoBG/uku6AClmCKsE/ZIa y4xbP/NCqjyBF0u1spAKV+lXccRTZSaiWg9gpWseX+WnYMROGko5BwCHs +IoenAv3hn5BuAa9ILW1gIdk49lxyCxVJIJUdNh1wwtBU5zOk38a16OMY 8=; IronPort-SDR: c1MTE6zIDsERP/BMd3ciZjJLwc1BK/AFz+nddmSExjM4R59h+QVfYXDT8uBjP+/NwrlmNNO5iq 2rJjWWTD1niQ== X-IronPort-AV: E=Sophos;i="5.70,483,1574121600"; d="scan'208";a="27313402" Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-1e-c7c08562.us-east-1.amazon.com) ([10.47.23.38]) by smtp-border-fw-out-9102.sea19.amazon.com with ESMTP; 25 Feb 2020 10:25:51 +0000 Received: from EX13MTAUEA002.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan2.iad.amazon.com [10.40.159.162]) by email-inbound-relay-1e-c7c08562.us-east-1.amazon.com (Postfix) with ESMTPS id BCFF124622A; Tue, 25 Feb 2020 10:25:41 +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, 25 Feb 2020 10:25:40 +0000 Received: from u886c93fd17d25d.ant.amazon.com (10.43.162.53) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Tue, 25 Feb 2020 10:25:29 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [RFC v3 5/7] mm/damon-test: Add kunit test case for regions age accounting Date: Tue, 25 Feb 2020 11:22:58 +0100 Message-ID: <20200225102300.23895-6-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200225102300.23895-1-sjpark@amazon.com> References: <20200225102300.23895-1-sjpark@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.43.162.53] X-ClientProxiedBy: EX13D30UWC001.ant.amazon.com (10.43.162.128) 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 After merges of regions, each region should know their last shape in proper way to measure the changes from the last modification and reset the age if the changes are significant. This commit adds kunit test cases checking whether the regions are knowing their last shape properly after merges of regions. Signed-off-by: SeongJae Park Reviewed-by: Brendan Higgins --- mm/damon-test.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mm/damon-test.h b/mm/damon-test.h index c7dc21325c77..2ba757357211 100644 --- a/mm/damon-test.h +++ b/mm/damon-test.h @@ -540,6 +540,8 @@ static void damon_test_merge_regions_of(struct kunit *test) unsigned long saddrs[] = {0, 114, 130, 156, 170}; unsigned long eaddrs[] = {112, 130, 156, 170, 230}; + unsigned long lsa[] = {0, 114, 130, 156, 184}; + unsigned long lea[] = {100, 122, 156, 170, 230}; int i; t = damon_new_task(42); @@ -556,6 +558,9 @@ static void damon_test_merge_regions_of(struct kunit *test) r = damon_nth_region_of(t, i); KUNIT_EXPECT_EQ(test, r->vm_start, saddrs[i]); KUNIT_EXPECT_EQ(test, r->vm_end, eaddrs[i]); + KUNIT_EXPECT_EQ(test, r->last_vm_start, lsa[i]); + KUNIT_EXPECT_EQ(test, r->last_vm_end, lea[i]); + } damon_free_task(t); } From patchwork Tue Feb 25 10:22:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 11403191 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 E9B2914D5 for ; Tue, 25 Feb 2020 10:26:19 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id AC4E7218AC for ; Tue, 25 Feb 2020 10:26:19 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="etDbxyu9" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org AC4E7218AC 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 015676B0008; Tue, 25 Feb 2020 05:26:19 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id F07F26B000A; Tue, 25 Feb 2020 05:26:18 -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 E1DDE6B000C; Tue, 25 Feb 2020 05:26:18 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0091.hostedemail.com [216.40.44.91]) by kanga.kvack.org (Postfix) with ESMTP id C67F76B0008 for ; Tue, 25 Feb 2020 05:26:18 -0500 (EST) Received: from smtpin10.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 88C9F2C78 for ; Tue, 25 Feb 2020 10:26:18 +0000 (UTC) X-FDA: 76528269636.10.alley72_3700aee030c20 X-Spam-Summary: 1,0,0,,d41d8cd98f00b204,prvs=317a3a273=sjpark@amazon.com,,RULES_HIT:30054:30056:30064,0,RBL:72.21.196.25:@amazon.com:.lbl8.mailshell.net-62.18.0.100 66.10.201.10,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:24,LUA_SUMMARY:none X-HE-Tag: alley72_3700aee030c20 X-Filterd-Recvd-Size: 4192 Received: from smtp-fw-2101.amazon.com (smtp-fw-2101.amazon.com [72.21.196.25]) by imf05.hostedemail.com (Postfix) with ESMTP for ; Tue, 25 Feb 2020 10:26:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1582626379; x=1614162379; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=I5lhnzWRKWvNaF4MApexOfC/PP8h9gbeTKWBJi6wddY=; b=etDbxyu9/PK0XU910Uu1LkmOCVwFcJ5lYNdjMpDgiTgVp7oMnmmC+HaZ mmRzQeh42S9JV9FuTeroE03qgmwR6fMTBuOPMKOVfjrWcrY+kEYOh553E Bw3DsysHJrCm+E/LuyEHNExsqZVEZtYuuQYFs+pTVVcQ5xBl8lxmzZthA U=; IronPort-SDR: R1LTugDP/R3oV6o9+9pnARTGmnAJbZvZMbTTzEsR9MRBvgDhthg4fJm0KWxoBSLAImvpjAAaiN bTDOpR4zEV5w== X-IronPort-AV: E=Sophos;i="5.70,483,1574121600"; d="scan'208";a="18757024" Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-1a-715bee71.us-east-1.amazon.com) ([10.43.8.2]) by smtp-border-fw-out-2101.iad2.amazon.com with ESMTP; 25 Feb 2020 10:26:06 +0000 Received: from EX13MTAUEA002.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan2.iad.amazon.com [10.40.159.162]) by email-inbound-relay-1a-715bee71.us-east-1.amazon.com (Postfix) with ESMTPS id 78CC1A1EFB; Tue, 25 Feb 2020 10:25:56 +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, 25 Feb 2020 10:25:55 +0000 Received: from u886c93fd17d25d.ant.amazon.com (10.43.162.53) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Tue, 25 Feb 2020 10:25:44 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [RFC v3 6/7] mm/damon/selftests: Add 'schemes' debugfs tests Date: Tue, 25 Feb 2020 11:22:59 +0100 Message-ID: <20200225102300.23895-7-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200225102300.23895-1-sjpark@amazon.com> References: <20200225102300.23895-1-sjpark@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.43.162.53] X-ClientProxiedBy: EX13D30UWC001.ant.amazon.com (10.43.162.128) 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 This commit adds simple selftets for 'schemes' debugfs file of DAMON. Signed-off-by: SeongJae Park --- .../testing/selftests/damon/debugfs_attrs.sh | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tools/testing/selftests/damon/debugfs_attrs.sh b/tools/testing/selftests/damon/debugfs_attrs.sh index d5188b0f71b1..82a98c81975b 100755 --- a/tools/testing/selftests/damon/debugfs_attrs.sh +++ b/tools/testing/selftests/damon/debugfs_attrs.sh @@ -97,6 +97,35 @@ fi echo $ORIG_CONTENT > $file +# Test schemes file +file="$DBGFS/schemes" + +ORIG_CONTENT=$(cat $file) +echo "1 2 3 4 5 6 3" > $file +if [ $? -ne 0 ] +then + echo "$file write fail" + echo $ORIG_CONTENT > $file + exit 1 +fi + +echo "1 2 +3 4 5 6 3" > $file +if [ $? -eq 0 ] +then + echo "$file splitted write success (expected fail)" + echo $ORIG_CONTENT > $file + exit 1 +fi + +echo > $file +if [ $? -ne 0 ] +then + echo "$file empty string writing fail" + echo $ORIG_CONTENT > $file + exit 1 +fi + # Test pids file file="$DBGFS/pids" From patchwork Tue Feb 25 10:23:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 11403193 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 BDD5D159A for ; Tue, 25 Feb 2020 10:26:48 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 629F3222C2 for ; Tue, 25 Feb 2020 10:26:48 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="lWmxZwRk" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 629F3222C2 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 9159F6B0003; Tue, 25 Feb 2020 05:26:47 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 8C8F06B0007; Tue, 25 Feb 2020 05:26:47 -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 78E136B000A; Tue, 25 Feb 2020 05:26:47 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0081.hostedemail.com [216.40.44.81]) by kanga.kvack.org (Postfix) with ESMTP id 5E67E6B0003 for ; Tue, 25 Feb 2020 05:26:47 -0500 (EST) Received: from smtpin27.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 15324180AD801 for ; Tue, 25 Feb 2020 10:26:47 +0000 (UTC) X-FDA: 76528270854.27.smoke93_3b1f214207e14 X-Spam-Summary: 1,0,0,,d41d8cd98f00b204,prvs=317a3a273=sjpark@amazon.com,,RULES_HIT:30004:30029:30034:30054:30064:30070:30079:30091,0,RBL:207.171.184.29:@amazon.com:.lbl8.mailshell.net-62.18.0.100 66.10.201.10,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:16,LUA_SUMMARY:none X-HE-Tag: smoke93_3b1f214207e14 X-Filterd-Recvd-Size: 26175 Received: from smtp-fw-9102.amazon.com (smtp-fw-9102.amazon.com [207.171.184.29]) by imf07.hostedemail.com (Postfix) with ESMTP for ; Tue, 25 Feb 2020 10:26:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1582626406; x=1614162406; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=0rVZ3zAnMg6+jq+1XhuvGZYxj3ZTAtM6kvkXuN/epew=; b=lWmxZwRkzW0uI07o22igVRaqsnTJ9i60aJpSYV+bdC9LXFnadCxpLPUy cYemPluVk/qr8kmZ0obOnB8ocTFHLNahYQqHciGuqqb8HE5KJqo1EE+F+ ClcZV6p4CfUgykLPyEhUBEo0TryMr3A/oqvZCKoGt+ZcS0LemJrB02Ael o=; IronPort-SDR: 7+UpqfbFR702J9UPCg8R0Y2I7pcNJIBdGPXNU3UTRMv+0W3vWfcLj6fFB0oj95+ihsT/Zm76wS E8Luy8WSry9g== X-IronPort-AV: E=Sophos;i="5.70,483,1574121600"; d="scan'208";a="27313521" Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-1a-e34f1ddc.us-east-1.amazon.com) ([10.47.23.38]) by smtp-border-fw-out-9102.sea19.amazon.com with ESMTP; 25 Feb 2020 10:26:43 +0000 Received: from EX13MTAUEA002.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan2.iad.amazon.com [10.40.159.162]) by email-inbound-relay-1a-e34f1ddc.us-east-1.amazon.com (Postfix) with ESMTPS id 22E1AA26DA; Tue, 25 Feb 2020 10:26:41 +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, 25 Feb 2020 10:26:40 +0000 Received: from u886c93fd17d25d.ant.amazon.com (10.43.162.53) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Tue, 25 Feb 2020 10:26:28 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [RFC v3 7/7] damon/tools: Support more human friendly 'schemes' control Date: Tue, 25 Feb 2020 11:23:00 +0100 Message-ID: <20200225102300.23895-8-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200225102300.23895-1-sjpark@amazon.com> References: <20200225102300.23895-1-sjpark@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.43.162.53] X-ClientProxiedBy: EX13D30UWC001.ant.amazon.com (10.43.162.128) 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 This commit implements 'schemes' subcommand of the damon userspace tool. It can be used to describe and apply the data access monitoring-based operation schemes in more human friendly fashion. Signed-off-by: SeongJae Park --- tools/damon/_convert_damos.py | 125 +++++++++++++++++++++++++++++ tools/damon/_damon.py | 143 ++++++++++++++++++++++++++++++++++ tools/damon/damo | 7 ++ tools/damon/record.py | 133 +++---------------------------- tools/damon/schemes.py | 105 +++++++++++++++++++++++++ 5 files changed, 391 insertions(+), 122 deletions(-) create mode 100755 tools/damon/_convert_damos.py create mode 100644 tools/damon/_damon.py create mode 100644 tools/damon/schemes.py diff --git a/tools/damon/_convert_damos.py b/tools/damon/_convert_damos.py new file mode 100755 index 000000000000..0f1e7e3d4ccc --- /dev/null +++ b/tools/damon/_convert_damos.py @@ -0,0 +1,125 @@ +#!/usr/bin/env python3 + +""" +Change human readable data access monitoring-based operation schemes to the low +level input for the '/damon/schemes' file. Below is an example of the +schemes written in the human readable format: + +# format is: +# lines starts with '#' or blank are ignored. +# B/K/M/G/T for Bytes/KiB/MiB/GiB/TiB +# us/ms/s/m/h/d for micro-seconds/milli-seconds/seconds/minutes/hours/days +# 'null' means zero, which passes the check + +# if a region (no matter of its size) keeps a high access frequency for more +# than 100ms, put the region on the head of the LRU list (call madvise() with +# MADV_WILLNEED). +null null 80 null 100ms null willneed + +# if a region keeps a low access frequency for more than 100ms, put the +# region on the tail of the LRU list (call madvise() with MADV_COLD). +0B 0B 10 20 200ms 1h cold + +# if a region keeps a very low access frequency for more than 100ms, swap +# out the region immediately (call madvise() with MADV_PAGEOUT). +0B null 0 10 100ms 2h pageout + +# if a region of a size bigger than 2MiB keeps a very high access frequency +# for more than 100ms, let the region to use huge pages (call madvise() +# with MADV_HUGEPAGE). +2M null 90 99 100ms 2h hugepage + +# If a regions of a size bigger than 2MiB keeps no high access frequency +# for more than 100ms, avoid the region from using huge pages (call +# madvise() with MADV_NOHUGEPAGE). +2M null 0 25 100ms 2h nohugepage +""" + +import argparse + +unit_to_bytes = {'B': 1, 'K': 1024, 'M': 1024 * 1024, 'G': 1024 * 1024 * 1024, + 'T': 1024 * 1024 * 1024 * 1024} + +def text_to_bytes(txt): + if txt == 'null': + return 0 + unit = txt[-1] + number = int(txt[:-1]) + return number * unit_to_bytes[unit] + +unit_to_usecs = {'us': 1, 'ms': 1000, 's': 1000 * 1000, 'm': 60 * 1000 * 1000, + 'h': 60 * 60 * 1000 * 1000, 'd': 24 * 60 * 60 * 1000 * 1000} + +def text_to_us(txt): + if txt == 'null': + return 0 + unit = txt[-2:] + if unit in ['us', 'ms']: + number = int(txt[:-2]) + else: + unit = txt[-1] + number = int(txt[:-1]) + return number * unit_to_usecs[unit] + +damos_action_to_int = {'DAMOS_WILLNEED': 0, 'DAMOS_COLD': 1, + 'DAMOS_PAGEOUT': 2, 'DAMOS_HUGEPAGE': 3, 'DAMOS_NOHUGEPAGE': 4} + +def text_to_damos_action(txt): + return damos_action_to_int['DAMOS_' + txt.upper()] + +def text_to_nr_accesses(txt, max_nr_accesses): + if txt == 'null': + return 0 + return int(int(txt) * max_nr_accesses / 100) + +def debugfs_scheme(line, sample_interval, aggr_interval): + fields = line.split() + if len(fields) != 7: + print('wrong input line: %s' % line) + exit(1) + + limit_nr_accesses = aggr_interval / sample_interval + try: + min_sz = text_to_bytes(fields[0]) + max_sz = text_to_bytes(fields[1]) + min_nr_accesses = text_to_nr_accesses(fields[2], limit_nr_accesses) + max_nr_accesses = text_to_nr_accesses(fields[3], limit_nr_accesses) + min_age = text_to_us(fields[4]) / aggr_interval + max_age = text_to_us(fields[5]) / aggr_interval + action = text_to_damos_action(fields[6]) + except: + print('wrong input field') + raise + return '%d\t%d\t%d\t%d\t%d\t%d\t%d' % (min_sz, max_sz, min_nr_accesses, + max_nr_accesses, min_age, max_age, action) + +def convert(schemes_file, sample_interval, aggr_interval): + lines = [] + with open(schemes_file, 'r') as f: + for line in f: + if line.startswith('#'): + continue + line = line.strip() + if line == '': + continue + lines.append(debugfs_scheme(line, sample_interval, aggr_interval)) + return '\n'.join(lines) + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('input', metavar='', + help='input file describing the schemes') + parser.add_argument('-s', '--sample', metavar='', type=int, + default=5000, help='sampling interval (us)') + parser.add_argument('-a', '--aggr', metavar='', type=int, + default=100000, help='aggregation interval (us)') + args = parser.parse_args() + + schemes_file = args.input + sample_interval = args.sample + aggr_interval = args.aggr + + print(convert(schemes_file, sample_interval, aggr_interval)) + +if __name__ == '__main__': + main() diff --git a/tools/damon/_damon.py b/tools/damon/_damon.py new file mode 100644 index 000000000000..0a703ec7471a --- /dev/null +++ b/tools/damon/_damon.py @@ -0,0 +1,143 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0 + +""" +Contains core functions for DAMON debugfs control. +""" + +import os +import subprocess + +debugfs_attrs = None +debugfs_record = None +debugfs_schemes = None +debugfs_pids = None +debugfs_monitor_on = None + +def set_target_pid(pid): + return subprocess.call('echo %s > %s' % (pid, debugfs_pids), shell=True, + executable='/bin/bash') + +def turn_damon(on_off): + return subprocess.call("echo %s > %s" % (on_off, debugfs_monitor_on), + shell=True, executable="/bin/bash") + +def is_damon_running(): + with open(debugfs_monitor_on, 'r') as f: + return f.read().strip() == 'on' + +class Attrs: + sample_interval = None + aggr_interval = None + regions_update_interval = None + min_nr_regions = None + max_nr_regions = None + rbuf_len = None + rfile_path = None + schemes = None + + def __init__(self, s, a, r, n, x, l, f, c): + self.sample_interval = s + self.aggr_interval = a + self.regions_update_interval = r + self.min_nr_regions = n + self.max_nr_regions = x + self.rbuf_len = l + self.rfile_path = f + self.schemes = c + + def __str__(self): + return "%s %s %s %s %s %s %s\n%s" % (self.sample_interval, + self.aggr_interval, self.regions_update_interval, + self.min_nr_regions, self.max_nr_regions, self.rbuf_len, + self.rfile_path, self.schemes) + + def attr_str(self): + return "%s %s %s %s %s " % (self.sample_interval, self.aggr_interval, + self.regions_update_interval, self.min_nr_regions, + self.max_nr_regions) + + def record_str(self): + return '%s %s ' % (self.rbuf_len, self.rfile_path) + + def apply(self): + ret = subprocess.call('echo %s > %s' % (self.attr_str(), debugfs_attrs), + shell=True, executable='/bin/bash') + if ret: + return ret + ret = subprocess.call('echo %s > %s' % (self.record_str(), + debugfs_record), shell=True, executable='/bin/bash') + if ret: + return ret + return subprocess.call('echo %s > %s' % ( + self.schemes.replace('\n', ' '), debugfs_schemes), shell=True, + executable='/bin/bash') + +def current_attrs(): + with open(debugfs_attrs, 'r') as f: + attrs = f.read().split() + attrs = [int(x) for x in attrs] + + with open(debugfs_record, 'r') as f: + rattrs = f.read().split() + attrs.append(int(rattrs[0])) + attrs.append(rattrs[1]) + + with open(debugfs_schemes, 'r') as f: + schemes = f.read() + attrs.append(schemes) + + return Attrs(*attrs) + +def chk_update_debugfs(debugfs): + global debugfs_attrs + global debugfs_record + global debugfs_schemes + global debugfs_pids + global debugfs_monitor_on + + debugfs_damon = os.path.join(debugfs, 'damon') + debugfs_attrs = os.path.join(debugfs_damon, 'attrs') + debugfs_record = os.path.join(debugfs_damon, 'record') + debugfs_schemes = os.path.join(debugfs_damon, 'schemes') + debugfs_pids = os.path.join(debugfs_damon, 'pids') + debugfs_monitor_on = os.path.join(debugfs_damon, 'monitor_on') + + if not os.path.isdir(debugfs_damon): + print("damon debugfs dir (%s) not found", debugfs_damon) + exit(1) + + for f in [debugfs_attrs, debugfs_record, debugfs_schemes, debugfs_pids, + debugfs_monitor_on]: + if not os.path.isfile(f): + print("damon debugfs file (%s) not found" % f) + exit(1) + +def cmd_args_to_attrs(args): + "Generate attributes with specified arguments" + sample_interval = args.sample + aggr_interval = args.aggr + regions_update_interval = args.updr + min_nr_regions = args.minr + max_nr_regions = args.maxr + rbuf_len = args.rbuf + if not os.path.isabs(args.out): + args.out = os.path.join(os.getcwd(), args.out) + rfile_path = args.out + schemes = args.schemes + return Attrs(sample_interval, aggr_interval, regions_update_interval, + min_nr_regions, max_nr_regions, rbuf_len, rfile_path, schemes) + +def set_attrs_argparser(parser): + parser.add_argument('-d', '--debugfs', metavar='', type=str, + default='/sys/kernel/debug', help='debugfs mounted path') + parser.add_argument('-s', '--sample', metavar='', type=int, + default=5000, help='sampling interval') + parser.add_argument('-a', '--aggr', metavar='', type=int, + default=100000, help='aggregate interval') + parser.add_argument('-u', '--updr', metavar='', type=int, + default=1000000, help='regions update interval') + parser.add_argument('-n', '--minr', metavar='<# regions>', type=int, + default=10, help='minimal number of regions') + parser.add_argument('-m', '--maxr', metavar='<# regions>', type=int, + default=1000, help='maximum number of regions') diff --git a/tools/damon/damo b/tools/damon/damo index 58e1099ae5fc..ce7180069bef 100755 --- a/tools/damon/damo +++ b/tools/damon/damo @@ -5,6 +5,7 @@ import argparse import record import report +import schemes class SubCmdHelpFormatter(argparse.RawDescriptionHelpFormatter): def _format_action(self, action): @@ -25,6 +26,10 @@ parser_record = subparser.add_parser('record', help='record data accesses of the given target processes') record.set_argparser(parser_record) +parser_schemes = subparser.add_parser('schemes', + help='apply operation schemes to the given target process') +schemes.set_argparser(parser_schemes) + parser_report = subparser.add_parser('report', help='report the recorded data accesses in the specified form') report.set_argparser(parser_report) @@ -33,5 +38,7 @@ args = parser.parse_args() if args.command == 'record': record.main(args) +elif args.command == 'schemes': + schemes.main(args) elif args.command == 'report': report.main(args) diff --git a/tools/damon/record.py b/tools/damon/record.py index a547d479a103..86252590fef9 100644 --- a/tools/damon/record.py +++ b/tools/damon/record.py @@ -6,28 +6,12 @@ Record data access patterns of the target process. """ import argparse -import copy import os import signal import subprocess import time -debugfs_attrs = None -debugfs_record = None -debugfs_pids = None -debugfs_monitor_on = None - -def set_target_pid(pid): - return subprocess.call('echo %s > %s' % (pid, debugfs_pids), shell=True, - executable='/bin/bash') - -def turn_damon(on_off): - return subprocess.call("echo %s > %s" % (on_off, debugfs_monitor_on), - shell=True, executable="/bin/bash") - -def is_damon_running(): - with open(debugfs_monitor_on, 'r') as f: - return f.read().strip() == 'on' +import _damon def do_record(target, is_target_cmd, attrs, old_attrs): if os.path.isfile(attrs.rfile_path): @@ -36,92 +20,28 @@ def do_record(target, is_target_cmd, attrs, old_attrs): if attrs.apply(): print('attributes (%s) failed to be applied' % attrs) cleanup_exit(old_attrs, -1) - print('# damon attrs: %s' % attrs) + print('# damon attrs: %s %s' % (attrs.attr_str(), attrs.record_str())) if is_target_cmd: p = subprocess.Popen(target, shell=True, executable='/bin/bash') target = p.pid - if set_target_pid(target): + if _damon.set_target_pid(target): print('pid setting (%s) failed' % target) cleanup_exit(old_attrs, -2) - if turn_damon('on'): + if _damon.turn_damon('on'): print('could not turn on damon' % target) cleanup_exit(old_attrs, -3) if is_target_cmd: p.wait() while True: # damon will turn it off by itself if the target tasks are terminated. - if not is_damon_running(): + if not _damon.is_damon_running(): break time.sleep(1) cleanup_exit(old_attrs, 0) -class Attrs: - sample_interval = None - aggr_interval = None - regions_update_interval = None - min_nr_regions = None - max_nr_regions = None - rbuf_len = None - rfile_path = None - - def __init__(self, s, a, r, n, x, l, f): - self.sample_interval = s - self.aggr_interval = a - self.regions_update_interval = r - self.min_nr_regions = n - self.max_nr_regions = x - self.rbuf_len = l - self.rfile_path = f - - def __str__(self): - return "%s %s %s %s %s %s %s" % (self.sample_interval, self.aggr_interval, - self.regions_update_interval, self.min_nr_regions, - self.max_nr_regions, self.rbuf_len, self.rfile_path) - - def attr_str(self): - return "%s %s %s %s %s " % (self.sample_interval, self.aggr_interval, - self.regions_update_interval, self.min_nr_regions, - self.max_nr_regions) - - def record_str(self): - return '%s %s ' % (self.rbuf_len, self.rfile_path) - - def apply(self): - ret = subprocess.call('echo %s > %s' % (self.attr_str(), debugfs_attrs), - shell=True, executable='/bin/bash') - if ret: - return ret - return subprocess.call('echo %s > %s' % (self.record_str(), - debugfs_record), shell=True, executable='/bin/bash') - -def current_attrs(): - with open(debugfs_attrs, 'r') as f: - attrs = f.read().split() - attrs = [int(x) for x in attrs] - - with open(debugfs_record, 'r') as f: - rattrs = f.read().split() - attrs.append(int(rattrs[0])) - attrs.append(rattrs[1]) - return Attrs(*attrs) - -def cmd_args_to_attrs(args): - "Generate attributes with specified arguments" - sample_interval = args.sample - aggr_interval = args.aggr - regions_update_interval = args.updr - min_nr_regions = args.minr - max_nr_regions = args.maxr - rbuf_len = args.rbuf - if not os.path.isabs(args.out): - args.out = os.path.join(os.getcwd(), args.out) - rfile_path = args.out - return Attrs(sample_interval, aggr_interval, regions_update_interval, - min_nr_regions, max_nr_regions, rbuf_len, rfile_path) - def cleanup_exit(orig_attrs, exit_code): - if is_damon_running(): + if _damon.is_damon_running(): if turn_damon('off'): print('failed to turn damon off!') if orig_attrs: @@ -133,51 +53,19 @@ def sighandler(signum, frame): print('\nsignal %s received' % signum) cleanup_exit(orig_attrs, signum) -def chk_update_debugfs(debugfs): - global debugfs_attrs - global debugfs_record - global debugfs_pids - global debugfs_monitor_on - - debugfs_damon = os.path.join(debugfs, 'damon') - debugfs_attrs = os.path.join(debugfs_damon, 'attrs') - debugfs_record = os.path.join(debugfs_damon, 'record') - debugfs_pids = os.path.join(debugfs_damon, 'pids') - debugfs_monitor_on = os.path.join(debugfs_damon, 'monitor_on') - - if not os.path.isdir(debugfs_damon): - print("damon debugfs dir (%s) not found", debugfs_damon) - exit(1) - - for f in [debugfs_attrs, debugfs_record, debugfs_pids, debugfs_monitor_on]: - if not os.path.isfile(f): - print("damon debugfs file (%s) not found" % f) - exit(1) - def chk_permission(): if os.geteuid() != 0: print("Run as root") exit(1) def set_argparser(parser): + _damon.set_attrs_argparser(parser) parser.add_argument('target', type=str, metavar='', help='the target command or the pid to record') - parser.add_argument('-s', '--sample', metavar='', type=int, - default=5000, help='sampling interval') - parser.add_argument('-a', '--aggr', metavar='', type=int, - default=100000, help='aggregate interval') - parser.add_argument('-u', '--updr', metavar='', type=int, - default=1000000, help='regions update interval') - parser.add_argument('-n', '--minr', metavar='<# regions>', type=int, - default=10, help='minimal number of regions') - parser.add_argument('-m', '--maxr', metavar='<# regions>', type=int, - default=1000, help='maximum number of regions') parser.add_argument('-l', '--rbuf', metavar='', type=int, default=1024*1024, help='length of record result buffer') parser.add_argument('-o', '--out', metavar='', type=str, default='damon.data', help='output file path') - parser.add_argument('-d', '--debugfs', metavar='', type=str, - default='/sys/kernel/debug', help='debugfs mounted path') def main(args=None): global orig_attrs @@ -187,13 +75,14 @@ def main(args=None): args = parser.parse_args() chk_permission() - chk_update_debugfs(args.debugfs) + _damon.chk_update_debugfs(args.debugfs) signal.signal(signal.SIGINT, sighandler) signal.signal(signal.SIGTERM, sighandler) - orig_attrs = current_attrs() + orig_attrs = _damon.current_attrs() - new_attrs = cmd_args_to_attrs(args) + args.schemes = '' + new_attrs = _damon.cmd_args_to_attrs(args) target = args.target target_fields = target.split() diff --git a/tools/damon/schemes.py b/tools/damon/schemes.py new file mode 100644 index 000000000000..408a73813234 --- /dev/null +++ b/tools/damon/schemes.py @@ -0,0 +1,105 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0 + +""" +Apply given operation schemes to the target process. +""" + +import argparse +import os +import signal +import subprocess +import time + +import _convert_damos +import _damon + +def run_damon(target, is_target_cmd, attrs, old_attrs): + if os.path.isfile(attrs.rfile_path): + os.rename(attrs.rfile_path, attrs.rfile_path + '.old') + + if attrs.apply(): + print('attributes (%s) failed to be applied' % attrs) + cleanup_exit(old_attrs, -1) + print('# damon attrs: %s %s' % (attrs.attr_str(), attrs.record_str())) + for line in attrs.schemes.split('\n'): + print('# scheme: %s' % line) + if is_target_cmd: + p = subprocess.Popen(target, shell=True, executable='/bin/bash') + target = p.pid + if _damon.set_target_pid(target): + print('pid setting (%s) failed' % target) + cleanup_exit(old_attrs, -2) + if _damon.turn_damon('on'): + print('could not turn on damon' % target) + cleanup_exit(old_attrs, -3) + if is_target_cmd: + p.wait() + while True: + # damon will turn it off by itself if the target tasks are terminated. + if not _damon.is_damon_running(): + break + time.sleep(1) + + cleanup_exit(old_attrs, 0) + +def cleanup_exit(orig_attrs, exit_code): + if _damon.is_damon_running(): + if turn_damon('off'): + print('failed to turn damon off!') + if orig_attrs: + if orig_attrs.apply(): + print('original attributes (%s) restoration failed!' % orig_attrs) + exit(exit_code) + +def sighandler(signum, frame): + print('\nsignal %s received' % signum) + cleanup_exit(orig_attrs, signum) + +def chk_permission(): + if os.geteuid() != 0: + print("Run as root") + exit(1) + +def set_argparser(parser): + _damon.set_attrs_argparser(parser) + parser.add_argument('target', type=str, metavar='', + help='the target command or the pid to record') + parser.add_argument('-c', '--schemes', metavar='', type=str, + default='damon.schemes', + help='data access monitoring-based operation schemes') + +def main(args=None): + global orig_attrs + if not args: + parser = argparse.ArgumentParser() + set_argparser(parser) + args = parser.parse_args() + + chk_permission() + _damon.chk_update_debugfs(args.debugfs) + + signal.signal(signal.SIGINT, sighandler) + signal.signal(signal.SIGTERM, sighandler) + orig_attrs = _damon.current_attrs() + + args.rbuf = 0 + args.out = 'null' + args.schemes = _convert_damos.convert(args.schemes, args.sample, args.aggr) + new_attrs = _damon.cmd_args_to_attrs(args) + target = args.target + + target_fields = target.split() + if not subprocess.call('which %s > /dev/null' % target_fields[0], + shell=True, executable='/bin/bash'): + run_damon(target, True, new_attrs, orig_attrs) + else: + try: + pid = int(target) + except: + print('target \'%s\' is neither a command, nor a pid' % target) + exit(1) + run_damon(target, False, new_attrs, orig_attrs) + +if __name__ == '__main__': + main()