Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp1789914ybt; Mon, 15 Jun 2020 09:26:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwjuC33rbhvH4lqcarT6mZ0cYuPvdd9PdnHDTQmfoGzJVcM2gVHDdT9cxgAoNGaYQuxZPk/ X-Received: by 2002:a17:906:4716:: with SMTP id y22mr14287058ejq.125.1592238379851; Mon, 15 Jun 2020 09:26:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592238379; cv=none; d=google.com; s=arc-20160816; b=DctQAhKK699jZrRgJuUf9ZXePZ5PvOK+k5Km5iSc+uB9pzoU27gB2pP2aqrwUqNYBZ lW3WqkEymNYazMRNtjeZns74fwLvKKUsc6tG1IsLU4+sHBF3rruvmGsxhvf0JtdqPWvT 1c5N3FNRRA4YKMb0Gxp11u35CjUyyO9/brprVLd0NtKXcniHvOrNzuSa3yV4g0Jpd0wG s8KT8gIHn7mVYpMCLsrQ5TU1i6tkrHPcZCye/kfX5bOqvvWH+R2+iAQKXY7512aXmw/b CCXJnnb3fRcRueaqF93oXSKL6Zulz2nKX4hQiGeEJmrgXYRT9vualwjCFKmg82GGesmh sdrQ== 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=kA9yf+k6JjXna5CJpm4uranTc+Jg3vcySjLbtefUsfk=; b=mLH1RaSXmhUDieD9MN574AnFvoPDQZsgUOy8lZ+kQSGjnIZ4w/WN6SrB/x1AWvEZtJ 6lnRtF1z+FRAGql42BwS7AhYWCYVYnQrS64f7nt09WNN1F9LCi16u7Co+yzg3HUbdkqD lb+SeG79s84TnJxq7tzV2iAn8Jk1ZYcSjCLREFFcayitU4DBWiv3GcftM9wjce67AsyK kQYw7XAwnYnxlMyIOR81WaX0yEc0vM14t3j35bwrqH3PFG8nR5z1B1CmALOlC1YWsywJ aLaRdCsQGCrwmi/Zb2jE1b2m+bzbENzII6xLkqFLHcxN47TW9hY7IS12ZCCLszohoTco hasQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=ZyJUcU36; 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 x11si8595374ejb.614.2020.06.15.09.25.56; Mon, 15 Jun 2020 09:26:19 -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=ZyJUcU36; 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 S1730914AbgFOQWT (ORCPT + 99 others); Mon, 15 Jun 2020 12:22:19 -0400 Received: from smtp-fw-9102.amazon.com ([207.171.184.29]:50491 "EHLO smtp-fw-9102.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727785AbgFOQWT (ORCPT ); Mon, 15 Jun 2020 12:22:19 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1592238138; x=1623774138; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=kA9yf+k6JjXna5CJpm4uranTc+Jg3vcySjLbtefUsfk=; b=ZyJUcU368byw0t5/VxGaBM48Jj1Pvzsfmr7K978MtGxj8njRTHcfyaB8 +EiB4qRCNSTU03R0Y2HaJ+JmbnZCJBiKWGwgMg7d11RqNGRHyKnvmHl0b pMsHB2gN6Pk9QZOsGb1rJhGweYqopFzjrFw9Cr4xZ5UEAAVc1bYdCdMr4 o=; IronPort-SDR: HMtpk4hX7EP6tp2faUhEPoHbtKon3AV8j0TXPUQWXgOaX3vBof5coDZGdniYwvkaXzyUMP9ObZ D7xsHhv3iY4w== X-IronPort-AV: E=Sophos;i="5.73,515,1583193600"; d="scan'208";a="52393057" Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-2b-baacba05.us-west-2.amazon.com) ([10.47.23.38]) by smtp-border-fw-out-9102.sea19.amazon.com with ESMTP; 15 Jun 2020 16:22:09 +0000 Received: from EX13MTAUEA002.ant.amazon.com (pdx4-ws-svc-p6-lb7-vlan2.pdx.amazon.com [10.170.41.162]) by email-inbound-relay-2b-baacba05.us-west-2.amazon.com (Postfix) with ESMTPS id 4A195A182F; Mon, 15 Jun 2020 16:22:07 +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, 15 Jun 2020 16:22:06 +0000 Received: from u886c93fd17d25d.ant.amazon.com (10.43.161.145) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Mon, 15 Jun 2020 16:21:49 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v16 07/14] mm/damon: Implement access pattern recording Date: Mon, 15 Jun 2020 18:19:20 +0200 Message-ID: <20200615161927.12637-8-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200615161927.12637-1-sjpark@amazon.com> References: <20200615161927.12637-1-sjpark@amazon.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.43.161.145] X-ClientProxiedBy: EX13D23UWA004.ant.amazon.com (10.43.160.72) 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 | 130 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 142 insertions(+), 3 deletions(-) diff --git a/include/linux/damon.h b/include/linux/damon.h index aad30c500964..030f34b5176f 100644 --- a/include/linux/damon.h +++ b/include/linux/damon.h @@ -75,6 +75,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. @@ -129,6 +137,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; @@ -154,6 +167,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 973244a531b1..006bb66c6cf6 100644 --- a/mm/damon.c +++ b/mm/damon.c @@ -58,6 +58,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)) @@ -707,16 +710,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: + * + *