Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp582655ybj; Tue, 5 May 2020 04:16:59 -0700 (PDT) X-Google-Smtp-Source: APiQypJsjOBBlKJ2rzxLeYq8D7cSk4wVZmysP5FQvaVTHV5JriLPmK+LGccKtDaGbBra7EI6caQp X-Received: by 2002:a50:d71e:: with SMTP id t30mr2146832edi.246.1588677418976; Tue, 05 May 2020 04:16:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588677418; cv=none; d=google.com; s=arc-20160816; b=JsMtNq5crWj0wvAuK1lI9y0ys/5LWOit5/25Rd0XW/GxkThsRTCMpRpD3LCFdSA6wq JPWzI8tFYsLsxMRInUi7VqUb3Jc+T5Yykc0F2mqtS5LnLQsioZVS3OPSLOC7ENEPmRTT zeWQ+WWT/fP8/aC5Ks+qrOtmLoVf17olipUhIJ7OHo2WViNI3Vv6zl+lAQ5SVhVXfShJ koP1S2Tb4hcovQtj6BG8BMvNHjjz/n8bKoDEVmgeVitg1R2Wsxs5ftXfRfhEL/vw8GWh WqGYcyvGI53eDKnLSCXWJpwXyZ+EdQRiFyGCBuSfn9IOTF69g8F2I5e7e42Jt8eDeQ+E PnMw== 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=g0IXOe13EZwndz+ijjOinzT9m8wjzvhrMhFFlIN3nwo=; b=D8+f9KlgxI45NKyIekK1OdYaAeBLS7WdyJtJK0R0Vg2NtEIrSGLXcmYqgudhblQe9v wpvf+vUHEGC5qHWUneKbfAYysFA87YCzpjzq6RfVDCLBaZ0S9gEg67FFhGFYkVb7HTms xXxohO8wH5HigW9WiKKgcSmjMBkLiaB4kfVHZHbk3HpqQPEFKJaxM6Kda41mlc+rV3cC so+mF7Ve02qR/K6E1Jrpu+t2XlCXz6lOVCVZlnVqJX27aGCgUuIpFaMbdB6FyoOkoXIb 0pHj1B42DPjA0RHExf19dQyBZzi1vg/44ng8Hrj2KgbfW71dIaJRx+o5auM9vtaK9106 xbIQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=e76XQsBb; 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 f3si1005901edw.598.2020.05.05.04.16.35; Tue, 05 May 2020 04:16:58 -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=e76XQsBb; 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 S1728733AbgEELMk (ORCPT + 99 others); Tue, 5 May 2020 07:12:40 -0400 Received: from smtp-fw-33001.amazon.com ([207.171.190.10]:15013 "EHLO smtp-fw-33001.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727090AbgEELMk (ORCPT ); Tue, 5 May 2020 07:12:40 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1588677158; x=1620213158; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=g0IXOe13EZwndz+ijjOinzT9m8wjzvhrMhFFlIN3nwo=; b=e76XQsBbGThIjObhVi67AVRGz/XVa88FYFvu+LRix0dY/HTvSUbZbzBL OlhVG5x1C84EQbZ5uQOcLXAVTe7ea+N+eXcIYaLDXoIYzj5keis2ssfJB PwgIlxlFfOjZCdjxHjNHnjuIFjLZxwJoQnVdEj9zmJVR20ofurkYIikKO M=; IronPort-SDR: MMeRY7VAnTOEnZm5LKNS3Zaf3Hc6Y4ntpwf+2ZqwrBVP2rktuUsZz2H6nW/rCcWXYnDs/gj+KU md00BiQeMWPA== X-IronPort-AV: E=Sophos;i="5.73,354,1583193600"; d="scan'208";a="42753874" Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-2c-397e131e.us-west-2.amazon.com) ([10.47.23.38]) by smtp-border-fw-out-33001.sea14.amazon.com with ESMTP; 05 May 2020 11:12:37 +0000 Received: from EX13MTAUEA002.ant.amazon.com (pdx4-ws-svc-p6-lb7-vlan3.pdx.amazon.com [10.170.41.166]) by email-inbound-relay-2c-397e131e.us-west-2.amazon.com (Postfix) with ESMTPS id BA088A16AF; Tue, 5 May 2020 11:12:35 +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; Tue, 5 May 2020 11:12:35 +0000 Received: from u886c93fd17d25d.ant.amazon.com (10.43.160.92) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Tue, 5 May 2020 11:12:19 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v10 09/16] mm/damon: Implement access pattern recording Date: Tue, 5 May 2020 13:08:08 +0200 Message-ID: <20200505110815.10532-10-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200505110815.10532-1-sjpark@amazon.com> References: <20200505110815.10532-1-sjpark@amazon.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.43.160.92] X-ClientProxiedBy: EX13D35UWB004.ant.amazon.com (10.43.161.230) 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 89ac5b245945..dd1c5719859e 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: + * + *