Skip to content

Commit 9664567

Browse files
Peter ZijlstraLinus Torvalds
authored andcommitted
lockstat: measure lock bouncing
__acquire | lock _____ | \ | __contended | | | wait | _______/ |/ | __acquired | __release | unlock We measure acquisition and contention bouncing. This is done by recording a cpu stamp in each lock instance. Contention bouncing requires the cpu stamp to be set on acquisition. Hence we move __acquired into the generic path. __acquired is then used to measure acquisition bouncing by comparing the current cpu with the old stamp before replacing it. __contended is used to measure contention bouncing (only useful for preemptable locks) [akpm@linux-foundation.org: cleanups] Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl> Acked-by: Ingo Molnar <mingo@elte.hu> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 443aef0 commit 9664567

4 files changed

Lines changed: 55 additions & 21 deletions

File tree

‎include/linux/lockdep.h‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,24 @@ struct lock_time {
130130
unsigned long nr;
131131
};
132132

133+
enum bounce_type {
134+
bounce_acquired_write,
135+
bounce_acquired_read,
136+
bounce_contended_write,
137+
bounce_contended_read,
138+
nr_bounce_types,
139+
140+
bounce_acquired = bounce_acquired_write,
141+
bounce_contended = bounce_contended_write,
142+
};
143+
133144
struct lock_class_stats {
134145
unsigned long contention_point[4];
135146
struct lock_time read_waittime;
136147
struct lock_time write_waittime;
137148
struct lock_time read_holdtime;
138149
struct lock_time write_holdtime;
150+
unsigned long bounces[nr_bounce_types];
139151
};
140152

141153
struct lock_class_stats lock_stats(struct lock_class *class);
@@ -150,6 +162,9 @@ struct lockdep_map {
150162
struct lock_class_key *key;
151163
struct lock_class *class_cache;
152164
const char *name;
165+
#ifdef CONFIG_LOCK_STAT
166+
int cpu;
167+
#endif
153168
};
154169

155170
/*
@@ -321,8 +336,8 @@ do { \
321336
if (!try(_lock)) { \
322337
lock_contended(&(_lock)->dep_map, _RET_IP_); \
323338
lock(_lock); \
324-
lock_acquired(&(_lock)->dep_map); \
325339
} \
340+
lock_acquired(&(_lock)->dep_map); \
326341
} while (0)
327342

328343
#else /* CONFIG_LOCK_STAT */

‎kernel/lockdep.c‎

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ struct lock_class_stats lock_stats(struct lock_class *class)
177177

178178
lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
179179
lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
180+
181+
for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
182+
stats.bounces[i] += pcs->bounces[i];
180183
}
181184

182185
return stats;
@@ -2325,6 +2328,9 @@ void lockdep_init_map(struct lockdep_map *lock, const char *name,
23252328
lock->name = name;
23262329
lock->key = key;
23272330
lock->class_cache = NULL;
2331+
#ifdef CONFIG_LOCK_STAT
2332+
lock->cpu = raw_smp_processor_id();
2333+
#endif
23282334
if (subclass)
23292335
register_lock_class(lock, subclass, 1);
23302336
}
@@ -2775,6 +2781,8 @@ __lock_contended(struct lockdep_map *lock, unsigned long ip)
27752781
stats = get_lock_stats(hlock->class);
27762782
if (point < ARRAY_SIZE(stats->contention_point))
27772783
stats->contention_point[i]++;
2784+
if (lock->cpu != smp_processor_id())
2785+
stats->bounces[bounce_contended + !!hlock->read]++;
27782786
put_lock_stats(stats);
27792787
}
27802788

@@ -2786,8 +2794,8 @@ __lock_acquired(struct lockdep_map *lock)
27862794
struct lock_class_stats *stats;
27872795
unsigned int depth;
27882796
u64 now;
2789-
s64 waittime;
2790-
int i;
2797+
s64 waittime = 0;
2798+
int i, cpu;
27912799

27922800
depth = curr->lockdep_depth;
27932801
if (DEBUG_LOCKS_WARN_ON(!depth))
@@ -2809,19 +2817,25 @@ __lock_acquired(struct lockdep_map *lock)
28092817
return;
28102818

28112819
found_it:
2812-
if (!hlock->waittime_stamp)
2813-
return;
2814-
2815-
now = sched_clock();
2816-
waittime = now - hlock->waittime_stamp;
2817-
hlock->holdtime_stamp = now;
2820+
cpu = smp_processor_id();
2821+
if (hlock->waittime_stamp) {
2822+
now = sched_clock();
2823+
waittime = now - hlock->waittime_stamp;
2824+
hlock->holdtime_stamp = now;
2825+
}
28182826

28192827
stats = get_lock_stats(hlock->class);
2820-
if (hlock->read)
2821-
lock_time_inc(&stats->read_waittime, waittime);
2822-
else
2823-
lock_time_inc(&stats->write_waittime, waittime);
2828+
if (waittime) {
2829+
if (hlock->read)
2830+
lock_time_inc(&stats->read_waittime, waittime);
2831+
else
2832+
lock_time_inc(&stats->write_waittime, waittime);
2833+
}
2834+
if (lock->cpu != cpu)
2835+
stats->bounces[bounce_acquired + !!hlock->read]++;
28242836
put_lock_stats(stats);
2837+
2838+
lock->cpu = cpu;
28252839
}
28262840

28272841
void lock_contended(struct lockdep_map *lock, unsigned long ip)

‎kernel/lockdep_proc.c‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -430,16 +430,18 @@ static void seq_stats(struct seq_file *m, struct lock_stat_data *data)
430430
else
431431
seq_printf(m, "%40s:", name);
432432

433+
seq_printf(m, "%14lu ", stats->bounces[bounce_contended_write]);
433434
seq_lock_time(m, &stats->write_waittime);
434-
seq_puts(m, " ");
435+
seq_printf(m, " %14lu ", stats->bounces[bounce_acquired_write]);
435436
seq_lock_time(m, &stats->write_holdtime);
436437
seq_puts(m, "\n");
437438
}
438439

439440
if (stats->read_holdtime.nr) {
440441
seq_printf(m, "%38s-R:", name);
442+
seq_printf(m, "%14lu ", stats->bounces[bounce_contended_read]);
441443
seq_lock_time(m, &stats->read_waittime);
442-
seq_puts(m, " ");
444+
seq_printf(m, " %14lu ", stats->bounces[bounce_acquired_read]);
443445
seq_lock_time(m, &stats->read_holdtime);
444446
seq_puts(m, "\n");
445447
}
@@ -469,26 +471,29 @@ static void seq_stats(struct seq_file *m, struct lock_stat_data *data)
469471
}
470472
if (i) {
471473
seq_puts(m, "\n");
472-
seq_line(m, '.', 0, 40 + 1 + 8 * (14 + 1));
474+
seq_line(m, '.', 0, 40 + 1 + 10 * (14 + 1));
473475
seq_puts(m, "\n");
474476
}
475477
}
476478

477479
static void seq_header(struct seq_file *m)
478480
{
479-
seq_printf(m, "lock_stat version 0.1\n");
480-
seq_line(m, '-', 0, 40 + 1 + 8 * (14 + 1));
481-
seq_printf(m, "%40s %14s %14s %14s %14s %14s %14s %14s %14s\n",
481+
seq_printf(m, "lock_stat version 0.2\n");
482+
seq_line(m, '-', 0, 40 + 1 + 10 * (14 + 1));
483+
seq_printf(m, "%40s %14s %14s %14s %14s %14s %14s %14s %14s "
484+
"%14s %14s\n",
482485
"class name",
486+
"con-bounces",
483487
"contentions",
484488
"waittime-min",
485489
"waittime-max",
486490
"waittime-total",
491+
"acq-bounces",
487492
"acquisitions",
488493
"holdtime-min",
489494
"holdtime-max",
490495
"holdtime-total");
491-
seq_line(m, '-', 0, 40 + 1 + 8 * (14 + 1));
496+
seq_line(m, '-', 0, 40 + 1 + 10 * (14 + 1));
492497
seq_printf(m, "\n");
493498
}
494499

‎kernel/mutex.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass)
180180
spin_lock_mutex(&lock->wait_lock, flags);
181181
}
182182

183-
lock_acquired(&lock->dep_map);
184183
done:
184+
lock_acquired(&lock->dep_map);
185185
/* got the lock - rejoice! */
186186
mutex_remove_waiter(lock, &waiter, task_thread_info(task));
187187
debug_mutex_set_owner(lock, task_thread_info(task));

0 commit comments

Comments
 (0)