From patchwork Tue Aug 28 10:39:14 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Whitchurch X-Patchwork-Id: 10578183 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E687D920 for ; Tue, 28 Aug 2018 10:39:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D6A15297D5 for ; Tue, 28 Aug 2018 10:39:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CABE7297F2; Tue, 28 Aug 2018 10:39:26 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 094CC297ED for ; Tue, 28 Aug 2018 10:39:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 540796B45B9; Tue, 28 Aug 2018 06:39:23 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 42A506B45BB; Tue, 28 Aug 2018 06:39:23 -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 0CB226B45BC; Tue, 28 Aug 2018 06:39:23 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-lj1-f197.google.com (mail-lj1-f197.google.com [209.85.208.197]) by kanga.kvack.org (Postfix) with ESMTP id 875196B45B9 for ; Tue, 28 Aug 2018 06:39:22 -0400 (EDT) Received: by mail-lj1-f197.google.com with SMTP id q188-v6so253157ljq.22 for ; Tue, 28 Aug 2018 03:39:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=a2ltXJoocwRvb3sHr3oMj0Nm1FLyijmjpFZ3Vunr1DY=; b=X780ov87E4Juybkfw7fTb90FVika20Urdl60PkTkLlAU7Gm1GMQl6eOkJBV3F2H437 dEw9txKDZsREUsJ/2gRE8js8KQl6IbX8E4NLzsDvqcOYUhKOm1/W7RTwedO5J+NV3Vee E8h+vD0u5UpUyVdvOFtdIYT2yttdMP0Es+5v+w1ebj5rtsC8xGlaOkPTbWoOxJBZ3aW6 6wDsB2Bvgw5vnj1QXVsYEYPFmFSUF2wBIkEgLeqmHg2Xv2Dd6iwOnCZp/i0dbIHjRR/t mBcjK2an+jNrw3nFKmCWl8Qf3BwAoALfSfnxu+oTGhNBsMXVNOlpj5/8oW2Dd9ZfWTKm h0vg== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of vincent.whitchurch@axis.com designates 195.60.68.11 as permitted sender) smtp.mailfrom=vincent.whitchurch@axis.com X-Gm-Message-State: APzg51AYiApxoAOpfDM6fWR6Mx5lcZ9l5tGO3QV5UCCuMG5YCCNYbcwO NoNcg0sPECJ69cyz2AgZhMZSkP7g3oNWAf+C6l1l8U+Ui2MzUTVxvbSH038qdab5ncniWmcosZo qGirQJZHOw7jq5sNvt9vwqmvjOgVeHnYSVf8aIl9mMxw+MXXBPblRQ2lIrCKc/CAvkg== X-Received: by 2002:a2e:360c:: with SMTP id d12-v6mr828504lja.88.1535452761882; Tue, 28 Aug 2018 03:39:21 -0700 (PDT) X-Google-Smtp-Source: ANB0VdYOJzIcebsbQ17BXXt5qTWpZOOzg8lTprN5axrcZ6kAV9kUu/eOYKBdzqhGSoxcQOTq+HuN X-Received: by 2002:a2e:360c:: with SMTP id d12-v6mr828453lja.88.1535452760690; Tue, 28 Aug 2018 03:39:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535452760; cv=none; d=google.com; s=arc-20160816; b=y6jGAtPtSL0ya5hioqdllJO0knm8DgVjNCj3fLY1Tpm+4lsR1a6R4OYr5vddbtwQWJ x3O3DBnuzqeop57q/0Vxp2Yyjr4R6OlycZg3tY4FXhhG1jf4ujYG6HqRbLdcrmuXcNFT MYFDMuHXF2tTtSVey7rOCKkF+5WEXZ+IxZQUnZ7ueo2O74PbhcuY0wa+kOUJShWidfdM Sh78WVFB2JfjbobUvrGsFnknmpnapzRmW/CmZTz1HRG0ZgksSRNiCNfOyQVfHI0aUaEc EGHM0tZqcZJt/9d1z5yy85yQ0gva/WjTC2tClV7C46cZHrVp6VJ7B4pGPVqX9/u+27iQ iQSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=a2ltXJoocwRvb3sHr3oMj0Nm1FLyijmjpFZ3Vunr1DY=; b=eM+X2eAbKyANy3sURuxYPXkg2Hme0kccCxgyCHMPrDosbidkobfpqi365TrcJF/fPy SR2LO17bBUwCreZE5Os7NEtyh26q3I0EVEKwpx+TLAxABjV45mYc/iHCCSE8QzZ3JZd6 pWxiN+5q+DsfoBLbRh83i0DzjZoS/psVBoLMHjUG8vdFXmvC9rkYzdCENy9PpRyczZm9 5gkIQcz0GAc9c53KsEIi2RemNbKU6QFJD3vEauZoMwXoGSHNbaO477fFq+sAx896yaHs BsXCGNiq8jOfLdLfTqEvjeU+TBZXV9F/vRKUWXeHKEKAkIIcVVPJ/mFFoGOgOX1JbdMz ZqaQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of vincent.whitchurch@axis.com designates 195.60.68.11 as permitted sender) smtp.mailfrom=vincent.whitchurch@axis.com Received: from bastet.se.axis.com (bastet.se.axis.com. [195.60.68.11]) by mx.google.com with ESMTPS id o9-v6si350803lfk.172.2018.08.28.03.39.20 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 28 Aug 2018 03:39:20 -0700 (PDT) Received-SPF: pass (google.com: domain of vincent.whitchurch@axis.com designates 195.60.68.11 as permitted sender) client-ip=195.60.68.11; Authentication-Results: mx.google.com; spf=pass (google.com: domain of vincent.whitchurch@axis.com designates 195.60.68.11 as permitted sender) smtp.mailfrom=vincent.whitchurch@axis.com Received: from localhost (localhost [127.0.0.1]) by bastet.se.axis.com (Postfix) with ESMTP id 256CF183AC; Tue, 28 Aug 2018 12:39:20 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at bastet.se.axis.com Received: from bastet.se.axis.com ([IPv6:::ffff:127.0.0.1]) by localhost (bastet.se.axis.com [::ffff:127.0.0.1]) (amavisd-new, port 10024) with LMTP id 2PcsWGhX8zer; Tue, 28 Aug 2018 12:39:18 +0200 (CEST) Received: from boulder03.se.axis.com (boulder03.se.axis.com [10.0.8.17]) by bastet.se.axis.com (Postfix) with ESMTPS id 076A7180D2; Tue, 28 Aug 2018 12:39:17 +0200 (CEST) Received: from boulder03.se.axis.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id DD2EE1E080; Tue, 28 Aug 2018 12:39:17 +0200 (CEST) Received: from boulder03.se.axis.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id D19BC1E07D; Tue, 28 Aug 2018 12:39:17 +0200 (CEST) Received: from seth.se.axis.com (unknown [10.0.2.172]) by boulder03.se.axis.com (Postfix) with ESMTP; Tue, 28 Aug 2018 12:39:17 +0200 (CEST) Received: from lnxartpec.se.axis.com (lnxartpec.se.axis.com [10.88.4.9]) by seth.se.axis.com (Postfix) with ESMTP id C51872012; Tue, 28 Aug 2018 12:39:17 +0200 (CEST) Received: by lnxartpec.se.axis.com (Postfix, from userid 10564) id C14D08016D; Tue, 28 Aug 2018 12:39:17 +0200 (CEST) From: Vincent Whitchurch To: catalin.marinas@arm.com, akpm@linux-foundation.org Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Vincent Whitchurch Subject: [PATCH 2/2] scripts: add kmemleak2pprof.py for slab usage analysis Date: Tue, 28 Aug 2018 12:39:14 +0200 Message-Id: <20180828103914.30434-2-vincent.whitchurch@axis.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20180828103914.30434-1-vincent.whitchurch@axis.com> References: <20180828103914.30434-1-vincent.whitchurch@axis.com> X-TM-AS-GCONF: 00 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: X-Virus-Scanned: ClamAV using ClamSMTP Add a script which converts /sys/kernel/debug/kmemleak_all to the pprof format, which can be used for analysing memory usage. See https://github.com/google/pprof. $ ./kmemleak2pprof.py kmemleak_all $ pprof -text -ignore free_area_init_node -compact_labels -nodecount 10 prof Showing nodes accounting for 4.85MB, 34.05% of 14.23MB total Dropped 3989 nodes (cum <= 0.07MB) Showing top 10 nodes out of 190 flat flat% sum% cum cum% 1.39MB 9.78% 9.78% 1.61MB 11.29% new_inode_pseudo+0x8/0x4c 0.75MB 5.27% 15.04% 0.75MB 5.27% alloc_large_system_hash+0x19c/0x2b8 0.73MB 5.12% 20.17% 0.86MB 6.07% kernfs_new_node+0x30/0x50 0.66MB 4.62% 24.79% 0.66MB 4.62% __vmalloc_node.constprop.9+0x48/0x50 0.61MB 4.28% 29.06% 0.61MB 4.28% d_alloc+0x10/0x78 0.22MB 1.52% 30.58% 0.22MB 1.52% alloc_inode+0x1c/0xa4 0.18MB 1.28% 31.86% 0.20MB 1.42% _do_fork+0xb0/0x41c 0.13MB 0.88% 32.74% 0.13MB 0.88% early_trace_init+0x16c/0x374 0.09MB 0.66% 33.40% 0.17MB 1.17% inet_init+0x128/0x24c 0.09MB 0.65% 34.05% 0.09MB 0.65% __kernfs_new_node+0x34/0x1a8 Signed-off-by: Vincent Whitchurch --- scripts/kmemleak2pprof.py | 164 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100755 scripts/kmemleak2pprof.py diff --git a/scripts/kmemleak2pprof.py b/scripts/kmemleak2pprof.py new file mode 100755 index 000000000000..1295d3ca9a9d --- /dev/null +++ b/scripts/kmemleak2pprof.py @@ -0,0 +1,164 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright (C) 2018 Axis Communications AB +# +# Converts /sys/kernel/debug/kmemleak_all to the pprof format, see +# https://github.com/google/pprof. +# +# profile_pb2.py can be generated with the following commands. protoc is +# packaged as protobuf-compiler in Debian: +# +# wget https://raw.githubusercontent.com/google/pprof/master/proto/profile.proto +# protoc -I. --python_out=. profile.proto + +import argparse + +from collections import defaultdict + +import profile_pb2 + + +# object 0xee0243b0 (size 464): +# comm "swapper/0", pid 0, jiffies 4294937296 +# [<80220673>] alloc_inode+0x13/0x60 +# [<80221cc5>] new_inode_pseudo+0xd/0x38 +# [<802568a3>] proc_setup_thread_self+0x37/0xc4 +# [<8020e8c1>] mount_ns+0x55/0x94 +# [<8024f2e1>] proc_mount+0x45/0x48 +# [<8020ee9b>] mount_fs+0x1f/0x104 +# [<80224785>] vfs_kern_mount.part.3+0x35/0xbc +# [<80224833>] kern_mount_data+0x17/0x2c +# [<8024f44b>] pid_ns_prepare_proc+0x13/0x24 +# [<8012ed0d>] alloc_pid+0x309/0x338 +# [<80118e2b>] copy_process.part.5+0xa2b/0x1308 +# [<80119807>] _do_fork+0x77/0x2f0 +# [<80119abf>] kernel_thread+0x23/0x28 +# [<8053517f>] rest_init+0x27/0xb4 +# [<80900afb>] start_kernel+0x369/0x372 +# [<0000807b>] 0x807b +class KmemleakAll(object): + def __init__(self): + pass + + def analyze(self, f): + allocs = defaultdict(int) + stack = [] + size = 0 + + while True: + line = f.readline() + if not line: + break + + line = line.strip() + + if line.startswith('['): + # (null) is in the address part so later parsing steps fail. + # Don't bother fixing it up since it's clearly bogus. + if '(null)' in line: + continue + + stack.append(line) + continue + elif line.startswith('comm'): + continue + + if size: + allocs[(tuple(stack), size)] += 1 + size = 0 + + stack = [] + size = int(line.split('(size ')[1].strip('):')) + + return sorted(allocs.items(), key=lambda x: x[0][1] * x[1], reverse=True) + + +class ProfileWriter(object): + def __init__(self, allocs): + self.profile = profile_pb2.Profile() + self.strings = [''] + self.allocs = allocs + self.locations = {} + self.functions = {} + + def stridx(self, s): + try: + idx = self.strings.index(s) + except ValueError: + idx = len(self.strings) + self.strings.append(s) + + return idx + + def get_function_id(self, funcname, filename): + try: + return self.functions[(funcname, filename)].id + except KeyError: + pass + + function = self.profile.function.add() + function.id = len(self.functions) + 1 + function.name = self.stridx(funcname) + function.filename = self.stridx(filename) + + self.functions[(funcname, filename)] = function + + return function.id + + def get_location_id(self, addr): + if addr.startswith('['): + _, func = addr.split(' ', maxsplit=1) + + try: + return self.locations[addr].id + except KeyError: + pass + + location = self.profile.location.add() + location.id = len(self.locations) + 1 + + # We don't have access to the file or line information. + locline = location.line.add() + locline.function_id = self.get_function_id(func, 'dummy.c') + + self.locations[addr] = location + + return location.id + + def write(self, fn): + valuetype = self.profile.sample_type.add() + valuetype.type = self.stridx('slab') + valuetype.unit = self.stridx('bytes') + + for i, alloc in enumerate(self.allocs): + stacksize, count = alloc + stack, size = stacksize + + for instance in range(count): + sample = self.profile.sample.add() + sample.value.append(size) + + for addr in stack: + sample.location_id.append(self.get_location_id(addr)) + + self.profile.string_table.extend(self.strings) + + with open(fn, 'wb') as f: + f.write(self.profile.SerializeToString()) + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--output', default='prof') + parser.add_argument('data') + args = parser.parse_args() + + with open(args.data) as f: + allocs = KmemleakAll().analyze(f) + + ProfileWriter(allocs).write(args.output) + + +if __name__ == '__main__': + main()