2022-07-30 04:07:55 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#ifndef PERF_LOCK_CONTENTION_H
|
|
|
|
#define PERF_LOCK_CONTENTION_H
|
|
|
|
|
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/rbtree.h>
|
|
|
|
|
perf lock contention: Add -Y/--type-filter option
The -Y/--type-filter option is to filter the result for specific lock
types only. It can accept comma-separated values. Note that it would
accept type names like one in the output. spinlock, mutex, rwsem:R and
so on.
For RW-variant lock types, it converts the name to the both variants.
In other words, "rwsem" is same as "rwsem:R,rwsem:W". Also note that
"mutex" has two different encoding - one for sleeping wait, another for
optimistic spinning. Add "mutex-spin" entry for the lock_type_table so
that we can add it for "mutex" under the table.
$ sudo ./perf lock record -a -- ./perf bench sched messaging
$ sudo ./perf lock con -E 5 -Y spinlock
contended total wait max wait avg wait type caller
802 1.26 ms 11.73 us 1.58 us spinlock __wake_up_common_lock+0x62
13 787.16 us 105.44 us 60.55 us spinlock remove_wait_queue+0x14
12 612.96 us 78.70 us 51.08 us spinlock prepare_to_wait+0x27
114 340.68 us 12.61 us 2.99 us spinlock try_to_wake_up+0x1f5
83 226.38 us 9.15 us 2.73 us spinlock folio_lruvec_lock_irqsave+0x5e
Committer notes:
Make get_type_flag() return UINT_MAX for error instad of -1UL, as that
function returns 'unsigned int' and we store the value on a 'unsigned
int' 'flags' variable which makes clang unhappy:
35 98.23 fedora:37 : FAIL clang version 15.0.6 (Fedora 15.0.6-1.fc37)
builtin-lock.c:2012:14: error: result of comparison of constant 18446744073709551615 with expression of type 'unsigned int' is always true [-Werror,-Wtautological-constant-out-of-range-compare]
if (flags != -1UL) {
~~~~~ ^ ~~~~
builtin-lock.c:2021:14: error: result of comparison of constant 18446744073709551615 with expression of type 'unsigned int' is always true [-Werror,-Wtautological-constant-out-of-range-compare]
if (flags != -1UL) {
~~~~~ ^ ~~~~
builtin-lock.c:2037:14: error: result of comparison of constant 18446744073709551615 with expression of type 'unsigned int' is always true [-Werror,-Wtautological-constant-out-of-range-compare]
if (flags != -1UL) {
~~~~~ ^ ~~~~
3 errors generated.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Blake Jones <blakejones@google.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Song Liu <song@kernel.org>
Cc: bpf@vger.kernel.org
Link: https://lore.kernel.org/r/20221219201732.460111-3-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2022-12-20 04:17:28 +08:00
|
|
|
struct lock_filter {
|
|
|
|
int nr_types;
|
2022-12-20 04:17:30 +08:00
|
|
|
int nr_addrs;
|
|
|
|
int nr_syms;
|
perf lock contention: Add -Y/--type-filter option
The -Y/--type-filter option is to filter the result for specific lock
types only. It can accept comma-separated values. Note that it would
accept type names like one in the output. spinlock, mutex, rwsem:R and
so on.
For RW-variant lock types, it converts the name to the both variants.
In other words, "rwsem" is same as "rwsem:R,rwsem:W". Also note that
"mutex" has two different encoding - one for sleeping wait, another for
optimistic spinning. Add "mutex-spin" entry for the lock_type_table so
that we can add it for "mutex" under the table.
$ sudo ./perf lock record -a -- ./perf bench sched messaging
$ sudo ./perf lock con -E 5 -Y spinlock
contended total wait max wait avg wait type caller
802 1.26 ms 11.73 us 1.58 us spinlock __wake_up_common_lock+0x62
13 787.16 us 105.44 us 60.55 us spinlock remove_wait_queue+0x14
12 612.96 us 78.70 us 51.08 us spinlock prepare_to_wait+0x27
114 340.68 us 12.61 us 2.99 us spinlock try_to_wake_up+0x1f5
83 226.38 us 9.15 us 2.73 us spinlock folio_lruvec_lock_irqsave+0x5e
Committer notes:
Make get_type_flag() return UINT_MAX for error instad of -1UL, as that
function returns 'unsigned int' and we store the value on a 'unsigned
int' 'flags' variable which makes clang unhappy:
35 98.23 fedora:37 : FAIL clang version 15.0.6 (Fedora 15.0.6-1.fc37)
builtin-lock.c:2012:14: error: result of comparison of constant 18446744073709551615 with expression of type 'unsigned int' is always true [-Werror,-Wtautological-constant-out-of-range-compare]
if (flags != -1UL) {
~~~~~ ^ ~~~~
builtin-lock.c:2021:14: error: result of comparison of constant 18446744073709551615 with expression of type 'unsigned int' is always true [-Werror,-Wtautological-constant-out-of-range-compare]
if (flags != -1UL) {
~~~~~ ^ ~~~~
builtin-lock.c:2037:14: error: result of comparison of constant 18446744073709551615 with expression of type 'unsigned int' is always true [-Werror,-Wtautological-constant-out-of-range-compare]
if (flags != -1UL) {
~~~~~ ^ ~~~~
3 errors generated.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Blake Jones <blakejones@google.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Song Liu <song@kernel.org>
Cc: bpf@vger.kernel.org
Link: https://lore.kernel.org/r/20221219201732.460111-3-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2022-12-20 04:17:28 +08:00
|
|
|
unsigned int *types;
|
2022-12-20 04:17:30 +08:00
|
|
|
unsigned long *addrs;
|
|
|
|
char **syms;
|
perf lock contention: Add -Y/--type-filter option
The -Y/--type-filter option is to filter the result for specific lock
types only. It can accept comma-separated values. Note that it would
accept type names like one in the output. spinlock, mutex, rwsem:R and
so on.
For RW-variant lock types, it converts the name to the both variants.
In other words, "rwsem" is same as "rwsem:R,rwsem:W". Also note that
"mutex" has two different encoding - one for sleeping wait, another for
optimistic spinning. Add "mutex-spin" entry for the lock_type_table so
that we can add it for "mutex" under the table.
$ sudo ./perf lock record -a -- ./perf bench sched messaging
$ sudo ./perf lock con -E 5 -Y spinlock
contended total wait max wait avg wait type caller
802 1.26 ms 11.73 us 1.58 us spinlock __wake_up_common_lock+0x62
13 787.16 us 105.44 us 60.55 us spinlock remove_wait_queue+0x14
12 612.96 us 78.70 us 51.08 us spinlock prepare_to_wait+0x27
114 340.68 us 12.61 us 2.99 us spinlock try_to_wake_up+0x1f5
83 226.38 us 9.15 us 2.73 us spinlock folio_lruvec_lock_irqsave+0x5e
Committer notes:
Make get_type_flag() return UINT_MAX for error instad of -1UL, as that
function returns 'unsigned int' and we store the value on a 'unsigned
int' 'flags' variable which makes clang unhappy:
35 98.23 fedora:37 : FAIL clang version 15.0.6 (Fedora 15.0.6-1.fc37)
builtin-lock.c:2012:14: error: result of comparison of constant 18446744073709551615 with expression of type 'unsigned int' is always true [-Werror,-Wtautological-constant-out-of-range-compare]
if (flags != -1UL) {
~~~~~ ^ ~~~~
builtin-lock.c:2021:14: error: result of comparison of constant 18446744073709551615 with expression of type 'unsigned int' is always true [-Werror,-Wtautological-constant-out-of-range-compare]
if (flags != -1UL) {
~~~~~ ^ ~~~~
builtin-lock.c:2037:14: error: result of comparison of constant 18446744073709551615 with expression of type 'unsigned int' is always true [-Werror,-Wtautological-constant-out-of-range-compare]
if (flags != -1UL) {
~~~~~ ^ ~~~~
3 errors generated.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Blake Jones <blakejones@google.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Song Liu <song@kernel.org>
Cc: bpf@vger.kernel.org
Link: https://lore.kernel.org/r/20221219201732.460111-3-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2022-12-20 04:17:28 +08:00
|
|
|
};
|
|
|
|
|
2022-07-30 04:07:55 +08:00
|
|
|
struct lock_stat {
|
|
|
|
struct hlist_node hash_entry;
|
|
|
|
struct rb_node rb; /* used for sorting */
|
|
|
|
|
|
|
|
u64 addr; /* address of lockdep_map, used as ID */
|
|
|
|
char *name; /* for strcpy(), we cannot use const */
|
2022-09-12 13:53:12 +08:00
|
|
|
u64 *callstack;
|
2022-07-30 04:07:55 +08:00
|
|
|
|
|
|
|
unsigned int nr_acquire;
|
|
|
|
unsigned int nr_acquired;
|
|
|
|
unsigned int nr_contended;
|
|
|
|
unsigned int nr_release;
|
|
|
|
|
|
|
|
union {
|
|
|
|
unsigned int nr_readlock;
|
|
|
|
unsigned int flags;
|
|
|
|
};
|
|
|
|
unsigned int nr_trylock;
|
|
|
|
|
|
|
|
/* these times are in nano sec. */
|
|
|
|
u64 avg_wait_time;
|
|
|
|
u64 wait_time_total;
|
|
|
|
u64 wait_time_min;
|
|
|
|
u64 wait_time_max;
|
|
|
|
|
|
|
|
int broken; /* flag of blacklist */
|
|
|
|
int combined;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* States of lock_seq_stat
|
|
|
|
*
|
|
|
|
* UNINITIALIZED is required for detecting first event of acquire.
|
|
|
|
* As the nature of lock events, there is no guarantee
|
|
|
|
* that the first event for the locks are acquire,
|
|
|
|
* it can be acquired, contended or release.
|
|
|
|
*/
|
|
|
|
#define SEQ_STATE_UNINITIALIZED 0 /* initial state */
|
|
|
|
#define SEQ_STATE_RELEASED 1
|
|
|
|
#define SEQ_STATE_ACQUIRING 2
|
|
|
|
#define SEQ_STATE_ACQUIRED 3
|
|
|
|
#define SEQ_STATE_READ_ACQUIRED 4
|
|
|
|
#define SEQ_STATE_CONTENDED 5
|
|
|
|
|
|
|
|
/*
|
|
|
|
* MAX_LOCK_DEPTH
|
|
|
|
* Imported from include/linux/sched.h.
|
|
|
|
* Should this be synchronized?
|
|
|
|
*/
|
|
|
|
#define MAX_LOCK_DEPTH 48
|
|
|
|
|
2023-02-03 10:13:23 +08:00
|
|
|
struct lock_stat *lock_stat_find(u64 addr);
|
|
|
|
struct lock_stat *lock_stat_findnew(u64 addr, const char *name, int flags);
|
|
|
|
|
2023-02-03 10:13:24 +08:00
|
|
|
bool match_callstack_filter(struct machine *machine, u64 *callstack);
|
|
|
|
|
2022-07-30 04:07:55 +08:00
|
|
|
/*
|
|
|
|
* struct lock_seq_stat:
|
|
|
|
* Place to put on state of one lock sequence
|
|
|
|
* 1) acquire -> acquired -> release
|
|
|
|
* 2) acquire -> contended -> acquired -> release
|
|
|
|
* 3) acquire (with read or try) -> release
|
|
|
|
* 4) Are there other patterns?
|
|
|
|
*/
|
|
|
|
struct lock_seq_stat {
|
|
|
|
struct list_head list;
|
|
|
|
int state;
|
|
|
|
u64 prev_event_time;
|
|
|
|
u64 addr;
|
|
|
|
|
|
|
|
int read_count;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct thread_stat {
|
|
|
|
struct rb_node rb;
|
|
|
|
|
|
|
|
u32 tid;
|
|
|
|
struct list_head seq_list;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* CONTENTION_STACK_DEPTH
|
|
|
|
* Number of stack trace entries to find callers
|
|
|
|
*/
|
|
|
|
#define CONTENTION_STACK_DEPTH 8
|
|
|
|
|
|
|
|
/*
|
|
|
|
* CONTENTION_STACK_SKIP
|
|
|
|
* Number of stack trace entries to skip when finding callers.
|
|
|
|
* The first few entries belong to the locking implementation itself.
|
|
|
|
*/
|
2022-10-29 02:01:28 +08:00
|
|
|
#define CONTENTION_STACK_SKIP 4
|
2022-07-30 04:07:55 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* flags for lock:contention_begin
|
|
|
|
* Imported from include/trace/events/lock.h.
|
|
|
|
*/
|
|
|
|
#define LCB_F_SPIN (1U << 0)
|
|
|
|
#define LCB_F_READ (1U << 1)
|
|
|
|
#define LCB_F_WRITE (1U << 2)
|
|
|
|
#define LCB_F_RT (1U << 3)
|
|
|
|
#define LCB_F_PERCPU (1U << 4)
|
|
|
|
#define LCB_F_MUTEX (1U << 5)
|
|
|
|
|
2022-07-30 04:07:56 +08:00
|
|
|
struct evlist;
|
2022-07-30 04:07:55 +08:00
|
|
|
struct machine;
|
2022-07-30 04:07:56 +08:00
|
|
|
struct target;
|
2022-07-30 04:07:55 +08:00
|
|
|
|
2023-03-28 06:57:11 +08:00
|
|
|
struct lock_contention_fails {
|
|
|
|
int task;
|
|
|
|
int stack;
|
|
|
|
int time;
|
|
|
|
};
|
|
|
|
|
2022-08-03 03:10:02 +08:00
|
|
|
struct lock_contention {
|
|
|
|
struct evlist *evlist;
|
|
|
|
struct target *target;
|
|
|
|
struct machine *machine;
|
|
|
|
struct hlist_head *result;
|
2022-12-20 04:17:29 +08:00
|
|
|
struct lock_filter *filters;
|
2023-03-28 06:57:11 +08:00
|
|
|
struct lock_contention_fails fails;
|
2022-08-03 03:10:03 +08:00
|
|
|
unsigned long map_nr_entries;
|
2022-09-12 13:53:13 +08:00
|
|
|
int max_stack;
|
|
|
|
int stack_skip;
|
2022-12-10 03:07:25 +08:00
|
|
|
int aggr_mode;
|
2023-02-07 08:24:02 +08:00
|
|
|
int owner;
|
2023-01-26 08:09:36 +08:00
|
|
|
bool save_callstack;
|
2022-08-03 03:10:02 +08:00
|
|
|
};
|
|
|
|
|
2022-07-30 04:07:55 +08:00
|
|
|
#ifdef HAVE_BPF_SKEL
|
|
|
|
|
2022-08-03 03:10:02 +08:00
|
|
|
int lock_contention_prepare(struct lock_contention *con);
|
2022-07-30 04:07:55 +08:00
|
|
|
int lock_contention_start(void);
|
|
|
|
int lock_contention_stop(void);
|
2022-08-03 03:10:02 +08:00
|
|
|
int lock_contention_read(struct lock_contention *con);
|
2022-07-30 04:07:55 +08:00
|
|
|
int lock_contention_finish(void);
|
|
|
|
|
|
|
|
#else /* !HAVE_BPF_SKEL */
|
|
|
|
|
2022-08-03 03:10:02 +08:00
|
|
|
static inline int lock_contention_prepare(struct lock_contention *con __maybe_unused)
|
2022-07-30 04:07:56 +08:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-07-30 04:07:55 +08:00
|
|
|
static inline int lock_contention_start(void) { return 0; }
|
|
|
|
static inline int lock_contention_stop(void) { return 0; }
|
|
|
|
static inline int lock_contention_finish(void) { return 0; }
|
|
|
|
|
2022-08-03 03:10:02 +08:00
|
|
|
static inline int lock_contention_read(struct lock_contention *con __maybe_unused)
|
2022-07-30 04:07:55 +08:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_BPF_SKEL */
|
|
|
|
|
|
|
|
#endif /* PERF_LOCK_CONTENTION_H */
|