Received: by 2002:a25:2c96:0:0:0:0:0 with SMTP id s144csp915768ybs; Mon, 25 May 2020 02:26:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzE9hxYpbA50dsqPsJAd1kBuiDDyYpo6wRhlfduWgXvw7Xro9m/PEP7osfccWOBpXEUYhw4 X-Received: by 2002:a05:6402:c0e:: with SMTP id co14mr14105414edb.177.1590398819670; Mon, 25 May 2020 02:26:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590398819; cv=none; d=google.com; s=arc-20160816; b=PqYslTSYH0SgDjqqcgfkl1lsL/yZRk1Dft0gpSchmqXux68d/oMp8EYy6jPcCDOpyA 3XJDmRcAhZ1tJCfpvnO43pyj1+BPmD5Xf+Pxm4O37ycLD002b0AcdAPNXQEZmUQicyi5 jRr5JrRK8maUWp+gSljTTccc+1opmUnuiZ0/sMUA0tFjnfeCE/WUiVduj9jZxxQhdTvj oKkWU2FiHLqnE45DzZh87VFUktpQLIniP2dlEdLUFuvfL8r4yU5sBGhxoskooG6DQuFQ cwUM+ic7RjRClPIVR7oFy016ec1Tij6KFLrq78I4UxTr+VwM86XgQBooFqwD5P61Pk77 Mtrg== 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=rQJwhIfRmrD9CYDS2nCUgH1BC5lTwU3ouFlV5ZiVuHY=; b=BWII3RD+LaNnUcVZDH3EVkc4pWPQZiUZKtNmf6pJ3iSXS/XfEAd8ElqpSlzZ6iYrDp IspwOXD+EdtoK2NsVREEE8jJBv0HoCk0Eom+yVS5OjXz9NiqtMyYDh/EvKwN9YW+J/4B AMMz1ZRl51tIWJ5BUAEzApIbrhKSeTDTRU5h28yctuW1HWvp6DOWCRNDKgK1Ye7fEbqr K0Y7ijkV10NRLwRR+ia+X+CDloXNRp2U4arFoKQgFShwr6gHoEUaufI5JrRXoUElaqUp Q1I6MVmoVmbXf7e5eLqwy5C9YfS9L3pozWUxMZVuVVYtjlM7yeJFc49EDL2bvr/Hxk06 llhA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=JJIITERm; 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 t7si8888704ejf.722.2020.05.25.02.26.35; Mon, 25 May 2020 02:26:59 -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=JJIITERm; 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 S2389485AbgEYJXj (ORCPT + 99 others); Mon, 25 May 2020 05:23:39 -0400 Received: from smtp-fw-33001.amazon.com ([207.171.190.10]:8806 "EHLO smtp-fw-33001.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389192AbgEYJXi (ORCPT ); Mon, 25 May 2020 05:23:38 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1590398617; x=1621934617; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=rQJwhIfRmrD9CYDS2nCUgH1BC5lTwU3ouFlV5ZiVuHY=; b=JJIITERmnqgfzRfHvBMJkCUsq/2IwXsguuajgj9kGE/KoBBOuaUmK+kj 5zIs6QLzZcJT/tkAr9v+N/+7hhpRFsRM+mYh90oMJ+9iC6UKTgqYGtL/A flVjt6xtHZjqRXLUIapaQX3FNvIXTX5msNILWZeNjS6HtBClW2Mp42IXJ Q=; IronPort-SDR: EAi2eCmECNZUb9jPtzWkKP+YYP9/CtaPhybwhaXv7pep5I7xb+jU7CDVgQRDGSVlhsdprFgF2M JTOFW+C6MTbQ== X-IronPort-AV: E=Sophos;i="5.73,432,1583193600"; d="scan'208";a="47144376" Received: from sea32-co-svc-lb4-vlan2.sea.corp.amazon.com (HELO email-inbound-relay-1e-97fdccfd.us-east-1.amazon.com) ([10.47.23.34]) by smtp-border-fw-out-33001.sea14.amazon.com with ESMTP; 25 May 2020 09:23:34 +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 10C1DA1FCF; Mon, 25 May 2020 09:23:23 +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, 25 May 2020 09:23:23 +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, 25 May 2020 09:23:07 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v13 08/15] mm/damon: Implement access pattern recording Date: Mon, 25 May 2020 11:15:05 +0200 Message-ID: <20200525091512.30391-9-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200525091512.30391-1-sjpark@amazon.com> References: <20200525091512.30391-1-sjpark@amazon.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.43.160.90] X-ClientProxiedBy: EX13P01UWA002.ant.amazon.com (10.43.160.46) 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 | 15 +++++ mm/damon.c | 131 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 143 insertions(+), 3 deletions(-) diff --git a/include/linux/damon.h b/include/linux/damon.h index e77518dd7f0d..31d436fd2843 100644 --- a/include/linux/damon.h +++ b/include/linux/damon.h @@ -66,6 +66,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. @@ -100,6 +108,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; @@ -115,6 +128,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 98367c91596a..ba002e0c3013 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: + * + *