Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp2597116ybk; Mon, 18 May 2020 03:12:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxHU2061RFVTuANszlfKW/wbVvgHd7j9ocjhSe8z0FwyjElxFYEwn93z7RiT6uphG7ckUSI X-Received: by 2002:a05:6402:1770:: with SMTP id da16mr1866775edb.122.1589796764672; Mon, 18 May 2020 03:12:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589796764; cv=none; d=google.com; s=arc-20160816; b=wSRfPyBq+y394s+a0wBf+DC+jGRB9Nw0/n63V9ThlMDzY5jUyMSwc78cDvCFxQjSWB ULepfBRRVT/TbIkHgHhsJ5GFSl+9+ahwTF01mGpDIkXGB4IDTybll/S3f7RmOETTT8Sv lIDozTaAGRKJZAX+PosGvW0QJz30tDPeyEjbRKKSW0TLyPCEcZ555pXC+NBOJoQDLkx1 +NhOaOFRKYOxJ8LzTQ23PehJK3BvB6OISyMNSwf9Dc5AzfYpkICCqrdPvMyhWW8o4eTt W+saKPEZPv24ZfxZDncJnpMkQA3Xzg0q+NwmoHHJ1f96QD5DJY0YC4t8C8R0BA0GlfBK LGwA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:ironport-sdr:dkim-signature; bh=yPGtxjnGdW9tLlgPI8w/Gq/33o5dSn7TJ6/TV2fK720=; b=UceNna64Xg3+bjDt1vr9TK0zNcCg9hcdRDaQkAkRng+7RnGCAuehdS/EPc1s8ZWwCR F2lskwlJbpWFsiCU9Gd2IPujp5qVsT6joJuWOySFno4umqc4xGXJama9SBCXUlcEooc8 nqzK8uNwUjYpFRNlpj7hdWlKTkV64HtgOwZ2YQlJTZ7+6T7qF6KoN2vsqR/uNFiD/H6T FrEdOn2NdcrjeQZ16y+m0flYY+76mjf/p3zbDH46Lq1ULmPolCotZugP4XQnP6ZVnfOd OtmqkI7hOnb+oT/6dnSLOSQfgr4IliBCmpew57ij6Ppg91Gpvvq9fhbCv3Wj0eNpN9u8 fgRw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b="A/E6TJEM"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d20si6665950edj.13.2020.05.18.03.12.21; Mon, 18 May 2020 03:12:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b="A/E6TJEM"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726511AbgERKIi (ORCPT + 99 others); Mon, 18 May 2020 06:08:38 -0400 Received: from smtp-fw-6002.amazon.com ([52.95.49.90]:25119 "EHLO smtp-fw-6002.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726040AbgERKIh (ORCPT ); Mon, 18 May 2020 06:08:37 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1589796517; x=1621332517; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=yPGtxjnGdW9tLlgPI8w/Gq/33o5dSn7TJ6/TV2fK720=; b=A/E6TJEMWMGmF0yZfEc5a7xvn6zESgWAq10i8zahYyuEWcvG76rTs5Qh VaVLNTKerLrrmyW5c+NO9kmRuJsjmNnJCL+dUQfV2yrYT7KhWuxBEhMSi yVPWKOrH6LuYFE94xlqSkSgAU27V47mdsfArNCTX87fcYgtqCpX39tMRe w=; IronPort-SDR: 5ybwXePamaaNKjYs3jc+6/JdHC5xMZSgaJeYxvlug3F2w1zuuCRna2V1aw7lz/vzEMn1h5RZuc aC16CY87MdNA== X-IronPort-AV: E=Sophos;i="5.73,406,1583193600"; d="scan'208";a="30703487" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-1e-57e1d233.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-out-6002.iad6.amazon.com with ESMTP; 18 May 2020 10:08:37 +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-57e1d233.us-east-1.amazon.com (Postfix) with ESMTPS id 1F19714181E; Mon, 18 May 2020 10:08:24 +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.1497.2; Mon, 18 May 2020 10:08:24 +0000 Received: from u886c93fd17d25d.ant.amazon.com (10.43.160.90) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Mon, 18 May 2020 10:08:08 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v12 09/16] mm/damon: Implement access pattern recording Date: Mon, 18 May 2020 12:00:11 +0200 Message-ID: <20200518100018.2293-10-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200518100018.2293-1-sjpark@amazon.com> References: <20200518100018.2293-1-sjpark@amazon.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.43.160.90] X-ClientProxiedBy: EX13D04UWA003.ant.amazon.com (10.43.160.212) To EX13D31EUA001.ant.amazon.com (10.43.165.15) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: SeongJae Park This commit implements the recording feature of DAMON. If this feature is enabled, DAMON writes the monitored access patterns in its binary format into a file which specified by the user. This is already able to be implemented by each user using the callbacks. However, as the recording is expected to be used widely, this commit implements the feature in the DAMON, for more convenience and efficiency. Signed-off-by: SeongJae Park --- include/linux/damon.h | 7 +++ mm/damon.c | 131 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 135 insertions(+), 3 deletions(-) diff --git a/include/linux/damon.h b/include/linux/damon.h index 264569b21502..bc46ea00e9a1 100644 --- a/include/linux/damon.h +++ b/include/linux/damon.h @@ -50,6 +50,11 @@ struct damon_ctx { struct timespec64 last_aggregation; struct timespec64 last_regions_update; + unsigned char *rbuf; + unsigned int rbuf_len; + unsigned int rbuf_offset; + char *rfile_path; + struct task_struct *kdamond; bool kdamond_stop; struct mutex kdamond_lock; @@ -65,6 +70,8 @@ int damon_set_pids(struct damon_ctx *ctx, int *pids, ssize_t nr_pids); int damon_set_attrs(struct damon_ctx *ctx, unsigned long sample_int, unsigned long aggr_int, unsigned long regions_update_int, unsigned long min_nr_reg, unsigned long max_nr_reg); +int damon_set_recording(struct damon_ctx *ctx, + unsigned int rbuf_len, char *rfile_path); int damon_start(struct damon_ctx *ctx); int damon_stop(struct damon_ctx *ctx); diff --git a/mm/damon.c b/mm/damon.c index 498160b5e086..ddd78843f79a 100644 --- a/mm/damon.c +++ b/mm/damon.c @@ -44,6 +44,9 @@ #define damon_for_each_task_safe(t, next, ctx) \ list_for_each_entry_safe(t, next, &(ctx)->tasks_list, list) +#define MAX_RECORD_BUFFER_LEN (4 * 1024 * 1024) +#define MAX_RFILE_PATH_LEN 256 + /* Get a random number in [l, r) */ #define damon_rand(l, r) (l + prandom_u32() % (r - l)) @@ -565,16 +568,80 @@ static bool kdamond_aggregate_interval_passed(struct damon_ctx *ctx) } /* - * Reset the aggregated monitoring results + * Flush the content in the result buffer to the result file + */ +static void damon_flush_rbuffer(struct damon_ctx *ctx) +{ + ssize_t sz; + loff_t pos = 0; + struct file *rfile; + + rfile = filp_open(ctx->rfile_path, O_CREAT | O_RDWR | O_APPEND, 0644); + if (IS_ERR(rfile)) { + pr_err("Cannot open the result file %s\n", + ctx->rfile_path); + return; + } + + while (ctx->rbuf_offset) { + sz = kernel_write(rfile, ctx->rbuf, ctx->rbuf_offset, &pos); + if (sz < 0) + break; + ctx->rbuf_offset -= sz; + } + filp_close(rfile, NULL); +} + +/* + * Write a data into the result buffer + */ +static void damon_write_rbuf(struct damon_ctx *ctx, void *data, ssize_t size) +{ + if (!ctx->rbuf_len || !ctx->rbuf) + return; + if (ctx->rbuf_offset + size > ctx->rbuf_len) + damon_flush_rbuffer(ctx); + + memcpy(&ctx->rbuf[ctx->rbuf_offset], data, size); + ctx->rbuf_offset += size; +} + +/* + * Flush the aggregated monitoring results to the result buffer + * + * Stores current tracking results to the result buffer and reset 'nr_accesses' + * of each region. The format for the result buffer is as below: + * + *