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