Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp1840739ybk; Mon, 11 May 2020 05:41:09 -0700 (PDT) X-Google-Smtp-Source: APiQypIGye19Q8KBeQ3NSxROlYH8M8Mmu0R68vc3VhnFox2bO+nRcB7u7pk1fs1Yo6mWqKkYiJkT X-Received: by 2002:a17:907:2142:: with SMTP id rk2mr12844002ejb.225.1589200868989; Mon, 11 May 2020 05:41:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589200868; cv=none; d=google.com; s=arc-20160816; b=uysJ37rQRDHu/BJznu3c2W/YmvOEN7j8YGkT1IolEjEuzTiDK08ijty0VeQwvsAVFG VVa3g2qZfDt32cWGOVklCwqt4CDiJ5TulMPiMLPvQGghdGmERK92DRpYxsX/hpDwYSYp tISxbNRnIpnZ2aC4QPjaXko8MpeRSH4+yRIRdvIdomxl3Wt5RLNS6KeR3DX1g7LjBcE1 k3tAiCtw2rP+yy2uGYHvwZUh1gi5vkByQLFlzqMk95dhwBrAN8G+vxCe0x6owPKp7lr0 2I0RZwj+7xvajWkFZ5+QSNIaKrdKsu8OSeXnPH+6TrsqKnMmtSiVF3/uF5MlcFlfy4ga cIxw== 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=W3k9486TiUCXIiW/oNgYHBamjnpyXjK4jEu9bY7t9VA=; b=FoCI+LONBtKzCx/WP5nNqK0dO5OuHahTBwX0pQ5AN6KXM3/8raGploaDyj4sx7gG0w 3RDqvzxDkCLlxDSHNf3MoipFyHXKQ5LLHIAt7sEq3hPWO92F3302Qqv9f5y9nWfSGgME TU7Vv/JksIdwgkpWy14ff23S3rpZKLAqZyIQ8gYff+Blsye5y9KXlQ7aMx3kR7zzWcnQ r7r9CbOjOCk/UTUIESp7wd4zzG/QZ9mYnTVKr1tnxN8MMreqQydfcDL9r4Ar40cqU9PJ UeTuLpWFExJYgQgCHFVhgAW/ihWhn9j18mMgepkbgaW2rFKUZbpbvocIgMH07v/eKqVX za1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=O1yYfprs; 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 y5si5749917edr.90.2020.05.11.05.40.45; Mon, 11 May 2020 05:41: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=O1yYfprs; 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 S1730121AbgEKMiJ (ORCPT + 99 others); Mon, 11 May 2020 08:38:09 -0400 Received: from smtp-fw-9101.amazon.com ([207.171.184.25]:1264 "EHLO smtp-fw-9101.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729563AbgEKMiJ (ORCPT ); Mon, 11 May 2020 08:38:09 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1589200688; x=1620736688; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=W3k9486TiUCXIiW/oNgYHBamjnpyXjK4jEu9bY7t9VA=; b=O1yYfprsITcicAtyu33UHHgmuZIOY7SaBoR6TwHskJhwPupxqRZtiWjk XFFiOivyGYyLih62aEMsbv0FCBRYQTKi7qeHaV6BE1jjka7GKiRi2vhMg wH/igvXPKjBLee5NOa3KzAN2wDxxPl56DAezgHXRSmaIhktZAIqhYY68S 4=; IronPort-SDR: 4Db7iDOJUcTamZ/BaSNIZdEn4u+dcGHJw/glBhf52nJ0svpAi4IJsBOH4yF8/NAw56gfmBKR6h 0V0BTcHZuBjA== X-IronPort-AV: E=Sophos;i="5.73,380,1583193600"; d="scan'208";a="34195384" Received: from sea32-co-svc-lb4-vlan2.sea.corp.amazon.com (HELO email-inbound-relay-1e-17c49630.us-east-1.amazon.com) ([10.47.23.34]) by smtp-border-fw-out-9101.sea19.amazon.com with ESMTP; 11 May 2020 12:38:05 +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-17c49630.us-east-1.amazon.com (Postfix) with ESMTPS id E30E5A20DC; Mon, 11 May 2020 12:37:53 +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, 11 May 2020 12:37:53 +0000 Received: from u886c93fd17d25d.ant.amazon.com (10.43.161.253) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Mon, 11 May 2020 12:37:37 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v11 09/16] mm/damon: Implement access pattern recording Date: Mon, 11 May 2020 14:32:55 +0200 Message-ID: <20200511123302.12520-10-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200511123302.12520-1-sjpark@amazon.com> References: <20200511123302.12520-1-sjpark@amazon.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.43.161.253] X-ClientProxiedBy: EX13D06UWC001.ant.amazon.com (10.43.162.91) 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 6b01aa641503..306640c72b7d 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: + * + *