From patchwork Thu May 16 12:54:30 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: 1936006 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=X9ugxn0s; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; 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 [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 4Vg96T5KRkz1yfq for ; Thu, 16 May 2024 22:54:57 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C22F03849AC7 for ; Thu, 16 May 2024 12:54:55 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by sourceware.org (Postfix) with ESMTPS id 589833858D38 for ; Thu, 16 May 2024 12:54:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 589833858D38 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 589833858D38 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::630 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715864077; cv=none; b=Jk/nYYIx+i0fD/q+eLMVfyNuJN13knAYHzu9VhV6E1sn4BJ0PUwg5fHLOh16Suk6XuWBC/DzplEmLsTc4v9mI2I7G2ITm5eCkYNVqhnjIPcsh9a/zSiGeX8dU0kVaJztBC1ydtyj2dFhUmx/gLuCWnoXJ/OOUzaaMv9VXvaQ1Oc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715864077; c=relaxed/simple; bh=wD6BN3BMEplG6fXD3ZMmapeap8CPOnr739ox2ZQSJFU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=HdGq2/pBb5QHG/l9y0Z5LwnYMJzZbBPfB7P/uUd8rjQCWtol/IjnNUD3ftTr4x0GwaHFgxlB9UvwFv9vi/mNpUpIhJup9I5P9Z8xP+mPsNPZnM9lS3KNRl1LkmCG8PD/Ow361NJipZ8/ln1xmY+6OP+w8tje++KqqekE65DjDxI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-1ed0abbf706so59587435ad.2 for ; Thu, 16 May 2024 05:54:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715864074; x=1716468874; 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=gt66wk1giRRuFrb3cpS8j39jUxfVaexuMnfnvV7qWMU=; b=X9ugxn0sNnew/9mWaVFUkkb97E4tRupHYhacQBfqh1OsVI8lT2Rl39qMwauIzGxpNp USbcjqrm6KwjP4KQUOag2SgYGKqP5crdN5U/3Gq71EIug+9rWAlx34T7XB02kWFzMn/V AB9GvRj64XCZBl1QdsMXi85Lu5jWF1r7wS2MwRNbVqV1il92uYiL6GUPJS6ZcaWj7kaM VRHyAfAjhTUjMLQYNcIigSyAre/aAFJ/yTZXSBJN10/Pw9ugUTIINK9Y5Q35oxujEHYy dYt9g4yYNQoYYSHpQbR12owiO5cqOtWtnkhl2n73U+HtAjAZdL5I9j9CzPhEq4aG3qxm E2EA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715864074; x=1716468874; 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=gt66wk1giRRuFrb3cpS8j39jUxfVaexuMnfnvV7qWMU=; b=DnQ5eqUWgmZYjZupXI54BOqhh9Nt7AzO2fq1gV3jB7OAIrdDvgK3FtHU6KM+eOLlMx BNCaAWTL5PEli9s5JOfP8S+dppTjM9E6MpWnek2cYTpZV+Vz8OmNizUN4c/aEycoM52s 9PtR7tDBKihm0cuHYhZ6afOW8VHj2EqHZT2pUbvXhMQaJ5AKKLmm9PkAw9LzDsYrK07R Y1/d0UalfXIz2ViNZarf9adJHIc7R9QvfIIEFa/hecGGvzsX2XsZoxAWQXadI0dGfziC LZKC6flNjB9D3DDsOKlgXVnKMl+Fd00/r2vNqLx2Tt+vvaW1ygnog9TJkqY4ii2trnVB k0CA== X-Gm-Message-State: AOJu0YxTR3AnO2BmQ1UuLG2EYG81WOyJySHuyV/EXuISinSJEmKaJw2t TlCaqNELqnokEiLaMjzjCpElmfnvK2nL7+kCGRd6tUH45ZBmecJX X-Google-Smtp-Source: AGHT+IHo9geLlEtE1GDnM0MQAeHmZ+j/DNNYGmMlHIyeK9xHaVhQXiWGJlmpQ6G856Ta5N3SkYqIwA== X-Received: by 2002:a17:902:c412:b0:1e5:caba:7fbd with SMTP id d9443c01a7336-1ef4404a18amr246470995ad.44.1715864074174; Thu, 16 May 2024 05:54:34 -0700 (PDT) Received: from gnu-cfl-3.localdomain ([172.56.168.158]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0c25628esm136788285ad.288.2024.05.16.05.54.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 May 2024 05:54:33 -0700 (PDT) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id EEFAA7407AF; Thu, 16 May 2024 05:54:31 -0700 (PDT) From: "H.J. Lu" To: libc-alpha@sourceware.org Cc: carlos@redhat.com, alexandre.ferrieux@orange.com, fweimer@redhat.com Subject: [PATCH v6] Use a doubly-linked list for _IO_list_all (bug 27777) Date: Thu, 16 May 2024 05:54:30 -0700 Message-ID: <20240516125430.1915935-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.45.0 MIME-Version: 1.0 X-Spam-Status: No, score=-3020.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_SBL_CSS, 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. After opening 2 million (FILE*), the fclose() of 100 of them takes quite a few seconds without the patch, and under 2 seconds with it on a loaded machine. No test is added since there are no functional changes. Co-Authored-By: H.J. Lu Signed-off-by: Alexandre Ferrieux Signed-off-by: H.J. Lu Reviewed-by: Carlos O'Donell --- libio/bits/types/struct_FILE.h | 4 ++-- libio/genops.c | 26 ++++++++++++++++++++++++++ libio/stdfiles.c | 15 +++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) 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..12db1645ef 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 double-linking stdin, stdout, and stderr in a constructor. + Static initialization cannot complete the _prevchain setup. */ + +__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)