Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp3297610ybb; Mon, 6 Apr 2020 06:15:50 -0700 (PDT) X-Google-Smtp-Source: APiQypK/jtROGWWNNij1kiK79X2U1EhStyX4Q6p3+utUNugLV8Fmdw55AYTThhvZDHNOsKJLLXo6 X-Received: by 2002:a4a:3b50:: with SMTP id s77mr17565592oos.53.1586178950295; Mon, 06 Apr 2020 06:15:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586178950; cv=none; d=google.com; s=arc-20160816; b=M6Ee+F3FMMa4dSsfnYpQJc1Zh9gnpufheLrbUa579lvzVypT2huIzXbltD21mX997E VtNVp0UhQlwGjA1mO/5b8yJQiEPKeAnbGXnTxHGcRuvD1f2d+bvLj5nTbb4voY3gUbEA nwIEcbk7vqricoGzSCu87oBoZHRRym15Rvw2UWTpHDixiXK6Q10tEIuJSLZHYYJwDTrj l1osWhl4X51ch4zWu2DPQ4YUQyr8AUii0SJfhSuxjia9yw97Bg9mlRsW15p1DUVP5fZy JpRvoqe5Rgnmto9L7BJW+VLjNm0VcPKY9jM5jS18MpMdsg7CbAW7hWWH+MhYi0h0PlHS fcDA== 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=3Hr6eCFUJGmr0peeA91u4VbTBCp2QEbtkR27QSCgsmY=; b=JrkcvOP/52NoK5XgF6KYavhCgm/4Qpydo5fskIRMTwkXbv/4cr5xqCi1lhBje12cIX 9qtyLAcAMC6FVYw3+4yurwPkT6nj2nUbr936y1mH1I0oLTKSK6Z0V6juqmryPeW3TRnz VC4XytcV8/hC8QOOiVUSiLUnRE0sh6KW/4FbfaIkhQSQTvEQIsViYpZB6x7Xb3f/MQuj YtgRS9SXFroDxRyeiBa2mXKvC3zCOBz+MtNFTcGoWEFyWjT/6g+9xVRgjFRNwqJ2wxc9 Pm5+qwF/959PvkOYZ/FjE9Z++rBlawCkDJCsMhdoj3w4hwP8Vx7prNM5KK4pVYMvMc0r 1ugQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=sBXySGlU; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id k185si7297716oif.13.2020.04.06.06.15.36; Mon, 06 Apr 2020 06:15:50 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=sBXySGlU; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1728424AbgDFNOs (ORCPT + 99 others); Mon, 6 Apr 2020 09:14:48 -0400 Received: from smtp-fw-6002.amazon.com ([52.95.49.90]:18091 "EHLO smtp-fw-6002.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728193AbgDFNOs (ORCPT ); Mon, 6 Apr 2020 09:14:48 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1586178886; x=1617714886; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=3Hr6eCFUJGmr0peeA91u4VbTBCp2QEbtkR27QSCgsmY=; b=sBXySGlUgd7OCQ9XCwkyqbCj6G9cBLS0l36BwT5giU6Y37kuBm4lsXr8 YCqbjGOeTbjtvcUDjPJOyZoAFLbxMIQ0RMTXE7deddXDMstV80ohCNlMH loLeSnTlm2dyqDq7s1rb4IhGPTB4JV7zG5YXfHOKTpEgw4RhmFnXj7Clp M=; IronPort-SDR: PnvmSjbBTUahDHOeAzqD7W6iFI9Xk/cGePXbXpH0agNpeIGcSFYsZmbFXdr2Yx3I3LPGWFfwQR sE7Lum2L7jGw== X-IronPort-AV: E=Sophos;i="5.72,351,1580774400"; d="scan'208";a="24290850" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-1e-97fdccfd.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-out-6002.iad6.amazon.com with ESMTP; 06 Apr 2020 13:14:45 +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-97fdccfd.us-east-1.amazon.com (Postfix) with ESMTPS id 6637EA24B6; Mon, 6 Apr 2020 13:14:34 +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, 6 Apr 2020 13:14:33 +0000 Received: from u886c93fd17d25d.ant.amazon.com (10.43.160.65) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Mon, 6 Apr 2020 13:14:19 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v8 08/15] mm/damon: Implement access pattern recording Date: Mon, 6 Apr 2020 15:09:31 +0200 Message-ID: <20200406130938.14066-9-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200406130938.14066-1-sjpark@amazon.com> References: <20200406130938.14066-1-sjpark@amazon.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.43.160.65] X-ClientProxiedBy: EX13D11UWB003.ant.amazon.com (10.43.161.206) 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 | 120 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 124 insertions(+), 3 deletions(-) diff --git a/include/linux/damon.h b/include/linux/damon.h index cad3ee3d78db..47fb0ec03030 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; struct mutex kdamond_lock; @@ -66,6 +71,8 @@ int damon_set_pids(struct damon_ctx *ctx, unsigned long *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 608787e88118..b1363e4fdab8 100644 --- a/mm/damon.c +++ b/mm/damon.c @@ -41,6 +41,8 @@ #define damon_for_each_task_safe(ctx, t, next) \ list_for_each_entry_safe(t, next, &(ctx)->tasks_list, list) +#define MAX_RFILE_PATH_LEN 256 + /* Get a random number in [l, r) */ #define damon_rand(ctx, l, r) (l + prandom_u32_state(&ctx->rndseed) % (r - l)) @@ -569,16 +571,81 @@ 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; + struct file *rfile; + + while (ctx->rbuf_offset) { + pos = 0; + 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; + } + + sz = kernel_write(rfile, ctx->rbuf, ctx->rbuf_offset, &pos); + filp_close(rfile, NULL); + + ctx->rbuf_offset -= sz; + } +} + +/* + * 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 regions. The format for the result buffer is as below: + * + *