perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / kernel / locking / qspinlock_stat.h
blob42d3d8dc8f49b4403028c10caa88dc232384027f
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.
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_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
34 * Writing to the "reset_counters" file will reset all the above counter
35 * values.
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.
42 * There may be slight difference between pv_kick_wake and pv_kick_unlock.
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,
65 #ifdef CONFIG_QUEUED_LOCK_STAT
67 * Collect pvqspinlock statistics
69 #include <linux/debugfs.h>
70 #include <linux/sched.h>
71 #include <linux/sched/clock.h>
72 #include <linux/fs.h>
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",
95 * Per-cpu counters
97 static DEFINE_PER_CPU(unsigned long, qstats[qstat_num]);
98 static DEFINE_PER_CPU(u64, pv_kick_time);
101 * Function to read and return the qlock statistical counter values
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
111 static ssize_t qstat_read(struct file *file, char __user *user_buf,
112 size_t count, loff_t *ppos)
114 char buf[64];
115 int cpu, counter, len;
116 u64 stat = 0, kicks = 0;
119 * Get the counter ID stored in file->f_inode->i_private
121 counter = (long)file_inode(file)->i_private;
123 if (counter >= qstat_num)
124 return -EBADF;
126 for_each_possible_cpu(cpu) {
127 stat += per_cpu(qstats[counter], cpu);
129 * Need to sum additional counter for some of them
131 switch (counter) {
133 case qstat_pv_latency_kick:
134 case qstat_pv_hash_hops:
135 kicks += per_cpu(qstats[qstat_pv_kick_unlock], cpu);
136 break;
138 case qstat_pv_latency_wake:
139 kicks += per_cpu(qstats[qstat_pv_kick_wake], cpu);
140 break;
144 if (counter == qstat_pv_hash_hops) {
145 u64 frac = 0;
147 if (kicks) {
148 frac = 100ULL * do_div(stat, kicks);
149 frac = DIV_ROUND_CLOSEST_ULL(frac, kicks);
153 * Return a X.XX decimal number
155 len = snprintf(buf, sizeof(buf) - 1, "%llu.%02llu\n", stat, frac);
156 } else {
158 * Round to the nearest ns
160 if ((counter == qstat_pv_latency_kick) ||
161 (counter == qstat_pv_latency_wake)) {
162 if (kicks)
163 stat = DIV_ROUND_CLOSEST_ULL(stat, kicks);
165 len = snprintf(buf, sizeof(buf) - 1, "%llu\n", stat);
168 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
172 * Function to handle write request
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.
178 static ssize_t qstat_write(struct file *file, const char __user *user_buf,
179 size_t count, loff_t *ppos)
181 int cpu;
184 * Get the counter ID stored in file->f_inode->i_private
186 if ((long)file_inode(file)->i_private != qstat_reset_cnts)
187 return count;
189 for_each_possible_cpu(cpu) {
190 int i;
191 unsigned long *ptr = per_cpu_ptr(qstats, cpu);
193 for (i = 0 ; i < qstat_num; i++)
194 WRITE_ONCE(ptr[i], 0);
196 return count;
200 * Debugfs data structures
202 static const struct file_operations fops_qstat = {
203 .read = qstat_read,
204 .write = qstat_write,
205 .llseek = default_llseek,
209 * Initialize debugfs for the qspinlock statistical counters
211 static int __init init_qspinlock_stat(void)
213 struct dentry *d_qstat = debugfs_create_dir("qlockstat", NULL);
214 int i;
216 if (!d_qstat)
217 goto out;
220 * Create the debugfs files
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.
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;
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;
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;
242 fs_initcall(init_qspinlock_stat);
245 * Increment the PV qspinlock statistical counters
247 static inline void qstat_inc(enum qlock_stats stat, bool cond)
249 if (cond)
250 this_cpu_inc(qstats[stat]);
254 * PV hash hop count
256 static inline void qstat_hop(int hopcnt)
258 this_cpu_add(qstats[qstat_pv_hash_hops], hopcnt);
262 * Replacement function for pv_kick()
264 static inline void __pv_kick(int cpu)
266 u64 start = sched_clock();
268 per_cpu(pv_kick_time, cpu) = start;
269 pv_kick(cpu);
270 this_cpu_add(qstats[qstat_pv_latency_kick], sched_clock() - start);
274 * Replacement function for pv_wait()
276 static inline void __pv_wait(u8 *ptr, u8 val)
278 u64 *pkick_time = this_cpu_ptr(&pv_kick_time);
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);
289 #define pv_kick(c) __pv_kick(c)
290 #define pv_wait(p, v) __pv_wait(p, v)
292 #else /* CONFIG_QUEUED_LOCK_STAT */
294 static inline void qstat_inc(enum qlock_stats stat, bool cond) { }
295 static inline void qstat_hop(int hopcnt) { }
297 #endif /* CONFIG_QUEUED_LOCK_STAT */