Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3) #include <inttypes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4) #include "builtin.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5) #include "perf.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include "util/evlist.h" // for struct evsel_str_handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include "util/evsel.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include "util/symbol.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include "util/thread.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include "util/header.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <subcmd/pager.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <subcmd/parse-options.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include "util/trace-event.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include "util/debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include "util/session.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include "util/tool.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include "util/data.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <sys/prctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <semaphore.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <pthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <math.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <limits.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <linux/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <linux/zalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) static struct perf_session *session;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) /* based on kernel/lockdep.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #define LOCKHASH_BITS		12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #define LOCKHASH_SIZE		(1UL << LOCKHASH_BITS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) static struct list_head lockhash_table[LOCKHASH_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #define __lockhashfn(key)	hash_long((unsigned long)key, LOCKHASH_BITS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #define lockhashentry(key)	(lockhash_table + __lockhashfn((key)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) struct lock_stat {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 	struct list_head	hash_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 	struct rb_node		rb;		/* used for sorting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 	 * FIXME: evsel__intval() returns u64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 	 * so address of lockdep_map should be dealed as 64bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	 * Is there more better solution?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	void			*addr;		/* address of lockdep_map, used as ID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 	char			*name;		/* for strcpy(), we cannot use const */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	unsigned int		nr_acquire;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	unsigned int		nr_acquired;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	unsigned int		nr_contended;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	unsigned int		nr_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 	unsigned int		nr_readlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 	unsigned int		nr_trylock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 	/* these times are in nano sec. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 	u64                     avg_wait_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	u64			wait_time_total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	u64			wait_time_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	u64			wait_time_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	int			discard; /* flag of blacklist */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76)  * States of lock_seq_stat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78)  * UNINITIALIZED is required for detecting first event of acquire.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79)  * As the nature of lock events, there is no guarantee
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80)  * that the first event for the locks are acquire,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81)  * it can be acquired, contended or release.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) #define SEQ_STATE_UNINITIALIZED      0	       /* initial state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) #define SEQ_STATE_RELEASED	1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) #define SEQ_STATE_ACQUIRING	2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) #define SEQ_STATE_ACQUIRED	3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) #define SEQ_STATE_READ_ACQUIRED	4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) #define SEQ_STATE_CONTENDED	5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91)  * MAX_LOCK_DEPTH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92)  * Imported from include/linux/sched.h.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93)  * Should this be synchronized?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) #define MAX_LOCK_DEPTH 48
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98)  * struct lock_seq_stat:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99)  * Place to put on state of one lock sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100)  * 1) acquire -> acquired -> release
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101)  * 2) acquire -> contended -> acquired -> release
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102)  * 3) acquire (with read or try) -> release
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103)  * 4) Are there other patterns?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) struct lock_seq_stat {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	struct list_head        list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	int			state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	u64			prev_event_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	void                    *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	int                     read_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) struct thread_stat {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	struct rb_node		rb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	u32                     tid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	struct list_head        seq_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) static struct rb_root		thread_stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) static struct thread_stat *thread_stat_find(u32 tid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	struct thread_stat *st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	node = thread_stats.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	while (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 		st = container_of(node, struct thread_stat, rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 		if (st->tid == tid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 			return st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 		else if (tid < st->tid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 			node = node->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 			node = node->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) static void thread_stat_insert(struct thread_stat *new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	struct rb_node **rb = &thread_stats.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	struct rb_node *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	struct thread_stat *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	while (*rb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 		p = container_of(*rb, struct thread_stat, rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 		parent = *rb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 		if (new->tid < p->tid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 			rb = &(*rb)->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 		else if (new->tid > p->tid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 			rb = &(*rb)->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 			BUG_ON("inserting invalid thread_stat\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	rb_link_node(&new->rb, parent, rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	rb_insert_color(&new->rb, &thread_stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) static struct thread_stat *thread_stat_findnew_after_first(u32 tid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	struct thread_stat *st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	st = thread_stat_find(tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	if (st)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 		return st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	st = zalloc(sizeof(struct thread_stat));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	if (!st) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 		pr_err("memory allocation failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	st->tid = tid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	INIT_LIST_HEAD(&st->seq_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	thread_stat_insert(st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	return st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) static struct thread_stat *thread_stat_findnew_first(u32 tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) static struct thread_stat *(*thread_stat_findnew)(u32 tid) =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	thread_stat_findnew_first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) static struct thread_stat *thread_stat_findnew_first(u32 tid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	struct thread_stat *st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	st = zalloc(sizeof(struct thread_stat));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	if (!st) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 		pr_err("memory allocation failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	st->tid = tid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	INIT_LIST_HEAD(&st->seq_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	rb_link_node(&st->rb, NULL, &thread_stats.rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	rb_insert_color(&st->rb, &thread_stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	thread_stat_findnew = thread_stat_findnew_after_first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	return st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) /* build simple key function one is bigger than two */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) #define SINGLE_KEY(member)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 	static int lock_stat_key_ ## member(struct lock_stat *one,	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 					 struct lock_stat *two)		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	{								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 		return one->member > two->member;			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) SINGLE_KEY(nr_acquired)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) SINGLE_KEY(nr_contended)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) SINGLE_KEY(avg_wait_time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) SINGLE_KEY(wait_time_total)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) SINGLE_KEY(wait_time_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) static int lock_stat_key_wait_time_min(struct lock_stat *one,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 					struct lock_stat *two)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	u64 s1 = one->wait_time_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	u64 s2 = two->wait_time_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	if (s1 == ULLONG_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 		s1 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	if (s2 == ULLONG_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 		s2 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	return s1 > s2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) struct lock_key {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	 * name: the value for specify by user
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	 * this should be simpler than raw name of member
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	 * e.g. nr_acquired -> acquired, wait_time_total -> wait_total
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	const char		*name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	int			(*key)(struct lock_stat*, struct lock_stat*);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) static const char		*sort_key = "acquired";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) static int			(*compare)(struct lock_stat *, struct lock_stat *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) static struct rb_root		result;	/* place to store sorted data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) #define DEF_KEY_LOCK(name, fn_suffix)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	{ #name, lock_stat_key_ ## fn_suffix }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) struct lock_key keys[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	DEF_KEY_LOCK(acquired, nr_acquired),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	DEF_KEY_LOCK(contended, nr_contended),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	DEF_KEY_LOCK(avg_wait, avg_wait_time),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	DEF_KEY_LOCK(wait_total, wait_time_total),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	DEF_KEY_LOCK(wait_min, wait_time_min),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	DEF_KEY_LOCK(wait_max, wait_time_max),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	/* extra comparisons much complicated should be here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	{ NULL, NULL }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) static int select_key(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	for (i = 0; keys[i].name; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 		if (!strcmp(keys[i].name, sort_key)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 			compare = keys[i].key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	pr_err("Unknown compare key: %s\n", sort_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) static void insert_to_result(struct lock_stat *st,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 			     int (*bigger)(struct lock_stat *, struct lock_stat *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	struct rb_node **rb = &result.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	struct rb_node *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	struct lock_stat *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	while (*rb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 		p = container_of(*rb, struct lock_stat, rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 		parent = *rb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 		if (bigger(st, p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 			rb = &(*rb)->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 			rb = &(*rb)->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	rb_link_node(&st->rb, parent, rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	rb_insert_color(&st->rb, &result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) /* returns left most element of result, and erase it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) static struct lock_stat *pop_from_result(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	struct rb_node *node = result.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	while (node->rb_left)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 		node = node->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	rb_erase(node, &result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	return container_of(node, struct lock_stat, rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) static struct lock_stat *lock_stat_findnew(void *addr, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	struct list_head *entry = lockhashentry(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	struct lock_stat *ret, *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	list_for_each_entry(ret, entry, hash_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 		if (ret->addr == addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	new = zalloc(sizeof(struct lock_stat));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	if (!new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 		goto alloc_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	new->addr = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	new->name = zalloc(sizeof(char) * strlen(name) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	if (!new->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 		free(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 		goto alloc_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	strcpy(new->name, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	new->wait_time_min = ULLONG_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	list_add(&new->hash_entry, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	return new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) alloc_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	pr_err("memory allocation failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) struct trace_lock_handler {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	int (*acquire_event)(struct evsel *evsel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 			     struct perf_sample *sample);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	int (*acquired_event)(struct evsel *evsel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 			      struct perf_sample *sample);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	int (*contended_event)(struct evsel *evsel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 			       struct perf_sample *sample);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	int (*release_event)(struct evsel *evsel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 			     struct perf_sample *sample);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) static struct lock_seq_stat *get_seq(struct thread_stat *ts, void *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	struct lock_seq_stat *seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	list_for_each_entry(seq, &ts->seq_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 		if (seq->addr == addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 			return seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	seq = zalloc(sizeof(struct lock_seq_stat));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	if (!seq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 		pr_err("memory allocation failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	seq->state = SEQ_STATE_UNINITIALIZED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	seq->addr = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	list_add(&seq->list, &ts->seq_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	return seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) enum broken_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	BROKEN_ACQUIRE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	BROKEN_ACQUIRED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	BROKEN_CONTENDED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	BROKEN_RELEASE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	BROKEN_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) static int bad_hist[BROKEN_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) enum acquire_flags {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	TRY_LOCK = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	READ_LOCK = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) static int report_lock_acquire_event(struct evsel *evsel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 				     struct perf_sample *sample)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	void *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	struct lock_stat *ls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	struct thread_stat *ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	struct lock_seq_stat *seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	const char *name = evsel__strval(evsel, sample, "name");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	u64 tmp	 = evsel__intval(evsel, sample, "lockdep_addr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	int flag = evsel__intval(evsel, sample, "flags");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	memcpy(&addr, &tmp, sizeof(void *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	ls = lock_stat_findnew(addr, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	if (!ls)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	if (ls->discard)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	ts = thread_stat_findnew(sample->tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	if (!ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	seq = get_seq(ts, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	if (!seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	switch (seq->state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	case SEQ_STATE_UNINITIALIZED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	case SEQ_STATE_RELEASED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 		if (!flag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 			seq->state = SEQ_STATE_ACQUIRING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 			if (flag & TRY_LOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 				ls->nr_trylock++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 			if (flag & READ_LOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 				ls->nr_readlock++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 			seq->state = SEQ_STATE_READ_ACQUIRED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 			seq->read_count = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 			ls->nr_acquired++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	case SEQ_STATE_READ_ACQUIRED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 		if (flag & READ_LOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 			seq->read_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 			ls->nr_acquired++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 			goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 			goto broken;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	case SEQ_STATE_ACQUIRED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	case SEQ_STATE_ACQUIRING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	case SEQ_STATE_CONTENDED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) broken:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 		/* broken lock sequence, discard it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 		ls->discard = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 		bad_hist[BROKEN_ACQUIRE]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 		list_del_init(&seq->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 		free(seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 		BUG_ON("Unknown state of lock sequence found!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	ls->nr_acquire++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	seq->prev_event_time = sample->time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) static int report_lock_acquired_event(struct evsel *evsel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 				      struct perf_sample *sample)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	void *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	struct lock_stat *ls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	struct thread_stat *ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	struct lock_seq_stat *seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	u64 contended_term;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	const char *name = evsel__strval(evsel, sample, "name");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	u64 tmp = evsel__intval(evsel, sample, "lockdep_addr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	memcpy(&addr, &tmp, sizeof(void *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	ls = lock_stat_findnew(addr, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	if (!ls)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	if (ls->discard)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	ts = thread_stat_findnew(sample->tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	if (!ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	seq = get_seq(ts, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	if (!seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	switch (seq->state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	case SEQ_STATE_UNINITIALIZED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		/* orphan event, do nothing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	case SEQ_STATE_ACQUIRING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	case SEQ_STATE_CONTENDED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 		contended_term = sample->time - seq->prev_event_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 		ls->wait_time_total += contended_term;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 		if (contended_term < ls->wait_time_min)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 			ls->wait_time_min = contended_term;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		if (ls->wait_time_max < contended_term)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 			ls->wait_time_max = contended_term;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	case SEQ_STATE_RELEASED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	case SEQ_STATE_ACQUIRED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	case SEQ_STATE_READ_ACQUIRED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 		/* broken lock sequence, discard it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 		ls->discard = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 		bad_hist[BROKEN_ACQUIRED]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 		list_del_init(&seq->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 		free(seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 		BUG_ON("Unknown state of lock sequence found!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	seq->state = SEQ_STATE_ACQUIRED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	ls->nr_acquired++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	ls->avg_wait_time = ls->nr_contended ? ls->wait_time_total/ls->nr_contended : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	seq->prev_event_time = sample->time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) static int report_lock_contended_event(struct evsel *evsel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 				       struct perf_sample *sample)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	void *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	struct lock_stat *ls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	struct thread_stat *ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	struct lock_seq_stat *seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	const char *name = evsel__strval(evsel, sample, "name");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	u64 tmp = evsel__intval(evsel, sample, "lockdep_addr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	memcpy(&addr, &tmp, sizeof(void *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	ls = lock_stat_findnew(addr, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	if (!ls)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	if (ls->discard)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	ts = thread_stat_findnew(sample->tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	if (!ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	seq = get_seq(ts, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	if (!seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	switch (seq->state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	case SEQ_STATE_UNINITIALIZED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 		/* orphan event, do nothing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	case SEQ_STATE_ACQUIRING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	case SEQ_STATE_RELEASED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	case SEQ_STATE_ACQUIRED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	case SEQ_STATE_READ_ACQUIRED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	case SEQ_STATE_CONTENDED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		/* broken lock sequence, discard it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 		ls->discard = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 		bad_hist[BROKEN_CONTENDED]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 		list_del_init(&seq->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 		free(seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 		BUG_ON("Unknown state of lock sequence found!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	seq->state = SEQ_STATE_CONTENDED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	ls->nr_contended++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	ls->avg_wait_time = ls->wait_time_total/ls->nr_contended;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	seq->prev_event_time = sample->time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) static int report_lock_release_event(struct evsel *evsel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 				     struct perf_sample *sample)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	void *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	struct lock_stat *ls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	struct thread_stat *ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	struct lock_seq_stat *seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	const char *name = evsel__strval(evsel, sample, "name");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	u64 tmp = evsel__intval(evsel, sample, "lockdep_addr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	memcpy(&addr, &tmp, sizeof(void *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	ls = lock_stat_findnew(addr, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	if (!ls)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	if (ls->discard)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	ts = thread_stat_findnew(sample->tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	if (!ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	seq = get_seq(ts, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	if (!seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	switch (seq->state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	case SEQ_STATE_UNINITIALIZED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	case SEQ_STATE_ACQUIRED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	case SEQ_STATE_READ_ACQUIRED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 		seq->read_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 		BUG_ON(seq->read_count < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		if (seq->read_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 			ls->nr_release++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 			goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	case SEQ_STATE_ACQUIRING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	case SEQ_STATE_CONTENDED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	case SEQ_STATE_RELEASED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 		/* broken lock sequence, discard it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 		ls->discard = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 		bad_hist[BROKEN_RELEASE]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 		goto free_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 		BUG_ON("Unknown state of lock sequence found!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	ls->nr_release++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) free_seq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	list_del_init(&seq->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	free(seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) /* lock oriented handlers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) /* TODO: handlers for CPU oriented, thread oriented */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) static struct trace_lock_handler report_lock_ops  = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	.acquire_event		= report_lock_acquire_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	.acquired_event		= report_lock_acquired_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	.contended_event	= report_lock_contended_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	.release_event		= report_lock_release_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) static struct trace_lock_handler *trace_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) static int evsel__process_lock_acquire(struct evsel *evsel, struct perf_sample *sample)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	if (trace_handler->acquire_event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 		return trace_handler->acquire_event(evsel, sample);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) static int evsel__process_lock_acquired(struct evsel *evsel, struct perf_sample *sample)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	if (trace_handler->acquired_event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 		return trace_handler->acquired_event(evsel, sample);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) static int evsel__process_lock_contended(struct evsel *evsel, struct perf_sample *sample)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	if (trace_handler->contended_event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 		return trace_handler->contended_event(evsel, sample);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) static int evsel__process_lock_release(struct evsel *evsel, struct perf_sample *sample)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	if (trace_handler->release_event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 		return trace_handler->release_event(evsel, sample);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) static void print_bad_events(int bad, int total)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	/* Output for debug, this have to be removed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	const char *name[4] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 		{ "acquire", "acquired", "contended", "release" };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	pr_info("\n=== output for debug===\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	pr_info("bad: %d, total: %d\n", bad, total);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	pr_info("bad rate: %.2f %%\n", (double)bad / (double)total * 100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	pr_info("histogram of events caused bad sequence\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	for (i = 0; i < BROKEN_MAX; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 		pr_info(" %10s: %d\n", name[i], bad_hist[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) /* TODO: various way to print, coloring, nano or milli sec */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) static void print_result(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	struct lock_stat *st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	char cut_name[20];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	int bad, total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	pr_info("%20s ", "Name");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	pr_info("%10s ", "acquired");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	pr_info("%10s ", "contended");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	pr_info("%15s ", "avg wait (ns)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	pr_info("%15s ", "total wait (ns)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	pr_info("%15s ", "max wait (ns)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	pr_info("%15s ", "min wait (ns)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	pr_info("\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	bad = total = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	while ((st = pop_from_result())) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		total++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		if (st->discard) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 			bad++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 		bzero(cut_name, 20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		if (strlen(st->name) < 16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 			/* output raw name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 			pr_info("%20s ", st->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 			strncpy(cut_name, st->name, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 			cut_name[16] = '.';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 			cut_name[17] = '.';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 			cut_name[18] = '.';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 			cut_name[19] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 			/* cut off name for saving output style */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 			pr_info("%20s ", cut_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 		pr_info("%10u ", st->nr_acquired);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 		pr_info("%10u ", st->nr_contended);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 		pr_info("%15" PRIu64 " ", st->avg_wait_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		pr_info("%15" PRIu64 " ", st->wait_time_total);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		pr_info("%15" PRIu64 " ", st->wait_time_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 		pr_info("%15" PRIu64 " ", st->wait_time_min == ULLONG_MAX ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 		       0 : st->wait_time_min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		pr_info("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	print_bad_events(bad, total);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) static bool info_threads, info_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) static void dump_threads(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	struct thread_stat *st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	struct thread *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	pr_info("%10s: comm\n", "Thread ID");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	node = rb_first(&thread_stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	while (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		st = container_of(node, struct thread_stat, rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 		t = perf_session__findnew(session, st->tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 		pr_info("%10d: %s\n", st->tid, thread__comm_str(t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		node = rb_next(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		thread__put(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) static void dump_map(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	struct lock_stat *st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	pr_info("Address of instance: name of class\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	for (i = 0; i < LOCKHASH_SIZE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 		list_for_each_entry(st, &lockhash_table[i], hash_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 			pr_info(" %p: %s\n", st->addr, st->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) static int dump_info(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	if (info_threads)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 		dump_threads();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	else if (info_map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 		dump_map();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 		rc = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 		pr_err("Unknown type of information\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) typedef int (*tracepoint_handler)(struct evsel *evsel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 				  struct perf_sample *sample);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) static int process_sample_event(struct perf_tool *tool __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 				union perf_event *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 				struct perf_sample *sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 				struct evsel *evsel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 				struct machine *machine)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	struct thread *thread = machine__findnew_thread(machine, sample->pid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 							sample->tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	if (thread == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		pr_debug("problem processing %d event, skipping it.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 			event->header.type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	if (evsel->handler != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 		tracepoint_handler f = evsel->handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 		err = f(evsel, sample);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	thread__put(thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) static void sort_result(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	struct lock_stat *st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	for (i = 0; i < LOCKHASH_SIZE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 		list_for_each_entry(st, &lockhash_table[i], hash_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 			insert_to_result(st, compare);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) static const struct evsel_str_handler lock_tracepoints[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	{ "lock:lock_acquire",	 evsel__process_lock_acquire,   }, /* CONFIG_LOCKDEP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	{ "lock:lock_acquired",	 evsel__process_lock_acquired,  }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	{ "lock:lock_contended", evsel__process_lock_contended, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	{ "lock:lock_release",	 evsel__process_lock_release,   }, /* CONFIG_LOCKDEP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) static bool force;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) static int __cmd_report(bool display_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	struct perf_tool eops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 		.sample		 = process_sample_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 		.comm		 = perf_event__process_comm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 		.namespaces	 = perf_event__process_namespaces,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 		.ordered_events	 = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	struct perf_data data = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 		.path  = input_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		.mode  = PERF_DATA_MODE_READ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 		.force = force,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	session = perf_session__new(&data, false, &eops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	if (IS_ERR(session)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		pr_err("Initializing perf session failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		return PTR_ERR(session);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	symbol__init(&session->header.env);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	if (!perf_session__has_traces(session, "lock record"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		goto out_delete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	if (perf_session__set_tracepoints_handlers(session, lock_tracepoints)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		pr_err("Initializing perf session tracepoint handlers failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		goto out_delete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	if (select_key())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 		goto out_delete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	err = perf_session__process_events(session);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 		goto out_delete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	setup_pager();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	if (display_info) /* used for info subcommand */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 		err = dump_info();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 		sort_result();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 		print_result();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) out_delete:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	perf_session__delete(session);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) static int __cmd_record(int argc, const char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	const char *record_args[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 		"record", "-R", "-m", "1024", "-c", "1",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	unsigned int rec_argc, i, j, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	const char **rec_argv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	for (i = 0; i < ARRAY_SIZE(lock_tracepoints); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 		if (!is_valid_tracepoint(lock_tracepoints[i].name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 				pr_err("tracepoint %s is not enabled. "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 				       "Are CONFIG_LOCKDEP and CONFIG_LOCK_STAT enabled?\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 				       lock_tracepoints[i].name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 				return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	rec_argc = ARRAY_SIZE(record_args) + argc - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	/* factor of 2 is for -e in front of each tracepoint */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	rec_argc += 2 * ARRAY_SIZE(lock_tracepoints);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	rec_argv = calloc(rec_argc + 1, sizeof(char *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	if (!rec_argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	for (i = 0; i < ARRAY_SIZE(record_args); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 		rec_argv[i] = strdup(record_args[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	for (j = 0; j < ARRAY_SIZE(lock_tracepoints); j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 		rec_argv[i++] = "-e";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		rec_argv[i++] = strdup(lock_tracepoints[j].name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	for (j = 1; j < (unsigned int)argc; j++, i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 		rec_argv[i] = argv[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	BUG_ON(i != rec_argc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	ret = cmd_record(i, rec_argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	free(rec_argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) int cmd_lock(int argc, const char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	const struct option lock_options[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	OPT_STRING('i', "input", &input_name, "file", "input file name"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	OPT_INCR('v', "verbose", &verbose, "be more verbose (show symbol address, etc)"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, "dump raw trace in ASCII"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	OPT_END()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	const struct option info_options[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	OPT_BOOLEAN('t', "threads", &info_threads,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 		    "dump thread list in perf.data"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	OPT_BOOLEAN('m', "map", &info_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 		    "map of lock instances (address:name table)"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	OPT_PARENT(lock_options)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	const struct option report_options[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	OPT_STRING('k', "key", &sort_key, "acquired",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 		    "key for sorting (acquired / contended / avg_wait / wait_total / wait_max / wait_min)"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	/* TODO: type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	OPT_PARENT(lock_options)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	const char * const info_usage[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		"perf lock info [<options>]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	const char *const lock_subcommands[] = { "record", "report", "script",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 						 "info", NULL };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	const char *lock_usage[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 		NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	const char * const report_usage[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 		"perf lock report [<options>]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 		NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	for (i = 0; i < LOCKHASH_SIZE; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 		INIT_LIST_HEAD(lockhash_table + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	argc = parse_options_subcommand(argc, argv, lock_options, lock_subcommands,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 					lock_usage, PARSE_OPT_STOP_AT_NON_OPTION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	if (!argc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 		usage_with_options(lock_usage, lock_options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	if (!strncmp(argv[0], "rec", 3)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 		return __cmd_record(argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	} else if (!strncmp(argv[0], "report", 6)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 		trace_handler = &report_lock_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 		if (argc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 			argc = parse_options(argc, argv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 					     report_options, report_usage, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 			if (argc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 				usage_with_options(report_usage, report_options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 		rc = __cmd_report(false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	} else if (!strcmp(argv[0], "script")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		/* Aliased to 'perf script' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		return cmd_script(argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	} else if (!strcmp(argv[0], "info")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		if (argc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 			argc = parse_options(argc, argv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 					     info_options, info_usage, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 			if (argc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 				usage_with_options(info_usage, info_options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 		/* recycling report_lock_ops */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 		trace_handler = &report_lock_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 		rc = __cmd_report(true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 		usage_with_options(lock_usage, lock_options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) }