Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp162786pxa; Tue, 4 Aug 2020 02:18:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxdT3iFpFILoPOyDCgiLelvGMLRnels++CC/66M9tu5wsp7bQnOvxWMRuS/ZgFm+ia23hUQ X-Received: by 2002:a1c:1d92:: with SMTP id d140mr3197093wmd.143.1596532724380; Tue, 04 Aug 2020 02:18:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596532724; cv=none; d=google.com; s=arc-20160816; b=kIkBQ/8IvHhIW9oxuxtH/8tJQFJV7FbDkg1JGtAbIq7PMzRECZkobO2cTZwyPf3vAW GQ/PRzDf+v3j2ij/vO5nOF275gauqosWvuqgaCQ80HlYqkRikiUQraIU7+j2aULWeblU dBMr1BHpQHA4KLSsvDJ9npyxl0Bb83TRJ0GqO53jBJIlvtiBo9VoBT7ul0mCnnabf0wN 5NDzxmmtFUEeW5wnfLtK2CxpxvUVoxx143KUZGUn1l+k+RzmgwSFdZEt2af9RUXR3T/l dz55QZiaXuAbPltKkwVx93wtol7jI4A2dVwOcUmo1ZcGOERGY9DfIz1LCfkM3d+GxcL6 yXig== 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=+msM65TSYoEYqlq1Q40WvWM/P8W82CwRgOMq1o/5Bps=; b=vodtd/m1eeUjhDDAW74E5FyO5/Jo54CGipO5ZJNZ0qM1OJN0bTnhMDx0H+pkXEmcXc +6ptaKkhEKUSJJFJ/plrUcb7rouxdAny3C/EaMlTJXvKQE4c6CRlWYeOc5L5DPLaO3rR bP//hC6Njf/fSjawok5YlPo6kIWLOgD2tgL6ZeTGtGizhOPXTM3XYNu4mNgMYJQ8Hxz0 lsxqWuKPdNcxUbuV5MlWJqT9WJO1S7h1B88T02zbILN2Jd/ed0fI4DLffv0jQjsA9VnP 7u4mYCSdRfERXU3EYtdlkDBi7852zDJqAU9xUGCu3Dbut8PyKJYtQ3qYj14GQr19eRCe AUFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=rFwbEAqI; 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 i7si4885200ejo.667.2020.08.04.02.18.21; Tue, 04 Aug 2020 02:18:44 -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=rFwbEAqI; 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 S1727113AbgHDJRb (ORCPT + 99 others); Tue, 4 Aug 2020 05:17:31 -0400 Received: from smtp-fw-6001.amazon.com ([52.95.48.154]:25659 "EHLO smtp-fw-6001.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726056AbgHDJRb (ORCPT ); Tue, 4 Aug 2020 05:17:31 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1596532649; x=1628068649; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=+msM65TSYoEYqlq1Q40WvWM/P8W82CwRgOMq1o/5Bps=; b=rFwbEAqIDYIpA/23LnJWKirdz81r0hataqyCrMpN4R1kJDO56yFd8pKy E2/A/Nbdgy7wxgWt7u+MEB9tYCgfBEbDywt6qBBewhbcspqedV8M04S/C TvRyWewnpEBBlMvyS1ZfJIxUSCeZjmy5AAxzZyR1mnSi8txVubX5KrGte c=; IronPort-SDR: 2LfGF0tsgh/wgkOuL82YJ6XTzOdgIhvKKNVgcxt+7J6ZBaHpd55+EIealJpLL4tCWnUTTCzOU3 TCRFj052VsVw== X-IronPort-AV: E=Sophos;i="5.75,433,1589241600"; d="scan'208";a="47335648" 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-6001.iad6.amazon.com with ESMTP; 04 Aug 2020 09:17:24 +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 AE3C2A2596; Tue, 4 Aug 2020 09:17:21 +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, 4 Aug 2020 09:17:21 +0000 Received: from u886c93fd17d25d.ant.amazon.com (10.43.162.248) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Tue, 4 Aug 2020 09:17:03 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v19 07/15] mm/damon: Implement access pattern recording Date: Tue, 4 Aug 2020 11:14:08 +0200 Message-ID: <20200804091416.31039-8-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200804091416.31039-1-sjpark@amazon.com> References: <20200804091416.31039-1-sjpark@amazon.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.43.162.248] X-ClientProxiedBy: EX13D36UWA004.ant.amazon.com (10.43.160.175) 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 widely used, this commit implements the feature in the DAMON, for more convenience. Signed-off-by: SeongJae Park Reviewed-by: Leonard Foerster --- include/linux/damon.h | 15 +++++ mm/damon.c | 141 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 153 insertions(+), 3 deletions(-) diff --git a/include/linux/damon.h b/include/linux/damon.h index f79112c56bbc..ad771c39d1a9 100644 --- a/include/linux/damon.h +++ b/include/linux/damon.h @@ -75,6 +75,14 @@ struct damon_target { * 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. @@ -139,6 +147,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; @@ -167,6 +180,8 @@ int damon_set_targets(struct damon_ctx *ctx, 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 fa908dc39270..b3420ba97fd2 100644 --- a/mm/damon.c +++ b/mm/damon.c @@ -57,6 +57,10 @@ #define damon_for_each_target_safe(t, next, ctx) \ list_for_each_entry_safe(t, next, &(ctx)->targets_list, list) +#define MIN_RECORD_BUFFER_LEN 1024 +#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)) @@ -772,16 +776,88 @@ 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; + + if (!ctx->rbuf_offset) + return; + + 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 || !ctx->rfile_path) + return; + if (ctx->rbuf_offset + size > ctx->rbuf_len) + damon_flush_rbuffer(ctx); + if (ctx->rbuf_offset + size > ctx->rbuf_len) { + pr_warn("%s: flush failed, or wrong size given(%u, %zu)\n", + __func__, ctx->rbuf_offset, size); + return; + } + + 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: + * + *