Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp2912941pxk; Mon, 28 Sep 2020 03:40:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzwPRg2t+9xxBDDPjaGy6S/SpKeCYtE+1NAKQNhkZCcXy3KE+lGdSvXGwHOOhVR8r5IwI4F X-Received: by 2002:a17:906:7f06:: with SMTP id d6mr927642ejr.553.1601289659172; Mon, 28 Sep 2020 03:40:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601289659; cv=none; d=google.com; s=arc-20160816; b=S+Myk5gUxBYMsavUEc343c9KZlKey5HXGo9A8QVzsGNW4cXofiLnV/W+4ClWk0GsgX BNlNVZTED7WJn71gLilAxPDj7cdOSFd9RfxsL2J1iEgZfjOCxlnk0yEKWyi7Kl6GCUNB Doe/zNFV1PumUP20eOTAtS95YZYejVuo73gwcLq24vwSJGLbK9hHeROdFxqBsISq+lHu f+xL6AyKIJ3kMydvD2yGjQqzk72meBzHGqlE11wGVQBdeHxd+AIb7C4YmYfCvg7Jfn4f SikmcDXTME1fYFhdnNfsoWdRVMJNkq5lbpzfSnvcDPy+hc87iD+FMqVD9bOE6u+tw5zA 2kkQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=qvrhquTl8+AtxVLoOsq6PQfl9fxedokru3rzzkYbCIA=; b=06xfNPErJt4fsK1W52k/cSh8TyDbu4wiG2GzErHXWU9Qmq34wn0GQDG3DB51LImE0D f4eFlC6yxA4vhVoficCX0P9XCumTGBRzFt6E4rmewaiYuYz2eWX7GrZA6WREA/LcDw/4 CxFhbYwJN/RDgM/x9z/3SG0kguhJA6oWLY+rTIxWvj5P6AqFoNsKm9Wrk0t9yHzJNhug mKf6auYBM56FMcx+nS4gvDsniIRYBrTchbsCTkBkG0hPt2qDL4lXrIjsBVoaYs5Vw0ye N+sg5z6fNzDmCu10UNbolKGft5KMEIaHFZXobqZAVQqvLePakuD1Eb/1Ajn0t+lbdEpY W06w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=rMbNg7JD; 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 n23si294656edv.598.2020.09.28.03.40.35; Mon, 28 Sep 2020 03:40:59 -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=rMbNg7JD; 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 S1726716AbgI1Kh1 (ORCPT + 99 others); Mon, 28 Sep 2020 06:37:27 -0400 Received: from smtp-fw-9102.amazon.com ([207.171.184.29]:19924 "EHLO smtp-fw-9102.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726540AbgI1Kh0 (ORCPT ); Mon, 28 Sep 2020 06:37:26 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1601289445; x=1632825445; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=qvrhquTl8+AtxVLoOsq6PQfl9fxedokru3rzzkYbCIA=; b=rMbNg7JD7CQ3Xv//ke616wzrddVdxCh3f3rEtCNjyDeM3pv4RRTwTkgH Y2SPBIMouXG2AL/NFs/YKRfWTcr2HMed+nHdc40c4LKoUvSuwitS+Wpek 9FG8m4QNM1gcdzlll2oQcGcJFac695KU6b+5rzX6kSGEcaNHohUbjBVB2 A=; X-IronPort-AV: E=Sophos;i="5.77,313,1596499200"; d="scan'208";a="79768620" Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-1d-5dd976cd.us-east-1.amazon.com) ([10.47.23.38]) by smtp-border-fw-out-9102.sea19.amazon.com with ESMTP; 28 Sep 2020 10:37:20 +0000 Received: from EX13D31EUA001.ant.amazon.com (iad12-ws-svc-p26-lb9-vlan2.iad.amazon.com [10.40.163.34]) by email-inbound-relay-1d-5dd976cd.us-east-1.amazon.com (Postfix) with ESMTPS id 8BE06A1CF1; Mon, 28 Sep 2020 10:37:06 +0000 (UTC) Received: from u3f2cd687b01c55.ant.amazon.com (10.43.161.237) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Mon, 28 Sep 2020 10:36:47 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [RFC PATCH 3/5] mm/damon: Move recording feature from core to dbgfs Date: Mon, 28 Sep 2020 12:35:26 +0200 Message-ID: <20200928103528.4256-4-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200928103528.4256-1-sjpark@amazon.com> References: <20200928103528.4256-1-sjpark@amazon.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.43.161.237] X-ClientProxiedBy: EX13D10UWA003.ant.amazon.com (10.43.160.248) To EX13D31EUA001.ant.amazon.com (10.43.165.15) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: SeongJae Park DAMON passes the monitoring results to user space via two ways: 1) a tracepoint and 2) it's recording feature. The recording feature is for the users who want simplest use. However, as the feature is for the user space only while the core is fundamentally a framework for the kernel space, keeping the feature in the core would make no sense. Therefore, this commit moves the feature to the debugfs interface of DAMON. Signed-off-by: SeongJae Park --- include/linux/damon.h | 23 +--- mm/damon/core-test.h | 57 ++------- mm/damon/core.c | 150 +----------------------- mm/damon/dbgfs-test.h | 87 +++++++++++++- mm/damon/dbgfs.c | 264 ++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 359 insertions(+), 222 deletions(-) diff --git a/include/linux/damon.h b/include/linux/damon.h index 505e6261cefa..606e59f785a2 100644 --- a/include/linux/damon.h +++ b/include/linux/damon.h @@ -134,14 +134,6 @@ struct damos { * 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. @@ -164,6 +156,8 @@ struct damos { * @targets_list: Head of monitoring targets (&damon_target) list. * @schemes_list: Head of schemes (&damos) list. * + * @private Private user data. + * * @init_target_regions: Constructs initial monitoring target regions. * @update_target_regions: Updates monitoring target regions. * @prepare_access_checks: Prepares next access check of target regions. @@ -214,11 +208,6 @@ 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; @@ -226,6 +215,8 @@ struct damon_ctx { struct list_head targets_list; /* 'damon_target' objects */ struct list_head schemes_list; /* 'damos' objects */ + void *private; + /* callbacks */ void (*init_target_regions)(struct damon_ctx *context); void (*update_target_regions)(struct damon_ctx *context); @@ -241,10 +232,6 @@ struct damon_ctx { #ifdef CONFIG_DAMON -#define MIN_RECORD_BUFFER_LEN 1024 -#define MAX_RECORD_BUFFER_LEN (4 * 1024 * 1024) -#define MAX_RFILE_PATH_LEN 256 - #define damon_next_region(r) \ (container_of(r->list.next, struct damon_region, list)) @@ -298,8 +285,6 @@ int damon_set_attrs(struct damon_ctx *ctx, unsigned long sample_int, unsigned long min_nr_reg, unsigned long max_nr_reg); int damon_set_schemes(struct damon_ctx *ctx, struct damos **schemes, ssize_t nr_schemes); -int damon_set_recording(struct damon_ctx *ctx, - unsigned int rbuf_len, char *rfile_path); int damon_nr_running_ctxs(void); int damon_start(struct damon_ctx *ctxs, int nr_ctxs); diff --git a/mm/damon/core-test.h b/mm/damon/core-test.h index c916d773397a..b815dfbfb5fd 100644 --- a/mm/damon/core-test.h +++ b/mm/damon/core-test.h @@ -36,6 +36,17 @@ static void damon_test_regions(struct kunit *test) damon_free_target(t); } +static unsigned int nr_damon_targets(struct damon_ctx *ctx) +{ + struct damon_target *t; + unsigned int nr_targets = 0; + + damon_for_each_target(t, ctx) + nr_targets++; + + return nr_targets; +} + static void damon_test_target(struct kunit *test) { struct damon_ctx *c = damon_new_ctx(); @@ -54,23 +65,6 @@ static void damon_test_target(struct kunit *test) damon_destroy_ctx(c); } -static void damon_test_set_recording(struct kunit *test) -{ - struct damon_ctx *ctx = damon_new_ctx(); - int err; - - err = damon_set_recording(ctx, 42, "foo"); - KUNIT_EXPECT_EQ(test, err, -EINVAL); - damon_set_recording(ctx, 4242, "foo.bar"); - KUNIT_EXPECT_EQ(test, ctx->rbuf_len, 4242u); - KUNIT_EXPECT_STREQ(test, ctx->rfile_path, "foo.bar"); - damon_set_recording(ctx, 424242, "foo"); - KUNIT_EXPECT_EQ(test, ctx->rbuf_len, 424242u); - KUNIT_EXPECT_STREQ(test, ctx->rfile_path, "foo"); - - damon_destroy_ctx(ctx); -} - /* * Test kdamond_reset_aggregated() * @@ -91,9 +85,7 @@ static void damon_test_aggregate(struct kunit *test) struct damon_target *t; struct damon_region *r; int it, ir; - ssize_t sz, sr, sp; - damon_set_recording(ctx, 4242, "damon.data"); damon_set_targets(ctx, target_ids, 3); it = 0; @@ -121,31 +113,6 @@ static void damon_test_aggregate(struct kunit *test) /* targets also should be preserved */ KUNIT_EXPECT_EQ(test, 3, it); - /* The aggregated information should be written in the buffer */ - sr = sizeof(r->ar.start) + sizeof(r->ar.end) + sizeof(r->nr_accesses); - sp = sizeof(t->id) + sizeof(unsigned int) + 3 * sr; - sz = sizeof(struct timespec64) + sizeof(unsigned int) + 3 * sp; - KUNIT_EXPECT_EQ(test, (unsigned int)sz, ctx->rbuf_offset); - - damon_destroy_ctx(ctx); -} - -static void damon_test_write_rbuf(struct kunit *test) -{ - struct damon_ctx *ctx = damon_new_ctx(); - char *data; - - damon_set_recording(ctx, 4242, "damon.data"); - - data = "hello"; - damon_write_rbuf(ctx, data, strnlen(data, 256)); - KUNIT_EXPECT_EQ(test, ctx->rbuf_offset, 5u); - - damon_write_rbuf(ctx, data, 0); - KUNIT_EXPECT_EQ(test, ctx->rbuf_offset, 5u); - - KUNIT_EXPECT_STREQ(test, (char *)ctx->rbuf, data); - damon_destroy_ctx(ctx); } @@ -267,9 +234,7 @@ static void damon_test_split_regions_of(struct kunit *test) static struct kunit_case damon_test_cases[] = { KUNIT_CASE(damon_test_target), KUNIT_CASE(damon_test_regions), - KUNIT_CASE(damon_test_set_recording), KUNIT_CASE(damon_test_aggregate), - KUNIT_CASE(damon_test_write_rbuf), KUNIT_CASE(damon_test_split_at), KUNIT_CASE(damon_test_merge_two), KUNIT_CASE(damon_test_merge_regions_of), diff --git a/mm/damon/core.c b/mm/damon/core.c index ba52421a2673..ba0035d7a27a 100644 --- a/mm/damon/core.c +++ b/mm/damon/core.c @@ -208,11 +208,6 @@ struct damon_ctx *damon_new_ctx(void) ktime_get_coarse_ts64(&ctx->last_aggregation); ctx->last_regions_update = ctx->last_aggregation; - if (damon_set_recording(ctx, 0, "none")) { - kfree(ctx); - return NULL; - } - mutex_init(&ctx->kdamond_lock); INIT_LIST_HEAD(&ctx->targets_list); @@ -328,54 +323,6 @@ int damon_set_schemes(struct damon_ctx *ctx, struct damos **schemes, return 0; } -/** - * damon_set_recording() - Set attributes for the recording. - * @ctx: target kdamond context - * @rbuf_len: length of the result buffer - * @rfile_path: path to the monitor result files - * - * Setting 'rbuf_len' 0 disables recording. - * - * This function should not be called while the kdamond is running. - * - * Return: 0 on success, negative error code otherwise. - */ -int damon_set_recording(struct damon_ctx *ctx, - unsigned int rbuf_len, char *rfile_path) -{ - size_t rfile_path_len; - - if (rbuf_len && (rbuf_len > MAX_RECORD_BUFFER_LEN || - rbuf_len < MIN_RECORD_BUFFER_LEN)) { - pr_err("result buffer size (%u) is out of [%d,%d]\n", - rbuf_len, MIN_RECORD_BUFFER_LEN, - MAX_RECORD_BUFFER_LEN); - return -EINVAL; - } - rfile_path_len = strnlen(rfile_path, MAX_RFILE_PATH_LEN); - if (rfile_path_len >= MAX_RFILE_PATH_LEN) { - pr_err("too long (>%d) result file path %s\n", - MAX_RFILE_PATH_LEN, rfile_path); - return -EINVAL; - } - ctx->rbuf_len = rbuf_len; - kfree(ctx->rbuf); - ctx->rbuf = NULL; - kfree(ctx->rfile_path); - ctx->rfile_path = NULL; - - if (rbuf_len) { - ctx->rbuf = kvmalloc(rbuf_len, GFP_KERNEL); - if (!ctx->rbuf) - return -ENOMEM; - } - ctx->rfile_path = kmalloc(rfile_path_len + 1, GFP_KERNEL); - if (!ctx->rfile_path) - return -ENOMEM; - strncpy(ctx->rfile_path, rfile_path, rfile_path_len + 1); - return 0; -} - /** * damon_nr_running_ctxs() - Return number of currently running contexts. */ @@ -390,17 +337,6 @@ int damon_nr_running_ctxs(void) return nr_ctxs; } -static unsigned int nr_damon_targets(struct damon_ctx *ctx) -{ - struct damon_target *t; - unsigned int nr_targets = 0; - - damon_for_each_target(t, ctx) - nr_targets++; - - return nr_targets; -} - /* Returns the size upper limit for each monitoring region */ static unsigned long damon_region_sz_limit(struct damon_ctx *ctx) { @@ -613,87 +549,18 @@ static bool kdamond_aggregate_interval_passed(struct damon_ctx *ctx) } /* - * 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 | O_LARGEFILE, 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: - * - *