Skip to contentSkip to conversation

perf lock: Fix parse_lock_type which only retrieve one lock flag

PATCH Superseded Chun-Tse Shao 2 participants 3 messages Nov 22, 2024 – Nov 26, 2024 perf events

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 perf lock: Fix parse_lock_type which only retrieve one lock flag

parse_lock_type can only add the first lock flag in lock_type_table given input str. For example, for Y rwlock, it only adds rwlock:R into this perf session. Another example is for -Y mutex, it only adds the mutex without LCB_F_SPIN flag. The patch fixes this issue, makes sure both rwlock:R and rwlock:W will be added with -Y rwlock, and so on.

Testing:

  $ ./perf lock con -ab -Y mutex,rwlock -- perf bench sched pipe
  # Running 'sched/pipe' benchmark:
  # Executed 1000000 pipe operations between two processes
       Total time: 8.425 [sec]

         8.425402 usecs/op
           118688 ops/sec

contended total wait max wait avg wait type caller

         194      1.68 ms     44.16 us      8.66 us        mutex   pipe_read+0x57
          10    423.03 us     44.27 us     42.30 us     rwlock:W   do_exit+0x365
          54    254.67 us     58.87 us      4.72 us        mutex   pipe_write+0x50
          21    146.64 us     11.54 us      6.98 us        mutex   pipe_read+0x282
          10    141.27 us     20.62 us     14.13 us     rwlock:W   release_task+0x6f
           5     58.92 us     16.37 us     11.78 us        mutex   do_epoll_wait+0x24d
           3     29.81 us     17.66 us      9.94 us        mutex   do_epoll_ctl+0x6c1
           4     26.82 us     11.02 us      6.70 us        mutex   do_epoll_wait+0x24d
           2     18.32 us     12.49 us      9.16 us     rwlock:W   do_epoll_wait+0x255
           1     11.34 us     11.34 us     11.34 us     rwlock:W   ep_done_scan+0x2d
           1     11.02 us     11.02 us     11.02 us     rwlock:R   mm_update_next_owner+0x4e
           1     10.60 us     10.60 us     10.60 us     rwlock:W   do_epoll_ctl+0xb65
           1      9.90 us      9.90 us      9.90 us     rwlock:W   do_exit+0x365
Fixes d783ea8f62c4 ("perf lock contention: Simplify parse_lock_type()")
---
 tools/perf/builtin-lock.c | 93 ++++++++++++++++++++++++++-------------
 1 file changed, 62 insertions(+), 31 deletions(-)
tools/perf/builtin-lock.c +62 -31
index 062e2b56a2ab..052dbf423efd 100644
@@ -15,6 +15,7 @@
 #include "util/lock-contention.h"
 #include "util/bpf_skel/lock_data.h"
 
+#include <string.h>
 #include <subcmd/pager.h>
 #include <subcmd/parse-options.h>
 #include "util/trace-event.h"
@@ -1575,8 +1576,13 @@ static void sort_result(void)
 
 static const struct {
 	unsigned int flags;
-	const char *str;
+	/* Name of the lock. */
 	const char *name;
+	/*
+	 * Name of the group this lock belongs to.
+	 * For example, both rwlock:R and rwlock:W belong to rwlock.
+	 */
+	const char *group;
 } lock_type_table[] = {
 	{ 0,				"semaphore",	"semaphore" },
 	{ LCB_F_SPIN,			"spinlock",	"spinlock" },
@@ -1591,42 +1597,38 @@ static const struct {
 	{ LCB_F_PERCPU | LCB_F_WRITE,	"pcpu-sem:W",	"percpu-rwsem" },
 	{ LCB_F_MUTEX,			"mutex",	"mutex" },
 	{ LCB_F_MUTEX | LCB_F_SPIN,	"mutex",	"mutex" },
-	/* alias for get_type_flag() */
-	{ LCB_F_MUTEX | LCB_F_SPIN,	"mutex-spin",	"mutex" },
 };
 
-static const char *get_type_str(unsigned int flags)
+static const char *get_type_name(unsigned int flags)
 {
 	flags &= LCB_F_MAX_FLAGS - 1;
 
 	for (unsigned int i = 0; i < ARRAY_SIZE(lock_type_table); i++) {
 		if (lock_type_table[i].flags == flags)
-			return lock_type_table[i].str;
+			return lock_type_table[i].name;
 	}
 	return "unknown";
 }
 
-static const char *get_type_name(unsigned int flags)
+static const char *get_type_group(unsigned int flags)
 {
 	flags &= LCB_F_MAX_FLAGS - 1;
 
 	for (unsigned int i = 0; i < ARRAY_SIZE(lock_type_table); i++) {
 		if (lock_type_table[i].flags == flags)
-			return lock_type_table[i].name;
+			return lock_type_table[i].group;
 	}
 	return "unknown";
 }
 
-static unsigned int get_type_flag(const char *str)
+static unsigned int get_type_flags_by_name(const char *name)
 {
 	for (unsigned int i = 0; i < ARRAY_SIZE(lock_type_table); i++) {
-		if (!strcmp(lock_type_table[i].name, str))
-			return lock_type_table[i].flags;
-	}
-	for (unsigned int i = 0; i < ARRAY_SIZE(lock_type_table); i++) {
-		if (!strcmp(lock_type_table[i].str, str))
+		if (!strcmp(lock_type_table[i].name, name))
 			return lock_type_table[i].flags;
 	}
+
+	pr_err("Unknown lock flags: %s\n", name);
 	return UINT_MAX;
 }
 
@@ -1732,7 +1734,8 @@ static void print_lock_stat_stdio(struct lock_contention *con, struct lock_stat
 
 	switch (aggr_mode) {
 	case LOCK_AGGR_CALLER:
-		fprintf(lock_output, "  %10s   %s\n", get_type_str(st->flags), st->name);
+		fprintf(lock_output, "  %10s   %s\n",
+			get_type_name(st->flags), st->name);
 		break;
 	case LOCK_AGGR_TASK:
 		pid = st->addr;
@@ -1742,7 +1745,7 @@ static void print_lock_stat_stdio(struct lock_contention *con, struct lock_stat
 		break;
 	case LOCK_AGGR_ADDR:
 		fprintf(lock_output, "  %016llx   %s (%s)\n", (unsigned long long)st->addr,
-			st->name, get_type_name(st->flags));
+			st->name, get_type_group(st->flags));
 		break;
 	case LOCK_AGGR_CGROUP:
 		fprintf(lock_output, "  %s\n", st->name);
@@ -1783,7 +1786,8 @@ static void print_lock_stat_csv(struct lock_contention *con, struct lock_stat *s
 
 	switch (aggr_mode) {
 	case LOCK_AGGR_CALLER:
-		fprintf(lock_output, "%s%s %s", get_type_str(st->flags), sep, st->name);
+		fprintf(lock_output, "%s%s %s",
+			get_type_name(st->flags), sep, st->name);
 		if (verbose <= 0)
 			fprintf(lock_output, "\n");
 		break;
@@ -1795,7 +1799,7 @@ static void print_lock_stat_csv(struct lock_contention *con, struct lock_stat *s
 		break;
 	case LOCK_AGGR_ADDR:
 		fprintf(lock_output, "%llx%s %s%s %s\n", (unsigned long long)st->addr, sep,
-			st->name, sep, get_type_name(st->flags));
+			st->name, sep, get_type_group(st->flags));
 		break;
 	case LOCK_AGGR_CGROUP:
 		fprintf(lock_output, "%s\n",st->name);
@@ -2338,41 +2342,68 @@ static bool add_lock_type(unsigned int flags)
 	unsigned int *tmp;
 
 	tmp = realloc(filters.types, (filters.nr_types + 1) * sizeof(*filters.types));
-	if (tmp == NULL)
+	if (tmp == NULL) {
+		pr_err("Failed to add lock flags: %u\n", flags);
 		return false;
+	}
 
 	tmp[filters.nr_types++] = flags;
 	filters.types = tmp;
 	return true;
 }
 
-static int parse_lock_type(const struct option *opt __maybe_unused, const char *str,
-			   int unset __maybe_unused)
+static int parse_lock_type(const struct option *opt __maybe_unused,
+			   const char *str, int unset __maybe_unused)
 {
 	char *s, *tmp, *tok;
-	int ret = 0;
 
 	s = strdup(str);
 	if (s == NULL)
 		return -1;
 
-	for (tok = strtok_r(s, ", ", &tmp); tok; tok = strtok_r(NULL, ", ", &tmp)) {
-		unsigned int flags = get_type_flag(tok);
+	for (tok = strtok_r(s, ", ", &tmp); tok;
+	     tok = strtok_r(NULL, ", ", &tmp)) {
+		bool found = false;
 
-		if (flags == -1U) {
-			pr_err("Unknown lock flags: %s\n", tok);
-			ret = -1;
-			break;
+		/* `tok` is a lock name if it contains ':'. */
+		if (strchr(tok, ':')) {
+			unsigned int flags = get_type_flags_by_name(tok);
+
+			if (flags == UINT_MAX || !add_lock_type(flags)) {
+				free(s);
+				return -1;
+			}
+			continue;
 		}
 
-		if (!add_lock_type(flags)) {
-			ret = -1;
-			break;
+		/* Otherwise look up flags by lock group */
+		/*
+		 * By documentation, `percpu-rwmem` should be `pcpu-sem`.
+		 * For backward compatibility, we replace pcpu-sem with percpu-rwmem.
+		 */
+		if (!strcmp(tok, "pcpu-sem"))
+			tok = (char *)"percpu-rwsem";
+
+		for (unsigned int i = 0; i < ARRAY_SIZE(lock_type_table); i++) {
+			if (!strcmp(lock_type_table[i].group, tok)) {
+				if (add_lock_type(lock_type_table[i].flags)) {
+					found = true;
+				} else {
+					free(s);
+					return -1;
+				}
+			}
+		}
+
+		if (!found) {
+			pr_err("Unknown lock flags: %s\n", tok);
+			free(s);
+			return -1;
 		}
 	}
 
 	free(s);
-	return ret;
+	return 0;
 }
 
 static bool add_lock_addr(unsigned long addr)
Signature · 1 line
2.47.0.371.ga323438b13-goog
maintainer
#1 lore raw

Hello,

Quoted 51 lines · Chun-Tse Shao

parse_lock_type can only add the first lock flag in lock_type_table given input str. For example, for Y rwlock, it only adds rwlock:R into this perf session. Another example is for -Y mutex, it only adds the mutex without LCB_F_SPIN flag. The patch fixes this issue, makes sure both rwlock:R and rwlock:W will be added with -Y rwlock, and so on.

Testing:

  $ ./perf lock con -ab -Y mutex,rwlock -- perf bench sched pipe
  # Running 'sched/pipe' benchmark:
  # Executed 1000000 pipe operations between two processes
       Total time: 8.425 [sec]

         8.425402 usecs/op
           118688 ops/sec

contended total wait max wait avg wait type caller

         194      1.68 ms     44.16 us      8.66 us        mutex   pipe_read+0x57
          10    423.03 us     44.27 us     42.30 us     rwlock:W   do_exit+0x365
          54    254.67 us     58.87 us      4.72 us        mutex   pipe_write+0x50
          21    146.64 us     11.54 us      6.98 us        mutex   pipe_read+0x282
          10    141.27 us     20.62 us     14.13 us     rwlock:W   release_task+0x6f
           5     58.92 us     16.37 us     11.78 us        mutex   do_epoll_wait+0x24d
           3     29.81 us     17.66 us      9.94 us        mutex   do_epoll_ctl+0x6c1
           4     26.82 us     11.02 us      6.70 us        mutex   do_epoll_wait+0x24d
           2     18.32 us     12.49 us      9.16 us     rwlock:W   do_epoll_wait+0x255
           1     11.34 us     11.34 us     11.34 us     rwlock:W   ep_done_scan+0x2d
           1     11.02 us     11.02 us     11.02 us     rwlock:R   mm_update_next_owner+0x4e
           1     10.60 us     10.60 us     10.60 us     rwlock:W   do_epoll_ctl+0xb65
           1      9.90 us      9.90 us      9.90 us     rwlock:W   do_exit+0x365
Fixes d783ea8f62c4 ("perf lock contention: Simplify parse_lock_type()")
---
 tools/perf/builtin-lock.c | 93 ++++++++++++++++++++++++++-------------
 1 file changed, 62 insertions(+), 31 deletions(-)
tools/perf/builtin-lock.c +1 -0
index 062e2b56a2ab..052dbf423efd 100644
@@ -15,6 +15,7 @@
 #include "util/lock-contention.h"
 #include "util/bpf_skel/lock_data.h"
 
+#include <string.h>
 #include <subcmd/pager.h>
 #include <subcmd/parse-options.h>
 #include "util/trace-event.h"

tools/perf/builtin-lock.c static void sort_result(void) · line 1580

Quoted 6 lines
tools/perf/builtin-lock.c +1 -1
@@ -1575,8 +1576,13 @@ static void sort_result(void)
 
 static const struct {
 	unsigned int flags;
-	const char *str;
+	/* Name of the lock. */
 	const char *name;

I think this is the name of flag or access - read or write.

tools/perf/builtin-lock.c static void sort_result(void) · line 1585

Quoted 5 lines
tools/perf/builtin-lock.c +5 -0
+	/*
+	 * Name of the group this lock belongs to.
+	 * For example, both rwlock:R and rwlock:W belong to rwlock.
+	 */
+	const char *group;

And this is the name of the lock (type) - rwlock or rwsem.

Quoted 41 lines
tools/perf/builtin-lock.c +5 -7
 } lock_type_table[] = {
 	{ 0,				"semaphore",	"semaphore" },
 	{ LCB_F_SPIN,			"spinlock",	"spinlock" },
@@ -1591,42 +1597,38 @@ static const struct {
 	{ LCB_F_PERCPU | LCB_F_WRITE,	"pcpu-sem:W",	"percpu-rwsem" },
 	{ LCB_F_MUTEX,			"mutex",	"mutex" },
 	{ LCB_F_MUTEX | LCB_F_SPIN,	"mutex",	"mutex" },
-	/* alias for get_type_flag() */
-	{ LCB_F_MUTEX | LCB_F_SPIN,	"mutex-spin",	"mutex" },
 };
 
-static const char *get_type_str(unsigned int flags)
+static const char *get_type_name(unsigned int flags)
 {
 	flags &= LCB_F_MAX_FLAGS - 1;
 
 	for (unsigned int i = 0; i < ARRAY_SIZE(lock_type_table); i++) {
 		if (lock_type_table[i].flags == flags)
-			return lock_type_table[i].str;
+			return lock_type_table[i].name;
 	}
 	return "unknown";
 }
 
-static const char *get_type_name(unsigned int flags)
+static const char *get_type_group(unsigned int flags)
 {
 	flags &= LCB_F_MAX_FLAGS - 1;
 
 	for (unsigned int i = 0; i < ARRAY_SIZE(lock_type_table); i++) {
 		if (lock_type_table[i].flags == flags)
-			return lock_type_table[i].name;
+			return lock_type_table[i].group;
 	}
 	return "unknown";
 }
 
-static unsigned int get_type_flag(const char *str)
+static unsigned int get_type_flags_by_name(const char *name)
 {
 	for (unsigned int i = 0; i < ARRAY_SIZE(lock_type_table); i++) {

tools/perf/builtin-lock.c static const struct { · line 1631

Quoted 10 lines
tools/perf/builtin-lock.c +3 -5
-		if (!strcmp(lock_type_table[i].name, str))
-			return lock_type_table[i].flags;
-	}
-	for (unsigned int i = 0; i < ARRAY_SIZE(lock_type_table); i++) {
-		if (!strcmp(lock_type_table[i].str, str))
+		if (!strcmp(lock_type_table[i].name, name))
 			return lock_type_table[i].flags;
 	}
+
+	pr_err("Unknown lock flags: %s\n", name);

I'm not sure if there will be another caller, but it seems better to have this error message in the parse function directly.

Quoted 62 lines
tools/perf/builtin-lock.c +11 -7
 	return UINT_MAX;
 }
 
@@ -1732,7 +1734,8 @@ static void print_lock_stat_stdio(struct lock_contention *con, struct lock_stat
 
 	switch (aggr_mode) {
 	case LOCK_AGGR_CALLER:
-		fprintf(lock_output, "  %10s   %s\n", get_type_str(st->flags), st->name);
+		fprintf(lock_output, "  %10s   %s\n",
+			get_type_name(st->flags), st->name);
 		break;
 	case LOCK_AGGR_TASK:
 		pid = st->addr;
@@ -1742,7 +1745,7 @@ static void print_lock_stat_stdio(struct lock_contention *con, struct lock_stat
 		break;
 	case LOCK_AGGR_ADDR:
 		fprintf(lock_output, "  %016llx   %s (%s)\n", (unsigned long long)st->addr,
-			st->name, get_type_name(st->flags));
+			st->name, get_type_group(st->flags));
 		break;
 	case LOCK_AGGR_CGROUP:
 		fprintf(lock_output, "  %s\n", st->name);
@@ -1783,7 +1786,8 @@ static void print_lock_stat_csv(struct lock_contention *con, struct lock_stat *s
 
 	switch (aggr_mode) {
 	case LOCK_AGGR_CALLER:
-		fprintf(lock_output, "%s%s %s", get_type_str(st->flags), sep, st->name);
+		fprintf(lock_output, "%s%s %s",
+			get_type_name(st->flags), sep, st->name);
 		if (verbose <= 0)
 			fprintf(lock_output, "\n");
 		break;
@@ -1795,7 +1799,7 @@ static void print_lock_stat_csv(struct lock_contention *con, struct lock_stat *s
 		break;
 	case LOCK_AGGR_ADDR:
 		fprintf(lock_output, "%llx%s %s%s %s\n", (unsigned long long)st->addr, sep,
-			st->name, sep, get_type_name(st->flags));
+			st->name, sep, get_type_group(st->flags));
 		break;
 	case LOCK_AGGR_CGROUP:
 		fprintf(lock_output, "%s\n",st->name);
@@ -2338,41 +2342,68 @@ static bool add_lock_type(unsigned int flags)
 	unsigned int *tmp;
 
 	tmp = realloc(filters.types, (filters.nr_types + 1) * sizeof(*filters.types));
-	if (tmp == NULL)
+	if (tmp == NULL) {
+		pr_err("Failed to add lock flags: %u\n", flags);
 		return false;
+	}
 
 	tmp[filters.nr_types++] = flags;
 	filters.types = tmp;
 	return true;
 }
 
-static int parse_lock_type(const struct option *opt __maybe_unused, const char *str,
-			   int unset __maybe_unused)
+static int parse_lock_type(const struct option *opt __maybe_unused,
+			   const char *str, int unset __maybe_unused)
 {
 	char *s, *tmp, *tok;

tools/perf/builtin-lock.c static bool add_lock_type(unsigned int flags) · line 2365

Quoted 10 lines
tools/perf/builtin-lock.c +2 -3
-	int ret = 0;
 
 	s = strdup(str);
 	if (s == NULL)
 		return -1;
 
-	for (tok = strtok_r(s, ", ", &tmp); tok; tok = strtok_r(NULL, ", ", &tmp)) {
-		unsigned int flags = get_type_flag(tok);
+	for (tok = strtok_r(s, ", ", &tmp); tok;
+	     tok = strtok_r(NULL, ", ", &tmp)) {

I think you changed this because of the 80 column limit. But I think it's recently increased to 100 so you don't need to change this line uncessarily.

Other lines containing actual changes may be accceptable. It's up to you but I generally prefer smaller changes.

Quoted 17 lines
tools/perf/builtin-lock.c +10 -4
+		bool found = false;
 
-		if (flags == -1U) {
-			pr_err("Unknown lock flags: %s\n", tok);
-			ret = -1;
-			break;
+		/* `tok` is a lock name if it contains ':'. */
+		if (strchr(tok, ':')) {
+			unsigned int flags = get_type_flags_by_name(tok);
+
+			if (flags == UINT_MAX || !add_lock_type(flags)) {
+				free(s);
+				return -1;
+			}
+			continue;
 		}
 

tools/perf/builtin-lock.c static bool add_lock_type(unsigned int flags) · line 2385

Quoted 10 lines
tools/perf/builtin-lock.c +7 -3
-		if (!add_lock_type(flags)) {
-			ret = -1;
-			break;
+		/* Otherwise look up flags by lock group */
+		/*
+		 * By documentation, `percpu-rwmem` should be `pcpu-sem`.
+		 * For backward compatibility, we replace pcpu-sem with percpu-rwmem.
+		 */
+		if (!strcmp(tok, "pcpu-sem"))
+			tok = (char *)"percpu-rwsem";

Can you please split this into a separate fix?

Thanks,
Namhyung

Quoted 28 lines
tools/perf/builtin-lock.c +17 -1
+
+		for (unsigned int i = 0; i < ARRAY_SIZE(lock_type_table); i++) {
+			if (!strcmp(lock_type_table[i].group, tok)) {
+				if (add_lock_type(lock_type_table[i].flags)) {
+					found = true;
+				} else {
+					free(s);
+					return -1;
+				}
+			}
+		}
+
+		if (!found) {
+			pr_err("Unknown lock flags: %s\n", tok);
+			free(s);
+			return -1;
 		}
 	}
 
 	free(s);
-	return ret;
+	return 0;
 }
 
 static bool add_lock_addr(unsigned long addr)
Signature · 1 line
2.47.0.371.ga323438b13-goog
#2 lore raw

Thanks Namhyung! please check my v3 patches:
https://lore.kernel.org/all/[email protected]/

Quoted 56 lines · Namhyung Kim

Hello,

Quoted 52 lines · Chun-Tse Shao

parse_lock_type can only add the first lock flag in lock_type_table given input str. For example, for Y rwlock, it only adds rwlock:R into this perf session. Another example is for -Y mutex, it only adds the mutex without LCB_F_SPIN flag. The patch fixes this issue, makes sure both rwlock:R and rwlock:W will be added with -Y rwlock, and so on.

Testing:

  $ ./perf lock con -ab -Y mutex,rwlock -- perf bench sched pipe
  # Running 'sched/pipe' benchmark:
  # Executed 1000000 pipe operations between two processes
       Total time: 8.425 [sec]

         8.425402 usecs/op
           118688 ops/sec

contended total wait max wait avg wait type caller

         194      1.68 ms     44.16 us      8.66 us        mutex   pipe_read+0x57
          10    423.03 us     44.27 us     42.30 us     rwlock:W   do_exit+0x365
          54    254.67 us     58.87 us      4.72 us        mutex   pipe_write+0x50
          21    146.64 us     11.54 us      6.98 us        mutex   pipe_read+0x282
          10    141.27 us     20.62 us     14.13 us     rwlock:W   release_task+0x6f
           5     58.92 us     16.37 us     11.78 us        mutex   do_epoll_wait+0x24d
           3     29.81 us     17.66 us      9.94 us        mutex   do_epoll_ctl+0x6c1
           4     26.82 us     11.02 us      6.70 us        mutex   do_epoll_wait+0x24d
           2     18.32 us     12.49 us      9.16 us     rwlock:W   do_epoll_wait+0x255
           1     11.34 us     11.34 us     11.34 us     rwlock:W   ep_done_scan+0x2d
           1     11.02 us     11.02 us     11.02 us     rwlock:R   mm_update_next_owner+0x4e
           1     10.60 us     10.60 us     10.60 us     rwlock:W   do_epoll_ctl+0xb65
           1      9.90 us      9.90 us      9.90 us     rwlock:W   do_exit+0x365
Fixes d783ea8f62c4 ("perf lock contention: Simplify parse_lock_type()")
---
 tools/perf/builtin-lock.c | 93 ++++++++++++++++++++++++++-------------
 1 file changed, 62 insertions(+), 31 deletions(-)
tools/perf/builtin-lock.c +1 -0
index 062e2b56a2ab..052dbf423efd 100644
@@ -15,6 +15,7 @@
 #include "util/lock-contention.h"
 #include "util/bpf_skel/lock_data.h"
 
+#include <string.h>
 #include <subcmd/pager.h>
 #include <subcmd/parse-options.h>
 #include "util/trace-event.h"
@@ -1575,8 +1576,13 @@ static void sort_result(void)
 
Quoted 8 lines

tools/perf/builtin-lock.c static void sort_result(void) · line 1580

Quoted 5 lines
tools/perf/builtin-lock.c +1 -1
 static const struct {
      unsigned int flags;
-     const char *str;
+     /* Name of the lock. */
      const char *name;

I think this is the name of flag or access - read or write.

Rename to flags_name.

Quoted 9 lines

tools/perf/builtin-lock.c static void sort_result(void) · line 1585

Quoted 6 lines
tools/perf/builtin-lock.c +5 -0
+     /*
+      * Name of the group this lock belongs to.
+      * For example, both rwlock:R and rwlock:W belong to rwlock.
+      */
+     const char *group;

And this is the name of the lock (type) - rwlock or rwsem.

Rename to lock_name.

Quoted 47 lines
Quoted 47 lines
tools/perf/builtin-lock.c +5 -12
 } lock_type_table[] = {
      { 0,                            "semaphore",    "semaphore" },
      { LCB_F_SPIN,                   "spinlock",     "spinlock" },
@@ -1591,42 +1597,38 @@ static const struct {
      { LCB_F_PERCPU | LCB_F_WRITE,   "pcpu-sem:W",   "percpu-rwsem" },
      { LCB_F_MUTEX,                  "mutex",        "mutex" },
      { LCB_F_MUTEX | LCB_F_SPIN,     "mutex",        "mutex" },
-     /* alias for get_type_flag() */
-     { LCB_F_MUTEX | LCB_F_SPIN,     "mutex-spin",   "mutex" },
 };
 
-static const char *get_type_str(unsigned int flags)
+static const char *get_type_name(unsigned int flags)
 {
      flags &= LCB_F_MAX_FLAGS - 1;
 
      for (unsigned int i = 0; i < ARRAY_SIZE(lock_type_table); i++) {
              if (lock_type_table[i].flags == flags)
-                     return lock_type_table[i].str;
+                     return lock_type_table[i].name;
      }
      return "unknown";
 }
 
-static const char *get_type_name(unsigned int flags)
+static const char *get_type_group(unsigned int flags)
 {
      flags &= LCB_F_MAX_FLAGS - 1;
 
      for (unsigned int i = 0; i < ARRAY_SIZE(lock_type_table); i++) {
              if (lock_type_table[i].flags == flags)
-                     return lock_type_table[i].name;
+                     return lock_type_table[i].group;
      }
      return "unknown";
 }
 
-static unsigned int get_type_flag(const char *str)
+static unsigned int get_type_flags_by_name(const char *name)
 {
      for (unsigned int i = 0; i < ARRAY_SIZE(lock_type_table); i++) {
-             if (!strcmp(lock_type_table[i].name, str))
-                     return lock_type_table[i].flags;
-     }
-     for (unsigned int i = 0; i < ARRAY_SIZE(lock_type_table); i++) {
-             if (!strcmp(lock_type_table[i].str, str))
Quoted 9 lines

tools/perf/builtin-lock.c static const struct { · line 1631

Quoted 5 lines
tools/perf/builtin-lock.c +3 -0
+             if (!strcmp(lock_type_table[i].name, name))
                      return lock_type_table[i].flags;
      }
+
+     pr_err("Unknown lock flags: %s\n", name);

I'm not sure if there will be another caller, but it seems better to have this error message in the parse function directly.

Done. I removed this function since imo it did not help readability, and was only used for once.

Quoted 78 lines
Quoted 63 lines
tools/perf/builtin-lock.c +11 -7
      return UINT_MAX;
 }
 
@@ -1732,7 +1734,8 @@ static void print_lock_stat_stdio(struct lock_contention *con, struct lock_stat
 
      switch (aggr_mode) {
      case LOCK_AGGR_CALLER:
-             fprintf(lock_output, "  %10s   %s\n", get_type_str(st->flags), st->name);
+             fprintf(lock_output, "  %10s   %s\n",
+                     get_type_name(st->flags), st->name);
              break;
      case LOCK_AGGR_TASK:
              pid = st->addr;
@@ -1742,7 +1745,7 @@ static void print_lock_stat_stdio(struct lock_contention *con, struct lock_stat
              break;
      case LOCK_AGGR_ADDR:
              fprintf(lock_output, "  %016llx   %s (%s)\n", (unsigned long long)st->addr,
-                     st->name, get_type_name(st->flags));
+                     st->name, get_type_group(st->flags));
              break;
      case LOCK_AGGR_CGROUP:
              fprintf(lock_output, "  %s\n", st->name);
@@ -1783,7 +1786,8 @@ static void print_lock_stat_csv(struct lock_contention *con, struct lock_stat *s
 
      switch (aggr_mode) {
      case LOCK_AGGR_CALLER:
-             fprintf(lock_output, "%s%s %s", get_type_str(st->flags), sep, st->name);
+             fprintf(lock_output, "%s%s %s",
+                     get_type_name(st->flags), sep, st->name);
              if (verbose <= 0)
                      fprintf(lock_output, "\n");
              break;
@@ -1795,7 +1799,7 @@ static void print_lock_stat_csv(struct lock_contention *con, struct lock_stat *s
              break;
      case LOCK_AGGR_ADDR:
              fprintf(lock_output, "%llx%s %s%s %s\n", (unsigned long long)st->addr, sep,
-                     st->name, sep, get_type_name(st->flags));
+                     st->name, sep, get_type_group(st->flags));
              break;
      case LOCK_AGGR_CGROUP:
              fprintf(lock_output, "%s\n",st->name);
@@ -2338,41 +2342,68 @@ static bool add_lock_type(unsigned int flags)
      unsigned int *tmp;
 
      tmp = realloc(filters.types, (filters.nr_types + 1) * sizeof(*filters.types));
-     if (tmp == NULL)
+     if (tmp == NULL) {
+             pr_err("Failed to add lock flags: %u\n", flags);
              return false;
+     }
 
      tmp[filters.nr_types++] = flags;
      filters.types = tmp;
      return true;
 }
 
-static int parse_lock_type(const struct option *opt __maybe_unused, const char *str,
-                        int unset __maybe_unused)
+static int parse_lock_type(const struct option *opt __maybe_unused,
+                        const char *str, int unset __maybe_unused)
 {
      char *s, *tmp, *tok;

tools/perf/builtin-lock.c static bool add_lock_type(unsigned int flags) · line 2365

Quoted 10 lines
tools/perf/builtin-lock.c +2 -3
-     int ret = 0;
 
      s = strdup(str);
      if (s == NULL)
              return -1;
 
-     for (tok = strtok_r(s, ", ", &tmp); tok; tok = strtok_r(NULL, ", ", &tmp)) {
-             unsigned int flags = get_type_flag(tok);
+     for (tok = strtok_r(s, ", ", &tmp); tok;
+          tok = strtok_r(NULL, ", ", &tmp)) {

I think you changed this because of the 80 column limit. But I think it's recently increased to 100 so you don't need to change this line uncessarily.

Quoted 3 lines

Other lines containing actual changes may be accceptable. It's up to you but I generally prefer smaller changes.

Done.

Quoted 29 lines
Quoted 18 lines
tools/perf/builtin-lock.c +10 -4
+             bool found = false;
 
-             if (flags == -1U) {
-                     pr_err("Unknown lock flags: %s\n", tok);
-                     ret = -1;
-                     break;
+             /* `tok` is a lock name if it contains ':'. */
+             if (strchr(tok, ':')) {
+                     unsigned int flags = get_type_flags_by_name(tok);
+
+                     if (flags == UINT_MAX || !add_lock_type(flags)) {
+                             free(s);
+                             return -1;
+                     }
+                     continue;
              }
 

tools/perf/builtin-lock.c static bool add_lock_type(unsigned int flags) · line 2385

Quoted 10 lines
tools/perf/builtin-lock.c +7 -3
-             if (!add_lock_type(flags)) {
-                     ret = -1;
-                     break;
+             /* Otherwise look up flags by lock group */
+             /*
+              * By documentation, `percpu-rwmem` should be `pcpu-sem`.
+              * For backward compatibility, we replace pcpu-sem with percpu-rwmem.
+              */
+             if (!strcmp(tok, "pcpu-sem"))
+                     tok = (char *)"percpu-rwsem";
Quoted 2 lines

Can you please split this into a separate fix?

Done. Now I have two patches, the first one is for renaming, the second one is for parsing logic.

Quoted 31 lines

Thanks,
Namhyung

Quoted 28 lines
tools/perf/builtin-lock.c +17 -2
+
+             for (unsigned int i = 0; i < ARRAY_SIZE(lock_type_table); i++) {
+                     if (!strcmp(lock_type_table[i].group, tok)) {
+                             if (add_lock_type(lock_type_table[i].flags)) {
+                                     found = true;
+                             } else {
+                                     free(s);
+                                     return -1;
+                             }
+                     }
+             }
+
+             if (!found) {
+                     pr_err("Unknown lock flags: %s\n", tok);
+                     free(s);
+                     return -1;
              }
      }
 
      free(s);
-     return ret;
+     return 0;
 }
 
 static bool add_lock_addr(unsigned long addr)
--

2.47.0.371.ga323438b13-goog