Skip to content

Commit ad53fa1

Browse files
Waiman-Longingomolnar
authored andcommitted
locking/qspinlock_stat: Introduce generic lockevent_*() counting APIs
The percpu event counts used by qspinlock code can be useful for other locking code as well. So a new set of lockevent_* counting APIs is introduced with the lock event names extracted out into the new lock_events_list.h header file for easier addition in the future. The existing qstat_inc() calls are replaced by either lockevent_inc() or lockevent_cond_inc() calls. The qstat_hop() call is renamed to lockevent_pv_hop(). The "reset_counters" debugfs file is also renamed to ".reset_counts". Signed-off-by: Waiman Long <longman@redhat.com> Acked-by: Peter Zijlstra <a.p.zijlstra@chello.nl> Acked-by: Davidlohr Bueso <dbueso@suse.de> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Arnd Bergmann <arnd@arndb.de> Cc: Borislav Petkov <bp@alien8.de> Cc: Davidlohr Bueso <dave@stgolabs.net> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Tim Chen <tim.c.chen@linux.intel.com> Cc: Will Deacon <will.deacon@arm.com> Link: http://lkml.kernel.org/r/20190404174320.22416-8-longman@redhat.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 3b4ba66 commit ad53fa1

5 files changed

Lines changed: 181 additions & 114 deletions

File tree

‎kernel/locking/lock_events.h‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* This program is free software; you can redistribute it and/or modify
4+
* it under the terms of the GNU General Public License as published by
5+
* the Free Software Foundation; either version 2 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* Authors: Waiman Long <longman@redhat.com>
14+
*/
15+
16+
enum lock_events {
17+
18+
#include "lock_events_list.h"
19+
20+
lockevent_num, /* Total number of lock event counts */
21+
LOCKEVENT_reset_cnts = lockevent_num,
22+
};
23+
24+
#ifdef CONFIG_QUEUED_LOCK_STAT
25+
/*
26+
* Per-cpu counters
27+
*/
28+
DECLARE_PER_CPU(unsigned long, lockevents[lockevent_num]);
29+
30+
/*
31+
* Increment the PV qspinlock statistical counters
32+
*/
33+
static inline void __lockevent_inc(enum lock_events event, bool cond)
34+
{
35+
if (cond)
36+
__this_cpu_inc(lockevents[event]);
37+
}
38+
39+
#define lockevent_inc(ev) __lockevent_inc(LOCKEVENT_ ##ev, true)
40+
#define lockevent_cond_inc(ev, c) __lockevent_inc(LOCKEVENT_ ##ev, c)
41+
42+
static inline void __lockevent_add(enum lock_events event, int inc)
43+
{
44+
__this_cpu_add(lockevents[event], inc);
45+
}
46+
47+
#define lockevent_add(ev, c) __lockevent_add(LOCKEVENT_ ##ev, c)
48+
49+
#else /* CONFIG_QUEUED_LOCK_STAT */
50+
51+
#define lockevent_inc(ev)
52+
#define lockevent_add(ev, c)
53+
#define lockevent_cond_inc(ev, c)
54+
55+
#endif /* CONFIG_QUEUED_LOCK_STAT */

‎kernel/locking/lock_events_list.h‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* This program is free software; you can redistribute it and/or modify
4+
* it under the terms of the GNU General Public License as published by
5+
* the Free Software Foundation; either version 2 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* Authors: Waiman Long <longman@redhat.com>
14+
*/
15+
16+
#ifndef LOCK_EVENT
17+
#define LOCK_EVENT(name) LOCKEVENT_ ## name,
18+
#endif
19+
20+
#ifdef CONFIG_QUEUED_SPINLOCKS
21+
#ifdef CONFIG_PARAVIRT_SPINLOCKS
22+
/*
23+
* Locking events for PV qspinlock.
24+
*/
25+
LOCK_EVENT(pv_hash_hops) /* Average # of hops per hashing operation */
26+
LOCK_EVENT(pv_kick_unlock) /* # of vCPU kicks issued at unlock time */
27+
LOCK_EVENT(pv_kick_wake) /* # of vCPU kicks for pv_latency_wake */
28+
LOCK_EVENT(pv_latency_kick) /* Average latency (ns) of vCPU kick */
29+
LOCK_EVENT(pv_latency_wake) /* Average latency (ns) of kick-to-wakeup */
30+
LOCK_EVENT(pv_lock_stealing) /* # of lock stealing operations */
31+
LOCK_EVENT(pv_spurious_wakeup) /* # of spurious wakeups in non-head vCPUs */
32+
LOCK_EVENT(pv_wait_again) /* # of wait's after queue head vCPU kick */
33+
LOCK_EVENT(pv_wait_early) /* # of early vCPU wait's */
34+
LOCK_EVENT(pv_wait_head) /* # of vCPU wait's at the queue head */
35+
LOCK_EVENT(pv_wait_node) /* # of vCPU wait's at non-head queue node */
36+
#endif /* CONFIG_PARAVIRT_SPINLOCKS */
37+
38+
/*
39+
* Locking events for qspinlock
40+
*
41+
* Subtracting lock_use_node[234] from lock_slowpath will give you
42+
* lock_use_node1.
43+
*/
44+
LOCK_EVENT(lock_pending) /* # of locking ops via pending code */
45+
LOCK_EVENT(lock_slowpath) /* # of locking ops via MCS lock queue */
46+
LOCK_EVENT(lock_use_node2) /* # of locking ops that use 2nd percpu node */
47+
LOCK_EVENT(lock_use_node3) /* # of locking ops that use 3rd percpu node */
48+
LOCK_EVENT(lock_use_node4) /* # of locking ops that use 4th percpu node */
49+
LOCK_EVENT(lock_no_node) /* # of locking ops w/o using percpu node */
50+
#endif /* CONFIG_QUEUED_SPINLOCKS */

‎kernel/locking/qspinlock.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,15 +395,15 @@ void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
395395
* 0,1,0 -> 0,0,1
396396
*/
397397
clear_pending_set_locked(lock);
398-
qstat_inc(qstat_lock_pending, true);
398+
lockevent_inc(lock_pending);
399399
return;
400400

401401
/*
402402
* End of pending bit optimistic spinning and beginning of MCS
403403
* queuing.
404404
*/
405405
queue:
406-
qstat_inc(qstat_lock_slowpath, true);
406+
lockevent_inc(lock_slowpath);
407407
pv_queue:
408408
node = this_cpu_ptr(&qnodes[0].mcs);
409409
idx = node->count++;
@@ -419,7 +419,7 @@ void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
419419
* simple enough.
420420
*/
421421
if (unlikely(idx >= MAX_NODES)) {
422-
qstat_inc(qstat_lock_no_node, true);
422+
lockevent_inc(lock_no_node);
423423
while (!queued_spin_trylock(lock))
424424
cpu_relax();
425425
goto release;
@@ -430,7 +430,7 @@ void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
430430
/*
431431
* Keep counts of non-zero index values:
432432
*/
433-
qstat_inc(qstat_lock_use_node2 + idx - 1, idx);
433+
lockevent_cond_inc(lock_use_node2 + idx - 1, idx);
434434

435435
/*
436436
* Ensure that we increment the head node->count before initialising

‎kernel/locking/qspinlock_paravirt.h‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static inline bool pv_hybrid_queued_unfair_trylock(struct qspinlock *lock)
8989

9090
if (!(val & _Q_LOCKED_PENDING_MASK) &&
9191
(cmpxchg_acquire(&lock->locked, 0, _Q_LOCKED_VAL) == 0)) {
92-
qstat_inc(qstat_pv_lock_stealing, true);
92+
lockevent_inc(pv_lock_stealing);
9393
return true;
9494
}
9595
if (!(val & _Q_TAIL_MASK) || (val & _Q_PENDING_MASK))
@@ -219,7 +219,7 @@ static struct qspinlock **pv_hash(struct qspinlock *lock, struct pv_node *node)
219219
hopcnt++;
220220
if (!cmpxchg(&he->lock, NULL, lock)) {
221221
WRITE_ONCE(he->node, node);
222-
qstat_hop(hopcnt);
222+
lockevent_pv_hop(hopcnt);
223223
return &he->lock;
224224
}
225225
}
@@ -320,8 +320,8 @@ static void pv_wait_node(struct mcs_spinlock *node, struct mcs_spinlock *prev)
320320
smp_store_mb(pn->state, vcpu_halted);
321321

322322
if (!READ_ONCE(node->locked)) {
323-
qstat_inc(qstat_pv_wait_node, true);
324-
qstat_inc(qstat_pv_wait_early, wait_early);
323+
lockevent_inc(pv_wait_node);
324+
lockevent_cond_inc(pv_wait_early, wait_early);
325325
pv_wait(&pn->state, vcpu_halted);
326326
}
327327

@@ -339,7 +339,8 @@ static void pv_wait_node(struct mcs_spinlock *node, struct mcs_spinlock *prev)
339339
* So it is better to spin for a while in the hope that the
340340
* MCS lock will be released soon.
341341
*/
342-
qstat_inc(qstat_pv_spurious_wakeup, !READ_ONCE(node->locked));
342+
lockevent_cond_inc(pv_spurious_wakeup,
343+
!READ_ONCE(node->locked));
343344
}
344345

345346
/*
@@ -416,7 +417,7 @@ pv_wait_head_or_lock(struct qspinlock *lock, struct mcs_spinlock *node)
416417
/*
417418
* Tracking # of slowpath locking operations
418419
*/
419-
qstat_inc(qstat_lock_slowpath, true);
420+
lockevent_inc(lock_slowpath);
420421

421422
for (;; waitcnt++) {
422423
/*
@@ -464,8 +465,8 @@ pv_wait_head_or_lock(struct qspinlock *lock, struct mcs_spinlock *node)
464465
}
465466
}
466467
WRITE_ONCE(pn->state, vcpu_hashed);
467-
qstat_inc(qstat_pv_wait_head, true);
468-
qstat_inc(qstat_pv_wait_again, waitcnt);
468+
lockevent_inc(pv_wait_head);
469+
lockevent_cond_inc(pv_wait_again, waitcnt);
469470
pv_wait(&lock->locked, _Q_SLOW_VAL);
470471

471472
/*
@@ -528,7 +529,7 @@ __pv_queued_spin_unlock_slowpath(struct qspinlock *lock, u8 locked)
528529
* vCPU is harmless other than the additional latency in completing
529530
* the unlock.
530531
*/
531-
qstat_inc(qstat_pv_kick_unlock, true);
532+
lockevent_inc(pv_kick_unlock);
532533
pv_kick(node->cpu);
533534
}
534535

0 commit comments

Comments
 (0)