Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 2 | /* |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 3 | * |
Waiman Long | fb346fd | 2019-04-04 13:43:17 -0400 | [diff] [blame] | 4 | * Authors: Waiman Long <longman@redhat.com> |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 5 | */ |
| 6 | |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame] | 7 | #include "lock_events.h" |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 8 | |
Waiman Long | fb346fd | 2019-04-04 13:43:17 -0400 | [diff] [blame] | 9 | #ifdef CONFIG_LOCK_EVENT_COUNTS |
| 10 | #ifdef CONFIG_PARAVIRT_SPINLOCKS |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 11 | /* |
Waiman Long | fb346fd | 2019-04-04 13:43:17 -0400 | [diff] [blame] | 12 | * Collect pvqspinlock locking event counts |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 13 | */ |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 14 | #include <linux/sched.h> |
Ingo Molnar | e601757 | 2017-02-01 16:36:40 +0100 | [diff] [blame] | 15 | #include <linux/sched/clock.h> |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 16 | #include <linux/fs.h> |
| 17 | |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame] | 18 | #define EVENT_COUNT(ev) lockevents[LOCKEVENT_ ## ev] |
| 19 | |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 20 | /* |
Waiman Long | fb346fd | 2019-04-04 13:43:17 -0400 | [diff] [blame] | 21 | * PV specific per-cpu counter |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 22 | */ |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 23 | static DEFINE_PER_CPU(u64, pv_kick_time); |
| 24 | |
| 25 | /* |
Waiman Long | fb346fd | 2019-04-04 13:43:17 -0400 | [diff] [blame] | 26 | * Function to read and return the PV qspinlock counts. |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 27 | * |
| 28 | * The following counters are handled specially: |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame] | 29 | * 1. pv_latency_kick |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 30 | * Average kick latency (ns) = pv_latency_kick/pv_kick_unlock |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame] | 31 | * 2. pv_latency_wake |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 32 | * Average wake latency (ns) = pv_latency_wake/pv_kick_wake |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame] | 33 | * 3. pv_hash_hops |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 34 | * Average hops/hash = pv_hash_hops/pv_kick_unlock |
| 35 | */ |
Waiman Long | fb346fd | 2019-04-04 13:43:17 -0400 | [diff] [blame] | 36 | ssize_t lockevent_read(struct file *file, char __user *user_buf, |
| 37 | size_t count, loff_t *ppos) |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 38 | { |
| 39 | char buf[64]; |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame] | 40 | int cpu, id, len; |
| 41 | u64 sum = 0, kicks = 0; |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * Get the counter ID stored in file->f_inode->i_private |
| 45 | */ |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame] | 46 | id = (long)file_inode(file)->i_private; |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 47 | |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame] | 48 | if (id >= lockevent_num) |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 49 | return -EBADF; |
| 50 | |
| 51 | for_each_possible_cpu(cpu) { |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame] | 52 | sum += per_cpu(lockevents[id], cpu); |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 53 | /* |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame] | 54 | * Need to sum additional counters for some of them |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 55 | */ |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame] | 56 | switch (id) { |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 57 | |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame] | 58 | case LOCKEVENT_pv_latency_kick: |
| 59 | case LOCKEVENT_pv_hash_hops: |
| 60 | kicks += per_cpu(EVENT_COUNT(pv_kick_unlock), cpu); |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 61 | break; |
| 62 | |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame] | 63 | case LOCKEVENT_pv_latency_wake: |
| 64 | kicks += per_cpu(EVENT_COUNT(pv_kick_wake), cpu); |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 65 | break; |
| 66 | } |
| 67 | } |
| 68 | |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame] | 69 | if (id == LOCKEVENT_pv_hash_hops) { |
Davidlohr Bueso | 6687659 | 2016-04-17 23:31:41 -0700 | [diff] [blame] | 70 | u64 frac = 0; |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 71 | |
Davidlohr Bueso | 6687659 | 2016-04-17 23:31:41 -0700 | [diff] [blame] | 72 | if (kicks) { |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame] | 73 | frac = 100ULL * do_div(sum, kicks); |
Davidlohr Bueso | 6687659 | 2016-04-17 23:31:41 -0700 | [diff] [blame] | 74 | frac = DIV_ROUND_CLOSEST_ULL(frac, kicks); |
| 75 | } |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 76 | |
| 77 | /* |
| 78 | * Return a X.XX decimal number |
| 79 | */ |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame] | 80 | len = snprintf(buf, sizeof(buf) - 1, "%llu.%02llu\n", |
| 81 | sum, frac); |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 82 | } else { |
| 83 | /* |
| 84 | * Round to the nearest ns |
| 85 | */ |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame] | 86 | if ((id == LOCKEVENT_pv_latency_kick) || |
| 87 | (id == LOCKEVENT_pv_latency_wake)) { |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 88 | if (kicks) |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame] | 89 | sum = DIV_ROUND_CLOSEST_ULL(sum, kicks); |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 90 | } |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame] | 91 | len = snprintf(buf, sizeof(buf) - 1, "%llu\n", sum); |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | return simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 95 | } |
| 96 | |
| 97 | /* |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 98 | * PV hash hop count |
| 99 | */ |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame] | 100 | static inline void lockevent_pv_hop(int hopcnt) |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 101 | { |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame] | 102 | this_cpu_add(EVENT_COUNT(pv_hash_hops), hopcnt); |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /* |
| 106 | * Replacement function for pv_kick() |
| 107 | */ |
| 108 | static inline void __pv_kick(int cpu) |
| 109 | { |
| 110 | u64 start = sched_clock(); |
| 111 | |
| 112 | per_cpu(pv_kick_time, cpu) = start; |
| 113 | pv_kick(cpu); |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame] | 114 | this_cpu_add(EVENT_COUNT(pv_latency_kick), sched_clock() - start); |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /* |
| 118 | * Replacement function for pv_wait() |
| 119 | */ |
| 120 | static inline void __pv_wait(u8 *ptr, u8 val) |
| 121 | { |
| 122 | u64 *pkick_time = this_cpu_ptr(&pv_kick_time); |
| 123 | |
| 124 | *pkick_time = 0; |
| 125 | pv_wait(ptr, val); |
| 126 | if (*pkick_time) { |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame] | 127 | this_cpu_add(EVENT_COUNT(pv_latency_wake), |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 128 | sched_clock() - *pkick_time); |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame] | 129 | lockevent_inc(pv_kick_wake); |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | #define pv_kick(c) __pv_kick(c) |
| 134 | #define pv_wait(p, v) __pv_wait(p, v) |
| 135 | |
Waiman Long | fb346fd | 2019-04-04 13:43:17 -0400 | [diff] [blame] | 136 | #endif /* CONFIG_PARAVIRT_SPINLOCKS */ |
| 137 | |
| 138 | #else /* CONFIG_LOCK_EVENT_COUNTS */ |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 139 | |
Waiman Long | ad53fa1 | 2019-04-04 13:43:16 -0400 | [diff] [blame] | 140 | static inline void lockevent_pv_hop(int hopcnt) { } |
Waiman Long | 45e898b | 2015-11-09 19:09:25 -0500 | [diff] [blame] | 141 | |
Waiman Long | fb346fd | 2019-04-04 13:43:17 -0400 | [diff] [blame] | 142 | #endif /* CONFIG_LOCK_EVENT_COUNTS */ |