Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp532430ybh; Wed, 18 Mar 2020 04:35:46 -0700 (PDT) X-Google-Smtp-Source: ADFU+vs6oCrSwTxLNhjRVmk0wvYD8QuCJzA4Q/xkdCpaCitf6NDWmXktOfZJr8b0jElxcth6c5pS X-Received: by 2002:a9d:bf7:: with SMTP id 110mr3536006oth.259.1584531346702; Wed, 18 Mar 2020 04:35:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584531346; cv=none; d=google.com; s=arc-20160816; b=bePFrzxp1V/dA2K9vUIzMz+qShrE8sByah7s/P7XF/yzvb9JlOFgGrUDXeU+wieipD BkzJH1h6agUsIBTU+RExWq4YHlh4aWXrU3OotlduXmno2Zkwx6hQmzVxUnY3+6TyDWQd bCfz+6pb5VZyVZuYTIQCu1D4E8cyP1iOcKSI+qjTQ2Us/xCdONM9U2ZVpkhzra3U0C0D WxtlrlC7TIHoRT0Me+gBSkYSAei5TFEMNm12hOo2cDA7sprdOG7U1ZKnZKeONtV5K96k B+lvQXyIfa3gYSckaAApZ72MyiGOyiSioA7LJUqbrLdYcvGNMaBfhxERbKp+ITtpj1eN 4bsA== 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=s0AfyZ6CiQ6AzJS05n/GM916ac2WmoGpXpoaffAKTPY=; b=grux14RsuyxYUdz//C0CVzzLUEfwVZ6pnRzz9F/csB59i2+QMcrXFvGt8TiXwJy6YV iQ1k287dRYm+KUGVg48SlFN2fYZlIJSwG0x54QmPn2JZ4Y1Hxzd0DykQcocx+cUKFORg j+wJ/j1FUbWdoiin+BFOzmE6Fk+cAFyBpO/5fCAreP3xkAVUTsKTY3P6F0FSPTjhQrpK KCZJ1genYuoYoVAWFiCWY1S/hX3yXWNcLarMAVE30a416vy8dtGRmPUdS6hfJYBT3FnK 6OKMfcabM0JObjJuOz4zxHFgoMspbpNXXFVbei3ewjbha9JDHsou16/agNtKHlA3Yvhc XqLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=p3saGtPp; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id d19si3336702otq.162.2020.03.18.04.35.26; Wed, 18 Mar 2020 04:35:46 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=p3saGtPp; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1727793AbgCRLdo (ORCPT + 99 others); Wed, 18 Mar 2020 07:33:44 -0400 Received: from smtp-fw-2101.amazon.com ([72.21.196.25]:55731 "EHLO smtp-fw-2101.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726855AbgCRLdo (ORCPT ); Wed, 18 Mar 2020 07:33:44 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1584531225; x=1616067225; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=s0AfyZ6CiQ6AzJS05n/GM916ac2WmoGpXpoaffAKTPY=; b=p3saGtPpSuoKmxTvO2FY2vQN4+/PEK2FPefK6Isvl6by1wbVpSuNkGSL M+BRY0DiwW4yJ8WToUOdseEwRCL1aedhYjbtDp26Pve7WyVMMv+pTxTH9 UcHpK877UyKeT5cZnHvudOnKE6OKWzbpivORQVfvW0f2DinqZDqPb+bFG s=; IronPort-SDR: ODNl+8upXqZPtYW0Y6p5MvnEOupndjxY/OoQ+9dHBo6dRPKg+lr3MHLx3F8aevk1QFZqwDrqGv WT5HT4ca0wAQ== X-IronPort-AV: E=Sophos;i="5.70,567,1574121600"; d="scan'208";a="22002595" Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-1a-7d76a15f.us-east-1.amazon.com) ([10.43.8.2]) by smtp-border-fw-out-2101.iad2.amazon.com with ESMTP; 18 Mar 2020 11:33:44 +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-7d76a15f.us-east-1.amazon.com (Postfix) with ESMTPS id BA158A26C4; Wed, 18 Mar 2020 11:33:33 +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; Wed, 18 Mar 2020 11:33:32 +0000 Received: from u886c93fd17d25d.ant.amazon.com (10.43.161.235) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Wed, 18 Mar 2020 11:33:14 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v7 08/15] mm/damon: Implement access pattern recording Date: Wed, 18 Mar 2020 12:27:15 +0100 Message-ID: <20200318112722.30143-9-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200318112722.30143-1-sjpark@amazon.com> References: <20200318112722.30143-1-sjpark@amazon.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.43.161.235] X-ClientProxiedBy: EX13D28UWC001.ant.amazon.com (10.43.162.166) 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 | 120 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 124 insertions(+), 3 deletions(-) diff --git a/include/linux/damon.h b/include/linux/damon.h index cad3ee3d78db..47fb0ec03030 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; struct mutex kdamond_lock; @@ -66,6 +71,8 @@ int damon_set_pids(struct damon_ctx *ctx, unsigned long *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 2e8af1ce4389..59d49b0990cf 100644 --- a/mm/damon.c +++ b/mm/damon.c @@ -41,6 +41,8 @@ #define damon_for_each_task_safe(ctx, t, next) \ list_for_each_entry_safe(t, next, &(ctx)->tasks_list, list) +#define MAX_RFILE_PATH_LEN 256 + /* Get a random number in [l, r) */ #define damon_rand(ctx, l, r) (l + prandom_u32_state(&ctx->rndseed) % (r - l)) @@ -523,16 +525,81 @@ 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; + struct file *rfile; + + while (ctx->rbuf_offset) { + pos = 0; + 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; + } + + sz = kernel_write(rfile, ctx->rbuf, ctx->rbuf_offset, &pos); + filp_close(rfile, NULL); + + ctx->rbuf_offset -= sz; + } +} + +/* + * 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 regions. The format for the result buffer is as below: + * + *