2015-11-10 07:09:25 +07:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* Authors: Waiman Long <waiman.long@hpe.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When queued spinlock statistical counters are enabled, the following
|
|
|
|
* debugfs files will be created for reporting the counter values:
|
|
|
|
*
|
|
|
|
* <debugfs>/qlockstat/
|
|
|
|
* pv_hash_hops - average # of hops per hashing operation
|
|
|
|
* pv_kick_unlock - # of vCPU kicks issued at unlock time
|
|
|
|
* pv_kick_wake - # of vCPU kicks used for computing pv_latency_wake
|
|
|
|
* pv_latency_kick - average latency (ns) of vCPU kick operation
|
|
|
|
* pv_latency_wake - average latency (ns) from vCPU kick to wakeup
|
2015-11-11 04:18:56 +07:00
|
|
|
* pv_lock_stealing - # of lock stealing operations
|
2016-05-31 23:53:47 +07:00
|
|
|
* pv_spurious_wakeup - # of spurious wakeups in non-head vCPUs
|
|
|
|
* pv_wait_again - # of wait's after a queue head vCPU kick
|
2015-11-10 07:09:27 +07:00
|
|
|
* pv_wait_early - # of early vCPU wait's
|
2015-11-10 07:09:25 +07:00
|
|
|
* pv_wait_head - # of vCPU wait's at the queue head
|
|
|
|
* pv_wait_node - # of vCPU wait's at a non-head queue node
|
2018-04-26 17:34:27 +07:00
|
|
|
* lock_pending - # of locking operations via pending code
|
|
|
|
* lock_slowpath - # of locking operations via MCS lock queue
|
2019-01-30 04:53:46 +07:00
|
|
|
* lock_use_node2 - # of locking operations that use 2nd per-CPU node
|
|
|
|
* lock_use_node3 - # of locking operations that use 3rd per-CPU node
|
|
|
|
* lock_use_node4 - # of locking operations that use 4th per-CPU node
|
|
|
|
* lock_no_node - # of locking operations without using per-CPU node
|
|
|
|
*
|
|
|
|
* Subtracting lock_use_node[234] from lock_slowpath will give you
|
|
|
|
* lock_use_node1.
|
2015-11-10 07:09:25 +07:00
|
|
|
*
|
2019-04-05 00:43:16 +07:00
|
|
|
* Writing to the special ".reset_counts" file will reset all the above
|
|
|
|
* counter values.
|
2015-11-10 07:09:25 +07:00
|
|
|
*
|
|
|
|
* These statistical counters are implemented as per-cpu variables which are
|
|
|
|
* summed and computed whenever the corresponding debugfs files are read. This
|
|
|
|
* minimizes added overhead making the counters usable even in a production
|
|
|
|
* environment.
|
|
|
|
*
|
|
|
|
* There may be slight difference between pv_kick_wake and pv_kick_unlock.
|
|
|
|
*/
|
2019-04-05 00:43:16 +07:00
|
|
|
#include "lock_events.h"
|
2015-11-10 07:09:25 +07:00
|
|
|
|
|
|
|
#ifdef CONFIG_QUEUED_LOCK_STAT
|
|
|
|
/*
|
|
|
|
* Collect pvqspinlock statistics
|
|
|
|
*/
|
|
|
|
#include <linux/debugfs.h>
|
|
|
|
#include <linux/sched.h>
|
2017-02-01 22:36:40 +07:00
|
|
|
#include <linux/sched/clock.h>
|
2015-11-10 07:09:25 +07:00
|
|
|
#include <linux/fs.h>
|
|
|
|
|
2019-04-05 00:43:16 +07:00
|
|
|
#define EVENT_COUNT(ev) lockevents[LOCKEVENT_ ## ev]
|
|
|
|
|
|
|
|
#undef LOCK_EVENT
|
|
|
|
#define LOCK_EVENT(name) [LOCKEVENT_ ## name] = #name,
|
|
|
|
|
|
|
|
static const char * const lockevent_names[lockevent_num + 1] = {
|
|
|
|
|
|
|
|
#include "lock_events_list.h"
|
|
|
|
|
|
|
|
[LOCKEVENT_reset_cnts] = ".reset_counts",
|
2015-11-10 07:09:25 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Per-cpu counters
|
|
|
|
*/
|
2019-04-05 00:43:16 +07:00
|
|
|
DEFINE_PER_CPU(unsigned long, lockevents[lockevent_num]);
|
2015-11-10 07:09:25 +07:00
|
|
|
static DEFINE_PER_CPU(u64, pv_kick_time);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Function to read and return the qlock statistical counter values
|
|
|
|
*
|
|
|
|
* The following counters are handled specially:
|
2019-04-05 00:43:16 +07:00
|
|
|
* 1. pv_latency_kick
|
2015-11-10 07:09:25 +07:00
|
|
|
* Average kick latency (ns) = pv_latency_kick/pv_kick_unlock
|
2019-04-05 00:43:16 +07:00
|
|
|
* 2. pv_latency_wake
|
2015-11-10 07:09:25 +07:00
|
|
|
* Average wake latency (ns) = pv_latency_wake/pv_kick_wake
|
2019-04-05 00:43:16 +07:00
|
|
|
* 3. pv_hash_hops
|
2015-11-10 07:09:25 +07:00
|
|
|
* Average hops/hash = pv_hash_hops/pv_kick_unlock
|
|
|
|
*/
|
2019-04-05 00:43:16 +07:00
|
|
|
static ssize_t lockevent_read(struct file *file, char __user *user_buf,
|
|
|
|
size_t count, loff_t *ppos)
|
2015-11-10 07:09:25 +07:00
|
|
|
{
|
|
|
|
char buf[64];
|
2019-04-05 00:43:16 +07:00
|
|
|
int cpu, id, len;
|
|
|
|
u64 sum = 0, kicks = 0;
|
2015-11-10 07:09:25 +07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the counter ID stored in file->f_inode->i_private
|
|
|
|
*/
|
2019-04-05 00:43:16 +07:00
|
|
|
id = (long)file_inode(file)->i_private;
|
2015-11-10 07:09:25 +07:00
|
|
|
|
2019-04-05 00:43:16 +07:00
|
|
|
if (id >= lockevent_num)
|
2015-11-10 07:09:25 +07:00
|
|
|
return -EBADF;
|
|
|
|
|
|
|
|
for_each_possible_cpu(cpu) {
|
2019-04-05 00:43:16 +07:00
|
|
|
sum += per_cpu(lockevents[id], cpu);
|
2015-11-10 07:09:25 +07:00
|
|
|
/*
|
2019-04-05 00:43:16 +07:00
|
|
|
* Need to sum additional counters for some of them
|
2015-11-10 07:09:25 +07:00
|
|
|
*/
|
2019-04-05 00:43:16 +07:00
|
|
|
switch (id) {
|
2015-11-10 07:09:25 +07:00
|
|
|
|
2019-04-05 00:43:16 +07:00
|
|
|
case LOCKEVENT_pv_latency_kick:
|
|
|
|
case LOCKEVENT_pv_hash_hops:
|
|
|
|
kicks += per_cpu(EVENT_COUNT(pv_kick_unlock), cpu);
|
2015-11-10 07:09:25 +07:00
|
|
|
break;
|
|
|
|
|
2019-04-05 00:43:16 +07:00
|
|
|
case LOCKEVENT_pv_latency_wake:
|
|
|
|
kicks += per_cpu(EVENT_COUNT(pv_kick_wake), cpu);
|
2015-11-10 07:09:25 +07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 00:43:16 +07:00
|
|
|
if (id == LOCKEVENT_pv_hash_hops) {
|
2016-04-18 13:31:41 +07:00
|
|
|
u64 frac = 0;
|
2015-11-10 07:09:25 +07:00
|
|
|
|
2016-04-18 13:31:41 +07:00
|
|
|
if (kicks) {
|
2019-04-05 00:43:16 +07:00
|
|
|
frac = 100ULL * do_div(sum, kicks);
|
2016-04-18 13:31:41 +07:00
|
|
|
frac = DIV_ROUND_CLOSEST_ULL(frac, kicks);
|
|
|
|
}
|
2015-11-10 07:09:25 +07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return a X.XX decimal number
|
|
|
|
*/
|
2019-04-05 00:43:16 +07:00
|
|
|
len = snprintf(buf, sizeof(buf) - 1, "%llu.%02llu\n",
|
|
|
|
sum, frac);
|
2015-11-10 07:09:25 +07:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Round to the nearest ns
|
|
|
|
*/
|
2019-04-05 00:43:16 +07:00
|
|
|
if ((id == LOCKEVENT_pv_latency_kick) ||
|
|
|
|
(id == LOCKEVENT_pv_latency_wake)) {
|
2015-11-10 07:09:25 +07:00
|
|
|
if (kicks)
|
2019-04-05 00:43:16 +07:00
|
|
|
sum = DIV_ROUND_CLOSEST_ULL(sum, kicks);
|
2015-11-10 07:09:25 +07:00
|
|
|
}
|
2019-04-05 00:43:16 +07:00
|
|
|
len = snprintf(buf, sizeof(buf) - 1, "%llu\n", sum);
|
2015-11-10 07:09:25 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
return simple_read_from_buffer(user_buf, count, ppos, buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Function to handle write request
|
|
|
|
*
|
2019-04-05 00:43:16 +07:00
|
|
|
* When id = .reset_cnts, reset all the counter values.
|
2015-11-10 07:09:25 +07:00
|
|
|
*/
|
2019-04-05 00:43:16 +07:00
|
|
|
static ssize_t lockevent_write(struct file *file, const char __user *user_buf,
|
2015-11-10 07:09:25 +07:00
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
int cpu;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the counter ID stored in file->f_inode->i_private
|
|
|
|
*/
|
2019-04-05 00:43:16 +07:00
|
|
|
if ((long)file_inode(file)->i_private != LOCKEVENT_reset_cnts)
|
2015-11-10 07:09:25 +07:00
|
|
|
return count;
|
|
|
|
|
|
|
|
for_each_possible_cpu(cpu) {
|
|
|
|
int i;
|
2019-04-05 00:43:16 +07:00
|
|
|
unsigned long *ptr = per_cpu_ptr(lockevents, cpu);
|
2015-11-10 07:09:25 +07:00
|
|
|
|
2019-04-05 00:43:16 +07:00
|
|
|
for (i = 0 ; i < lockevent_num; i++)
|
2015-11-10 07:09:25 +07:00
|
|
|
WRITE_ONCE(ptr[i], 0);
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Debugfs data structures
|
|
|
|
*/
|
2019-04-05 00:43:16 +07:00
|
|
|
static const struct file_operations fops_lockevent = {
|
|
|
|
.read = lockevent_read,
|
|
|
|
.write = lockevent_write,
|
2015-11-10 07:09:25 +07:00
|
|
|
.llseek = default_llseek,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize debugfs for the qspinlock statistical counters
|
|
|
|
*/
|
|
|
|
static int __init init_qspinlock_stat(void)
|
|
|
|
{
|
2019-04-05 00:43:16 +07:00
|
|
|
struct dentry *d_counts = debugfs_create_dir("qlockstat", NULL);
|
2015-11-10 07:09:25 +07:00
|
|
|
int i;
|
|
|
|
|
2019-04-05 00:43:16 +07:00
|
|
|
if (!d_counts)
|
2016-04-20 11:17:25 +07:00
|
|
|
goto out;
|
2015-11-10 07:09:25 +07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the debugfs files
|
|
|
|
*
|
|
|
|
* As reading from and writing to the stat files can be slow, only
|
|
|
|
* root is allowed to do the read/write to limit impact to system
|
|
|
|
* performance.
|
|
|
|
*/
|
2019-04-05 00:43:16 +07:00
|
|
|
for (i = 0; i < lockevent_num; i++)
|
|
|
|
if (!debugfs_create_file(lockevent_names[i], 0400, d_counts,
|
|
|
|
(void *)(long)i, &fops_lockevent))
|
2016-04-20 11:17:25 +07:00
|
|
|
goto fail_undo;
|
|
|
|
|
2019-04-05 00:43:16 +07:00
|
|
|
if (!debugfs_create_file(lockevent_names[LOCKEVENT_reset_cnts], 0200,
|
|
|
|
d_counts, (void *)(long)LOCKEVENT_reset_cnts,
|
|
|
|
&fops_lockevent))
|
2016-04-20 11:17:25 +07:00
|
|
|
goto fail_undo;
|
2015-11-10 07:09:25 +07:00
|
|
|
|
|
|
|
return 0;
|
2016-04-20 11:17:25 +07:00
|
|
|
fail_undo:
|
2019-04-05 00:43:16 +07:00
|
|
|
debugfs_remove_recursive(d_counts);
|
2016-04-20 11:17:25 +07:00
|
|
|
out:
|
|
|
|
pr_warn("Could not create 'qlockstat' debugfs entries\n");
|
|
|
|
return -ENOMEM;
|
2015-11-10 07:09:25 +07:00
|
|
|
}
|
|
|
|
fs_initcall(init_qspinlock_stat);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PV hash hop count
|
|
|
|
*/
|
2019-04-05 00:43:16 +07:00
|
|
|
static inline void lockevent_pv_hop(int hopcnt)
|
2015-11-10 07:09:25 +07:00
|
|
|
{
|
2019-04-05 00:43:16 +07:00
|
|
|
this_cpu_add(EVENT_COUNT(pv_hash_hops), hopcnt);
|
2015-11-10 07:09:25 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Replacement function for pv_kick()
|
|
|
|
*/
|
|
|
|
static inline void __pv_kick(int cpu)
|
|
|
|
{
|
|
|
|
u64 start = sched_clock();
|
|
|
|
|
|
|
|
per_cpu(pv_kick_time, cpu) = start;
|
|
|
|
pv_kick(cpu);
|
2019-04-05 00:43:16 +07:00
|
|
|
this_cpu_add(EVENT_COUNT(pv_latency_kick), sched_clock() - start);
|
2015-11-10 07:09:25 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Replacement function for pv_wait()
|
|
|
|
*/
|
|
|
|
static inline void __pv_wait(u8 *ptr, u8 val)
|
|
|
|
{
|
|
|
|
u64 *pkick_time = this_cpu_ptr(&pv_kick_time);
|
|
|
|
|
|
|
|
*pkick_time = 0;
|
|
|
|
pv_wait(ptr, val);
|
|
|
|
if (*pkick_time) {
|
2019-04-05 00:43:16 +07:00
|
|
|
this_cpu_add(EVENT_COUNT(pv_latency_wake),
|
2015-11-10 07:09:25 +07:00
|
|
|
sched_clock() - *pkick_time);
|
2019-04-05 00:43:16 +07:00
|
|
|
lockevent_inc(pv_kick_wake);
|
2015-11-10 07:09:25 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define pv_kick(c) __pv_kick(c)
|
|
|
|
#define pv_wait(p, v) __pv_wait(p, v)
|
|
|
|
|
|
|
|
#else /* CONFIG_QUEUED_LOCK_STAT */
|
|
|
|
|
2019-04-05 00:43:16 +07:00
|
|
|
static inline void lockevent_pv_hop(int hopcnt) { }
|
2015-11-10 07:09:25 +07:00
|
|
|
|
|
|
|
#endif /* CONFIG_QUEUED_LOCK_STAT */
|