From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: kan.liang@intel.com
Cc: peterz@infradead.org, mingo@redhat.com,
linux-kernel@vger.kernel.org, jolsa@kernel.org,
namhyung@kernel.org, adrian.hunter@intel.com,
lukasz.odzioba@intel.com, ak@linux.intel.com
Subject: Re: [PATCH RFC 06/10] perf tools: lock to protect comm_str rb tree
Date: Fri, 8 Sep 2017 11:27:45 -0300 [thread overview]
Message-ID: <20170908142745.GG11725@kernel.org> (raw)
In-Reply-To: <1504806954-150842-7-git-send-email-kan.liang@intel.com>
Em Thu, Sep 07, 2017 at 10:55:50AM -0700, kan.liang@intel.com escreveu:
> From: Kan Liang <kan.liang@intel.com>
>
> Add comm_str_lock to protect comm_str rb tree.
>
> Signed-off-by: Kan Liang <kan.liang@intel.com>
> ---
> tools/perf/util/comm.c | 18 ++++++++++++++----
> 1 file changed, 14 insertions(+), 4 deletions(-)
>
> diff --git a/tools/perf/util/comm.c b/tools/perf/util/comm.c
> index 7bc981b..1bdfef1 100644
> --- a/tools/perf/util/comm.c
> +++ b/tools/perf/util/comm.c
> @@ -5,6 +5,7 @@
> #include <stdio.h>
> #include <string.h>
> #include <linux/refcount.h>
> +#include <pthread.h>
>
> struct comm_str {
> char *str;
> @@ -14,6 +15,7 @@ struct comm_str {
>
> /* Should perhaps be moved to struct machine */
> static struct rb_root comm_str_root;
> +static pthread_mutex_t comm_str_lock = PTHREAD_MUTEX_INITIALIZER;
>
> static struct comm_str *comm_str__get(struct comm_str *cs)
> {
> @@ -24,11 +26,13 @@ static struct comm_str *comm_str__get(struct comm_str *cs)
>
> static void comm_str__put(struct comm_str *cs)
> {
> + pthread_mutex_lock(&comm_str_lock);
> if (cs && refcount_dec_and_test(&cs->refcnt)) {
> rb_erase(&cs->rb_node, &comm_str_root);
> zfree(&cs->str);
> free(cs);
> }
> + pthread_mutex_unlock(&comm_str_lock);
> }
The above should use a smaller locked section, i.e.:
static void comm_str__put(struct comm_str *cs)
{
if (cs && refcount_dec_and_test(&cs->refcnt)) {
+ pthread_mutex_lock(&comm_str_lock);
rb_erase(&cs->rb_node, &comm_str_root);
+ pthread_mutex_unlock(&comm_str_lock);
zfree(&cs->str);
free(cs);
}
}
> static struct comm_str *comm_str__alloc(const char *str)
> @@ -52,18 +56,22 @@ static struct comm_str *comm_str__alloc(const char *str)
>
> static struct comm_str *comm_str__findnew(const char *str, struct rb_root *root)
The usual way is to just rename the above to __comm_str__findnew(),
leaving it unlocked, and then add a locked wrapper:
static struct comm_str *comm_str__findnew(const char *str, struct rb_root *root)
{
struct comm_str *cs;
pthread_mutex_lock(&comm_str_lock);
cs = __comm_str__findnew(str, root);
pthread_mutex_unlock(&comm_str_lock);
return cs;
}
> {
> - struct rb_node **p = &root->rb_node;
> struct rb_node *parent = NULL;
> struct comm_str *iter, *new;
> + struct rb_node **p;
> int cmp;
>
> + pthread_mutex_lock(&comm_str_lock);
> + p = &root->rb_node;
> while (*p != NULL) {
> parent = *p;
> iter = rb_entry(parent, struct comm_str, rb_node);
>
> cmp = strcmp(str, iter->str);
> - if (!cmp)
> - return comm_str__get(iter);
> + if (!cmp) {
> + new = comm_str__get(iter);
> + goto unlock;
> + }
>
> if (cmp < 0)
> p = &(*p)->rb_left;
> @@ -73,11 +81,13 @@ static struct comm_str *comm_str__findnew(const char *str, struct rb_root *root)
>
> new = comm_str__alloc(str);
> if (!new)
> - return NULL;
> + goto unlock;
>
> rb_link_node(&new->rb_node, parent, p);
> rb_insert_color(&new->rb_node, root);
>
> +unlock:
> + pthread_mutex_unlock(&comm_str_lock);
> return new;
> }
>
> --
> 2.5.5
next prev parent reply other threads:[~2017-09-08 14:27 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-09-07 17:55 [PATCH RFC 00/10] perf top optimization kan.liang
2017-09-07 17:55 ` [PATCH RFC 01/10] perf tools: hashtable for machine threads kan.liang
2017-09-08 14:18 ` Arnaldo Carvalho de Melo
2017-09-07 17:55 ` [PATCH RFC 02/10] perf tools: using scandir to replace readdir kan.liang
2017-09-08 14:11 ` Arnaldo Carvalho de Melo
2017-09-22 16:35 ` [tip:perf/core] perf tools: Use scandir() to replace readdir() tip-bot for Kan Liang
2017-09-07 17:55 ` [PATCH RFC 03/10] petf tools: using comm_str to replace comm in hist_entry kan.liang
2017-09-07 17:55 ` [PATCH RFC 04/10] petf tools: introduce a new function to set namespaces id kan.liang
2017-09-07 17:55 ` [PATCH RFC 05/10] perf tools: lock to protect thread list kan.liang
2017-09-07 17:55 ` [PATCH RFC 06/10] perf tools: lock to protect comm_str rb tree kan.liang
2017-09-08 14:27 ` Arnaldo Carvalho de Melo [this message]
2017-09-07 17:55 ` [PATCH RFC 07/10] perf tools: change machine comm_exec type to atomic kan.liang
2017-09-07 17:55 ` [PATCH RFC 08/10] perf top: implement multithreading for perf_event__synthesize_threads kan.liang
2017-09-07 17:55 ` [PATCH RFC 09/10] perf top: add option to set the number of thread for event synthesize kan.liang
2017-09-07 17:55 ` [PATCH RFC 10/10] perf top: switch back to overwrite mode kan.liang
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=20170908142745.GG11725@kernel.org \
--to=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=ak@linux.intel.com \
--cc=jolsa@kernel.org \
--cc=kan.liang@intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=lukasz.odzioba@intel.com \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.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