From patchwork Wed Jan 17 08:20:50 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Vagin X-Patchwork-Id: 10168731 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 07DC1601D3 for ; Wed, 17 Jan 2018 08:21:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DED0F2842B for ; Wed, 17 Jan 2018 08:21:00 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D3B382844E; Wed, 17 Jan 2018 08:21:00 +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=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 802F02842B for ; Wed, 17 Jan 2018 08:20:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750847AbeAQIU5 (ORCPT ); Wed, 17 Jan 2018 03:20:57 -0500 Received: from mail-pf0-f194.google.com ([209.85.192.194]:44814 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750810AbeAQIU4 (ORCPT ); Wed, 17 Jan 2018 03:20:56 -0500 Received: by mail-pf0-f194.google.com with SMTP id m26so11144644pfj.11 for ; Wed, 17 Jan 2018 00:20:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=NbYD9DIT6HrFPjrX3uLwOzJnbEdtfGux9j5OgigqDsQ=; b=ItzHxmZ2C/Yk2F9nDlWIHybj9PzJZnq4MLzudtcwRv9kasPdfk+V3xWMlOSqRPvYX4 QmX1zyRYDEBg4kVHSUAGQ9LeOchEUTbuzRhluWIkdMWm+BQDzFyfyaqxGLbQc3cdV6Pi GYKLsgYTnugbHF2GvsrfqE0d/j5UHwW3PDDlHTPaIcHozR+8PVzGtyiXlNDQnRtzeyKo JhdtqUhpNDM9rE4nuJeJVRpcm0NA5BzPOz++i/vYEfE3wJjOA6xlJAqL/EpqyP9fp4S0 Z7yr8FmtPYuHRXYSdWSAIdH4fZT8ohnz4eizmwTzfziYWX13EIHKF9yCp89RR5CSqNaj D5nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=NbYD9DIT6HrFPjrX3uLwOzJnbEdtfGux9j5OgigqDsQ=; b=KcBNQVvb/lcq+4C8aV++oUSFnRcXF2rF/TACRmFZJxWetcKCLiHmjdS0hwGGYwb3Cz tP34HifXE2Zi6Iwi1ISZscPOBFrPTIjajseVBO4EiI73hsep/6ZxdfwsV/Jm/PRwIij3 sG1CR4qQOoZZmJCPe8hUZXR1QelgX0yxvzvpMupQwHyQFAdl/nUkmLNLBJo7D+eJMLCH a0LVW5qdPhPjK/45GkRn3IW8Ex+NYKJ/VFWOH+QOTTJfPGagJUK49GDBpPZ7IZC0Gsab wuWFWC3mZi+t9AC+RdvGAT/2rrfp0q05I0IqxRjgrcVr2Fl67JHRMSM5jt4FHcCfpzwf t8qg== X-Gm-Message-State: AKGB3mKg0kqgiaWmFSoeTyx1wbxnFwBrLtH3JjXFaNvpggXQcwGSHWya YTq3C3ChroGZ383aPf3F5QRLkRe2 X-Google-Smtp-Source: ACJfBosTiVm6mC/FqA/L6OtPuzBGKOWlm2DJQHY+84X/a+QbQqA3wlzzAv1fFxkJmjSqmwA5zK5BAg== X-Received: by 10.101.76.75 with SMTP id l11mr32762878pgr.187.1516177255754; Wed, 17 Jan 2018 00:20:55 -0800 (PST) Received: from localhost.localdomain (c-73-140-212-29.hsd1.wa.comcast.net. [73.140.212.29]) by smtp.gmail.com with ESMTPSA id a20sm6061972pgd.4.2018.01.17.00.20.54 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 17 Jan 2018 00:20:55 -0800 (PST) From: Andrei Vagin To: linux-fsdevel@vger.kernel.org, Andrew Morton Cc: Andrei Vagin , Alexey Dobriyan , KAMEZAWA Hiroyuki Subject: [PATCH v2] procfs: add seq_put_hex_ll to speed up /proc/pid/maps Date: Wed, 17 Jan 2018 00:20:50 -0800 Message-Id: <20180117082050.25406-1-avagin@openvz.org> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20180112153304.f4a7dfbae2942e3fdd93eab9@linux-foundation.org> References: <20180112153304.f4a7dfbae2942e3fdd93eab9@linux-foundation.org> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP seq_put_hex_ll() prints a number in hexadecimal notation and works faster than seq_printf(). == test.py num = 0 with open("/proc/1/maps") as f: while num < 10000 : data = f.read() f.seek(0, 0) num = num + 1 == == Before patch == $ time python test.py real 0m1.561s user 0m0.257s sys 0m1.302s == After patch == $ time python test.py real 0m0.986s user 0m0.279s sys 0m0.707s $ perf -g record python test.py: == Before patch == - 67.42% 2.82% python [kernel.kallsyms] [k] show_map_vma.isra.22 - 64.60% show_map_vma.isra.22 - 44.98% seq_printf - seq_vprintf - vsnprintf + 14.85% number + 12.22% format_decode 5.56% memcpy_erms + 15.06% seq_path + 4.42% seq_pad + 2.45% __GI___libc_read == After patch == - 47.35% 3.38% python [kernel.kallsyms] [k] show_map_vma.isra.23 - 43.97% show_map_vma.isra.23 + 20.84% seq_path - 15.73% show_vma_header_prefix 10.55% seq_put_hex_ll + 2.65% seq_put_decimal_ull 0.95% seq_putc + 6.96% seq_pad + 2.94% __GI___libc_read v2: don't create a separate function to print delimiter and a few fixes to make code more readable. Cc: Alexey Dobriyan Cc: KAMEZAWA Hiroyuki Signed-off-by: Andrei Vagin --- fs/proc/task_mmu.c | 21 ++++++++++++--------- fs/seq_file.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ include/linux/seq_file.h | 3 +++ 3 files changed, 60 insertions(+), 9 deletions(-) diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 339e4c1c044d..3a08685ef27c 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -282,15 +282,18 @@ static void show_vma_header_prefix(struct seq_file *m, dev_t dev, unsigned long ino) { seq_setwidth(m, 25 + sizeof(void *) * 6 - 1); - seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu ", - start, - end, - flags & VM_READ ? 'r' : '-', - flags & VM_WRITE ? 'w' : '-', - flags & VM_EXEC ? 'x' : '-', - flags & VM_MAYSHARE ? 's' : 'p', - pgoff, - MAJOR(dev), MINOR(dev), ino); + seq_put_hex_ll(m, NULL, start, 8); + seq_put_hex_ll(m, "-", end, 8); + seq_putc(m, ' '); + seq_putc(m, flags & VM_READ ? 'r' : '-'); + seq_putc(m, flags & VM_WRITE ? 'w' : '-'); + seq_putc(m, flags & VM_EXEC ? 'x' : '-'); + seq_putc(m, flags & VM_MAYSHARE ? 's' : 'p'); + seq_put_hex_ll(m, " ", pgoff, 8); + seq_put_hex_ll(m, " ", MAJOR(dev), 2); + seq_put_hex_ll(m, ":", MINOR(dev), 2); + seq_put_decimal_ull(m, " ", ino); + seq_putc(m, ' '); } static void diff --git a/fs/seq_file.c b/fs/seq_file.c index bce089cc1771..d2885220ecf7 100644 --- a/fs/seq_file.c +++ b/fs/seq_file.c @@ -712,6 +712,51 @@ void seq_put_decimal_ull(struct seq_file *m, const char *delimiter, } EXPORT_SYMBOL(seq_put_decimal_ull); +/** + * seq_put_hex_ll - put a number in hexadecimal notation + * @m: seq_file identifying the buffer to which data should be written + * @delimiter: a string which is printed before the number + * @v: the number + * @width: a minimum field width + * + * seq_put_hex_ll(m, "", v, 8) is equal to seq_printf(m, "0x08llx", v) + * + * This routine is very quick when you show lots of numbers. + * In usual cases, it will be better to use seq_printf(). It's easier to read. + */ +void seq_put_hex_ll(struct seq_file *m, const char *delimiter, + unsigned long long v, int width) +{ + int i, len; + + if (delimiter && delimiter[0]) { + if (delimiter[1] == 0) + seq_putc(m, delimiter[0]); + else + seq_puts(m, delimiter); + } + + /* If x is 0, the result of __builtin_clzll is undefined */ + if (v == 0) + len = 1; + else + len = (sizeof(v) * 8 - __builtin_clzll(v) + 3) / 4; + + if (len < width) + len = width; + + if (m->count + len > m->size) { + seq_set_overflow(m); + return; + } + + for (i = len - 1; i >= 0; i--) { + m->buf[m->count + i] = hex_asc[0xf & v]; + v = v >> 4; + } + m->count += len; +} + void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num) { int len; diff --git a/include/linux/seq_file.h b/include/linux/seq_file.h index 09c6e28746f9..53f238934d7f 100644 --- a/include/linux/seq_file.h +++ b/include/linux/seq_file.h @@ -121,6 +121,9 @@ void seq_puts(struct seq_file *m, const char *s); void seq_put_decimal_ull(struct seq_file *m, const char *delimiter, unsigned long long num); void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num); +void seq_put_hex_ll(struct seq_file *m, const char *delimiter, + unsigned long long v, int width); + void seq_escape(struct seq_file *m, const char *s, const char *esc); void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,