treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 156
[linux-block.git] / kernel / locking / qspinlock_stat.h
CommitLineData
45e898b7
WL
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
fb346fd9 12 * Authors: Waiman Long <longman@redhat.com>
45e898b7
WL
13 */
14
ad53fa10 15#include "lock_events.h"
45e898b7 16
fb346fd9
WL
17#ifdef CONFIG_LOCK_EVENT_COUNTS
18#ifdef CONFIG_PARAVIRT_SPINLOCKS
45e898b7 19/*
fb346fd9 20 * Collect pvqspinlock locking event counts
45e898b7 21 */
45e898b7 22#include <linux/sched.h>
e6017571 23#include <linux/sched/clock.h>
45e898b7
WL
24#include <linux/fs.h>
25
ad53fa10
WL
26#define EVENT_COUNT(ev) lockevents[LOCKEVENT_ ## ev]
27
45e898b7 28/*
fb346fd9 29 * PV specific per-cpu counter
45e898b7 30 */
45e898b7
WL
31static DEFINE_PER_CPU(u64, pv_kick_time);
32
33/*
fb346fd9 34 * Function to read and return the PV qspinlock counts.
45e898b7
WL
35 *
36 * The following counters are handled specially:
ad53fa10 37 * 1. pv_latency_kick
45e898b7 38 * Average kick latency (ns) = pv_latency_kick/pv_kick_unlock
ad53fa10 39 * 2. pv_latency_wake
45e898b7 40 * Average wake latency (ns) = pv_latency_wake/pv_kick_wake
ad53fa10 41 * 3. pv_hash_hops
45e898b7
WL
42 * Average hops/hash = pv_hash_hops/pv_kick_unlock
43 */
fb346fd9
WL
44ssize_t lockevent_read(struct file *file, char __user *user_buf,
45 size_t count, loff_t *ppos)
45e898b7
WL
46{
47 char buf[64];
ad53fa10
WL
48 int cpu, id, len;
49 u64 sum = 0, kicks = 0;
45e898b7
WL
50
51 /*
52 * Get the counter ID stored in file->f_inode->i_private
53 */
ad53fa10 54 id = (long)file_inode(file)->i_private;
45e898b7 55
ad53fa10 56 if (id >= lockevent_num)
45e898b7
WL
57 return -EBADF;
58
59 for_each_possible_cpu(cpu) {
ad53fa10 60 sum += per_cpu(lockevents[id], cpu);
45e898b7 61 /*
ad53fa10 62 * Need to sum additional counters for some of them
45e898b7 63 */
ad53fa10 64 switch (id) {
45e898b7 65
ad53fa10
WL
66 case LOCKEVENT_pv_latency_kick:
67 case LOCKEVENT_pv_hash_hops:
68 kicks += per_cpu(EVENT_COUNT(pv_kick_unlock), cpu);
45e898b7
WL
69 break;
70
ad53fa10
WL
71 case LOCKEVENT_pv_latency_wake:
72 kicks += per_cpu(EVENT_COUNT(pv_kick_wake), cpu);
45e898b7
WL
73 break;
74 }
75 }
76
ad53fa10 77 if (id == LOCKEVENT_pv_hash_hops) {
66876595 78 u64 frac = 0;
45e898b7 79
66876595 80 if (kicks) {
ad53fa10 81 frac = 100ULL * do_div(sum, kicks);
66876595
DB
82 frac = DIV_ROUND_CLOSEST_ULL(frac, kicks);
83 }
45e898b7
WL
84
85 /*
86 * Return a X.XX decimal number
87 */
ad53fa10
WL
88 len = snprintf(buf, sizeof(buf) - 1, "%llu.%02llu\n",
89 sum, frac);
45e898b7
WL
90 } else {
91 /*
92 * Round to the nearest ns
93 */
ad53fa10
WL
94 if ((id == LOCKEVENT_pv_latency_kick) ||
95 (id == LOCKEVENT_pv_latency_wake)) {
45e898b7 96 if (kicks)
ad53fa10 97 sum = DIV_ROUND_CLOSEST_ULL(sum, kicks);
45e898b7 98 }
ad53fa10 99 len = snprintf(buf, sizeof(buf) - 1, "%llu\n", sum);
45e898b7
WL
100 }
101
102 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
103}
104
45e898b7
WL
105/*
106 * PV hash hop count
107 */
ad53fa10 108static inline void lockevent_pv_hop(int hopcnt)
45e898b7 109{
ad53fa10 110 this_cpu_add(EVENT_COUNT(pv_hash_hops), hopcnt);
45e898b7
WL
111}
112
113/*
114 * Replacement function for pv_kick()
115 */
116static inline void __pv_kick(int cpu)
117{
118 u64 start = sched_clock();
119
120 per_cpu(pv_kick_time, cpu) = start;
121 pv_kick(cpu);
ad53fa10 122 this_cpu_add(EVENT_COUNT(pv_latency_kick), sched_clock() - start);
45e898b7
WL
123}
124
125/*
126 * Replacement function for pv_wait()
127 */
128static inline void __pv_wait(u8 *ptr, u8 val)
129{
130 u64 *pkick_time = this_cpu_ptr(&pv_kick_time);
131
132 *pkick_time = 0;
133 pv_wait(ptr, val);
134 if (*pkick_time) {
ad53fa10 135 this_cpu_add(EVENT_COUNT(pv_latency_wake),
45e898b7 136 sched_clock() - *pkick_time);
ad53fa10 137 lockevent_inc(pv_kick_wake);
45e898b7
WL
138 }
139}
140
141#define pv_kick(c) __pv_kick(c)
142#define pv_wait(p, v) __pv_wait(p, v)
143
fb346fd9
WL
144#endif /* CONFIG_PARAVIRT_SPINLOCKS */
145
146#else /* CONFIG_LOCK_EVENT_COUNTS */
45e898b7 147
ad53fa10 148static inline void lockevent_pv_hop(int hopcnt) { }
45e898b7 149
fb346fd9 150#endif /* CONFIG_LOCK_EVENT_COUNTS */