Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 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 |
Waiman Long | 1c4941f | 2015-11-10 16:18:56 -0500 | [diff] [blame] | 25 | * pv_lock_stealing - # of lock stealing operations |
Waiman Long | 08be8f6 | 2016-05-31 12:53:47 -0400 | [diff] [blame] | 26 | * pv_spurious_wakeup - # of spurious wakeups in non-head vCPUs |
| 27 | * pv_wait_again - # of wait's after a queue head vCPU kick |
Waiman Long | cd0272f | 2015-11-09 19:09:27 -0500 | [diff] [blame] | 28 | * pv_wait_early - # of early vCPU wait's |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 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 |
Waiman Long | 81d3dc9 | 2018-04-26 11:34:27 +0100 | [diff] [blame] | 31 | * lock_pending - # of locking operations via pending code |
| 32 | * lock_slowpath - # of locking operations via MCS lock queue |
Waiman Long | 412f34a | 2019-01-29 22:53:46 +0100 | [diff] [blame] | 33 | * lock_use_node2 - # of locking operations that use 2nd per-CPU node |
| 34 | * lock_use_node3 - # of locking operations that use 3rd per-CPU node |
| 35 | * lock_use_node4 - # of locking operations that use 4th per-CPU node |
| 36 | * lock_no_node - # of locking operations without using per-CPU node |
| 37 | * |
| 38 | * Subtracting lock_use_node[234] from lock_slowpath will give you |
| 39 | * lock_use_node1. |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 40 | * |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 41 | * Writing to the special ".reset_counts" file will reset all the above |
| 42 | * counter values. |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 43 | * |
| 44 | * These statistical counters are implemented as per-cpu variables which are |
| 45 | * summed and computed whenever the corresponding debugfs files are read. This |
| 46 | * minimizes added overhead making the counters usable even in a production |
| 47 | * environment. |
| 48 | * |
| 49 | * There may be slight difference between pv_kick_wake and pv_kick_unlock. |
| 50 | */ |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 51 | #include "lock_events.h" |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 52 | |
| 53 | #ifdef CONFIG_QUEUED_LOCK_STAT |
| 54 | /* |
| 55 | * Collect pvqspinlock statistics |
| 56 | */ |
| 57 | #include <linux/debugfs.h> |
| 58 | #include <linux/sched.h> |
Ingo Molnar | e601757 | 2017-02-01 16:36:40 +0100 | [diff] [blame] | 59 | #include <linux/sched/clock.h> |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 60 | #include <linux/fs.h> |
| 61 | |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 62 | #define EVENT_COUNT(ev) lockevents[LOCKEVENT_ ## ev] |
| 63 | |
| 64 | #undef LOCK_EVENT |
| 65 | #define LOCK_EVENT(name) [LOCKEVENT_ ## name] = #name, |
| 66 | |
| 67 | static const char * const lockevent_names[lockevent_num + 1] = { |
| 68 | |
| 69 | #include "lock_events_list.h" |
| 70 | |
| 71 | [LOCKEVENT_reset_cnts] = ".reset_counts", |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | /* |
| 75 | * Per-cpu counters |
| 76 | */ |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 77 | DEFINE_PER_CPU(unsigned long, lockevents[lockevent_num]); |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 78 | static DEFINE_PER_CPU(u64, pv_kick_time); |
| 79 | |
| 80 | /* |
| 81 | * Function to read and return the qlock statistical counter values |
| 82 | * |
| 83 | * The following counters are handled specially: |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 84 | * 1. pv_latency_kick |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 85 | * Average kick latency (ns) = pv_latency_kick/pv_kick_unlock |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 86 | * 2. pv_latency_wake |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 87 | * Average wake latency (ns) = pv_latency_wake/pv_kick_wake |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 88 | * 3. pv_hash_hops |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 89 | * Average hops/hash = pv_hash_hops/pv_kick_unlock |
| 90 | */ |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 91 | static ssize_t lockevent_read(struct file *file, char __user *user_buf, |
| 92 | size_t count, loff_t *ppos) |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 93 | { |
| 94 | char buf[64]; |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 95 | int cpu, id, len; |
| 96 | u64 sum = 0, kicks = 0; |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 97 | |
| 98 | /* |
| 99 | * Get the counter ID stored in file->f_inode->i_private |
| 100 | */ |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 101 | id = (long)file_inode(file)->i_private; |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 102 | |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 103 | if (id >= lockevent_num) |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 104 | return -EBADF; |
| 105 | |
| 106 | for_each_possible_cpu(cpu) { |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 107 | sum += per_cpu(lockevents[id], cpu); |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 108 | /* |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 109 | * Need to sum additional counters for some of them |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 110 | */ |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 111 | switch (id) { |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 112 | |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 113 | case LOCKEVENT_pv_latency_kick: |
| 114 | case LOCKEVENT_pv_hash_hops: |
| 115 | kicks += per_cpu(EVENT_COUNT(pv_kick_unlock), cpu); |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 116 | break; |
| 117 | |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 118 | case LOCKEVENT_pv_latency_wake: |
| 119 | kicks += per_cpu(EVENT_COUNT(pv_kick_wake), cpu); |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 120 | break; |
| 121 | } |
| 122 | } |
| 123 | |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 124 | if (id == LOCKEVENT_pv_hash_hops) { |
Davidlohr Bueso | 6687659 | 2016-04-17 23:31:41 -0700 | [diff] [blame] | 125 | u64 frac = 0; |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 126 | |
Davidlohr Bueso | 6687659 | 2016-04-17 23:31:41 -0700 | [diff] [blame] | 127 | if (kicks) { |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 128 | frac = 100ULL * do_div(sum, kicks); |
Davidlohr Bueso | 6687659 | 2016-04-17 23:31:41 -0700 | [diff] [blame] | 129 | frac = DIV_ROUND_CLOSEST_ULL(frac, kicks); |
| 130 | } |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 131 | |
| 132 | /* |
| 133 | * Return a X.XX decimal number |
| 134 | */ |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 135 | len = snprintf(buf, sizeof(buf) - 1, "%llu.%02llu\n", |
| 136 | sum, frac); |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 137 | } else { |
| 138 | /* |
| 139 | * Round to the nearest ns |
| 140 | */ |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 141 | if ((id == LOCKEVENT_pv_latency_kick) || |
| 142 | (id == LOCKEVENT_pv_latency_wake)) { |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 143 | if (kicks) |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 144 | sum = DIV_ROUND_CLOSEST_ULL(sum, kicks); |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 145 | } |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 146 | len = snprintf(buf, sizeof(buf) - 1, "%llu\n", sum); |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | return simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Function to handle write request |
| 154 | * |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 155 | * When id = .reset_cnts, reset all the counter values. |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 156 | */ |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 157 | static ssize_t lockevent_write(struct file *file, const char __user *user_buf, |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 158 | size_t count, loff_t *ppos) |
| 159 | { |
| 160 | int cpu; |
| 161 | |
| 162 | /* |
| 163 | * Get the counter ID stored in file->f_inode->i_private |
| 164 | */ |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 165 | if ((long)file_inode(file)->i_private != LOCKEVENT_reset_cnts) |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 166 | return count; |
| 167 | |
| 168 | for_each_possible_cpu(cpu) { |
| 169 | int i; |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 170 | unsigned long *ptr = per_cpu_ptr(lockevents, cpu); |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 171 | |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 172 | for (i = 0 ; i < lockevent_num; i++) |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 173 | WRITE_ONCE(ptr[i], 0); |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 174 | } |
| 175 | return count; |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * Debugfs data structures |
| 180 | */ |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 181 | static const struct file_operations fops_lockevent = { |
| 182 | .read = lockevent_read, |
| 183 | .write = lockevent_write, |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 184 | .llseek = default_llseek, |
| 185 | }; |
| 186 | |
| 187 | /* |
| 188 | * Initialize debugfs for the qspinlock statistical counters |
| 189 | */ |
| 190 | static int __init init_qspinlock_stat(void) |
| 191 | { |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 192 | struct dentry *d_counts = debugfs_create_dir("qlockstat", NULL); |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 193 | int i; |
| 194 | |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 195 | if (!d_counts) |
Davidlohr Bueso | b96bbdd | 2016-04-19 21:17:25 -0700 | [diff] [blame] | 196 | goto out; |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 197 | |
| 198 | /* |
| 199 | * Create the debugfs files |
| 200 | * |
| 201 | * As reading from and writing to the stat files can be slow, only |
| 202 | * root is allowed to do the read/write to limit impact to system |
| 203 | * performance. |
| 204 | */ |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 205 | for (i = 0; i < lockevent_num; i++) |
| 206 | if (!debugfs_create_file(lockevent_names[i], 0400, d_counts, |
| 207 | (void *)(long)i, &fops_lockevent)) |
Davidlohr Bueso | b96bbdd | 2016-04-19 21:17:25 -0700 | [diff] [blame] | 208 | goto fail_undo; |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 209 | |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 210 | if (!debugfs_create_file(lockevent_names[LOCKEVENT_reset_cnts], 0200, |
| 211 | d_counts, (void *)(long)LOCKEVENT_reset_cnts, |
| 212 | &fops_lockevent)) |
Davidlohr Bueso | b96bbdd | 2016-04-19 21:17:25 -0700 | [diff] [blame] | 213 | goto fail_undo; |
| 214 | |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 215 | return 0; |
Davidlohr Bueso | b96bbdd | 2016-04-19 21:17:25 -0700 | [diff] [blame] | 216 | fail_undo: |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 217 | debugfs_remove_recursive(d_counts); |
Davidlohr Bueso | b96bbdd | 2016-04-19 21:17:25 -0700 | [diff] [blame] | 218 | out: |
| 219 | pr_warn("Could not create 'qlockstat' debugfs entries\n"); |
| 220 | return -ENOMEM; |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 221 | } |
| 222 | fs_initcall(init_qspinlock_stat); |
| 223 | |
| 224 | /* |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 225 | * PV hash hop count |
| 226 | */ |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 227 | static inline void lockevent_pv_hop(int hopcnt) |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 228 | { |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 229 | this_cpu_add(EVENT_COUNT(pv_hash_hops), hopcnt); |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /* |
| 233 | * Replacement function for pv_kick() |
| 234 | */ |
| 235 | static inline void __pv_kick(int cpu) |
| 236 | { |
| 237 | u64 start = sched_clock(); |
| 238 | |
| 239 | per_cpu(pv_kick_time, cpu) = start; |
| 240 | pv_kick(cpu); |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 241 | this_cpu_add(EVENT_COUNT(pv_latency_kick), sched_clock() - start); |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /* |
| 245 | * Replacement function for pv_wait() |
| 246 | */ |
| 247 | static inline void __pv_wait(u8 *ptr, u8 val) |
| 248 | { |
| 249 | u64 *pkick_time = this_cpu_ptr(&pv_kick_time); |
| 250 | |
| 251 | *pkick_time = 0; |
| 252 | pv_wait(ptr, val); |
| 253 | if (*pkick_time) { |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 254 | this_cpu_add(EVENT_COUNT(pv_latency_wake), |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 255 | sched_clock() - *pkick_time); |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 256 | lockevent_inc(pv_kick_wake); |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
| 260 | #define pv_kick(c) __pv_kick(c) |
| 261 | #define pv_wait(p, v) __pv_wait(p, v) |
| 262 | |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 263 | #else /* CONFIG_QUEUED_LOCK_STAT */ |
| 264 | |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame^] | 265 | static inline void lockevent_pv_hop(int hopcnt) { } |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 266 | |
| 267 | #endif /* CONFIG_QUEUED_LOCK_STAT */ |