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