From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id D17583850431; Wed, 5 Aug 2020 13:51:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D17583850431 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1596635501; bh=/YX9KzKjIa7P2wCuc3RUiN91Ck3u6FLebP8W0wUJTBA=; h=From:To:Subject:Date:From; b=ntFOzfw1TQ60wxhN4oyqsOZdiOOEo0JAlRysPG0NCepouBL0v8MjeoVuNtkls4Kwn 86K8JWA+EBsT/YbHadGaPVGKEt7GwCcgOeZWljXSEYrbxGmQFTdTJec2b20s/Xrqao lChaJP2C6kc5nu0KfKdAA0pJvYwmuOuiwClsgfZk= From: "terra at gnome dot org" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/96484] New: Horrible performance of std::read_symlink Date: Wed, 05 Aug 2020 13:51:41 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 10.1.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: terra at gnome dot org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone attachments.created Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 05 Aug 2020 13:51:41 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D96484 Bug ID: 96484 Summary: Horrible performance of std::read_symlink Product: gcc Version: 10.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: terra at gnome dot org Target Milestone: --- Created attachment 49004 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=3D49004&action=3Dedit Preprocessed ttt.C This program takes ~0.25s per round in the loop for a total of minutes. It should take millisecons in total. To observe the problem, "foo" needs to be a large file -- more than 500M in= my case -- that is not a symlink. Mine is on an NFS server. #include #include namespace fs =3D std::filesystem; int main() { for (int i =3D 0; i < 1000; i++) { std::error_code ec; std::cerr << i << ": " << fs::read_symlink ("foo", ec) << std::endl; } } The problem is in the implementation of read_symlink. First an lstat call = is done (fine), but then the st_size field is used as a hint for the symlink's size. There is no check that the file is a symlink at this point, so in my case a 500M buffer is allocated via an mmap system call and the kernel takes its sweet time. Note: a file with size 4G-1 on a 32-bit system might produce an infinite lo= op as the buffer will have size 0 and get doubled from that. Suggestion 1: if the lstat doesn't show a symlink, set the error code right then and exit. Suggestion 2: cap st_size at something reasonable and let the loop handle i= t if it needs to be bigger. # /usr/local/products/gcc/10.1.0/bin/g++ -v Using built-in specs. COLLECT_GCC=3D/usr/local/products/gcc/10.1.0/bin/g++ COLLECT_LTO_WRAPPER=3D/usr/local/products/gcc/10.1.0/lib/gcc/x86_64-suse-li= nux/10.1.0/lto-wrapper Target: x86_64-suse-linux Configured with: ../../gcc-10.1.0/configure --enable-languages=3Dc,c++,fort= ran --enable-targets=3Dx86_64-suse-linux,i686-suse-linux --prefix=3D/usr/local/products/gcc/10.1.0 --with-gnu-as --with-as=3D/usr/local/products/gcc/binutils-2.32/bin/as --with-gnu-ld --with-ld=3D/usr/local/products/gcc/binutils-2.32/bin/ld --enable-threads= =3Dposix --enable-shared --enable-__cxa_atexit --enable-libstdcxx-allocator=3Dpool x86_64-suse-linux Thread model: posix Supported LTO compression algorithms: zlib gcc version 10.1.0 (GCC)=20 # uname -a Linux monsterd09 5.0.13-1-default #1 SMP Sun May 5 15:48:04 UTC 2019 (b11e2= d7) x86_64 x86_64 x86_64 GNU/Linux=