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