blob: 54152670ff2489fd7997e6505f26da957c35b492 [file] [log] [blame]
Waiman Long45e898b2015-11-09 19:09:25 -05001/*
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 Longfb346fd2019-04-04 13:43:17 -040012 * Authors: Waiman Long <longman@redhat.com>
Waiman Long45e898b2015-11-09 19:09:25 -050013 */
14
Waiman Longad53fa12019-04-04 13:43:16 -040015#include "lock_events.h"
Waiman Long45e898b2015-11-09 19:09:25 -050016
Waiman Longfb346fd2019-04-04 13:43:17 -040017#ifdef CONFIG_LOCK_EVENT_COUNTS
18#ifdef CONFIG_PARAVIRT_SPINLOCKS
Waiman Long45e898b2015-11-09 19:09:25 -050019/*
Waiman Longfb346fd2019-04-04 13:43:17 -040020 * Collect pvqspinlock locking event counts
Waiman Long45e898b2015-11-09 19:09:25 -050021 */
Waiman Long45e898b2015-11-09 19:09:25 -050022#include <linux/sched.h>
Ingo Molnare6017572017-02-01 16:36:40 +010023#include <linux/sched/clock.h>
Waiman Long45e898b2015-11-09 19:09:25 -050024#include <linux/fs.h>
25
Waiman Longad53fa12019-04-04 13:43:16 -040026#define EVENT_COUNT(ev) lockevents[LOCKEVENT_ ## ev]
27
Waiman Long45e898b2015-11-09 19:09:25 -050028/*
Waiman Longfb346fd2019-04-04 13:43:17 -040029 * PV specific per-cpu counter
Waiman Long45e898b2015-11-09 19:09:25 -050030 */
Waiman Long45e898b2015-11-09 19:09:25 -050031static DEFINE_PER_CPU(u64, pv_kick_time);
32
33/*
Waiman Longfb346fd2019-04-04 13:43:17 -040034 * Function to read and return the PV qspinlock counts.
Waiman Long45e898b2015-11-09 19:09:25 -050035 *
36 * The following counters are handled specially:
Waiman Longad53fa12019-04-04 13:43:16 -040037 * 1. pv_latency_kick
Waiman Long45e898b2015-11-09 19:09:25 -050038 * Average kick latency (ns) = pv_latency_kick/pv_kick_unlock
Waiman Longad53fa12019-04-04 13:43:16 -040039 * 2. pv_latency_wake
Waiman Long45e898b2015-11-09 19:09:25 -050040 * Average wake latency (ns) = pv_latency_wake/pv_kick_wake
Waiman Longad53fa12019-04-04 13:43:16 -040041 * 3. pv_hash_hops
Waiman Long45e898b2015-11-09 19:09:25 -050042 * Average hops/hash = pv_hash_hops/pv_kick_unlock
43 */
Waiman Longfb346fd2019-04-04 13:43:17 -040044ssize_t lockevent_read(struct file *file, char __user *user_buf,
45 size_t count, loff_t *ppos)
Waiman Long45e898b2015-11-09 19:09:25 -050046{
47 char buf[64];
Waiman Longad53fa12019-04-04 13:43:16 -040048 int cpu, id, len;
49 u64 sum = 0, kicks = 0;
Waiman Long45e898b2015-11-09 19:09:25 -050050
51 /*
52 * Get the counter ID stored in file->f_inode->i_private
53 */
Waiman Longad53fa12019-04-04 13:43:16 -040054 id = (long)file_inode(file)->i_private;
Waiman Long45e898b2015-11-09 19:09:25 -050055
Waiman Longad53fa12019-04-04 13:43:16 -040056 if (id >= lockevent_num)
Waiman Long45e898b2015-11-09 19:09:25 -050057 return -EBADF;
58
59 for_each_possible_cpu(cpu) {
Waiman Longad53fa12019-04-04 13:43:16 -040060 sum += per_cpu(lockevents[id], cpu);
Waiman Long45e898b2015-11-09 19:09:25 -050061 /*
Waiman Longad53fa12019-04-04 13:43:16 -040062 * Need to sum additional counters for some of them
Waiman Long45e898b2015-11-09 19:09:25 -050063 */
Waiman Longad53fa12019-04-04 13:43:16 -040064 switch (id) {
Waiman Long45e898b2015-11-09 19:09:25 -050065
Waiman Longad53fa12019-04-04 13:43:16 -040066 case LOCKEVENT_pv_latency_kick:
67 case LOCKEVENT_pv_hash_hops:
68 kicks += per_cpu(EVENT_COUNT(pv_kick_unlock), cpu);
Waiman Long45e898b2015-11-09 19:09:25 -050069 break;
70
Waiman Longad53fa12019-04-04 13:43:16 -040071 case LOCKEVENT_pv_latency_wake:
72 kicks += per_cpu(EVENT_COUNT(pv_kick_wake), cpu);
Waiman Long45e898b2015-11-09 19:09:25 -050073 break;
74 }
75 }
76
Waiman Longad53fa12019-04-04 13:43:16 -040077 if (id == LOCKEVENT_pv_hash_hops) {
Davidlohr Bueso66876592016-04-17 23:31:41 -070078 u64 frac = 0;
Waiman Long45e898b2015-11-09 19:09:25 -050079
Davidlohr Bueso66876592016-04-17 23:31:41 -070080 if (kicks) {
Waiman Longad53fa12019-04-04 13:43:16 -040081 frac = 100ULL * do_div(sum, kicks);
Davidlohr Bueso66876592016-04-17 23:31:41 -070082 frac = DIV_ROUND_CLOSEST_ULL(frac, kicks);
83 }
Waiman Long45e898b2015-11-09 19:09:25 -050084
85 /*
86 * Return a X.XX decimal number
87 */
Waiman Longad53fa12019-04-04 13:43:16 -040088 len = snprintf(buf, sizeof(buf) - 1, "%llu.%02llu\n",
89 sum, frac);
Waiman Long45e898b2015-11-09 19:09:25 -050090 } else {
91 /*
92 * Round to the nearest ns
93 */
Waiman Longad53fa12019-04-04 13:43:16 -040094 if ((id == LOCKEVENT_pv_latency_kick) ||
95 (id == LOCKEVENT_pv_latency_wake)) {
Waiman Long45e898b2015-11-09 19:09:25 -050096 if (kicks)
Waiman Longad53fa12019-04-04 13:43:16 -040097 sum = DIV_ROUND_CLOSEST_ULL(sum, kicks);
Waiman Long45e898b2015-11-09 19:09:25 -050098 }
Waiman Longad53fa12019-04-04 13:43:16 -040099 len = snprintf(buf, sizeof(buf) - 1, "%llu\n", sum);
Waiman Long45e898b2015-11-09 19:09:25 -0500100 }
101
102 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
103}
104
105/*
Waiman Long45e898b2015-11-09 19:09:25 -0500106 * PV hash hop count
107 */
Waiman Longad53fa12019-04-04 13:43:16 -0400108static inline void lockevent_pv_hop(int hopcnt)
Waiman Long45e898b2015-11-09 19:09:25 -0500109{
Waiman Longad53fa12019-04-04 13:43:16 -0400110 this_cpu_add(EVENT_COUNT(pv_hash_hops), hopcnt);
Waiman Long45e898b2015-11-09 19:09:25 -0500111}
112
113/*
114 * Replacement function for pv_kick()
115 */
116static 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 Longad53fa12019-04-04 13:43:16 -0400122 this_cpu_add(EVENT_COUNT(pv_latency_kick), sched_clock() - start);
Waiman Long45e898b2015-11-09 19:09:25 -0500123}
124
125/*
126 * Replacement function for pv_wait()
127 */
128static 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 Longad53fa12019-04-04 13:43:16 -0400135 this_cpu_add(EVENT_COUNT(pv_latency_wake),
Waiman Long45e898b2015-11-09 19:09:25 -0500136 sched_clock() - *pkick_time);
Waiman Longad53fa12019-04-04 13:43:16 -0400137 lockevent_inc(pv_kick_wake);
Waiman Long45e898b2015-11-09 19:09:25 -0500138 }
139}
140
141#define pv_kick(c) __pv_kick(c)
142#define pv_wait(p, v) __pv_wait(p, v)
143
Waiman Longfb346fd2019-04-04 13:43:17 -0400144#endif /* CONFIG_PARAVIRT_SPINLOCKS */
145
146#else /* CONFIG_LOCK_EVENT_COUNTS */
Waiman Long45e898b2015-11-09 19:09:25 -0500147
Waiman Longad53fa12019-04-04 13:43:16 -0400148static inline void lockevent_pv_hop(int hopcnt) { }
Waiman Long45e898b2015-11-09 19:09:25 -0500149
Waiman Longfb346fd2019-04-04 13:43:17 -0400150#endif /* CONFIG_LOCK_EVENT_COUNTS */