Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp4312686ybg; Mon, 8 Jun 2020 04:51:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwyNTH7pgOZC6asn+Mh501/86ck3gUU4tr0rLbnhY2LWKMFV33E3/R3wL5yPc8zHYNCKeKS X-Received: by 2002:a17:906:b1c3:: with SMTP id bv3mr21016025ejb.292.1591617089078; Mon, 08 Jun 2020 04:51:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591617089; cv=none; d=google.com; s=arc-20160816; b=DBnx0W6pEz2dIcqLT4ZwPUwp2uK1KYD5CIX8loNOPa5q0ooE0eAwoKfxlVRmZ+X/hz j8Amzt0Hic85vbc6megSmUeBE9ltae6MPg0VA431BpcssiT5bX+xCFkFN6Hs9AKJt/kb 4xmmm2e/R50o7iiaBeyR0ymTR8iE5SDVnqhl1JZ0Tl2bVco8PybP050f2JLSleSQkP3v roXrn7UuMoRwuwikEzaeCwtokErMFb2e/hRTkKOF0oAEYkHxPTXRPvjhZ3wsw1S3QOat U2d1Q5JTq08zG8RRubAnv9VJimyGmQRquJJKh1vf93k3cjRlFKSs0fwTsH+m4pyHFvyV NUNQ== 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=xCHjIKBUCWoffWfqf85DIvBSdJh6i/aiaibM/ebIAGo=; b=Qddb2Y8dja+sy/RuVO1EbJpD6evSK0XeqWYmK0LE+Hx6SI0Fh1Tn/W/Z0CjowxzPgY ojB4TXyEkqfN6PeFExd+9CxVtqLKFQ+LcVURRaPBAlw0VdcGK+SYAoMlLzFNPEmZxvFO uM1A7QhS2VYb9fy63dig/6zWhe2HAhgegnySWd3kFJWrpcmKe41q/qSdPiCqU1ywjrE/ Jy8WzBJBBAyBhQs70PiP0hnSjXFCObpx3oQ8/+5EA7B9FLk5+lrfBglcf262cigOss2I +0ycOtaRvpUWHSyCEuOku/m4bqPOOduY+ob1mk/AzVt5bBm9HQe5xkAyludpu5So3Hlo eokg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=UmxWXSZ2; 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 n12si8246744ejz.397.2020.06.08.04.51.05; Mon, 08 Jun 2020 04:51:29 -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=UmxWXSZ2; 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 S1729648AbgFHLq5 (ORCPT + 99 others); Mon, 8 Jun 2020 07:46:57 -0400 Received: from smtp-fw-33001.amazon.com ([207.171.190.10]:8004 "EHLO smtp-fw-33001.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729568AbgFHLql (ORCPT ); Mon, 8 Jun 2020 07:46:41 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1591616799; x=1623152799; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=xCHjIKBUCWoffWfqf85DIvBSdJh6i/aiaibM/ebIAGo=; b=UmxWXSZ2EQoMhMeP3HyZZVcbD5pvsRmvgl2eIAHnMeq0uxHNKNdHD5/F riCT7vld++OXmA7XNwm4ajZJvrQWvdnSCOmBuEqXT5cn5ww3V2hfMPSBk 9g9PBymg2xPazWLzU320qOMM/uk5YJdVx+jbv7P4sqe1FjulhhokA9BEy Y=; IronPort-SDR: UQ9E77mdhMplBEMlrVEXMIOJWvARxvjiTHtBFv96DQXK+6vAZc5rS4bswxGb2Vw0q41UAMGKTp V4eUcxmetqPA== X-IronPort-AV: E=Sophos;i="5.73,487,1583193600"; d="scan'208";a="49246934" Received: from sea32-co-svc-lb4-vlan2.sea.corp.amazon.com (HELO email-inbound-relay-1d-38ae4ad2.us-east-1.amazon.com) ([10.47.23.34]) by smtp-border-fw-out-33001.sea14.amazon.com with ESMTP; 08 Jun 2020 11:46:36 +0000 Received: from EX13MTAUEA002.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan3.iad.amazon.com [10.40.159.166]) by email-inbound-relay-1d-38ae4ad2.us-east-1.amazon.com (Postfix) with ESMTPS id 5BFE5A20E7; Mon, 8 Jun 2020 11:46:25 +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, 8 Jun 2020 11:46:24 +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.1497.2; Mon, 8 Jun 2020 11:46:08 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v15 07/14] mm/damon: Implement access pattern recording Date: Mon, 8 Jun 2020 13:40:40 +0200 Message-ID: <20200608114047.26589-8-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200608114047.26589-1-sjpark@amazon.com> References: <20200608114047.26589-1-sjpark@amazon.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.43.162.53] X-ClientProxiedBy: EX13D46UWB004.ant.amazon.com (10.43.161.204) 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 Reviewed-by: Leonard Foerster --- 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 12536d9d2f74..c4796a10cb1a 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 1978ec4114bf..43f47755967e 100644 --- a/mm/damon.c +++ b/mm/damon.c @@ -58,6 +58,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)) @@ -676,16 +679,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: + * + *