From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by sourceware.org (Postfix) with ESMTPS id 1F1913870896 for ; Mon, 13 May 2024 13:50:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1F1913870896 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 1F1913870896 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::435 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715608222; cv=none; b=wX9puErQHi/isqK5/Qz4CLg09x9ih6Bu4bZmU5LTjVxneQUixOiwRVtmiWSHtXaYf5NDH6i4M4f/bKNoHQ/kake/e0W35ENXxzmW/TQB90I7p/ojX2Bb50pVmgTkQTTHmKWvL0QgNoPVX4wYncfBROjPJBvrhbufQn5I+hGE6rg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715608222; c=relaxed/simple; bh=fIvLNuB/vXtSI04jZmoSrEpte4ggzjeSC8dx0MgSGO8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=qi7O18zcZcY4u/iueXUxDUFug1+2Eb+p0wAfR7MsDYPM/t1vl17nBLKavd8jVsvHPqDaUnKGShjUlNVJha7+h66W3nCVRvWzLwczz5Qgv2AkWzX01yduKqXjBI6rzXJ1nm6CKnLcnM5WsYk569fJKwCyEHXsN7r+rTH9n+S1Cqw= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-6f47787a0c3so3962709b3a.0 for ; Mon, 13 May 2024 06:50:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715608218; x=1716213018; 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=50B8qoN0edsgh2X7BdPsi0iGAO8lyYNROnOFcStLgwQ=; b=BO7L5selMK0y++5j4rQTNM1GE+KAM6hQnwtfmcu3XB+eB50JAqY/TFKgOPE8j6kGPc r3ripmB4pW+KqDjmuu35voYsqini5agjq1i0WCjBmW5KTA4tBpZv/4Xro4ygPTZR0o1Q K9UWZQqLyT1IAtBBYwsLiPA7RzcNGTShu8ywAEVJALSThQ6cbyPZIO2+vdDNCG3iSLb9 pl6soOrYtF0mDt0a7WkRM+ppU8ZfXFkcRT3+jY+a+T7a1qOyIol60WtrQsNfgGj/NDhH z+Bb2TopJLtyxaSLU2SljRKydBD02pbxonUIKsFzq5VZtU//Z3zdcxDpzzFAekX7mZdl wd5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715608218; x=1716213018; 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=50B8qoN0edsgh2X7BdPsi0iGAO8lyYNROnOFcStLgwQ=; b=pSWa6afmJwSrOZqZLtDcSUYeMy0GamKKs4MyulMfiDPszt1+Lq9G/4iCnIUGBcdZ3I GFTMHepLXqkVO+tF4lot0v2gcWfZIVXiDXEWTv9/I5dBFovlvZFYCUpYCNOVJ1DDrU3Z vPqkOk5A7F2dd7s46sjHDF1/19sJ3QgCTiBp7ac4xarKqHJKmvH7ao7ydBrjqPDaInBE 9VhQkWBly+XuUhkvXo4yav7PpJ4ijD9u1mdlsldXsQW7N0hxHFuPRSFh5L0fcmNO6jNh zANooPrTdu8HXifL7mvsJkZYaIaU9PIdz/XbEQPf3N/O1HqZJP0uNKhA0xsL0MtWEE5D z6Mw== X-Gm-Message-State: AOJu0YzuxV0o6eAcAbgGCNPN5ZmxI2RyVKmiTK8apRZuDKvfZvA4BkBc S/nBLlYs1Tfnw/wiKCxA0W1k/PbWLG6ahe0JBnCVt2ODunpAh6q5 X-Google-Smtp-Source: AGHT+IHU+D2OE6WGP4uc8n6dVkpMiZe5tYxtd1NtwOIlmISsapmi6DK0SciUNvNPPDqFm4XXMCBKpA== X-Received: by 2002:a05:6a00:39a0:b0:6f3:8aa5:829f with SMTP id d2e1a72fcca58-6f4e03298c9mr13005551b3a.33.1715608217921; Mon, 13 May 2024 06:50:17 -0700 (PDT) Received: from gnu-cfl-3.localdomain ([172.56.168.158]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-6f4d2a9d9acsm7562239b3a.90.2024.05.13.06.50.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 May 2024 06:50:17 -0700 (PDT) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id DFAC6740123; Mon, 13 May 2024 06:50:15 -0700 (PDT) From: "H.J. Lu" To: libc-alpha@sourceware.org Cc: alexandre.ferrieux@orange.com, fweimer@redhat.com Subject: [PATCH v5] Fix #27777 - now use a doubly-linked list for _IO_list_all Date: Mon, 13 May 2024 06:50:14 -0700 Message-ID: <20240513135014.1328169-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.45.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 List-Id: 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 --- 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..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) -- 2.45.0