Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1730130

[PATCH RFC V2 05/10] perf tools: lock to protect thread list

From kan.liang@intel.com
Newsgroups linux.kernel
Subject [PATCH RFC V2 05/10] perf tools: lock to protect thread list
Date 2017-09-11 04:30 +0200
Message-ID <uomE2-4Gu-31@gated-at.bofh.it> (permalink)
References <uomE1-4Gu-1@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


From: Kan Liang <kan.liang@intel.com>

Add two locks to protect namespaces_list and comm_list.

The comm which is used in db-export are not protected. Because the
multithread code will not touch it. It can be added later if required.

Signed-off-by: Kan Liang <kan.liang@intel.com>
---
 tools/perf/ui/browsers/hists.c |  2 +-
 tools/perf/util/thread.c       | 60 +++++++++++++++++++++++++++++++++---------
 tools/perf/util/thread.h       |  6 +++--
 3 files changed, 52 insertions(+), 16 deletions(-)

diff --git a/tools/perf/ui/browsers/hists.c b/tools/perf/ui/browsers/hists.c
index 13dfb0a..429e1dd 100644
--- a/tools/perf/ui/browsers/hists.c
+++ b/tools/perf/ui/browsers/hists.c
@@ -2370,7 +2370,7 @@ static int perf_evsel_browser_title(struct hist_browser *browser,
 	char unit;
 	int printed;
 	const struct dso *dso = hists->dso_filter;
-	const struct thread *thread = hists->thread_filter;
+	struct thread *thread = hists->thread_filter;
 	int socket_id = hists->socket_filter;
 	unsigned long nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
 	u64 nr_events = hists->stats.total_period;
diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
index b7957da..25830b2 100644
--- a/tools/perf/util/thread.c
+++ b/tools/perf/util/thread.c
@@ -45,6 +45,8 @@ struct thread *thread__new(pid_t pid, pid_t tid)
 		thread->cpu = -1;
 		INIT_LIST_HEAD(&thread->namespaces_list);
 		INIT_LIST_HEAD(&thread->comm_list);
+		pthread_mutex_init(&thread->namespaces_lock, NULL);
+		pthread_mutex_init(&thread->comm_lock, NULL);
 
 		comm_str = malloc(32);
 		if (!comm_str)
@@ -83,15 +85,21 @@ void thread__delete(struct thread *thread)
 		map_groups__put(thread->mg);
 		thread->mg = NULL;
 	}
+	pthread_mutex_lock(&thread->namespaces_lock);
 	list_for_each_entry_safe(namespaces, tmp_namespaces,
 				 &thread->namespaces_list, list) {
 		list_del(&namespaces->list);
 		namespaces__free(namespaces);
 	}
+	pthread_mutex_unlock(&thread->namespaces_lock);
+
+	pthread_mutex_lock(&thread->comm_lock);
 	list_for_each_entry_safe(comm, tmp_comm, &thread->comm_list, list) {
 		list_del(&comm->list);
 		comm__free(comm);
 	}
+	pthread_mutex_unlock(&thread->comm_lock);
+
 	unwind__finish_access(thread);
 	nsinfo__zput(thread->nsinfo);
 
@@ -128,11 +136,17 @@ struct namespaces *thread__namespaces(const struct thread *thread)
 int thread__set_namespaces(struct thread *thread, u64 timestamp,
 			   struct namespaces_event *event)
 {
-	struct namespaces *new, *curr = thread__namespaces(thread);
+	struct namespaces *new, *curr;
+
+	pthread_mutex_lock(&thread->namespaces_lock);
+
+	curr = thread__namespaces(thread);
 
 	new = namespaces__new(event);
-	if (!new)
+	if (!new) {
+		pthread_mutex_unlock(&thread->namespaces_lock);
 		return -ENOMEM;
+	}
 
 	list_add(&new->list, &thread->namespaces_list);
 
@@ -146,17 +160,21 @@ int thread__set_namespaces(struct thread *thread, u64 timestamp,
 		curr->end_time = timestamp;
 	}
 
+	pthread_mutex_unlock(&thread->namespaces_lock);
+
 	return 0;
 }
 
-void thread__namespaces_id(const struct thread *thread,
+void thread__namespaces_id(struct thread *thread,
 			   u64 *dev, u64 *ino)
 {
 	struct namespaces *ns;
 
+	pthread_mutex_lock(&thread->namespaces_lock);
 	ns = thread__namespaces(thread);
 	*dev = ns ? ns->link_info[CGROUP_NS_INDEX].dev : 0;
 	*ino = ns ? ns->link_info[CGROUP_NS_INDEX].ino : 0;
+	pthread_mutex_unlock(&thread->namespaces_lock);
 }
 
 struct comm *thread__comm(const struct thread *thread)
@@ -183,17 +201,23 @@ struct comm *thread__exec_comm(const struct thread *thread)
 int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
 		       bool exec)
 {
-	struct comm *new, *curr = thread__comm(thread);
+	struct comm *new, *curr;
+	int err = 0;
+
+	pthread_mutex_lock(&thread->comm_lock);
+	curr = thread__comm(thread);
 
 	/* Override the default :tid entry */
 	if (!thread->comm_set) {
-		int err = comm__override(curr, str, timestamp, exec);
+		err = comm__override(curr, str, timestamp, exec);
 		if (err)
-			return err;
+			goto unlock;
 	} else {
 		new = comm__new(str, timestamp, exec);
-		if (!new)
-			return -ENOMEM;
+		if (!new) {
+			err = -ENOMEM;
+			goto unlock;
+		}
 		list_add(&new->list, &thread->comm_list);
 
 		if (exec)
@@ -202,7 +226,9 @@ int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
 
 	thread->comm_set = true;
 
-	return 0;
+unlock:
+	pthread_mutex_unlock(&thread->comm_lock);
+	return err;
 }
 
 int thread__set_comm_from_proc(struct thread *thread)
@@ -222,14 +248,22 @@ int thread__set_comm_from_proc(struct thread *thread)
 	return err;
 }
 
-const char *thread__comm_str(const struct thread *thread)
+const char *thread__comm_str(struct thread *thread)
 {
-	const struct comm *comm = thread__comm(thread);
+	const struct comm *comm;
+	const char *str;
+
+	pthread_mutex_lock(&thread->comm_lock);
+	comm = thread__comm(thread);
 
-	if (!comm)
+	if (!comm) {
+		pthread_mutex_unlock(&thread->comm_lock);
 		return NULL;
+	}
+	str = comm__str(comm);
 
-	return comm__str(comm);
+	pthread_mutex_unlock(&thread->comm_lock);
+	return str;
 }
 
 /* CHECKME: it should probably better return the max comm len from its comm list */
diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h
index fd7bd41..1d3062a 100644
--- a/tools/perf/util/thread.h
+++ b/tools/perf/util/thread.h
@@ -29,7 +29,9 @@ struct thread {
 	int			comm_len;
 	bool			dead; /* if set thread has exited */
 	struct list_head	namespaces_list;
+	pthread_mutex_t		namespaces_lock;
 	struct list_head	comm_list;
+	pthread_mutex_t		comm_lock;
 	u64			db_id;
 
 	void			*priv;
@@ -68,7 +70,7 @@ static inline void thread__exited(struct thread *thread)
 struct namespaces *thread__namespaces(const struct thread *thread);
 int thread__set_namespaces(struct thread *thread, u64 timestamp,
 			   struct namespaces_event *event);
-void thread__namespaces_id(const struct thread *thread,
+void thread__namespaces_id(struct thread *thread,
 			   u64 *dev, u64 *ino);
 
 int __thread__set_comm(struct thread *thread, const char *comm, u64 timestamp,
@@ -84,7 +86,7 @@ int thread__set_comm_from_proc(struct thread *thread);
 int thread__comm_len(struct thread *thread);
 struct comm *thread__comm(const struct thread *thread);
 struct comm *thread__exec_comm(const struct thread *thread);
-const char *thread__comm_str(const struct thread *thread);
+const char *thread__comm_str(struct thread *thread);
 int thread__insert_map(struct thread *thread, struct map *map);
 int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp);
 size_t thread__fprintf(struct thread *thread, FILE *fp);
-- 
2.5.5

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH RFC V2 00/10] perf top optimization kan.liang@intel.com - 2017-09-11 04:30 +0200
  [PATCH RFC V2 10/10] perf top: switch back to overwrite mode kan.liang@intel.com - 2017-09-11 04:30 +0200
  [PATCH RFC V2 04/10] petf tools: introduce a new function to set namespaces id kan.liang@intel.com - 2017-09-11 04:30 +0200
  [PATCH RFC V2 06/10] perf tools: lock to protect comm_str rb tree kan.liang@intel.com - 2017-09-11 04:30 +0200
  [PATCH RFC V2 02/10] perf tools: using scandir to replace readdir kan.liang@intel.com - 2017-09-11 04:30 +0200
    Re: [PATCH RFC V2 02/10] perf tools: using scandir to replace readdir Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-09-13 17:30 +0200
  [PATCH RFC V2 03/10] petf tools: using comm_str to replace comm in hist_entry kan.liang@intel.com - 2017-09-11 04:30 +0200
    Re: [PATCH RFC V2 03/10] petf tools: using comm_str to replace comm  in hist_entry Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-09-13 17:30 +0200
      Re: [PATCH RFC V2 03/10] petf tools: using comm_str to replace comm  in hist_entry Jiri Olsa <jolsa@redhat.com> - 2017-09-18 10:40 +0200
  [PATCH RFC V2 01/10] perf tools: hashtable for machine threads kan.liang@intel.com - 2017-09-11 04:30 +0200
    Re: [PATCH RFC V2 01/10] perf tools: hashtable for machine threads Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-09-13 15:30 +0200
    [tip:perf/core] perf machine: Use hashtable for machine threads tip-bot for Kan Liang <tipbot@zytor.com> - 2017-09-22 18:50 +0200
  [PATCH RFC V2 08/10] perf top: implement multithreading for perf_event__synthesize_threads kan.liang@intel.com - 2017-09-11 04:30 +0200
    Re: [PATCH RFC V2 08/10] perf top: implement multithreading for  perf_event__synthesize_threads Jiri Olsa <jolsa@redhat.com> - 2017-09-18 13:30 +0200
  [PATCH RFC V2 07/10] perf tools: change machine comm_exec type to atomic kan.liang@intel.com - 2017-09-11 04:30 +0200
    Re: [PATCH RFC V2 07/10] perf tools: change machine comm_exec type  to atomic Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-09-13 17:30 +0200
      RE: [PATCH RFC V2 07/10] perf tools: change machine comm_exec type  to atomic "Liang, Kan" <kan.liang@intel.com> - 2017-09-15 22:10 +0200
        Re: [PATCH RFC V2 07/10] perf tools: change machine comm_exec type  to atomic Jiri Olsa <jolsa@redhat.com> - 2017-09-18 13:40 +0200
  [PATCH RFC V2 05/10] perf tools: lock to protect thread list kan.liang@intel.com - 2017-09-11 04:30 +0200
    Re: [PATCH RFC V2 05/10] perf tools: lock to protect thread list Jiri Olsa <jolsa@redhat.com> - 2017-09-18 11:00 +0200
      RE: [PATCH RFC V2 05/10] perf tools: lock to protect thread list "Liang, Kan" <kan.liang@intel.com> - 2017-09-18 18:20 +0200
  [PATCH RFC V2 09/10] perf top: add option to set the number of thread for event synthesize kan.liang@intel.com - 2017-09-11 04:30 +0200
  RE: [PATCH RFC V2 00/10] perf top optimization "Liang, Kan" <kan.liang@intel.com> - 2017-09-13 17:30 +0200
    Re: [PATCH RFC V2 00/10] perf top optimization Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-09-13 17:40 +0200
      Re: [PATCH RFC V2 00/10] perf top optimization Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-09-14 23:20 +0200
        RE: [PATCH RFC V2 00/10] perf top optimization "Liang, Kan" <kan.liang@intel.com> - 2017-09-15 17:20 +0200
          Re: [PATCH RFC V2 00/10] perf top optimization Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-09-15 19:30 +0200
            RE: [PATCH RFC V2 00/10] perf top optimization "Liang, Kan" <kan.liang@intel.com> - 2017-09-15 19:30 +0200
              Re: [PATCH RFC V2 00/10] perf top optimization Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-09-15 20:30 +0200
                RE: [PATCH RFC V2 00/10] perf top optimization "Liang, Kan" <kan.liang@intel.com> - 2017-09-15 20:30 +0200
  Re: [PATCH RFC V2 00/10] perf top optimization Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-09-13 17:30 +0200
  Re: [PATCH RFC V2 00/10] perf top optimization Jiri Olsa <jolsa@redhat.com> - 2017-09-18 11:00 +0200
    Re: [PATCH RFC V2 00/10] perf top optimization Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-09-18 15:10 +0200
      RE: [PATCH RFC V2 00/10] perf top optimization "Liang, Kan" <kan.liang@intel.com> - 2017-09-18 18:30 +0200
      Re: [PATCH RFC V2 00/10] perf top optimization Jiri Olsa <jolsa@redhat.com> - 2017-09-19 10:20 +0200
        RE: [PATCH RFC V2 00/10] perf top optimization "Liang, Kan" <kan.liang@intel.com> - 2017-09-19 14:50 +0200
          Re: [PATCH RFC V2 00/10] perf top optimization Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-09-19 16:30 +0200

csiph-web