From patchwork Tue Oct 1 17:28:17 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Joel Fernandes X-Patchwork-Id: 11169331 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CE41314DB for ; Tue, 1 Oct 2019 17:28:29 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8EB1721A4C for ; Tue, 1 Oct 2019 17:28:29 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=joelfernandes.org header.i=@joelfernandes.org header.b="m+omBAHk" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8EB1721A4C Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=joelfernandes.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id AC9DD6B0003; Tue, 1 Oct 2019 13:28:28 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id A7ACF6B0005; Tue, 1 Oct 2019 13:28:28 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 969DC8E0001; Tue, 1 Oct 2019 13:28:28 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0110.hostedemail.com [216.40.44.110]) by kanga.kvack.org (Postfix) with ESMTP id 751006B0003 for ; Tue, 1 Oct 2019 13:28:28 -0400 (EDT) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with SMTP id 22BB5181AC9AE for ; Tue, 1 Oct 2019 17:28:28 +0000 (UTC) X-FDA: 75995899896.14.sky00_840fb2fbdeb24 X-Spam-Summary: 2,0,0,6e3d6dd9a6f61849,d41d8cd98f00b204,joel@joelfernandes.org,:linux-kernel@vger.kernel.org:joel@joelfernandes.org:mhocko@suse.com:timmurray@google.com:carmenjackson@google.com:mayankgupta@google.com:dancol@google.com:rostedt@goodmis.org:minchan@kernel.org:akpm@linux-foundation.org:kernel-team@android.com:aneesh.kumar@linux.ibm.com:dan.j.williams@intel.com:jglisse@redhat.com::willy@infradead.org:mhocko@suse.cz:rcampbell@nvidia.com:vbabka@suse.cz,RULES_HIT:41:152:355:379:541:800:960:967:968:973:988:989:1260:1263:1277:1311:1313:1314:1345:1431:1434:1437:1515:1516:1518:1535:1544:1593:1594:1605:1711:1730:1747:1777:1792:2393:2525:2553:2559:2563:2682:2685:2859:2895:2897:2902:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3865:3866:3867:3868:3870:3871:3872:3874:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4119:4250:4321:4605:5007:6119:6120:6261:6653:6742:7875:7901:7903:8599:8603:8957:9025:9121:10004:10913:11026:11233:11473:11658:11914:1204 3:12291: X-HE-Tag: sky00_840fb2fbdeb24 X-Filterd-Recvd-Size: 8036 Received: from mail-pf1-f194.google.com (mail-pf1-f194.google.com [209.85.210.194]) by imf18.hostedemail.com (Postfix) with ESMTP for ; Tue, 1 Oct 2019 17:28:27 +0000 (UTC) Received: by mail-pf1-f194.google.com with SMTP id y22so8508988pfr.3 for ; Tue, 01 Oct 2019 10:28:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=89LjCF4nnVOchv6tRtvlCTR+voV7AebrnejBV1VPn1I=; b=m+omBAHkhT51nLWKjmRCWs7DoVSmLgkv4Wbp9PCKSu2KIar+DmNGXmca84Dn1RX1vV XNqHXyWm0AVQd6FglFYggt+TymOKbuC3MjV+tUWyrPitTj4xwWD8W2eSABP7jC2LjpEQ FbKfRiHVgzAWwdkYx+iYyzmXi5MYd2TVJAiZw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=89LjCF4nnVOchv6tRtvlCTR+voV7AebrnejBV1VPn1I=; b=kEe0NfXSd8piMHMsRIsfwdOYwL7tq6mn3JHYSz0zE4HMtItP7NwGPXQvV3uzSo+O1f gFPphKsqTqHlov6C6N3w0XUP+oF8Jc961v/Rd9J6W3yM6no47Wrebb33rS52WH2RLK+N CjrR2eSSeIrk8Se4heM7qbK8yL8MX6vKawmdopf1jIC3RRCceK8+BlNUNrUcESaOUIiq OvUj8vSKcwaSuWH2eYxZ8O2m+AkhyWol7dEu4isSh4yhl3YJT9Y+3fmecp99qkhxW4w/ xjO/KN7pFm5jF+JlkyMeMQidNXtRibtbq5j5dhOQFIP/ukCTdkdBMlPCZf04gSOHD8p2 73fQ== X-Gm-Message-State: APjAAAUM6QERzWfVbNl6WiNSvS5PwCV1n+TJbbQsq9l1x6Nv3Cftk2zL XQWO69aQ4DBPDQwYbBLhsCWi2w== X-Google-Smtp-Source: APXvYqwoLCJEyEQN9U9LFMOf6V2m/TjY4kIVxiHBfOBUHULteY3NFbfqAsNWQvh9aQJhaURFioR49Q== X-Received: by 2002:a62:7d54:: with SMTP id y81mr28985112pfc.86.1569950906178; Tue, 01 Oct 2019 10:28:26 -0700 (PDT) Received: from joelaf.cam.corp.google.com ([2620:15c:6:12:9c46:e0da:efbf:69cc]) by smtp.gmail.com with ESMTPSA id z20sm17564093pfj.156.2019.10.01.10.28.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Oct 2019 10:28:25 -0700 (PDT) From: "Joel Fernandes (Google)" To: linux-kernel@vger.kernel.org Cc: "Joel Fernandes (Google)" , Michal Hocko , Tim Murray , carmenjackson@google.com, mayankgupta@google.com, dancol@google.com, rostedt@goodmis.org, minchan@kernel.org, akpm@linux-foundation.org, kernel-team@android.com, "Aneesh Kumar K.V" , Dan Williams , "Jerome Glisse" , linux-mm@kvack.org, Matthew Wilcox , Michal Hocko , Ralph Campbell , Vlastimil Babka Subject: [PATCH v3] mm: emit tracepoint when RSS changes Date: Tue, 1 Oct 2019 13:28:17 -0400 Message-Id: <20191001172817.234886-1-joel@joelfernandes.org> X-Mailer: git-send-email 2.23.0.444.g18eeb5a265-goog MIME-Version: 1.0 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: Useful to track how RSS is changing per TGID to detect spikes in RSS and memory hogs. Several Android teams have been using this patch in various kernel trees for half a year now. Many reported to me it is really useful so I'm posting it upstream. Initial patch developed by Tim Murray. Changes I made from original patch: o Prevent any additional space consumed by mm_struct. Regarding the fact that the RSS may change too often thus flooding the traces - note that, there is some "hysterisis" with this already. That is - We update the counter only if we receive 64 page faults due to SPLIT_RSS_ACCOUNTING. However, during zapping or copying of pte range, the RSS is updated immediately which can become noisy/flooding. In a previous discussion, we agreed that BPF or ftrace can be used to rate limit the signal if this becomes an issue. Also note that I added wrappers to trace_rss_stat to prevent compiler errors where linux/mm.h is included from tracing code, causing errors such as: CC kernel/trace/power-traces.o In file included from ./include/trace/define_trace.h:102, from ./include/trace/events/kmem.h:342, from ./include/linux/mm.h:31, from ./include/linux/ring_buffer.h:5, from ./include/linux/trace_events.h:6, from ./include/trace/events/power.h:12, from kernel/trace/power-traces.c:15: ./include/trace/trace_events.h:113:22: error: field ‘ent’ has incomplete type struct trace_entry ent; \ Link: http://lore.kernel.org/r/20190903200905.198642-1-joel@joelfernandes.org Acked-by: Michal Hocko Co-developed-by: Tim Murray Signed-off-by: Tim Murray Signed-off-by: Joel Fernandes (Google) --- v2->v3: Removed optimization for rate limitting and we can do so from tracing code. Added Michal's ack after private discussion. v1->v2: Added more commit message. Cc: carmenjackson@google.com Cc: mayankgupta@google.com Cc: dancol@google.com Cc: rostedt@goodmis.org Cc: minchan@kernel.org Cc: akpm@linux-foundation.org Cc: kernel-team@android.com include/linux/mm.h | 14 +++++++++++--- include/trace/events/kmem.h | 21 +++++++++++++++++++++ mm/memory.c | 6 ++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 0334ca97c584..fb8619c5a87d 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1671,19 +1671,27 @@ static inline unsigned long get_mm_counter(struct mm_struct *mm, int member) return (unsigned long)val; } +void mm_trace_rss_stat(int member, long count); + static inline void add_mm_counter(struct mm_struct *mm, int member, long value) { - atomic_long_add(value, &mm->rss_stat.count[member]); + long count = atomic_long_add_return(value, &mm->rss_stat.count[member]); + + mm_trace_rss_stat(member, count); } static inline void inc_mm_counter(struct mm_struct *mm, int member) { - atomic_long_inc(&mm->rss_stat.count[member]); + long count = atomic_long_inc_return(&mm->rss_stat.count[member]); + + mm_trace_rss_stat(member, count); } static inline void dec_mm_counter(struct mm_struct *mm, int member) { - atomic_long_dec(&mm->rss_stat.count[member]); + long count = atomic_long_dec_return(&mm->rss_stat.count[member]); + + mm_trace_rss_stat(member, count); } /* Optimized variant when page is already known not to be PageAnon */ diff --git a/include/trace/events/kmem.h b/include/trace/events/kmem.h index eb57e3037deb..8b88e04fafbf 100644 --- a/include/trace/events/kmem.h +++ b/include/trace/events/kmem.h @@ -315,6 +315,27 @@ TRACE_EVENT(mm_page_alloc_extfrag, __entry->change_ownership) ); +TRACE_EVENT(rss_stat, + + TP_PROTO(int member, + long count), + + TP_ARGS(member, count), + + TP_STRUCT__entry( + __field(int, member) + __field(long, size) + ), + + TP_fast_assign( + __entry->member = member; + __entry->size = (count << PAGE_SHIFT); + ), + + TP_printk("member=%d size=%ldB", + __entry->member, + __entry->size) + ); #endif /* _TRACE_KMEM_H */ /* This part must be outside protection */ diff --git a/mm/memory.c b/mm/memory.c index e2bb51b6242e..4b31ac2fef42 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -72,6 +72,8 @@ #include #include +#include + #include #include #include @@ -140,6 +142,10 @@ static int __init init_zero_pfn(void) } core_initcall(init_zero_pfn); +void mm_trace_rss_stat(int member, long count) +{ + trace_rss_stat(member, count); +} #if defined(SPLIT_RSS_COUNTING)