From patchwork Tue Apr 30 17:20:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 1929772 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=dmVwde6b; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VTRmw0LTNz1ydT for ; Wed, 1 May 2024 03:21:03 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 021DD3858426 for ; Tue, 30 Apr 2024 17:21:02 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x430.google.com (mail-pf1-x430.google.com [IPv6:2607:f8b0:4864:20::430]) by sourceware.org (Postfix) with ESMTPS id 4868C3858D1E for ; Tue, 30 Apr 2024 17:20:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4868C3858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4868C3858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::430 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714497645; cv=none; b=NzDXISUti5CRL0b+UxZouokrj6g7zlnJwpiL+moT3Ng0nqQuH5Rj6NIJIYqxlnd55oaqnRkq1IxvenxXZjolmNLmb9bwLZ4xaT+2ToA4Nqb/bB3x1z2b9dpBYVzn60iFCUboAn+QZvS0uN1LLX+/s5gW0ERKUdT8xY9AxwHeCqI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714497645; c=relaxed/simple; bh=XI9CrKdNDuFydkK70FBOK/TQv0KRBVKwDkPr3pKfEpU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=HY2c8CH/tfYki/rrCcbguDIQsBdB2uw4cRPbpKXBUjl8ynCZ1J8CmBEm12Ga1TuY3dBjII1DJFO44746HBBDVFRj6JMxClxFSleXxmRybqb7q4evscjWAUE/j+NolFYQB39DnzRUovg6ksyseH6vjSV7xLhOLOKKUBJXyIbystY= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x430.google.com with SMTP id d2e1a72fcca58-6ee12766586so38895b3a.0 for ; Tue, 30 Apr 2024 10:20:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714497642; x=1715102442; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=NyBfsfwUyxo9c5VT7+6yMx0rw2efp1v8hid7+3pZQWA=; b=dmVwde6bVHxq/b0eQseRx/OnXH/JQF40fob0Y6LPVprHZCxKDmD7cXH2pbmUFBtu8C K4ElfmHhXxM1c4hAvUko+LGJroGxV+fXpS4BGqqsbYGx/adhZFv9Uu/XBIrE/YjU/GdQ 7AlBP1wqUNSiwe9eY1FnEBFj/F3RFLq0B0AILXaMT8zfeIzXYd6WcaPeGAappXcXf3ta VZUotFkITEJwlWJ+0pTZ+8JQ00T3H0lglo4SuXBvmEqgudpA+OD9RfxmenA7o5fBgGBL H++CwaxI9jOrxEqGjI34hzNEz/kvi4PLUlessCX27eWe6PTufehNyzDNXWDNLf1Z1PwR Go0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714497642; x=1715102442; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=NyBfsfwUyxo9c5VT7+6yMx0rw2efp1v8hid7+3pZQWA=; b=A1fawiSy4ZfULNVhbZR7ElWQEvWluYpEUvhewfpqYZI1I/Dyrr/avRUqcIfiZCllZh zd2Qnnbc8MH4wtjmBIh8ZocA1DpWqwmbni0u6wr5phEpx+6b/6WUg3snqR4bGgCOosWi JLC7H78yjISQnNNoJCxqGBdcL6uEOxvt95GQ5xzA81viJm99mZZMK/gjMREIEuyZdg+4 bYx+ls1fk+hPuI0478lYTz5fXUpaHdtLfgeGBnpy09SJ5ZMEknsdEwFe4cDhp2b2XJRT LC0RVGn2sNGAMZM04SgrRC1aJdmJ4jeWhEhXbFg8jQPBtUpGkry/enBLmHPWJRm8BC60 YUYw== X-Gm-Message-State: AOJu0YxxZEtRTplaIh093Sv2eiltQMPCpRHHIu1xtL0nSgzjKM5P7O/J RLcXaR8ZynpXAOU3w9lEH/Ohrlt00LaiZXYw32RS9Qp8xKjXf1eC X-Google-Smtp-Source: AGHT+IF11dViHkYpmtFOND9EMMWNwkF7eh+8R20OjyQu5tfpjoEMzODa+RtViR8je56NnqUZvmuEog== X-Received: by 2002:a05:6a00:939e:b0:6ed:5f9e:39d7 with SMTP id ka30-20020a056a00939e00b006ed5f9e39d7mr4876424pfb.5.1714497641911; Tue, 30 Apr 2024 10:20:41 -0700 (PDT) Received: from gnu-cfl-3.localdomain ([172.56.168.158]) by smtp.gmail.com with ESMTPSA id k13-20020a63ff0d000000b005dc816b2369sm21138085pgi.28.2024.04.30.10.20.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Apr 2024 10:20:41 -0700 (PDT) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 4C730740151; Tue, 30 Apr 2024 10:20:40 -0700 (PDT) From: "H.J. Lu" To: libc-alpha@sourceware.org Cc: alexandre.ferrieux@orange.com Subject: [PATCH] Fix #27777 - now use a doubly-linked list for _IO_list_all Date: Tue, 30 Apr 2024 10:20:34 -0700 Message-ID: <20240430172034.2667970-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.44.0 MIME-Version: 1.0 X-Spam-Status: No, score=-3022.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org From: Alexandre Ferrieux This patch fixes BZ #27777 "fclose does a linear search, takes ages when many FILE* are opened". Simply put, the master list of opened (FILE*), namely _IO_list_all, is a singly-linked list. As a consequence, the removal of a single element is in O(N), which cripples the performance of fclose(). The patch switches to a doubly-linked list, yielding O(1) removal. The one padding field in struct _IO_FILE, __pad5, is renamed to _prevchain for a doubly-linked list. Since fields in struct _IO_FILE after the _lock field are internal to glibc and opaque to applications. We can change them as long as the size of struct _IO_FILE is unchanged, which is checked as the part of glibc ABI with sizes of _IO_2_1_stdin_, _IO_2_1_stdout_ and _IO_2_1_stderr_. NB: When _IO_vtable_offset (fp) == 0, copy relocation will cover the whole struct _IO_FILE. Otherwise, only fields up to the _lock field will be copied to applications at run-time. It is used to check if the _prevchain field can be safely accessed. As tst-fclose.c shows, after opening 2 million (FILE*), the fclose() of 100 of them takes more than a few seconds without the patch, and under 2 seconds with it. Co-Authored-By: H.J. Lu --- libio/Makefile | 1 + libio/bits/types/struct_FILE.h | 4 +-- libio/genops.c | 26 +++++++++++++++ libio/stdfiles.c | 15 +++++++++ libio/tst-fclose.c | 60 ++++++++++++++++++++++++++++++++++ 5 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 libio/tst-fclose.c diff --git a/libio/Makefile b/libio/Makefile index 0c1f16ee3b..7f598c840c 100644 --- a/libio/Makefile +++ b/libio/Makefile @@ -94,6 +94,7 @@ tests = \ tst-eof \ tst-ext \ tst-ext2 \ + tst-fclose \ tst-fgetc-after-eof \ tst-fgetwc \ tst-fgetws \ diff --git a/libio/bits/types/struct_FILE.h b/libio/bits/types/struct_FILE.h index 7cdaae86f8..d8d26639d1 100644 --- a/libio/bits/types/struct_FILE.h +++ b/libio/bits/types/struct_FILE.h @@ -92,10 +92,10 @@ struct _IO_FILE_complete struct _IO_wide_data *_wide_data; struct _IO_FILE *_freeres_list; void *_freeres_buf; - size_t __pad5; + struct _IO_FILE **_prevchain; int _mode; /* Make sure we don't get into trouble again. */ - char _unused2[15 * sizeof (int) - 4 * sizeof (void *) - sizeof (size_t)]; + char _unused2[15 * sizeof (int) - 5 * sizeof (void *)]; }; /* These macros are used by bits/stdio.h and internal headers. */ diff --git a/libio/genops.c b/libio/genops.c index bc45e60a09..994ee9c0b1 100644 --- a/libio/genops.c +++ b/libio/genops.c @@ -48,6 +48,19 @@ flush_cleanup (void *not_used) } #endif +/* Fields in struct _IO_FILE after the _lock field are internal to + glibc and opaque to applications. We can change them as long as + the size of struct _IO_FILE is unchanged, which is checked as the + part of glibc ABI with sizes of _IO_2_1_stdin_, _IO_2_1_stdout_ + and _IO_2_1_stderr_. + + NB: When _IO_vtable_offset (fp) == 0, copy relocation will cover the + whole struct _IO_FILE. Otherwise, only fields up to the _lock field + will be copied. */ +_Static_assert (offsetof (struct _IO_FILE, _prevchain) + > offsetof (struct _IO_FILE, _lock), + "offset of _prevchain > offset of _lock"); + void _IO_un_link (struct _IO_FILE_plus *fp) { @@ -62,6 +75,14 @@ _IO_un_link (struct _IO_FILE_plus *fp) #endif if (_IO_list_all == NULL) ; + else if (_IO_vtable_offset ((FILE *) fp) == 0) + { + FILE **pr = fp->file._prevchain; + FILE *nx = fp->file._chain; + *pr = nx; + if (nx != NULL) + nx->_prevchain = pr; + } else if (fp == _IO_list_all) _IO_list_all = (struct _IO_FILE_plus *) _IO_list_all->file._chain; else @@ -95,6 +116,11 @@ _IO_link_in (struct _IO_FILE_plus *fp) _IO_flockfile ((FILE *) fp); #endif fp->file._chain = (FILE *) _IO_list_all; + if (_IO_vtable_offset ((FILE *) fp) == 0) + { + fp->file._prevchain = (FILE **) &_IO_list_all; + _IO_list_all->file._prevchain = &fp->file._chain; + } _IO_list_all = fp; #ifdef _IO_MTSAFE_IO _IO_funlockfile ((FILE *) fp); diff --git a/libio/stdfiles.c b/libio/stdfiles.c index cd8eca8bf3..d607fa02e0 100644 --- a/libio/stdfiles.c +++ b/libio/stdfiles.c @@ -54,4 +54,19 @@ DEF_STDFILE(_IO_2_1_stdout_, 1, &_IO_2_1_stdin_, _IO_NO_READS); DEF_STDFILE(_IO_2_1_stderr_, 2, &_IO_2_1_stdout_, _IO_NO_READS+_IO_UNBUFFERED); struct _IO_FILE_plus *_IO_list_all = &_IO_2_1_stderr_; + +/* Finish the double-linking for stdfiles as static initialization + cannot. */ + +__THROW __attribute__ ((constructor)) +static void +_IO_stdfiles_init (void) +{ + struct _IO_FILE **f; + for (f = (struct _IO_FILE **) &_IO_list_all; + *f != NULL; + f = &(*f)->_chain) + (*f)->_prevchain = f; +} + libc_hidden_data_def (_IO_list_all) diff --git a/libio/tst-fclose.c b/libio/tst-fclose.c new file mode 100644 index 0000000000..792c69beec --- /dev/null +++ b/libio/tst-fclose.c @@ -0,0 +1,60 @@ +/* Verify fclose performance with many opened files. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include + +#define N 2000000 +#define M 100 + +static int +do_test (void) +{ + FILE *ff, *keep[M]; + int i; + + fprintf (stderr, "Preparing %d FILEs ...\n", N); + fflush (stderr); + for (i = 0; i < N; i++) + { + ff = fdopen (STDIN_FILENO, "r"); + if (!ff) + { + fprintf (stderr, "### failed to fdopen: %m\n"); + return EXIT_FAILURE; + } + if (i < M) + keep[i] = ff; + } + fprintf (stderr, "Now fclosing ..."); + fflush (stderr); + for (i = 0; i < M; i++) + fclose (keep[i]); + fprintf (stderr, "DONE\n"); + fflush (stderr); + fprintf (stderr, "Now calling fcloseall( )..."); + fcloseall (); + fprintf (stderr, "DONE\n"); + fflush (stderr); + return EXIT_SUCCESS; +} + +#define TIMEOUT 2 +#include