Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp814377ybh; Mon, 13 Jul 2020 01:46:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyrohrUsS9AeV+4mz9SuWeOi2e+M7ILJyHnKNeQaEmE3C4L2RvjVBQqyMaatLT90Yumi+yO X-Received: by 2002:aa7:db57:: with SMTP id n23mr89120253edt.235.1594629974273; Mon, 13 Jul 2020 01:46:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594629974; cv=none; d=google.com; s=arc-20160816; b=Xt+d+HXa6Wh8+Wtd0QAC6PfFlXz012ABLogLaPRhwe4SNktJLog/mGRdw9WVyXfdnp G1q7GAkampNdWpsZvc70nrBKY3pS5R4iJ3nC77aFOmvujqKqcnOqM6MA6AcB2rj1UhY8 tTtC4Qu+WWieWa8DDtmXbkEjDS/4DtdnA8q52xNHITSCY2yUowDyuSqIO3HBbl4t7e7k pmr0bSLk3RtskQz4WPTcph5GYkSLb3xfb/9Ua7z64pt9BAXdOuCFQkzPNXk1Y9CtwSrw cwMjBoNLAXfq0NEwosfAi2BqTiat3HxV8T7fNkhKRqiJChrx7O/TmWKRRKY/MbJCfdkJ c6Jw== 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=4dEm8qF8U2OLHXMZvIgPP2aakCi1Ynpipox7pl3k7r0=; b=suJ83AUEm38StBThG+OzQMqHmHN1tPzyQ7a76CyMvtHA1WGmr15c6Gurn7Jopoan1O E5GI+ZtBPN72vgbITrKqiW5lPOaGHrmsNZVr9dT4nplMAN2+Jil9R0cMylfuTxv/jw4d uFm2jtI8e+WNEklyxeECyW7JhLXQkd3JnR1JBjTHtvM45X71MbRv3yp5e/gWYAlSfxte bqXvmwAMSRyJezwCUjHx1iCPDbfTnaauuAPxRACZYGkx0Q1eJfkAHx7RTis1XXwwfx7s RVkv3SvUazzl0KcpTHgvAnT7C3YWrpkny7AzpI7CMKK4Bmm0w0gRmAeRD5PfijyT6LPN g6bw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=L27qYpT8; 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 j12si8903158ejy.723.2020.07.13.01.45.51; Mon, 13 Jul 2020 01:46:14 -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=L27qYpT8; 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 S1729498AbgGMIo7 (ORCPT + 99 others); Mon, 13 Jul 2020 04:44:59 -0400 Received: from smtp-fw-2101.amazon.com ([72.21.196.25]:43519 "EHLO smtp-fw-2101.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728883AbgGMIo6 (ORCPT ); Mon, 13 Jul 2020 04:44:58 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1594629897; x=1626165897; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=4dEm8qF8U2OLHXMZvIgPP2aakCi1Ynpipox7pl3k7r0=; b=L27qYpT8tSTo5ClqbqcaDVU60n1a2oqn0lA2vOi55mhUkkybICq6IjlW odB1JSPVM0MttG7iwu7AOz11Zf9y2ijipTbHP7AU4mn3wwnYotT+EvdoU 0ydD7pLq1URt/1n3RZH/WkURqvJh69O47AgJNhsl909qf8G+n4Qz7OeJX 0=; IronPort-SDR: cSOLeUs8Hgp75JkccFIitz+po+b8pcCnr/OutrIow4mPhAs2RJBR55bf92ZmH+tkjxKVOJpxNr sOjK+UBI/asw== X-IronPort-AV: E=Sophos;i="5.75,346,1589241600"; d="scan'208";a="41429853" Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-2b-55156cd4.us-west-2.amazon.com) ([10.43.8.2]) by smtp-border-fw-out-2101.iad2.amazon.com with ESMTP; 13 Jul 2020 08:44:51 +0000 Received: from EX13MTAUEA002.ant.amazon.com (pdx4-ws-svc-p6-lb7-vlan2.pdx.amazon.com [10.170.41.162]) by email-inbound-relay-2b-55156cd4.us-west-2.amazon.com (Postfix) with ESMTPS id 3D2CFA186E; Mon, 13 Jul 2020 08:44:47 +0000 (UTC) Received: from EX13D31EUA004.ant.amazon.com (10.43.165.161) by EX13MTAUEA002.ant.amazon.com (10.43.61.77) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Mon, 13 Jul 2020 08:44:46 +0000 Received: from u886c93fd17d25d.ant.amazon.com (10.43.161.146) by EX13D31EUA004.ant.amazon.com (10.43.165.161) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Mon, 13 Jul 2020 08:44:31 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v18 07/14] mm/damon: Implement access pattern recording Date: Mon, 13 Jul 2020 10:41:37 +0200 Message-ID: <20200713084144.4430-8-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200713084144.4430-1-sjpark@amazon.com> References: <20200713084144.4430-1-sjpark@amazon.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.43.161.146] X-ClientProxiedBy: EX13P01UWA002.ant.amazon.com (10.43.160.46) To EX13D31EUA004.ant.amazon.com (10.43.165.161) 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 widely used, this commit implements the feature in the DAMON, for more convenience. Signed-off-by: SeongJae Park Reviewed-by: Leonard Foerster --- include/linux/damon.h | 15 +++++ mm/damon.c | 141 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 153 insertions(+), 3 deletions(-) diff --git a/include/linux/damon.h b/include/linux/damon.h index 310d36d123b3..b0e7e31a22b3 100644 --- a/include/linux/damon.h +++ b/include/linux/damon.h @@ -72,6 +72,14 @@ struct damon_task { * in case of virtual memory monitoring) and applies the changes for each * @regions_update_interval. All time intervals are in micro-seconds. * + * @rbuf: In-memory buffer for monitoring result recording. + * @rbuf_len: The length of @rbuf. + * @rbuf_offset: The offset for next write to @rbuf. + * @rfile_path: Record file path. + * + * If @rbuf, @rbuf_len, and @rfile_path are set, the monitored results are + * automatically stored in @rfile_path file. + * * @kdamond: Kernel thread who does the monitoring. * @kdamond_stop: Notifies whether kdamond should stop. * @kdamond_lock: Mutex for the synchronizations with @kdamond. @@ -129,6 +137,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; @@ -154,6 +167,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 386780739007..55ecfab64220 100644 --- a/mm/damon.c +++ b/mm/damon.c @@ -58,6 +58,10 @@ #define damon_for_each_task_safe(t, next, ctx) \ list_for_each_entry_safe(t, next, &(ctx)->tasks_list, list) +#define MIN_RECORD_BUFFER_LEN 1024 +#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)) @@ -707,16 +711,88 @@ 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; + + if (!ctx->rbuf_offset) + return; + + 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 || !ctx->rfile_path) + return; + if (ctx->rbuf_offset + size > ctx->rbuf_len) + damon_flush_rbuffer(ctx); + if (ctx->rbuf_offset + size > ctx->rbuf_len) { + pr_warn("%s: flush failed, or wrong size given(%u, %zu)\n", + __func__, ctx->rbuf_offset, size); + return; + } + + 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: + * + *