From patchwork Fri Nov 17 20:17:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Jires X-Patchwork-Id: 166318 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9910:0:b0:403:3b70:6f57 with SMTP id i16csp789346vqn; Fri, 17 Nov 2023 12:17:53 -0800 (PST) X-Google-Smtp-Source: AGHT+IEzF5rfd7qgN+QLjX0i6lRwbcJMxOk8A43R19PnhDcmUo3eb34vVWLRkmj2SNxYyYgkuQRB X-Received: by 2002:a05:6870:8184:b0:1e9:cb91:1b05 with SMTP id k4-20020a056870818400b001e9cb911b05mr313649oae.53.1700252273261; Fri, 17 Nov 2023 12:17:53 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1700252273; cv=pass; d=google.com; s=arc-20160816; b=w7ttJue+2m8gNEbNMzktPS3vAjNG8YsHShTX5sbXUlPHJ+oHqMwknlXPidxj+ShOd3 HJyigPBiBJ13rfJZbOqbrJ6zg6v6YVgdAdh6ABYd8/WOR69hKr7JvRIqEXsuwJ27N7+m JxIPIzV/DQj+YskNzLVHSaSaIxViZ9SKbWfwEWxc6hHsZl0RVEnMCxAh0rRn9Vl1qXqY 8OUp76WbN8a1lTpAXXV1qNq5CfSmdUv+MR/LYVgkQj3giJ45jqSsrWZhkQwyRks0at59 12glcc13fGFLtmCwwPf/+yt9z+btsEjjNr+hTMfpUT8/+6qy/7epFfaVAXxbuv8odPcm ePCA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:in-reply-to:content-disposition :mime-version:references:message-id:subject:to:from:date :dkim-signature:dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=CJ0SS3mli0cQQ4cGzfn3Vow56RLldVPq+z/mLh/ntdw=; fh=hPrbWPhweUx4V0GV9uXJqbyAzg2ABmTz7kczrAQqMmM=; b=DlSJPPPVETM9Kpel7WqnTuAulVef5l6lUVy6K+jYKJ5f1ags+eYZDMGUmCYsHtDoQM xsE0RN2pipPd6YBaXTcyeZ5l1z+aEEle5foK8aY+5VKeYtKx7lc3mAq4YcWMDSrMjWou WVNASJteeW4WD15WidGdzF+dkqxc9iN3RUHgFeG+ZQNnwkWEr1MnNoCO24oAm6lk5MVA R9H9KP0zslftfkWpo8irp8sRFmyxjC6RpprYi5NIPt+MRXzMjEAM42ViznkOer3hXdZV GYAVNfZTZIJa77L2Veqv5ScEBbY1KlzkxI2mIQPwZbyFno+VnuS4qGAddOo9GpOUSkku R+2g== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=nizzB5XO; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519 header.b=nRb18Xd4; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id v8-20020a05622a188800b0041993fce7aasi2347932qtc.94.2023.11.17.12.17.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Nov 2023 12:17:53 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=nizzB5XO; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519 header.b=nRb18Xd4; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3BA54385B504 for ; Fri, 17 Nov 2023 20:17:50 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by sourceware.org (Postfix) with ESMTPS id 4DA2B385828F for ; Fri, 17 Nov 2023 20:17:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4DA2B385828F Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=suse.cz ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4DA2B385828F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2001:67c:2178:6::1c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700252227; cv=none; b=EyYlPC6bzZVJ7YOSe/GCopgvFAFHA8lJRNs1OGl1su8c3Un/kHnBvjQzNyeuI4Olm1G+0JwR9dCA+dv93jhf+qU1wCQkM0mZa8RxqM0JryXZrPtlFih2VahZSukGseCLQG10IDJY0VDTeRaqpF55aNLWOrthVJQcTnN953cYnPg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700252227; c=relaxed/simple; bh=N5JWao+n+5bILwHxzEpvWa/msIM8pHKGmLMIihGGAP0=; h=DKIM-Signature:DKIM-Signature:Date:From:To:Subject:Message-ID: MIME-Version; b=UnZ2WcUc7u/ZixVNa5z2BEhDYfClEX0zgGZ1QKcBvY6BegtS6BLsMVQy8eEowBHIiJGvPkF/1DEXXH7DogpL7LvERXoztUoF7Dw8lKTO3C8jKz2mtThnjAe1UFmRncy9hWwTapqEEjUFGSUjG3Ek36rdSFhCR/L9BNR2kl+mMpA= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 2C162228FA for ; Fri, 17 Nov 2023 20:17:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1700252223; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=CJ0SS3mli0cQQ4cGzfn3Vow56RLldVPq+z/mLh/ntdw=; b=nizzB5XO7+U5Kv3kXzRHF/LdUJv+GZstXzyHb1nMUcFikI6eGB8GvKPV8ejgSOZSA+y/VR ZJRd4+r69FoLDXLOOAeS0pRQ9MEMapcz5d3mtFwilxMxQ5JjBk+icd/Pe3y4WaCtu01jeL Kzoc09XGxwsDRBuZWKSWlNNcrvNR7yU= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1700252223; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=CJ0SS3mli0cQQ4cGzfn3Vow56RLldVPq+z/mLh/ntdw=; b=nRb18Xd4jUUOVb5Djpm4gNu+gWDPQmfjNowthhHXzC3GHGuDaduaNjrqopGfpg8zdpyG1k 8hTeJMgCE8c0GCDA== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 0BCB81341F for ; Fri, 17 Nov 2023 20:17:03 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id TTEcAT/KV2UcGgAAMHmgww (envelope-from ) for ; Fri, 17 Nov 2023 20:17:03 +0000 Date: Fri, 17 Nov 2023 21:17:01 +0100 From: Michal Jires To: gcc-patches@gcc.gnu.org Subject: [PATCH 3/7] Lockfile. Message-ID: References: <18cc1c3980551ac1881eea6e78811a629c7baa82.1700222403.git.mjires@suse.cz> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <18cc1c3980551ac1881eea6e78811a629c7baa82.1700222403.git.mjires@suse.cz> Authentication-Results: smtp-out1.suse.de; none X-Spam-Level: X-Spam-Score: -3.29 X-Spamd-Result: default: False [-3.29 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; MIME_GOOD(-0.10)[text/plain]; PREVIOUSLY_DELIVERED(0.00)[gcc-patches@gcc.gnu.org]; TO_DN_NONE(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; DKIM_SIGNED(0.00)[suse.cz:s=susede2_rsa,suse.cz:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.19)[-0.930]; MID_CONTAINS_FROM(1.00)[]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-3.00)[100.00%] X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, SPF_HELO_NONE, SPF_SOFTFAIL, TXREP, T_SCC_BODY_TEXT_LINE 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: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782843728035130760 X-GMAIL-MSGID: 1782843728035130760 This patch implements lockfile used for incremental LTO. Bootstrapped/regtested on x86_64-pc-linux-gnu gcc/ChangeLog: * Makefile.in: Add lockfile.o. * lockfile.cc: New file. * lockfile.h: New file. --- gcc/Makefile.in | 5 +- gcc/lockfile.cc | 136 ++++++++++++++++++++++++++++++++++++++++++++++++ gcc/lockfile.h | 85 ++++++++++++++++++++++++++++++ 3 files changed, 224 insertions(+), 2 deletions(-) create mode 100644 gcc/lockfile.cc create mode 100644 gcc/lockfile.h diff --git a/gcc/Makefile.in b/gcc/Makefile.in index 7b7a4ff789a..2c527245c81 100644 --- a/gcc/Makefile.in +++ b/gcc/Makefile.in @@ -1831,7 +1831,7 @@ ALL_HOST_BACKEND_OBJS = $(GCC_OBJS) $(OBJS) $(OBJS-libcommon) \ $(OBJS-libcommon-target) main.o c-family/cppspec.o \ $(COLLECT2_OBJS) $(EXTRA_GCC_OBJS) $(GCOV_OBJS) $(GCOV_DUMP_OBJS) \ $(GCOV_TOOL_OBJS) $(GENGTYPE_OBJS) gcc-ar.o gcc-nm.o gcc-ranlib.o \ - lto-wrapper.o collect-utils.o + lto-wrapper.o collect-utils.o lockfile.o # for anything that is shared use the cc1plus profile data, as that # is likely the most exercised during the build @@ -2359,7 +2359,8 @@ collect2$(exeext): $(COLLECT2_OBJS) $(LIBDEPS) CFLAGS-collect2.o += -DTARGET_MACHINE=\"$(target_noncanonical)\" \ @TARGET_SYSTEM_ROOT_DEFINE@ -LTO_WRAPPER_OBJS = lto-wrapper.o collect-utils.o ggc-none.o +LTO_WRAPPER_OBJS = lto-wrapper.o collect-utils.o ggc-none.o lockfile.o + lto-wrapper$(exeext): $(LTO_WRAPPER_OBJS) libcommon-target.a $(LIBDEPS) +$(LINKER) $(ALL_LINKERFLAGS) $(LDFLAGS) -o T$@ \ $(LTO_WRAPPER_OBJS) libcommon-target.a $(LIBS) diff --git a/gcc/lockfile.cc b/gcc/lockfile.cc new file mode 100644 index 00000000000..9440e8938f3 --- /dev/null +++ b/gcc/lockfile.cc @@ -0,0 +1,136 @@ +/* File locking. + Copyright (C) 2009-2023 Free Software Foundation, Inc. + +This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 3, or (at your option) any later +version. + +GCC 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 General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +. */ + +#include "config.h" +#include "system.h" + +#include "lockfile.h" + + +/* Unique write lock. No other lock can be held on this lockfile. + Blocking call. */ +int +lockfile::lock_write () +{ + fd = open (filename.c_str (), O_RDWR | O_CREAT, 0666); + if (fd < 0) + return -1; + +#if HAVE_FCNTL_H + struct flock s_flock; + + s_flock.l_whence = SEEK_SET; + s_flock.l_start = 0; + s_flock.l_len = 0; + s_flock.l_pid = getpid (); + s_flock.l_type = F_WRLCK; + + while (fcntl (fd, F_SETLKW, &s_flock) && errno == EINTR) + continue; +#endif + return 0; +} + +/* Unique write lock. No other lock can be held on this lockfile. + Only locks if this filelock is not locked by any other process. + Return whether locking was successful. */ +int +lockfile::try_lock_write () +{ + fd = open (filename.c_str (), O_RDWR | O_CREAT, 0666); + if (fd < 0) + return -1; + +#if HAVE_FCNTL_H + struct flock s_flock; + + s_flock.l_whence = SEEK_SET; + s_flock.l_start = 0; + s_flock.l_len = 0; + s_flock.l_pid = getpid (); + s_flock.l_type = F_WRLCK; + + if (fcntl (fd, F_SETLK, &s_flock) == -1) + { + close (fd); + fd = -1; + return 1; + } +#endif + return 0; +} + +/* Shared read lock. Only read lock can be held concurrently. + If write lock is already held by this process, it will be + changed to read lock. + Blocking call. */ +int +lockfile::lock_read () +{ + fd = open (filename.c_str (), O_RDWR | O_CREAT, 0666); + if (fd < 0) + return -1; + +#if HAVE_FCNTL_H + struct flock s_flock; + + s_flock.l_whence = SEEK_SET; + s_flock.l_start = 0; + s_flock.l_len = 0; + s_flock.l_pid = getpid (); + s_flock.l_type = F_RDLCK; + + while (fcntl (fd, F_SETLKW, &s_flock) && errno == EINTR) + continue; +#endif + return 0; +} + +/* Unlock all previously placed locks. */ +void +lockfile::unlock () +{ + if (fd < 0) + { +#if HAVE_FCNTL_H + struct flock s_flock; + + s_flock.l_whence = SEEK_SET; + s_flock.l_start = 0; + s_flock.l_len = 0; + s_flock.l_pid = getpid (); + s_flock.l_type = F_UNLCK; + + fcntl (fd, F_SETLK, &s_flock); +#endif + close (fd); + fd = -1; + } +} + +/* Are lockfiles supported? */ +bool +lockfile::lockfile_supported () +{ +#if HAVE_FCNTL_H + return true; +#else + return false; +#endif +} diff --git a/gcc/lockfile.h b/gcc/lockfile.h new file mode 100644 index 00000000000..afcbaf599c1 --- /dev/null +++ b/gcc/lockfile.h @@ -0,0 +1,85 @@ +/* File locking. + Copyright (C) 2009-2023 Free Software Foundation, Inc. + +This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 3, or (at your option) any later +version. + +GCC 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 General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +. */ + +#ifndef LOCKFILE_H +#define LOCKFILE_H + +#include + +/* Used to synchronize across multiple processes. */ +class lockfile { +public: + /* Default constructor. */ + lockfile (): fd (-1) + {} + /* Intended constructor for use. Filename should not be used for anything + other than locking to prevent unintentional unlock. */ + lockfile (std::string filename): lockfile () + { + this->filename = std::move (filename); + } + lockfile (lockfile const& o): lockfile (o.filename) + {} + + void operator=(lockfile o) + { + unlock (); + this->filename = o.filename; + this->fd = o.fd; + o.fd = -1; + } + + /* Unique write lock. No other lock can be held on this lockfile. + Blocking call. */ + int + lock_write (); + + /* Unique write lock. No other lock can be held on this lockfile. + Only locks if this filelock is not locked by any other process. + Return whether locking was successful. */ + int + try_lock_write (); + + /* Shared read lock. Only read lock can be held concurrently. + If write lock is already held by this process, it will be + changed to read lock. + Blocking call. */ + int + lock_read (); + + /* Unlock all previously placed locks. */ + void + unlock (); + + /* Returns whether any lock is held. */ + bool + locked () + { + return fd < 0; + } + + /* Are lockfiles supported? */ + static bool + lockfile_supported (); +private: + std::string filename; + int fd; +}; + +#endif