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  *
12  * Authors: Waiman Long <waiman.long@hpe.com>
13  */
14 
15 /*
16  * When queued spinlock statistical counters are enabled, the following
17  * debugfs files will be created for reporting the counter values:
18  *
19  * <debugfs>/qlockstat/
20  *   pv_hash_hops	- average # of hops per hashing operation
21  *   pv_kick_unlock	- # of vCPU kicks issued at unlock time
22  *   pv_kick_wake	- # of vCPU kicks used for computing pv_latency_wake
23  *   pv_latency_kick	- average latency (ns) of vCPU kick operation
24  *   pv_latency_wake	- average latency (ns) from vCPU kick to wakeup
25  *   pv_lock_stealing	- # of lock stealing operations
26  *   pv_spurious_wakeup	- # of spurious wakeups in non-head vCPUs
27  *   pv_wait_again	- # of wait's after a queue head vCPU kick
28  *   pv_wait_early	- # of early vCPU wait's
29  *   pv_wait_head	- # of vCPU wait's at the queue head
30  *   pv_wait_node	- # of vCPU wait's at a non-head queue node
31  *   lock_pending	- # of locking operations via pending code
32  *   lock_slowpath	- # of locking operations via MCS lock queue
33  *
34  * Writing to the "reset_counters" file will reset all the above counter
35  * values.
36  *
37  * These statistical counters are implemented as per-cpu variables which are
38  * summed and computed whenever the corresponding debugfs files are read. This
39  * minimizes added overhead making the counters usable even in a production
40  * environment.
41  *
42  * There may be slight difference between pv_kick_wake and pv_kick_unlock.
43  */
44 enum qlock_stats {
45 	qstat_pv_hash_hops,
46 	qstat_pv_kick_unlock,
47 	qstat_pv_kick_wake,
48 	qstat_pv_latency_kick,
49 	qstat_pv_latency_wake,
50 	qstat_pv_lock_stealing,
51 	qstat_pv_spurious_wakeup,
52 	qstat_pv_wait_again,
53 	qstat_pv_wait_early,
54 	qstat_pv_wait_head,
55 	qstat_pv_wait_node,
56 	qstat_lock_pending,
57 	qstat_lock_slowpath,
58 	qstat_lock_idx1,
59 	qstat_lock_idx2,
60 	qstat_lock_idx3,
61 	qstat_num,	/* Total number of statistical counters */
62 	qstat_reset_cnts = qstat_num,
63 };
64 
65 #ifdef CONFIG_QUEUED_LOCK_STAT
66 /*
67  * Collect pvqspinlock statistics
68  */
69 #include <linux/debugfs.h>
70 #include <linux/sched.h>
71 #include <linux/sched/clock.h>
72 #include <linux/fs.h>
73 
74 static const char * const qstat_names[qstat_num + 1] = {
75 	[qstat_pv_hash_hops]	   = "pv_hash_hops",
76 	[qstat_pv_kick_unlock]     = "pv_kick_unlock",
77 	[qstat_pv_kick_wake]       = "pv_kick_wake",
78 	[qstat_pv_spurious_wakeup] = "pv_spurious_wakeup",
79 	[qstat_pv_latency_kick]	   = "pv_latency_kick",
80 	[qstat_pv_latency_wake]    = "pv_latency_wake",
81 	[qstat_pv_lock_stealing]   = "pv_lock_stealing",
82 	[qstat_pv_wait_again]      = "pv_wait_again",
83 	[qstat_pv_wait_early]      = "pv_wait_early",
84 	[qstat_pv_wait_head]       = "pv_wait_head",
85 	[qstat_pv_wait_node]       = "pv_wait_node",
86 	[qstat_lock_pending]       = "lock_pending",
87 	[qstat_lock_slowpath]      = "lock_slowpath",
88 	[qstat_lock_idx1]	   = "lock_index1",
89 	[qstat_lock_idx2]	   = "lock_index2",
90 	[qstat_lock_idx3]	   = "lock_index3",
91 	[qstat_reset_cnts]         = "reset_counters",
92 };
93 
94 /*
95  * Per-cpu counters
96  */
97 static DEFINE_PER_CPU(unsigned long, qstats[qstat_num]);
98 static DEFINE_PER_CPU(u64, pv_kick_time);
99 
100 /*
101  * Function to read and return the qlock statistical counter values
102  *
103  * The following counters are handled specially:
104  * 1. qstat_pv_latency_kick
105  *    Average kick latency (ns) = pv_latency_kick/pv_kick_unlock
106  * 2. qstat_pv_latency_wake
107  *    Average wake latency (ns) = pv_latency_wake/pv_kick_wake
108  * 3. qstat_pv_hash_hops
109  *    Average hops/hash = pv_hash_hops/pv_kick_unlock
110  */
111 static ssize_t qstat_read(struct file *file, char __user *user_buf,
112 			  size_t count, loff_t *ppos)
113 {
114 	char buf[64];
115 	int cpu, counter, len;
116 	u64 stat = 0, kicks = 0;
117 
118 	/*
119 	 * Get the counter ID stored in file->f_inode->i_private
120 	 */
121 	counter = (long)file_inode(file)->i_private;
122 
123 	if (counter >= qstat_num)
124 		return -EBADF;
125 
126 	for_each_possible_cpu(cpu) {
127 		stat += per_cpu(qstats[counter], cpu);
128 		/*
129 		 * Need to sum additional counter for some of them
130 		 */
131 		switch (counter) {
132 
133 		case qstat_pv_latency_kick:
134 		case qstat_pv_hash_hops:
135 			kicks += per_cpu(qstats[qstat_pv_kick_unlock], cpu);
136 			break;
137 
138 		case qstat_pv_latency_wake:
139 			kicks += per_cpu(qstats[qstat_pv_kick_wake], cpu);
140 			break;
141 		}
142 	}
143 
144 	if (counter == qstat_pv_hash_hops) {
145 		u64 frac = 0;
146 
147 		if (kicks) {
148 			frac = 100ULL * do_div(stat, kicks);
149 			frac = DIV_ROUND_CLOSEST_ULL(frac, kicks);
150 		}
151 
152 		/*
153 		 * Return a X.XX decimal number
154 		 */
155 		len = snprintf(buf, sizeof(buf) - 1, "%llu.%02llu\n", stat, frac);
156 	} else {
157 		/*
158 		 * Round to the nearest ns
159 		 */
160 		if ((counter == qstat_pv_latency_kick) ||
161 		    (counter == qstat_pv_latency_wake)) {
162 			if (kicks)
163 				stat = DIV_ROUND_CLOSEST_ULL(stat, kicks);
164 		}
165 		len = snprintf(buf, sizeof(buf) - 1, "%llu\n", stat);
166 	}
167 
168 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
169 }
170 
171 /*
172  * Function to handle write request
173  *
174  * When counter = reset_cnts, reset all the counter values.
175  * Since the counter updates aren't atomic, the resetting is done twice
176  * to make sure that the counters are very likely to be all cleared.
177  */
178 static ssize_t qstat_write(struct file *file, const char __user *user_buf,
179 			   size_t count, loff_t *ppos)
180 {
181 	int cpu;
182 
183 	/*
184 	 * Get the counter ID stored in file->f_inode->i_private
185 	 */
186 	if ((long)file_inode(file)->i_private != qstat_reset_cnts)
187 		return count;
188 
189 	for_each_possible_cpu(cpu) {
190 		int i;
191 		unsigned long *ptr = per_cpu_ptr(qstats, cpu);
192 
193 		for (i = 0 ; i < qstat_num; i++)
194 			WRITE_ONCE(ptr[i], 0);
195 	}
196 	return count;
197 }
198 
199 /*
200  * Debugfs data structures
201  */
202 static const struct file_operations fops_qstat = {
203 	.read = qstat_read,
204 	.write = qstat_write,
205 	.llseek = default_llseek,
206 };
207 
208 /*
209  * Initialize debugfs for the qspinlock statistical counters
210  */
211 static int __init init_qspinlock_stat(void)
212 {
213 	struct dentry *d_qstat = debugfs_create_dir("qlockstat", NULL);
214 	int i;
215 
216 	if (!d_qstat)
217 		goto out;
218 
219 	/*
220 	 * Create the debugfs files
221 	 *
222 	 * As reading from and writing to the stat files can be slow, only
223 	 * root is allowed to do the read/write to limit impact to system
224 	 * performance.
225 	 */
226 	for (i = 0; i < qstat_num; i++)
227 		if (!debugfs_create_file(qstat_names[i], 0400, d_qstat,
228 					 (void *)(long)i, &fops_qstat))
229 			goto fail_undo;
230 
231 	if (!debugfs_create_file(qstat_names[qstat_reset_cnts], 0200, d_qstat,
232 				 (void *)(long)qstat_reset_cnts, &fops_qstat))
233 		goto fail_undo;
234 
235 	return 0;
236 fail_undo:
237 	debugfs_remove_recursive(d_qstat);
238 out:
239 	pr_warn("Could not create 'qlockstat' debugfs entries\n");
240 	return -ENOMEM;
241 }
242 fs_initcall(init_qspinlock_stat);
243 
244 /*
245  * Increment the PV qspinlock statistical counters
246  */
247 static inline void qstat_inc(enum qlock_stats stat, bool cond)
248 {
249 	if (cond)
250 		this_cpu_inc(qstats[stat]);
251 }
252 
253 /*
254  * PV hash hop count
255  */
256 static inline void qstat_hop(int hopcnt)
257 {
258 	this_cpu_add(qstats[qstat_pv_hash_hops], hopcnt);
259 }
260 
261 /*
262  * Replacement function for pv_kick()
263  */
264 static inline void __pv_kick(int cpu)
265 {
266 	u64 start = sched_clock();
267 
268 	per_cpu(pv_kick_time, cpu) = start;
269 	pv_kick(cpu);
270 	this_cpu_add(qstats[qstat_pv_latency_kick], sched_clock() - start);
271 }
272 
273 /*
274  * Replacement function for pv_wait()
275  */
276 static inline void __pv_wait(u8 *ptr, u8 val)
277 {
278 	u64 *pkick_time = this_cpu_ptr(&pv_kick_time);
279 
280 	*pkick_time = 0;
281 	pv_wait(ptr, val);
282 	if (*pkick_time) {
283 		this_cpu_add(qstats[qstat_pv_latency_wake],
284 			     sched_clock() - *pkick_time);
285 		qstat_inc(qstat_pv_kick_wake, true);
286 	}
287 }
288 
289 #define pv_kick(c)	__pv_kick(c)
290 #define pv_wait(p, v)	__pv_wait(p, v)
291 
292 #else /* CONFIG_QUEUED_LOCK_STAT */
293 
294 static inline void qstat_inc(enum qlock_stats stat, bool cond)	{ }
295 static inline void qstat_hop(int hopcnt)			{ }
296 
297 #endif /* CONFIG_QUEUED_LOCK_STAT */
298