Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp2816513ybz; Mon, 27 Apr 2020 05:11:08 -0700 (PDT) X-Google-Smtp-Source: APiQypKOda1AzVpUC2QuXNs9GcpAtap/R8VUUq8HSzFatlmYBQPSmlicrTtP9MwnyOEl71iOYErQ X-Received: by 2002:a17:906:16ca:: with SMTP id t10mr19791909ejd.122.1587989468084; Mon, 27 Apr 2020 05:11:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587989468; cv=none; d=google.com; s=arc-20160816; b=qJpEB8dvgGJR2qEiV4Dvpujuo2EBjfbUc5ooqU5wvNORAqVwsEgcPOvCHFg76F0uZM j2LTWgsMLjlJdtjH244HreXu8SVHdIkVQze21h21YQ3lMZ+xSW4Nl1sHDnApixdmdbAa lAywvKOU8Neq7+7cv2yH8uPXiPi2gVqHeBf+6fZYWdNhy94kaO4omI/H38tXrU7tnnsZ J//YLC5WqLscySQ04TxkSlg40KrzAjKkIxwboRI1zdDgcY4bM2Di8mN7Bci79En8mu3J kr/lsk1tiE8nV0xdpt2jO4iL+L/BweIJ3ky482v1gAhbKrQMzXNq/AOqRxhTJydR14Lh YSvg== 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=DE1OnifYiXqw9Om7Vnyy3tf4L5PRElje19LPPu8+r8w=; b=brHIXnn8fQgnrtnd9/ELDBxhJ4BOChOn/2wj5W8dPlFDjbxLpWJ2sRkXJ+o6HPlqd3 OJ3RG5sObSAFGxXZWd5unrV+bxt/+Qk5pwau9sPwLad0/6RckQ8UpOegd6GZIUsZ1TBM 2UWK75cMRZYcw7RqepBIjEB0C9d/DEI+2ZNQZPQ2xZPjn6aZSrKfxFcGxShYS0XlkW0h kOcHb/SFCOvtrGulO/JqLGycdW3LEHB5PBaLc43BSulvmoGWMwvgrSGG0D24ic68eix8 IEcZH6rKaWvjKwpBvD1tpWLdNTUlXxOC7ReVtK9FgeR5upndm1yTYS03Lrdh9dpXbuUy ds4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=fLBvj22t; 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 g12si8487559ejw.314.2020.04.27.05.10.44; Mon, 27 Apr 2020 05:11:08 -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=fLBvj22t; 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 S1727787AbgD0MJA (ORCPT + 99 others); Mon, 27 Apr 2020 08:09:00 -0400 Received: from smtp-fw-6002.amazon.com ([52.95.49.90]:42975 "EHLO smtp-fw-6002.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726504AbgD0MI7 (ORCPT ); Mon, 27 Apr 2020 08:08:59 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1587989338; x=1619525338; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=DE1OnifYiXqw9Om7Vnyy3tf4L5PRElje19LPPu8+r8w=; b=fLBvj22tO93Z4jI/Yzs8SxlyPdN788pFH0D5oImz8gqy/SdA0BystRo8 pfW3xtf9kPyim2rcbpQ0VIeimISqBu2q/DG2x/95I9ThP+ac2xD5irAgT tZbs9hhoDjKduTWc7cST6L7wZSUtLBjww7o39tW2xLJX6Uvtbnj5yUdCE g=; IronPort-SDR: 6n+gskDtQxCEeeAUGOeucIh80bubTS2Fj8gzEHZFCwRC/P6JHy5jaMsjA37c7nkDPaUR9j0cq0 OregcYHKWmew== X-IronPort-AV: E=Sophos;i="5.73,324,1583193600"; d="scan'208";a="27431295" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-2a-f14f4a47.us-west-2.amazon.com) ([10.43.8.6]) by smtp-border-fw-out-6002.iad6.amazon.com with ESMTP; 27 Apr 2020 12:08:43 +0000 Received: from EX13MTAUEA002.ant.amazon.com (pdx4-ws-svc-p6-lb7-vlan2.pdx.amazon.com [10.170.41.162]) by email-inbound-relay-2a-f14f4a47.us-west-2.amazon.com (Postfix) with ESMTPS id CDFE4A18D5; Mon, 27 Apr 2020 12:08:39 +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, 27 Apr 2020 12:08:39 +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, 27 Apr 2020 12:08:23 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v9 08/15] mm/damon: Implement access pattern recording Date: Mon, 27 Apr 2020 14:04:35 +0200 Message-ID: <20200427120442.24179-9-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200427120442.24179-1-sjpark@amazon.com> References: <20200427120442.24179-1-sjpark@amazon.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.43.162.53] X-ClientProxiedBy: EX13D34UWC004.ant.amazon.com (10.43.162.155) 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 863b60e16a4a..621217b58173 100644 --- a/mm/damon.c +++ b/mm/damon.c @@ -44,6 +44,9 @@ #define damon_for_each_task_safe(ctx, t, next) \ 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: + * + *