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