From patchwork Tue Sep 5 03:52:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 13374336 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E3371C83F33 for ; Tue, 5 Sep 2023 03:52:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5ED12900002; Mon, 4 Sep 2023 23:52:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 59D638E001A; Mon, 4 Sep 2023 23:52:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 43D98900002; Mon, 4 Sep 2023 23:52:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 2DAF98E001A for ; Mon, 4 Sep 2023 23:52:23 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id EFAD61A0950 for ; Tue, 5 Sep 2023 03:52:22 +0000 (UTC) X-FDA: 81201171324.04.FB50DE3 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf12.hostedemail.com (Postfix) with ESMTP id B9F1F4000A for ; Tue, 5 Sep 2023 03:52:19 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=QCHOoIgh; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf12.hostedemail.com: domain of sj@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=sj@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1693885940; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=I1eQC3jmItLjHExJyJ4wNQhF+MwCkuR9tdgJsFGZ9lo=; b=SpPvBQo4xY+GgHGqZzoS2ze1hEpkTJ/Nki8aNJ//kTgcdznqeisFlR2XnYLN7dpZPrZgMg slqrEtOKJq0U+0Yh/itkZeBwG0A2BMsprXWAtxAt2W1/XwzQnoJWvpmaUlmLrPZzfAFGyj Bpv0p2OqZMFerkqAd5ecT1Xbzym06Q0= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=QCHOoIgh; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf12.hostedemail.com: domain of sj@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=sj@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1693885940; a=rsa-sha256; cv=none; b=cKxfi8hM268x7o4JR7DImy9aXyz5k53aTvMJsnfsEvTaFtTjCJX6DGz9Kfbeefs5uUv6o3 LjfVjts8PoxPGO0AyQFg/mypq1T4KZ/VugLsLGahihaKntjBz5UDnk/SwQ6c5jaoNbBA+2 kw82QrcKAdmNtFL79k45F60lu+nsfQ8= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by sin.source.kernel.org (Postfix) with ESMTPS id 462F9CE1020; Tue, 5 Sep 2023 03:52:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 405EFC433C8; Tue, 5 Sep 2023 03:52:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1693885934; bh=zWzeHtkbksTfIlg2BoHf0syKSzRvcKMsehHua2+C7C8=; h=From:To:Cc:Subject:Date:From; b=QCHOoIghh4LGxIoAuSiyWqe8yPYZukbDSip1dZdh/hxdu6s4ndmCkTnrDav4yPEyM gX/GTJ3mP3105VZas+4qPuW+6cV4ufzQW4JB/XruphRbil7UyWi7IBudmkjK37/Gkp VHQm4n4RJ9P2GRA3WBHxMQVLr1CAHUFQ2Z382eTLqI/1DAaENyQb1VVDyZnae0NcQm nFdTf0a762+sEl9zmpVoqPEteYezlJgzx/+yNzyYfIBktQs/DuF8u0D9ufLPn1Xv0G wZ4n/EIZGqx/KkreQ3rc7UyMEsi5b2TqjSZ989f57qNMuqJasekMdfHTg2BygLj+iQ N4A3fgvL009qw== From: SeongJae Park To: damon@lists.linux.dev Cc: SeongJae Park , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC v2] mm/damon/core: use number of passed access sampling as a timer Date: Tue, 5 Sep 2023 03:52:10 +0000 Message-Id: <20230905035210.127868-1-sj@kernel.org> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Rspamd-Queue-Id: B9F1F4000A X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 95io7dyxceueqjptxbai13wxdwipn4pr X-HE-Tag: 1693885939-278943 X-HE-Meta: U2FsdGVkX1/j70jZm2+Emm0Z4XG7YDWfAvr1oRFSb/gZeqeR16GGVp4GIoUYsAmlpAzkbuAWZoTpkuJ8zPNz3w5aC35fzvxzc3d4PdkuIAaq26NkEXfTUmsBSPS2kan+oOKqEdjzstkBbFxBLMhhuU0eH7dVeL/1SKf+asMPUgwvxl02bAaYcy+Jvr875tD+McSxB4DC+MQa6u/O3GDyJCk+hbSzSupB6nxLN8QHD1XfUCYmczHSvOzLqshtIgMbfT7W4GoIppBc1+6pj3BHpoIe6Oo5T5U4gFDPgRa4iBtSIFWt+uV2H1H85F0BkGviPNli2UMI7X9I0NctkiZRxYHUoQKHbAN5OQ2WstBxtNe+XN8tCzC9iqIXJI0idS8b2cfMzSCBTR7T7lJhn5LSirUYTm0FugwRG1JVq/ldI9VkpzkU+jUARhNh7IwAylPObR4Bi0Vd81Bf0oFfFMMuU7/sXHQEnhpvvz92ZDz43Kl3arcqInkjzuejWe3boigu1EPPFAFdEW03nEiWfUnI6WtdrdI165V0e4dd12HpdJBVOcngCBKnvflhRgeqTvhHwIvPmd1GY+s8x332iVfTpR7LwCHv2cyswHxo12jw8KCd6uiH2bogVVf3YVArpdrLt0qof7in3C0M1h/BWl23zhNqKB3ilmh1upU4fkZg4nB6CKRVwHjyB3nOP2xxUouyLV8k4kwhzIP/ScY1DkDPZSf06yVZG1YcyYHBzvJwJhnP+m/suSj/PvV+lEy5g1dHcDdo8XRntZvXdpWG8jdCdO5Apajzs51UGf27JPMIrvOPhxdcjixYpSzSTLOF+wWWiCNNq9JPSBwuYoCeEnbP9Aulq7bZPpBhKsI7gjbwR+2mHPv1W4Lm2xlOp7FseMIEspUBMmCvuiSh1DMF+Aj3nyXpS9dO3CLRVZQGQaB6tq7CRPzHE/fR+l2C/JJbQhAx8GdnCWzfYhMYD4OoZH6 kX3Q6w3l twqkuUwlaA4LN3LDLSDR7zJ+lQZphBiA7Enh6+u3Uye9xXCSQzRyUJGs6pT/teMSVSrWSThF4fG+h1GtbdjbUoete6tENE29gxa1XAyWZ9ixCU++4wJppCB3J1rC7JfX2a56MDSKOHc+M1qtdBt025zZiT/UVrSKP8QqxOX+HnDk8SkY2PIpHmJnGzXBQvwf7W3ZzR1BzbvDyxg1PUxohJcId0cz5GNHNymHTRh9JCBRo/Ve/t3Ysd34FqUhmIRVbqOxVWd95EzVSbMf/qNapm66AEb6KZg03xf8PWIBKVl4T40+oC4ZZMXM4Vk8cP2AZRsN9VUN09nWAak/mL1daZyEyoA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.002266, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Changes from v1 (https://lore.kernel.org/damon/20230827003727.49369-1-sj@kernel.org/) - Initalize next_*_sis at the beginning of kdamond_fn() - Remove unnecessary remaining intervals compensations in damon_set_attrs() DAMON sleeps for sampling interval after each sampling, and check if the aggregation interval and the ops update interval have passed using ktime_get_coarse_ts64() and baseline timestamps for the intervals. That design is for making the operations occur at deterministic timing regardless of the time that spend for each work. However, it turned out it is not that useful, and incur not-that-intuitive results. After all, timer functions, and especially sleep functions that DAMON uses to wait for specific timing, are not necessarily strictly accurate. It is legal design, so no problem. However, depending on such inaccuracies, the nr_accesses can be larger than aggregation interval divided by sampling interval. For example, with the default setting (5 ms sampling interval and 100 ms aggregation interval) we frequently show regions having nr_accesses larger than 20. Also, if the execution of a DAMOS scheme takes a long time, next aggregation could happen before enough number of samples are collected. This is not what usual users would intuitively expect. Since access check sampling is the smallest unit work of DAMON, using the number of passed sampling intervals as the DAMON-internal timer can easily avoid these problems. That is, convert aggregation and ops update intervals to numbers of sampling intervals that need to be passed before those operations be executed, count the number of passed sampling intervals, and invoke the operations as soon as the specific amount of sampling intervals passed. Make the change. Note that this could make a behavioral change to settings that using intervals that not aligned by the sampling interval. For example, if the sampling interval is 5 ms and the aggregation interval is 12 ms, DAMON effectively used 15 ms as its aggregation interval with the old design, hence it checks whether the aggregation interval after sleeping the sampling interval. This change will make DAMON to effectively use 10 ms as aggregation interval, since it uses 'aggregation interval / sampling interval * sampling interval' as the effective aggregation interval, and we don't use floating point types. Usual users would have used aligned intervals, so this behavioral change is not expected to make any meaningful impact, so just make this change. Signed-off-by: SeongJae Park --- include/linux/damon.h | 14 ++++++- mm/damon/core.c | 87 +++++++++++++++++++------------------------ 2 files changed, 51 insertions(+), 50 deletions(-) diff --git a/include/linux/damon.h b/include/linux/damon.h index ab3089de1478..9a32b8fd0bd3 100644 --- a/include/linux/damon.h +++ b/include/linux/damon.h @@ -524,8 +524,18 @@ struct damon_ctx { struct damon_attrs attrs; /* private: internal use only */ - struct timespec64 last_aggregation; - struct timespec64 last_ops_update; + /* number of sample intervals that passed since this context started */ + unsigned long passed_sample_intervals; + /* + * number of sample intervals that should be passed before next + * aggregation + */ + unsigned long next_aggregation_sis; + /* + * number of sample intervals that should be passed before next ops + * update + */ + unsigned long next_ops_update_sis; /* public: */ struct task_struct *kdamond; diff --git a/mm/damon/core.c b/mm/damon/core.c index b895f70acb2d..d64ddaa472ca 100644 --- a/mm/damon/core.c +++ b/mm/damon/core.c @@ -456,8 +456,10 @@ struct damon_ctx *damon_new_ctx(void) ctx->attrs.aggr_interval = 100 * 1000; ctx->attrs.ops_update_interval = 60 * 1000 * 1000; - ktime_get_coarse_ts64(&ctx->last_aggregation); - ctx->last_ops_update = ctx->last_aggregation; + ctx->passed_sample_intervals = 0; + /* These will be set from kdamond_init_intervals_sis() */ + ctx->next_aggregation_sis = 0; + ctx->next_ops_update_sis = 0; mutex_init(&ctx->kdamond_lock); @@ -581,6 +583,8 @@ static void damon_update_monitoring_results(struct damon_ctx *ctx, */ int damon_set_attrs(struct damon_ctx *ctx, struct damon_attrs *attrs) { + unsigned long sample_interval; + if (attrs->min_nr_regions < 3) return -EINVAL; if (attrs->min_nr_regions > attrs->max_nr_regions) @@ -588,6 +592,12 @@ int damon_set_attrs(struct damon_ctx *ctx, struct damon_attrs *attrs) if (attrs->sample_interval > attrs->aggr_interval) return -EINVAL; + sample_interval = attrs->sample_interval ? attrs->sample_interval : 1; + ctx->next_aggregation_sis = ctx->passed_sample_intervals + + attrs->aggr_interval / sample_interval; + ctx->next_ops_update_sis = ctx->passed_sample_intervals + + attrs->ops_update_interval / sample_interval; + damon_update_monitoring_results(ctx, attrs); ctx->attrs = *attrs; return 0; @@ -761,38 +771,6 @@ int damon_stop(struct damon_ctx **ctxs, int nr_ctxs) return err; } -/* - * damon_check_reset_time_interval() - Check if a time interval is elapsed. - * @baseline: the time to check whether the interval has elapsed since - * @interval: the time interval (microseconds) - * - * See whether the given time interval has passed since the given baseline - * time. If so, it also updates the baseline to current time for next check. - * - * Return: true if the time interval has passed, or false otherwise. - */ -static bool damon_check_reset_time_interval(struct timespec64 *baseline, - unsigned long interval) -{ - struct timespec64 now; - - ktime_get_coarse_ts64(&now); - if ((timespec64_to_ns(&now) - timespec64_to_ns(baseline)) < - interval * 1000) - return false; - *baseline = now; - return true; -} - -/* - * Check whether it is time to flush the aggregated information - */ -static bool kdamond_aggregate_interval_passed(struct damon_ctx *ctx) -{ - return damon_check_reset_time_interval(&ctx->last_aggregation, - ctx->attrs.aggr_interval); -} - /* * Reset the aggregated monitoring results ('nr_accesses' of each region). */ @@ -1296,18 +1274,6 @@ static void kdamond_split_regions(struct damon_ctx *ctx) last_nr_regions = nr_regions; } -/* - * Check whether it is time to check and apply the operations-related data - * structures. - * - * Returns true if it is. - */ -static bool kdamond_need_update_operations(struct damon_ctx *ctx) -{ - return damon_check_reset_time_interval(&ctx->last_ops_update, - ctx->attrs.ops_update_interval); -} - /* * Check whether current monitoring should be stopped * @@ -1419,6 +1385,17 @@ static int kdamond_wait_activation(struct damon_ctx *ctx) return -EBUSY; } +static void kdamond_init_intervals_sis(struct damon_ctx *ctx) +{ + unsigned long sample_interval = ctx->attrs.sample_interval ? + ctx->attrs.sample_interval : 1; + + ctx->passed_sample_intervals = 0; + ctx->next_aggregation_sis = ctx->attrs.aggr_interval / sample_interval; + ctx->next_ops_update_sis = ctx->attrs.ops_update_interval / + sample_interval; +} + /* * The monitoring daemon that runs as a kernel thread */ @@ -1432,6 +1409,8 @@ static int kdamond_fn(void *data) pr_debug("kdamond (%d) starts\n", current->pid); + kdamond_init_intervals_sis(ctx); + if (ctx->ops.init) ctx->ops.init(ctx); if (ctx->callback.before_start && ctx->callback.before_start(ctx)) @@ -1440,6 +1419,8 @@ static int kdamond_fn(void *data) sz_limit = damon_region_sz_limit(ctx); while (!kdamond_need_stop(ctx)) { + unsigned long sample_interval; + if (kdamond_wait_activation(ctx)) break; @@ -1450,11 +1431,17 @@ static int kdamond_fn(void *data) break; kdamond_usleep(ctx->attrs.sample_interval); + ctx->passed_sample_intervals++; if (ctx->ops.check_accesses) max_nr_accesses = ctx->ops.check_accesses(ctx); - if (kdamond_aggregate_interval_passed(ctx)) { + sample_interval = ctx->attrs.sample_interval ? + ctx->attrs.sample_interval : 1; + if (ctx->passed_sample_intervals == + ctx->next_aggregation_sis) { + ctx->next_aggregation_sis += + ctx->attrs.aggr_interval / sample_interval; kdamond_merge_regions(ctx, max_nr_accesses / 10, sz_limit); @@ -1469,7 +1456,11 @@ static int kdamond_fn(void *data) ctx->ops.reset_aggregated(ctx); } - if (kdamond_need_update_operations(ctx)) { + if (ctx->passed_sample_intervals == + ctx->next_ops_update_sis) { + ctx->next_ops_update_sis += + ctx->attrs.ops_update_interval / + sample_interval; if (ctx->ops.update) ctx->ops.update(ctx); sz_limit = damon_region_sz_limit(ctx);