From patchwork Wed Aug 24 18:03:05 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexey Dobriyan X-Patchwork-Id: 9298221 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 4A992607F0 for ; Wed, 24 Aug 2016 18:59:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3B6F029099 for ; Wed, 24 Aug 2016 18:59:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2C17F290AE; Wed, 24 Aug 2016 18:59:20 +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_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, 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 A8E9629099 for ; Wed, 24 Aug 2016 18:59:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753968AbcHXS7T (ORCPT ); Wed, 24 Aug 2016 14:59:19 -0400 Received: from mail-lf0-f66.google.com ([209.85.215.66]:34522 "EHLO mail-lf0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755286AbcHXS7R (ORCPT ); Wed, 24 Aug 2016 14:59:17 -0400 Received: by mail-lf0-f66.google.com with SMTP id k135so1311924lfb.1 for ; Wed, 24 Aug 2016 11:59:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :user-agent; bh=jA+9+3f7W4zezUer6EVvN+NV4iOvdK6oj5H9PkHaNus=; b=eJ0ak9NqGUdBEwL9a7XgiMH3G8apj4HzRq2uoMPGJwdWC0lXpUPRfBn+V2q3e5DAct J4HgZ7Tdj9PqqlovTHscSB4pYRPJCaI6jWUDGad7JDjc8StasVB7deGSNQ5ElVZ2p+k0 wzz42bH+6ykUCYpS8dnerVIIfcnymNO3DjTMklwmQy2oiVqQ/o5fzNrpsLz5w2m4/Rr9 d4HvtAOxQjvkftbw5O5N2SZE+3F7W57C1A50timg+bENwJCsqbP4lp71XjrYGbMGOBSA wihXW8lKGyfqO7GICHLDpLAVa9qpg5wbvA3U7VgWWyXVzWqi8izzae78G1CSBAIxhHjj 8xWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:user-agent; bh=jA+9+3f7W4zezUer6EVvN+NV4iOvdK6oj5H9PkHaNus=; b=X8PCNoVZ2tAwe1SIgdCOKlzPHnVhINZlWZ+11LceHsXD66UJrO73e1AbaqcyWRjb0G Th8pVyy0I3BcYZm9pbijI9rX1iiqQW5GXTf3SplKrhU0PlMeD5bWURKpxkLPYdD4VmYr R/1VaCxnq0JztycsDCb+nLJ2Zysjblh/rtruBWmL6tK3NZyq8+ylgAYUNxBxfemfHnIQ RsIaj7NZibdkaVHUgvM7bJHXxczoOWiPKYF6zZbgyXhal5Tq9nSjBvcHyM/ytI21hrI0 EB9hkz1chtne4CoL+ylgNWr1iFGOMwv6bNncgLoyBQGNC3cxNhR5AhHM0KB7pyClbXZu ntGA== X-Gm-Message-State: AEkooutRtKHv6JUdVh+L/4KKj+pIO7YRU1Q1/qd6DutdZfK7uK6zmL9djtLXhXBeXnofMQ== X-Received: by 10.46.1.163 with SMTP id f35mr1230609lji.52.1472061777727; Wed, 24 Aug 2016 11:02:57 -0700 (PDT) Received: from p183.telecom.by (nat5-minsk-pool-46-53-217-92.telecom.by. [46.53.217.92]) by smtp.gmail.com with ESMTPSA id 98sm2156843lja.37.2016.08.24.11.02.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 24 Aug 2016 11:02:56 -0700 (PDT) Date: Wed, 24 Aug 2016 21:03:05 +0300 From: Alexey Dobriyan To: mmarek@suse.com Cc: akpm@linux-foundation.org, linux-kbuild@vger.kernel.org Subject: [PATCH v2] fixdep: faster CONFIG_ search Message-ID: <20160824180305.GA22266@p183.telecom.by> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kbuild-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Do you think kernel build is 100% dominated by gcc? You are wrong! One small utility called "fixdep" consistently manages to sneak into profile's first page (unless you have small monitor of course). The choke point is this clever code: for (; m < end; m++) { if (*m == INT_CONF) { p = (char *) m ; goto conf; } if (*m == INT_ONFI) { p = (char *) m-1; goto conf; } if (*m == INT_NFIG) { p = (char *) m-2; goto conf; } if (*m == INT_FIG_) { p = (char *) m-3; goto conf; } 4 branches per 4 characters is not fast. Use strstr(3), so that SSE2 etc can be used. With this patch, fixdep is so deep at the bottom, it is hard to find it. Signed-off-by: Alexey Dobriyan --- scripts/basic/fixdep.c | 86 +++++++++++++++---------------------------------- 1 file changed, 28 insertions(+), 58 deletions(-) -- To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html --- a/scripts/basic/fixdep.c +++ b/scripts/basic/fixdep.c @@ -82,8 +82,7 @@ * to date before even starting the recursive build, so it's too late * at this point anyway. * - * The algorithm to grep for "CONFIG_..." is bit unusual, but should - * be fast ;-) We don't even try to really parse the header files, but + * We don't even try to really parse the header files, but * merely grep, i.e. if CONFIG_FOO is mentioned in a comment, it will * be picked up as well. It's not a problem with respect to * correctness, since that can only give too many dependencies, thus @@ -115,11 +114,6 @@ #include #include -#define INT_CONF ntohl(0x434f4e46) -#define INT_ONFI ntohl(0x4f4e4649) -#define INT_NFIG ntohl(0x4e464947) -#define INT_FIG_ ntohl(0x4649475f) - int insert_extra_deps; char *target; char *depfile; @@ -241,37 +235,22 @@ static void use_config(const char *m, int slen) print_config(m, slen); } -static void parse_config_file(const char *map, size_t len) +static void parse_config_file(const char *p) { - const int *end = (const int *) (map + len); - /* start at +1, so that p can never be < map */ - const int *m = (const int *) map + 1; - const char *p, *q; - - for (; m < end; m++) { - if (*m == INT_CONF) { p = (char *) m ; goto conf; } - if (*m == INT_ONFI) { p = (char *) m-1; goto conf; } - if (*m == INT_NFIG) { p = (char *) m-2; goto conf; } - if (*m == INT_FIG_) { p = (char *) m-3; goto conf; } - continue; - conf: - if (p > map + len - 7) - continue; - if (memcmp(p, "CONFIG_", 7)) - continue; + const char *q, *r; + + while ((p = strstr(p, "CONFIG_"))) { p += 7; - for (q = p; q < map + len; q++) { - if (!(isalnum(*q) || *q == '_')) - goto found; - } - continue; - - found: - if (!memcmp(q - 7, "_MODULE", 7)) - q -= 7; - if (q - p < 0) - continue; - use_config(p, q - p); + q = p; + while (*q && (isalnum(*q) || *q == '_')) + q++; + if (memcmp(q - 7, "_MODULE", 7) == 0) + r = q - 7; + else + r = q; + if (r > p) + use_config(p, r - p); + p = q; } } @@ -291,7 +270,7 @@ static void do_config_file(const char *filename) { struct stat st; int fd; - void *map; + char *map; fd = open(filename, O_RDONLY); if (fd < 0) { @@ -308,18 +287,23 @@ static void do_config_file(const char *filename) close(fd); return; } - map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); - if ((long) map == -1) { - perror("fixdep: mmap"); + map = malloc(st.st_size + 1); + if (!map) { + perror("fixdep: malloc"); close(fd); return; } + if (read(fd, map, st.st_size) != st.st_size) { + perror("fixdep: read"); + close(fd); + return; + } + map[st.st_size] = '\0'; + close(fd); - parse_config_file(map, st.st_size); - - munmap(map, st.st_size); + parse_config_file(map); - close(fd); + free(map); } /* @@ -446,22 +430,8 @@ static void print_deps(void) close(fd); } -static void traps(void) -{ - static char test[] __attribute__((aligned(sizeof(int)))) = "CONF"; - int *p = (int *)test; - - if (*p != INT_CONF) { - fprintf(stderr, "fixdep: sizeof(int) != 4 or wrong endianness? %#x\n", - *p); - exit(2); - } -} - int main(int argc, char *argv[]) { - traps(); - if (argc == 5 && !strcmp(argv[1], "-e")) { insert_extra_deps = 1; argv++;