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