From: Richard Biener <rguenther@suse.de>
To: Michal Jires <mjires@suse.cz>
Cc: gcc-patches@gcc.gnu.org, hubicka@ucw.cz
Subject: Re: [PATCH 3/7 v2] Lockfile.
Date: Thu, 20 Jun 2024 14:36:33 +0200 (CEST) [thread overview]
Message-ID: <43o53202-78qp-r665-1qpo-706994720or3@fhfr.qr> (raw)
In-Reply-To: <crcqxt3yayuryht5nbi6odvouuamwnrfmtlxrkaqv4czj47khl@qm22lrqve4yy>
On Thu, 20 Jun 2024, Michal Jires wrote:
> This version differs by using INCLUDE_STRING instead of <string>.
> (+whitespace and year)
OK (though I'm not happy to see more std::string use)
Richard.
> ___
>
> 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 | 78 +++++++++++++++++++++++++++
> 3 files changed, 217 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 f5adb647d3f..90ec59dca75 100644
> --- a/gcc/Makefile.in
> +++ b/gcc/Makefile.in
> @@ -1855,7 +1855,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
> @@ -2384,7 +2384,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..8ecb4dc2848
> --- /dev/null
> +++ b/gcc/lockfile.cc
> @@ -0,0 +1,136 @@
> +/* File locking.
> + Copyright (C) 2023-2024 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
> +<http://www.gnu.org/licenses/>. */
> +
> +#define INCLUDE_STRING
> +#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..c222eb6d3b6
> --- /dev/null
> +++ b/gcc/lockfile.h
> @@ -0,0 +1,78 @@
> +/* File locking.
> + Copyright (C) 2023-2024 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
> +<http://www.gnu.org/licenses/>. */
> +
> +#ifndef LOCKFILE_H
> +#define LOCKFILE_H
> +
> +/* 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
>
--
Richard Biener <rguenther@suse.de>
SUSE Software Solutions Germany GmbH,
Frankenstrasse 146, 90461 Nuernberg, Germany;
GF: Ivo Totev, Andrew McDonald, Werner Knoblich; (HRB 36809, AG Nuernberg)
next prev parent reply other threads:[~2024-06-20 12:36 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-17 20:16 [PATCH 0/7] lto: Incremental LTO Michal Jires
2023-11-17 20:16 ` [PATCH 1/7] lto: Skip flag OPT_fltrans_output_list_ Michal Jires
2023-12-29 21:16 ` Jan Hubicka
2023-11-17 20:16 ` [PATCH 2/7] lto: Remove random_seed from section name Michal Jires
2023-12-29 21:17 ` Jan Hubicka
2024-01-09 16:49 ` [PATCH 2/7 v2] " Michal Jires
[not found] ` <c480760c-f167-4e60-a27e-52bebdd1351b@suse.cz>
2024-05-14 11:28 ` Fwd: " Jan Hubicka
2023-11-17 20:17 ` [PATCH 3/7] Lockfile Michal Jires
2023-12-29 21:23 ` Jan Hubicka
2024-01-09 17:10 ` Michal Jires
2024-06-20 11:24 ` [PATCH 3/7 v2] Lockfile Michal Jires
2024-06-20 12:36 ` Richard Biener [this message]
2023-11-17 20:17 ` [PATCH 4/7] lto: Implement ltrans cache Michal Jires
2024-05-14 11:51 ` Jan Hubicka
2024-06-20 11:31 ` [PATCH 4/7 v2] " Michal Jires
2024-06-20 17:45 ` Andi Kleen
2024-06-21 11:12 ` Jan Hubicka
2024-06-21 19:07 ` Andi Kleen
2024-06-21 19:16 ` Sam James
2024-06-21 16:59 ` Michal Jireš
2024-06-21 19:09 ` Andi Kleen
2023-11-17 20:17 ` [PATCH 5/7] lto: Implement cache partitioning Michal Jires
2024-05-14 12:10 ` Jan Hubicka
2023-11-17 20:17 ` [PATCH 6/7] lto: squash order of symbols in partitions Michal Jires
2024-05-14 12:20 ` Jan Hubicka
2023-11-17 20:17 ` [PATCH 7/7] lto: partition specific lto_clone_numbers Michal Jires
2024-05-14 12:11 ` Jan Hubicka
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=43o53202-78qp-r665-1qpo-706994720or3@fhfr.qr \
--to=rguenther@suse.de \
--cc=gcc-patches@gcc.gnu.org \
--cc=hubicka@ucw.cz \
--cc=mjires@suse.cz \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).