blob: e625bb410aa2d695d756c193c83207c5025cdd62 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Waiman Long45e898b2015-11-09 19:09:25 -05002/*
Waiman Long45e898b2015-11-09 19:09:25 -05003 *
Waiman Longfb346fd2019-04-04 13:43:17 -04004 * Authors: Waiman Long <longman@redhat.com>
Waiman Long45e898b2015-11-09 19:09:25 -05005 */
6
Waiman Longad53fa12019-04-04 13:43:16 -04007#include "lock_events.h"
Waiman Long45e898b2015-11-09 19:09:25 -05008
Waiman Longfb346fd2019-04-04 13:43:17 -04009#ifdef CONFIG_LOCK_EVENT_COUNTS
10#ifdef CONFIG_PARAVIRT_SPINLOCKS
Waiman Long45e898b2015-11-09 19:09:25 -050011/*
Waiman Longfb346fd2019-04-04 13:43:17 -040012 * Collect pvqspinlock locking event counts
Waiman Long45e898b2015-11-09 19:09:25 -050013 */
Waiman Long45e898b2015-11-09 19:09:25 -050014#include <linux/sched.h>
Ingo Molnare6017572017-02-01 16:36:40 +010015#include <linux/sched/clock.h>
Waiman Long45e898b2015-11-09 19:09:25 -050016#include <linux/fs.h>
17
Waiman Longad53fa12019-04-04 13:43:16 -040018#define EVENT_COUNT(ev) lockevents[LOCKEVENT_ ## ev]
19
Waiman Long45e898b2015-11-09 19:09:25 -050020/*
Waiman Longfb346fd2019-04-04 13:43:17 -040021 * PV specific per-cpu counter
Waiman Long45e898b2015-11-09 19:09:25 -050022 */
Waiman Long45e898b2015-11-09 19:09:25 -050023static DEFINE_PER_CPU(u64, pv_kick_time);
24
25/*
Waiman Longfb346fd2019-04-04 13:43:17 -040026 * Function to read and return the PV qspinlock counts.
Waiman Long45e898b2015-11-09 19:09:25 -050027 *
28 * The following counters are handled specially:
Waiman Longad53fa12019-04-04 13:43:16 -040029 * 1. pv_latency_kick
Waiman Long45e898b2015-11-09 19:09:25 -050030 * Average kick latency (ns) = pv_latency_kick/pv_kick_unlock
Waiman Longad53fa12019-04-04 13:43:16 -040031 * 2. pv_latency_wake
Waiman Long45e898b2015-11-09 19:09:25 -050032 * Average wake latency (ns) = pv_latency_wake/pv_kick_wake
Waiman Longad53fa12019-04-04 13:43:16 -040033 * 3. pv_hash_hops
Waiman Long45e898b2015-11-09 19:09:25 -050034 * Average hops/hash = pv_hash_hops/pv_kick_unlock
35 */
Waiman Longfb346fd2019-04-04 13:43:17 -040036ssize_t lockevent_read(struct file *file, char __user *user_buf,
37 size_t count, loff_t *ppos)
Waiman Long45e898b2015-11-09 19:09:25 -050038{
39 char buf[64];
Waiman Longad53fa12019-04-04 13:43:16 -040040 int cpu, id, len;
41 u64 sum = 0, kicks = 0;
Waiman Long45e898b2015-11-09 19:09:25 -050042
43 /*
44 * Get the counter ID stored in file->f_inode->i_private
45 */
Waiman Longad53fa12019-04-04 13:43:16 -040046 id = (long)file_inode(file)->i_private;
Waiman Long45e898b2015-11-09 19:09:25 -050047
Waiman Longad53fa12019-04-04 13:43:16 -040048 if (id >= lockevent_num)
Waiman Long45e898b2015-11-09 19:09:25 -050049 return -EBADF;
50
51 for_each_possible_cpu(cpu) {
Waiman Longad53fa12019-04-04 13:43:16 -040052 sum += per_cpu(lockevents[id], cpu);
Waiman Long45e898b2015-11-09 19:09:25 -050053 /*
Waiman Longad53fa12019-04-04 13:43:16 -040054 * Need to sum additional counters for some of them
Waiman Long45e898b2015-11-09 19:09:25 -050055 */
Waiman Longad53fa12019-04-04 13:43:16 -040056 switch (id) {
Waiman Long45e898b2015-11-09 19:09:25 -050057
Waiman Longad53fa12019-04-04 13:43:16 -040058 case LOCKEVENT_pv_latency_kick:
59 case LOCKEVENT_pv_hash_hops:
60 kicks += per_cpu(EVENT_COUNT(pv_kick_unlock), cpu);
Waiman Long45e898b2015-11-09 19:09:25 -050061 break;
62
Waiman Longad53fa12019-04-04 13:43:16 -040063 case LOCKEVENT_pv_latency_wake:
64 kicks += per_cpu(EVENT_COUNT(pv_kick_wake), cpu);
Waiman Long45e898b2015-11-09 19:09:25 -050065 break;
66 }
67 }
68
Waiman Longad53fa12019-04-04 13:43:16 -040069 if (id == LOCKEVENT_pv_hash_hops) {
Davidlohr Bueso66876592016-04-17 23:31:41 -070070 u64 frac = 0;
Waiman Long45e898b2015-11-09 19:09:25 -050071
Davidlohr Bueso66876592016-04-17 23:31:41 -070072 if (kicks) {
Waiman Longad53fa12019-04-04 13:43:16 -040073 frac = 100ULL * do_div(sum, kicks);
Davidlohr Bueso66876592016-04-17 23:31:41 -070074 frac = DIV_ROUND_CLOSEST_ULL(frac, kicks);
75 }
Waiman Long45e898b2015-11-09 19:09:25 -050076
77 /*
78 * Return a X.XX decimal number
79 */
Waiman Longad53fa12019-04-04 13:43:16 -040080 len = snprintf(buf, sizeof(buf) - 1, "%llu.%02llu\n",
81 sum, frac);
Waiman Long45e898b2015-11-09 19:09:25 -050082 } else {
83 /*
84 * Round to the nearest ns
85 */
Waiman Longad53fa12019-04-04 13:43:16 -040086 if ((id == LOCKEVENT_pv_latency_kick) ||
87 (id == LOCKEVENT_pv_latency_wake)) {
Waiman Long45e898b2015-11-09 19:09:25 -050088 if (kicks)
Waiman Longad53fa12019-04-04 13:43:16 -040089 sum = DIV_ROUND_CLOSEST_ULL(sum, kicks);
Waiman Long45e898b2015-11-09 19:09:25 -050090 }
Waiman Longad53fa12019-04-04 13:43:16 -040091 len = snprintf(buf, sizeof(buf) - 1, "%llu\n", sum);
Waiman Long45e898b2015-11-09 19:09:25 -050092 }
93
94 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
95}
96
97/*
Waiman Long45e898b2015-11-09 19:09:25 -050098 * PV hash hop count
99 */
Waiman Longad53fa12019-04-04 13:43:16 -0400100static inline void lockevent_pv_hop(int hopcnt)
Waiman Long45e898b2015-11-09 19:09:25 -0500101{
Waiman Longad53fa12019-04-04 13:43:16 -0400102 this_cpu_add(EVENT_COUNT(pv_hash_hops), hopcnt);
Waiman Long45e898b2015-11-09 19:09:25 -0500103}
104
105/*
106 * Replacement function for pv_kick()
107 */
108static 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 Longad53fa12019-04-04 13:43:16 -0400114 this_cpu_add(EVENT_COUNT(pv_latency_kick), sched_clock() - start);
Waiman Long45e898b2015-11-09 19:09:25 -0500115}
116
117/*
118 * Replacement function for pv_wait()
119 */
120static 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 Longad53fa12019-04-04 13:43:16 -0400127 this_cpu_add(EVENT_COUNT(pv_latency_wake),
Waiman Long45e898b2015-11-09 19:09:25 -0500128 sched_clock() - *pkick_time);
Waiman Longad53fa12019-04-04 13:43:16 -0400129 lockevent_inc(pv_kick_wake);
Waiman Long45e898b2015-11-09 19:09:25 -0500130 }
131}
132
133#define pv_kick(c) __pv_kick(c)
134#define pv_wait(p, v) __pv_wait(p, v)
135
Waiman Longfb346fd2019-04-04 13:43:17 -0400136#endif /* CONFIG_PARAVIRT_SPINLOCKS */
137
138#else /* CONFIG_LOCK_EVENT_COUNTS */
Waiman Long45e898b2015-11-09 19:09:25 -0500139
Waiman Longad53fa12019-04-04 13:43:16 -0400140static inline void lockevent_pv_hop(int hopcnt) { }
Waiman Long45e898b2015-11-09 19:09:25 -0500141
Waiman Longfb346fd2019-04-04 13:43:17 -0400142#endif /* CONFIG_LOCK_EVENT_COUNTS */