From patchwork Thu Oct 10 00:35:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shakeel Butt X-Patchwork-Id: 13829390 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 80A93CED611 for ; Thu, 10 Oct 2024 00:36:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ACABA6B0082; Wed, 9 Oct 2024 20:36:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A7B136B0083; Wed, 9 Oct 2024 20:36:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 942C66B0085; Wed, 9 Oct 2024 20:36:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 724E26B0082 for ; Wed, 9 Oct 2024 20:36:32 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 173C81C7481 for ; Thu, 10 Oct 2024 00:36:29 +0000 (UTC) X-FDA: 82655826624.27.A93A8EE Received: from out-174.mta0.migadu.com (out-174.mta0.migadu.com [91.218.175.174]) by imf08.hostedemail.com (Postfix) with ESMTP id A003E160005 for ; Thu, 10 Oct 2024 00:36:29 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=kBzn2ELn; spf=pass (imf08.hostedemail.com: domain of shakeel.butt@linux.dev designates 91.218.175.174 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728520409; 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=E4azzCPSUP2ym5GbRLYr6Ea8ttlwwCo8y2/9zZR2OjY=; b=FLlWg9iQyNj4PQQS5PNX4bFaMBv8BASD711MmgoH6PmllxDt2ohyjlCD5XXWEPy/e+WgFK Hej0O924+skUWTLNlpGoVPUWdORRu5+g4PpyduH0kvtP6R6arHYVgXrRs1TZIAqtOh207i 8GuGWyVm5TF/Qz7N5uAsCx/jb+fZ9PY= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=kBzn2ELn; spf=pass (imf08.hostedemail.com: domain of shakeel.butt@linux.dev designates 91.218.175.174 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728520409; a=rsa-sha256; cv=none; b=UKFeG2UCpMCnBthj6YC5WZXhBIie02FDmtzcte/Vy5N23y30/obBkeaUm62as95MWs8ybe 8I3nLFptXC95Vhfx8f0OjPhcjcqW/7SWDDTlyMHWEQg3HXGiSpG37rGflpNYo9LFtXH8nH nqHPVMxrl4uHImWFkB1jl+Ga9qh+XxA= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1728520588; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=E4azzCPSUP2ym5GbRLYr6Ea8ttlwwCo8y2/9zZR2OjY=; b=kBzn2ELnca3IMfd44T9vN70huxNFinLsPBYXxBTonV6AZLRrQ/yXoCUIuADpphbCHwN+GV eJQXbweYBN/8Vk8rrPstw9HqODQxcXxhf96pFu++SblmDmUJh6Oe7vaDHnzyeLxlCWVFrW DTFZlrso4hRz/xaS4Qv6GJDamWGhc3U= From: Shakeel Butt To: Andrew Morton Cc: Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , Steven Rostedt , JP Kobryn , Yosry Ahmed , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Meta kernel team Subject: [PATCH] memcg: add tracing for memcg stat updates Date: Wed, 9 Oct 2024 17:35:50 -0700 Message-ID: <20241010003550.3695245-1-shakeel.butt@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: A003E160005 X-Stat-Signature: 7beprs7gt86y4148j7xe73r5xgkgksce X-Rspam-User: X-HE-Tag: 1728520589-381875 X-HE-Meta: U2FsdGVkX18E6GM6TlTFnkWCZmqqORrJBtqnJJpz9EvMQI6sYTIBH4k3mFMQRIew3h7cOwPv1ns8w5RTThPYTDukvMfhzUkGEjMgM3yVxsug6xtt5fCSh4f2zbf2U+JqxUT/XA4LjD/r+4RKR6MWVmRX6hFGxIpSP0sYP18U+4xLa8aYcRuwEab4v3mnbyFK0vVx7F7X+2HuCiAF+uTXgQh9I24fCC9MhlljPOK/FOMYCz9Eypq3IW5VpPk87uHXcrYUBoRYlyARFBoItKjL2w9MsnE2lj9sG1Vav3yPWHOb8ImLCldD5yuq4odaDvJI1rZk/ppnAy0vGdb3NScekgrOih1LnHPgpiVT4nKOfSbCek699y4vc1YrbA2SrEiiGj9YvQlbVfrNCbYZ1Ut9ZUdx4N8h4qrBJwYsm+PYtzbti7NR4G2oUgfcWTMgFWXihMdnNwOU0hJ8gp8JrVs0KWaSFsFlD9JIUxF2eJNBDQ5UG+294+vOJ3xr+cEA3io5XKk5786MJRt/lG+4svJVcBlt3G28VM9eck+mfkYq2ORMM2h/ED0+Y7zlDwSqPqMNpXruA8FGPU/R9Yykg/ZWlvy5CG7V6XPZ314KnEjHoDFkWeAX6GcaxNeucrbe/LjM/0uzWxCIr1fD+mwGowl9g0AEQPGgtJfvJw+cT/Kv974DcjKJixG7M2ygQ0n6Uo9QrnFDk8L0PhhmqY0x0C7CY7Jy2zNGp2BvonSHfixHnEUSFZjKeIrx84fxyor30Ebj+IGzZjB2Mrw1aVCnTcwYAreoMDNcLcm13CLuEe2sKF6B8viVO5GXg+Tgqblqc5nZHhShpR4PLwHGxzNkAkbVJgLRCekCgRUgUEb8rRjcxjau8MKPYakFXEJrK0wkFhjuvECfNdwScTY3lZSw9cmGbgQZsHhK6FIKBLBIbVA60LtIVO3nfQFoQ4k9+FTvqjbPFr4tr+521KcbxIWcSQz zk4KxQWB 9d7xohTmt2MqGz3Mn9qtjB0+9ah2zF7CnQjW0sbOlCg/uQgi3a2mXWeIHoQTobZ9R4NSO9h316M3IBKsJ0WNs5TIHIIqizfSDTY1Ev4MtQ+P/w+DyXdekwPXXA7fy+pBV5UkPP3HYqbDlAj0= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: The memcg stats are maintained in rstat infrastructure which provides very fast updates side and reasonable read side. However memcg added plethora of stats and made the read side, which is cgroup rstat flush, very slow. To solve that, threshold was added in the memcg stats read side i.e. no need to flush the stats if updates are within the threshold. This threshold based improvement worked for sometime but more stats were added to memcg and also the read codepath was getting triggered in the performance sensitive paths which made threshold based ratelimiting ineffective. We need more visibility into the hot and cold stats i.e. stats with a lot of updates. Let's add trace to get that visibility. Signed-off-by: Shakeel Butt Acked-by: Roman Gushchin Reviewed-by: Yosry Ahmed --- include/trace/events/memcg.h | 59 ++++++++++++++++++++++++++++++++++++ mm/memcontrol.c | 13 ++++++-- 2 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 include/trace/events/memcg.h diff --git a/include/trace/events/memcg.h b/include/trace/events/memcg.h new file mode 100644 index 000000000000..913db9aba580 --- /dev/null +++ b/include/trace/events/memcg.h @@ -0,0 +1,59 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM memcg + +#if !defined(_TRACE_MEMCG_H) || defined(TRACE_HEADER_MULTI_READ) +#define _TRACE_MEMCG_H + +#include +#include + + +DECLARE_EVENT_CLASS(memcg_rstat, + + TP_PROTO(struct mem_cgroup *memcg, int item, int val), + + TP_ARGS(memcg, item, val), + + TP_STRUCT__entry( + __field(u64, id) + __field(int, item) + __field(int, val) + ), + + TP_fast_assign( + __entry->id = cgroup_id(memcg->css.cgroup); + __entry->item = item; + __entry->val = val; + ), + + TP_printk("memcg_id=%llu item=%d val=%d", + __entry->id, __entry->item, __entry->val) +); + +DEFINE_EVENT(memcg_rstat, mod_memcg_state, + + TP_PROTO(struct mem_cgroup *memcg, int item, int val), + + TP_ARGS(memcg, item, val) +); + +DEFINE_EVENT(memcg_rstat, mod_memcg_lruvec_state, + + TP_PROTO(struct mem_cgroup *memcg, int item, int val), + + TP_ARGS(memcg, item, val) +); + +DEFINE_EVENT(memcg_rstat, count_memcg_events, + + TP_PROTO(struct mem_cgroup *memcg, int item, int val), + + TP_ARGS(memcg, item, val) +); + + +#endif /* _TRACE_MEMCG_H */ + +/* This part must be outside protection */ +#include diff --git a/mm/memcontrol.c b/mm/memcontrol.c index c098fd7f5c5e..17af08367c68 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -71,6 +71,10 @@ #include +#define CREATE_TRACE_POINTS +#include +#undef CREATE_TRACE_POINTS + #include struct cgroup_subsys memory_cgrp_subsys __read_mostly; @@ -682,7 +686,9 @@ void __mod_memcg_state(struct mem_cgroup *memcg, enum memcg_stat_item idx, return; __this_cpu_add(memcg->vmstats_percpu->state[i], val); - memcg_rstat_updated(memcg, memcg_state_val_in_pages(idx, val)); + val = memcg_state_val_in_pages(idx, val); + memcg_rstat_updated(memcg, val); + trace_mod_memcg_state(memcg, idx, val); } /* idx can be of type enum memcg_stat_item or node_stat_item. */ @@ -741,7 +747,9 @@ static void __mod_memcg_lruvec_state(struct lruvec *lruvec, /* Update lruvec */ __this_cpu_add(pn->lruvec_stats_percpu->state[i], val); - memcg_rstat_updated(memcg, memcg_state_val_in_pages(idx, val)); + val = memcg_state_val_in_pages(idx, val); + memcg_rstat_updated(memcg, val); + trace_mod_memcg_lruvec_state(memcg, idx, val); memcg_stats_unlock(); } @@ -832,6 +840,7 @@ void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx, memcg_stats_lock(); __this_cpu_add(memcg->vmstats_percpu->events[i], count); memcg_rstat_updated(memcg, count); + trace_count_memcg_events(memcg, idx, count); memcg_stats_unlock(); }