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