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