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.
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.
12 * Authors: Waiman Long <waiman.long@hpe.com>
16 * When queued spinlock statistical counters are enabled, the following
17 * debugfs files will be created for reporting the counter values:
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_slowpath - # of locking operations via the slowpath
26 * pv_lock_stealing - # of lock stealing operations
27 * pv_spurious_wakeup - # of spurious wakeups
28 * pv_wait_again - # of vCPU wait's that happened after a vCPU kick
29 * pv_wait_early - # of early vCPU wait's
30 * pv_wait_head - # of vCPU wait's at the queue head
31 * pv_wait_node - # of vCPU wait's at a non-head queue node
33 * Writing to the "reset_counters" file will reset all the above counter
36 * These statistical counters are implemented as per-cpu variables which are
37 * summed and computed whenever the corresponding debugfs files are read. This
38 * minimizes added overhead making the counters usable even in a production
41 * There may be slight difference between pv_kick_wake and pv_kick_unlock.
47 qstat_pv_latency_kick,
48 qstat_pv_latency_wake,
49 qstat_pv_lock_slowpath,
50 qstat_pv_lock_stealing,
51 qstat_pv_spurious_wakeup,
56 qstat_num, /* Total number of statistical counters */
57 qstat_reset_cnts = qstat_num,
60 #ifdef CONFIG_QUEUED_LOCK_STAT
62 * Collect pvqspinlock statistics
64 #include <linux/debugfs.h>
65 #include <linux/sched.h>
68 static const char * const qstat_names[qstat_num + 1] = {
69 [qstat_pv_hash_hops] = "pv_hash_hops",
70 [qstat_pv_kick_unlock] = "pv_kick_unlock",
71 [qstat_pv_kick_wake] = "pv_kick_wake",
72 [qstat_pv_spurious_wakeup] = "pv_spurious_wakeup",
73 [qstat_pv_latency_kick] = "pv_latency_kick",
74 [qstat_pv_latency_wake] = "pv_latency_wake",
75 [qstat_pv_lock_slowpath] = "pv_lock_slowpath",
76 [qstat_pv_lock_stealing] = "pv_lock_stealing",
77 [qstat_pv_wait_again] = "pv_wait_again",
78 [qstat_pv_wait_early] = "pv_wait_early",
79 [qstat_pv_wait_head] = "pv_wait_head",
80 [qstat_pv_wait_node] = "pv_wait_node",
81 [qstat_reset_cnts] = "reset_counters",
87 static DEFINE_PER_CPU(unsigned long, qstats[qstat_num]);
88 static DEFINE_PER_CPU(u64, pv_kick_time);
91 * Function to read and return the qlock statistical counter values
93 * The following counters are handled specially:
94 * 1. qstat_pv_latency_kick
95 * Average kick latency (ns) = pv_latency_kick/pv_kick_unlock
96 * 2. qstat_pv_latency_wake
97 * Average wake latency (ns) = pv_latency_wake/pv_kick_wake
98 * 3. qstat_pv_hash_hops
99 * Average hops/hash = pv_hash_hops/pv_kick_unlock
101 static ssize_t qstat_read(struct file *file, char __user *user_buf,
102 size_t count, loff_t *ppos)
105 int cpu, counter, len;
106 u64 stat = 0, kicks = 0;
109 * Get the counter ID stored in file->f_inode->i_private
111 if (!file->f_inode) {
115 counter = (long)(file->f_inode->i_private);
117 if (counter >= qstat_num)
120 for_each_possible_cpu(cpu) {
121 stat += per_cpu(qstats[counter], cpu);
123 * Need to sum additional counter for some of them
127 case qstat_pv_latency_kick:
128 case qstat_pv_hash_hops:
129 kicks += per_cpu(qstats[qstat_pv_kick_unlock], cpu);
132 case qstat_pv_latency_wake:
133 kicks += per_cpu(qstats[qstat_pv_kick_wake], cpu);
138 if (counter == qstat_pv_hash_hops) {
142 frac = 100ULL * do_div(stat, kicks);
143 frac = DIV_ROUND_CLOSEST_ULL(frac, kicks);
147 * Return a X.XX decimal number
149 len = snprintf(buf, sizeof(buf) - 1, "%llu.%02llu\n", stat, frac);
152 * Round to the nearest ns
154 if ((counter == qstat_pv_latency_kick) ||
155 (counter == qstat_pv_latency_wake)) {
158 stat = DIV_ROUND_CLOSEST_ULL(stat, kicks);
160 len = snprintf(buf, sizeof(buf) - 1, "%llu\n", stat);
163 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
167 * Function to handle write request
169 * When counter = reset_cnts, reset all the counter values.
170 * Since the counter updates aren't atomic, the resetting is done twice
171 * to make sure that the counters are very likely to be all cleared.
173 static ssize_t qstat_write(struct file *file, const char __user *user_buf,
174 size_t count, loff_t *ppos)
179 * Get the counter ID stored in file->f_inode->i_private
181 if (!file->f_inode) {
185 if ((long)(file->f_inode->i_private) != qstat_reset_cnts)
188 for_each_possible_cpu(cpu) {
190 unsigned long *ptr = per_cpu_ptr(qstats, cpu);
192 for (i = 0 ; i < qstat_num; i++)
193 WRITE_ONCE(ptr[i], 0);
199 * Debugfs data structures
201 static const struct file_operations fops_qstat = {
203 .write = qstat_write,
204 .llseek = default_llseek,
208 * Initialize debugfs for the qspinlock statistical counters
210 static int __init init_qspinlock_stat(void)
212 struct dentry *d_qstat = debugfs_create_dir("qlockstat", NULL);
219 * Create the debugfs files
221 * As reading from and writing to the stat files can be slow, only
222 * root is allowed to do the read/write to limit impact to system
225 for (i = 0; i < qstat_num; i++)
226 if (!debugfs_create_file(qstat_names[i], 0400, d_qstat,
227 (void *)(long)i, &fops_qstat))
230 if (!debugfs_create_file(qstat_names[qstat_reset_cnts], 0200, d_qstat,
231 (void *)(long)qstat_reset_cnts, &fops_qstat))
236 debugfs_remove_recursive(d_qstat);
238 pr_warn("Could not create 'qlockstat' debugfs entries\n");
241 fs_initcall(init_qspinlock_stat);
244 * Increment the PV qspinlock statistical counters
246 static inline void qstat_inc(enum qlock_stats stat, bool cond)
249 this_cpu_inc(qstats[stat]);
255 static inline void qstat_hop(int hopcnt)
257 this_cpu_add(qstats[qstat_pv_hash_hops], hopcnt);
261 * Replacement function for pv_kick()
263 static inline void __pv_kick(int cpu)
265 u64 start = sched_clock();
267 per_cpu(pv_kick_time, cpu) = start;
269 this_cpu_add(qstats[qstat_pv_latency_kick], sched_clock() - start);
273 * Replacement function for pv_wait()
275 static inline void __pv_wait(u8 *ptr, u8 val)
277 u64 *pkick_time = this_cpu_ptr(&pv_kick_time);
282 this_cpu_add(qstats[qstat_pv_latency_wake],
283 sched_clock() - *pkick_time);
284 qstat_inc(qstat_pv_kick_wake, true);
288 #define pv_kick(c) __pv_kick(c)
289 #define pv_wait(p, v) __pv_wait(p, v)
291 #else /* CONFIG_QUEUED_LOCK_STAT */
293 static inline void qstat_inc(enum qlock_stats stat, bool cond) { }
294 static inline void qstat_hop(int hopcnt) { }
296 #endif /* CONFIG_QUEUED_LOCK_STAT */