From patchwork Tue Aug 28 10:39:13 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Whitchurch X-Patchwork-Id: 10578181 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 C432914E1 for ; Tue, 28 Aug 2018 10:39:23 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B253F297D5 for ; Tue, 28 Aug 2018 10:39:23 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A6446297F2; Tue, 28 Aug 2018 10:39:23 +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 1E1B0297ED for ; Tue, 28 Aug 2018 10:39:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 27AF26B45B8; Tue, 28 Aug 2018 06:39:22 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 201646B45B9; Tue, 28 Aug 2018 06:39:22 -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 0CAC16B45BA; Tue, 28 Aug 2018 06:39:22 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-lf1-f72.google.com (mail-lf1-f72.google.com [209.85.167.72]) by kanga.kvack.org (Postfix) with ESMTP id 88E9D6B45B8 for ; Tue, 28 Aug 2018 06:39:21 -0400 (EDT) Received: by mail-lf1-f72.google.com with SMTP id y74-v6so225197lfd.4 for ; Tue, 28 Aug 2018 03:39:21 -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; bh=si6LZ+tIYZgjIwdNQ3qlYFweoxWHC5nk2wB1anOMjL8=; b=FDkFs0CkWAcrovToORE81Mf/24DCITp4t5KYWKzdr3fvfuMlQUQCyzdpO6IIZdll2u B4MtgbJWr7MleVuYnyZuBUhqOYrATKz+dg4I54ET3nhNK6N5LENjaV1rGyN2jQ0FLoeG TPtlgPeDd8iX1ZdsxgmdH0zdV8bC2THfdOtOMJvmBQ6HLtKu9EvjJmtNpmcbpYtbTewN IXJBMDdbGPHFwrmW7FUF1V8qUd1LUXgm+/YmdfQKWc6z/m6cEV2gQicc5dwMcB8fxe/K cA2BsjDHRm8X4mxsY2IE4SoW9nApdv8eJXPgmyP+EbG/GR4TWrLqbl9kfamtPfuPEX3F 1SIQ== 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: APzg51BTJq3w/NMF3ND0IQLneJ3GqunA1QPOL5dGVS+EbKyAQnvbh4IX POv6bX2jeG31hAzm+8udnLDqs9KmaM0Gp4iW6VYkrAWRMdPRZSe+wi6PQnCScMB7bMGOy5nRaK7 gr9WqmUoKMalAGdudYSkcVRjATYYerpeNLp1hYSFmdsO41qib9+MxVIHz00uOQeq6zg== X-Received: by 2002:a19:5154:: with SMTP id f81-v6mr868995lfb.55.1535452760980; Tue, 28 Aug 2018 03:39:20 -0700 (PDT) X-Google-Smtp-Source: ANB0Vda11lvWaghkzxRYAOKhv7fkIDbDklbxaZ2ORb4SvmWndI/u1pz/SX6hd2znhyGY7HnCgf3p X-Received: by 2002:a19:5154:: with SMTP id f81-v6mr868926lfb.55.1535452759488; Tue, 28 Aug 2018 03:39:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535452759; cv=none; d=google.com; s=arc-20160816; b=jL42r5nMjTL4gnnwdXanE/F/cXU1u44T1n4ljUOR2BGS3EsrZtw9pxNWDKult/Uzlt 8IRLbcGh/DM/vM3sVohbd9zuWA1lOw6Akg3c7yXn662TcGdzPoAJvYVuvj7IqyDYMhOS mXC20WigZAHbhy9AhWBzDjVI9ZfqQDYh8GQcix2QxKHAU6pSUAKEO7++IDt3zmrzLQlK SDLxz9VoSn6JXYWg4+IyRLkJhQfK8zCpU80ooiBYnBVXOwEvsGYv0vXpe8WziHsZYje8 MzD1kRdAtFj7a0UYp62CLZD7PSDTBtW9nLUsxJ0XnlJb0SXacP7/vsBlsSxwZVUfb8aF 2EXw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:cc:to:from:arc-authentication-results; bh=si6LZ+tIYZgjIwdNQ3qlYFweoxWHC5nk2wB1anOMjL8=; b=SqX8JVPOsPNeaPsugt+i0HcFw4egRrS+XoNBr/GSHEwtHu6BtyAa3hKyYWPbQTBEqM nRQPG++Z6BUwpVDUSFXX+Q3ZkIUg8yBoq0MaWll08ba9gswqeMqfZZSTkUCAsF/4RVj5 bYz1MyARBr6QTE52vrvl1mm57btZLIW1C+HEtHrzxbkPyeSYTtzpz0Hi2/2WWfylIRt8 K42S+jLIKE/dYqppGJ0vE6XBBGkK3vOquQOIyiQJQbdX2sI6xnkX+nI9eH0BlV2sku6m QJNbhsqmt52KTWbVqlSQpRRk7q6/XNT1E7IAzCKxCuLN9J4K+YBfkRLlllYG7nrrg8V6 NDuQ== 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 e15-v6si342976lfg.95.2018.08.28.03.39.19 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 28 Aug 2018 03:39:19 -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 E11D6183A0; Tue, 28 Aug 2018 12:39:18 +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 BAoW5Ap7omb3; 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 076BF1839E; 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 DBB271E07E; 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 D07521E07C; Tue, 28 Aug 2018 12:39:17 +0200 (CEST) Received: from thoth.se.axis.com (unknown [10.0.2.173]) 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 thoth.se.axis.com (Postfix) with ESMTP id C44F02150; Tue, 28 Aug 2018 12:39:17 +0200 (CEST) Received: by lnxartpec.se.axis.com (Postfix, from userid 10564) id BEE958016F; 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 1/2] kmemleak: dump all objects for slab usage analysis Date: Tue, 28 Aug 2018 12:39:13 +0200 Message-Id: <20180828103914.30434-1-vincent.whitchurch@axis.com> X-Mailer: git-send-email 2.11.0 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 In order to be able to analyse the kernel's slab usage, we'd need a list of allocated objects and their allocation stacks. Kmemleak already maintains such a list internally, so we expose it via debugfs file. This file can be post-processed in userspace and converted to a suitable format for slab usage analysis. Signed-off-by: Vincent Whitchurch --- mm/kmemleak.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/mm/kmemleak.c b/mm/kmemleak.c index 17dd883198ae..7bef05c690d6 100644 --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -1759,6 +1759,34 @@ static int kmemleak_seq_show(struct seq_file *seq, void *v) return 0; } +static void kmemleak_print_object(struct seq_file *seq, + struct kmemleak_object *object) +{ + int i; + + seq_printf(seq, "object 0x%08lx (size %zu):\n", + object->pointer, object->size); + seq_printf(seq, " comm \"%s\", pid %d, jiffies %lu\n", + object->comm, object->pid, object->jiffies); + + for (i = 0; i < object->trace_len; i++) { + void *ptr = (void *)object->trace[i]; + + seq_printf(seq, " [<%p>] %pS\n", ptr, ptr); + } +} + +static int kmemleak_all_seq_show(struct seq_file *seq, void *v) +{ + struct kmemleak_object *object = v; + unsigned long flags; + + spin_lock_irqsave(&object->lock, flags); + kmemleak_print_object(seq, object); + spin_unlock_irqrestore(&object->lock, flags); + return 0; +} + static const struct seq_operations kmemleak_seq_ops = { .start = kmemleak_seq_start, .next = kmemleak_seq_next, @@ -1766,11 +1794,23 @@ static const struct seq_operations kmemleak_seq_ops = { .show = kmemleak_seq_show, }; +static const struct seq_operations kmemleak_all_seq_ops = { + .start = kmemleak_seq_start, + .next = kmemleak_seq_next, + .stop = kmemleak_seq_stop, + .show = kmemleak_all_seq_show, +}; + static int kmemleak_open(struct inode *inode, struct file *file) { return seq_open(file, &kmemleak_seq_ops); } +static int kmemleak_all_open(struct inode *inode, struct file *file) +{ + return seq_open(file, &kmemleak_all_seq_ops); +} + static int dump_str_object_info(const char *str) { unsigned long flags; @@ -1911,6 +1951,14 @@ static const struct file_operations kmemleak_fops = { .release = seq_release, }; +static const struct file_operations kmemleak_all_fops = { + .owner = THIS_MODULE, + .open = kmemleak_all_open, + .read = seq_read, + .llseek = seq_lseek, + .release = seq_release, +}; + static void __kmemleak_do_cleanup(void) { struct kmemleak_object *object; @@ -2102,6 +2150,11 @@ static int __init kmemleak_late_init(void) if (!dentry) pr_warn("Failed to create the debugfs kmemleak file\n"); + dentry = debugfs_create_file("kmemleak_all", 0400, NULL, NULL, + &kmemleak_all_fops); + if (!dentry) + pr_warn("Failed to create the debugfs kmemleak_all file\n"); + if (kmemleak_error) { /* * Some error occurred and kmemleak was disabled. There is a 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()