Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp1282644ybg; Tue, 2 Jun 2020 06:11:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxbVschSkbpVdRDFPJPQqyPD9RbYSqiZur1s0eD0o2ITviUtJ0IpQ7MC74OLJN+37dTYfxQ X-Received: by 2002:a05:6402:b47:: with SMTP id bx7mr7469205edb.274.1591103461230; Tue, 02 Jun 2020 06:11:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591103461; cv=none; d=google.com; s=arc-20160816; b=euwSMWW0FbxkhTZhRfcgdMKr6vnxdif3OuLkeDURWWFOVxG1lRi7XNC/SjHJVJJYiS 6/lDhK6EM51Jv6INHkH3aaO8dh8S8UeEeAShCgqIeAe8aVbL323UvxFSbowZ6IcVlHFO 7zOcRzX0O2PVI4IwA4GTvF92xEUIkbn7thUapeCHxKtu6q/oMI3qAaY9uzfQPR1Xz9Ht 4mzItcow4aVpj8xu+FbUwU+DwJxiOVLnADO0dNFPlHgouSSpynIE2ZJJ6XZVoXIvgZJA X55eSUpGa9uF3kGCc4aRkrLdn9/UB38AHcArbSLRLASMpJL6VghvstWPxyxvfFkaU1Xi MngA== 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=9r8KYnrcusulDTuu4PUZ0utxTKjX9E9H4ARRS8sK/Ak=; b=p2ZRVqDCHmj2SKhBzdbiFDo8x0DoFH7F/tLLbXLQPfjS3G6fObBLfg52IWDf29XkWJ sliToLTzjoNjjQoFoL/QLtaMVpLKGBrFXv7zVF+Hcxt5kRKTf6Q6Cnz6YuQdRttil8/h tD86CrDtQOcHHX7fL7d4LukbARFsRkWzuBRJI7lZNYk3ZHwPHnxpNvqNe/R7/i46zEgT txxuqcAzN7AUjgRQyFaWJzxv0r104WeFIm1/GrzaTqZef3y/Eck9tAAY4Q9YxmBJAq9X uxhp2FdB2cOvu/lsNlr48ZbcIYFdF7rrACEzp2G4i5OQGbn6//pr5BKxPPUyNow0DPJX 8rqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=vyfYZcis; 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 e4si1421328edm.144.2020.06.02.06.10.37; Tue, 02 Jun 2020 06:11:01 -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=vyfYZcis; 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 S1728638AbgFBNIF (ORCPT + 99 others); Tue, 2 Jun 2020 09:08:05 -0400 Received: from smtp-fw-33001.amazon.com ([207.171.190.10]:12625 "EHLO smtp-fw-33001.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726485AbgFBNID (ORCPT ); Tue, 2 Jun 2020 09:08:03 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1591103282; x=1622639282; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=9r8KYnrcusulDTuu4PUZ0utxTKjX9E9H4ARRS8sK/Ak=; b=vyfYZcisANJ4Yxb5D+zbUKTeGnDPBBtF3a2INx6S9vO80Y594rG7UxRB UHpHCW02nqRIH+YdNFGv6CTqGSzaExFHyEANK8VlHl2i3Z4c6PYpO57Wt gQAoD29atq0ZzCcPZP8s/VOF+HUF2J4F2JkJCCc41SZOF3Ge45zM0uyDn k=; IronPort-SDR: BGM9BoaSXT4d86sKHbUY2ReEYK73L0zuEP6m5p8hPjO42ov3afVzkj3w/qttteGSyWgwOjuC36 zmHd3f2Q6beQ== X-IronPort-AV: E=Sophos;i="5.73,464,1583193600"; d="scan'208";a="47840163" Received: from sea32-co-svc-lb4-vlan2.sea.corp.amazon.com (HELO email-inbound-relay-2c-87a10be6.us-west-2.amazon.com) ([10.47.23.34]) by smtp-border-fw-out-33001.sea14.amazon.com with ESMTP; 02 Jun 2020 13:08:01 +0000 Received: from EX13MTAUEA002.ant.amazon.com (pdx4-ws-svc-p6-lb7-vlan2.pdx.amazon.com [10.170.41.162]) by email-inbound-relay-2c-87a10be6.us-west-2.amazon.com (Postfix) with ESMTPS id 1BDF6A1EC7; Tue, 2 Jun 2020 13:07:59 +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, 2 Jun 2020 13:07:58 +0000 Received: from u886c93fd17d25d.ant.amazon.com (10.43.161.175) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Tue, 2 Jun 2020 13:07:42 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v14 07/15] mm/damon: Implement access pattern recording Date: Tue, 2 Jun 2020 15:01:17 +0200 Message-ID: <20200602130125.20467-8-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200602130125.20467-1-sjpark@amazon.com> References: <20200602130125.20467-1-sjpark@amazon.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.43.161.175] X-ClientProxiedBy: EX13D29UWC002.ant.amazon.com (10.43.162.254) 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 42ce9df47f8b..768ffd08f7ab 100644 --- a/mm/damon.c +++ b/mm/damon.c @@ -44,6 +44,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)) @@ -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: + * + *