The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Milos Vyletel <milos@redhat.com>
To: Peter Zijlstra <a.p.zijlstra@chello.nl>,
	Paul Mackerras <paulus@samba.org>, Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>,
	Namhyung Kim <namhyung@kernel.org>,
	Milos Vyletel <milos@redhat.com>, Jiri Olsa <jolsa@kernel.org>,
	He Kuang <hekuang@huawei.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	linux-kernel@vger.kernel.org (open list:PERFORMANCE EVENT...)
Subject: [PATCH 1/2] perf/tools: add read/write buildid dir locks
Date: Thu, 14 May 2015 09:28:10 +0200	[thread overview]
Message-ID: <1431588493-1474-2-git-send-email-milos@redhat.com> (raw)
In-Reply-To: <1431588493-1474-1-git-send-email-milos@redhat.com>

Protect access to buildid dir by flock to prevent race conditions. This
patch adds buildid_dir_read/write_lock/unlock functions.

Signed-off-by: Milos Vyletel <milos@redhat.com>
---
 tools/perf/util/build-id.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/build-id.h |  5 +++++
 2 files changed, 61 insertions(+)

diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c
index 61867df..46c41e1 100644
--- a/tools/perf/util/build-id.c
+++ b/tools/perf/util/build-id.c
@@ -536,3 +536,59 @@ bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
 
 	return ret;
 }
+
+static int buildid_dir_lock(int op)
+{
+	int lockfd, wait = 30;
+	const size_t size = PATH_MAX;
+	char *lockfile = zalloc(size);
+
+	scnprintf(lockfile, size, "%s/.lock", buildid_dir);
+
+	lockfd = open(lockfile, O_RDONLY|O_CREAT, S_IRUSR);
+	if (lockfd  == -1)
+		goto out;
+
+	while (1) {
+		if (flock(lockfd, op | LOCK_NB) == 0)
+			goto out;
+		else if (wait == 0) {
+			close(lockfd);
+			lockfd = -1;
+			goto out;
+		}
+		if (--wait % 10 == 0)
+			pr_info("Waiting for %s lock to be available\n", buildid_dir);
+		usleep(100);
+	}
+
+out:
+	free(lockfile);
+	return lockfd;
+}
+
+static void buildid_dir_unlock(int fd)
+{
+	flock(fd, LOCK_UN);
+	close(fd);
+}
+
+void buildid_dir_write_lock(int *fd)
+{
+	*fd = buildid_dir_lock(LOCK_EX);
+}
+
+void buildid_dir_read_lock(int *fd)
+{
+	*fd = buildid_dir_lock(LOCK_SH);
+}
+
+void buildid_dir_write_unlock(int fd)
+{
+	buildid_dir_unlock(fd);
+}
+
+void buildid_dir_read_unlock(int fd)
+{
+	buildid_dir_unlock(fd);
+}
diff --git a/tools/perf/util/build-id.h b/tools/perf/util/build-id.h
index 8501122..326cd2e 100644
--- a/tools/perf/util/build-id.h
+++ b/tools/perf/util/build-id.h
@@ -31,4 +31,9 @@ int build_id_cache__add_s(const char *sbuild_id,
 int build_id_cache__remove_s(const char *sbuild_id);
 void disable_buildid_cache(void);
 
+void buildid_dir_write_lock(int *);
+void buildid_dir_read_lock(int *);
+void buildid_dir_write_unlock(int);
+void buildid_dir_read_unlock(int);
+
 #endif
-- 
2.4.0


  reply	other threads:[~2015-05-14  7:29 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-14  7:28 [PATCH 0/2] Add perf debug dir locking Milos Vyletel
2015-05-14  7:28 ` Milos Vyletel [this message]
2015-05-14  7:28 ` [PATCH 2/2] perf/tools: put new buildid locks to use Milos Vyletel
2015-05-14 10:40   ` Ingo Molnar
2015-05-14 11:38     ` Jiri Olsa
2015-05-14 15:44       ` Milos Vyletel
2015-05-14 17:38         ` Ingo Molnar
2015-05-14 17:38       ` Ingo Molnar
2015-05-20  7:27         ` Milos Vyletel
2015-06-03 11:13         ` Milos Vyletel
2015-06-03 11:21           ` Ingo Molnar
2015-06-03 11:27             ` Milos Vyletel

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=1431588493-1474-2-git-send-email-milos@redhat.com \
    --to=milos@redhat.com \
    --cc=a.p.zijlstra@chello.nl \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=hekuang@huawei.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=masami.hiramatsu.pt@hitachi.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=paulus@samba.org \
    /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