123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- enum qlock_stats {
- qstat_pv_hash_hops,
- qstat_pv_kick_unlock,
- qstat_pv_kick_wake,
- qstat_pv_latency_kick,
- qstat_pv_latency_wake,
- qstat_pv_lock_stealing,
- qstat_pv_spurious_wakeup,
- qstat_pv_wait_again,
- qstat_pv_wait_early,
- qstat_pv_wait_head,
- qstat_pv_wait_node,
- qstat_lock_pending,
- qstat_lock_slowpath,
- qstat_num,
- qstat_reset_cnts = qstat_num,
- };
- #ifdef CONFIG_QUEUED_LOCK_STAT
- #include <linux/debugfs.h>
- #include <linux/sched.h>
- #include <linux/sched/clock.h>
- #include <linux/fs.h>
- static const char * const qstat_names[qstat_num + 1] = {
- [qstat_pv_hash_hops] = "pv_hash_hops",
- [qstat_pv_kick_unlock] = "pv_kick_unlock",
- [qstat_pv_kick_wake] = "pv_kick_wake",
- [qstat_pv_spurious_wakeup] = "pv_spurious_wakeup",
- [qstat_pv_latency_kick] = "pv_latency_kick",
- [qstat_pv_latency_wake] = "pv_latency_wake",
- [qstat_pv_lock_stealing] = "pv_lock_stealing",
- [qstat_pv_wait_again] = "pv_wait_again",
- [qstat_pv_wait_early] = "pv_wait_early",
- [qstat_pv_wait_head] = "pv_wait_head",
- [qstat_pv_wait_node] = "pv_wait_node",
- [qstat_lock_pending] = "lock_pending",
- [qstat_lock_slowpath] = "lock_slowpath",
- [qstat_reset_cnts] = "reset_counters",
- };
- static DEFINE_PER_CPU(unsigned long, qstats[qstat_num]);
- static DEFINE_PER_CPU(u64, pv_kick_time);
- static ssize_t qstat_read(struct file *file, char __user *user_buf,
- size_t count, loff_t *ppos)
- {
- char buf[64];
- int cpu, counter, len;
- u64 stat = 0, kicks = 0;
-
- counter = (long)file_inode(file)->i_private;
- if (counter >= qstat_num)
- return -EBADF;
- for_each_possible_cpu(cpu) {
- stat += per_cpu(qstats[counter], cpu);
-
- switch (counter) {
- case qstat_pv_latency_kick:
- case qstat_pv_hash_hops:
- kicks += per_cpu(qstats[qstat_pv_kick_unlock], cpu);
- break;
- case qstat_pv_latency_wake:
- kicks += per_cpu(qstats[qstat_pv_kick_wake], cpu);
- break;
- }
- }
- if (counter == qstat_pv_hash_hops) {
- u64 frac = 0;
- if (kicks) {
- frac = 100ULL * do_div(stat, kicks);
- frac = DIV_ROUND_CLOSEST_ULL(frac, kicks);
- }
-
- len = snprintf(buf, sizeof(buf) - 1, "%llu.%02llu\n", stat, frac);
- } else {
-
- if ((counter == qstat_pv_latency_kick) ||
- (counter == qstat_pv_latency_wake)) {
- if (kicks)
- stat = DIV_ROUND_CLOSEST_ULL(stat, kicks);
- }
- len = snprintf(buf, sizeof(buf) - 1, "%llu\n", stat);
- }
- return simple_read_from_buffer(user_buf, count, ppos, buf, len);
- }
- static ssize_t qstat_write(struct file *file, const char __user *user_buf,
- size_t count, loff_t *ppos)
- {
- int cpu;
-
- if ((long)file_inode(file)->i_private != qstat_reset_cnts)
- return count;
- for_each_possible_cpu(cpu) {
- int i;
- unsigned long *ptr = per_cpu_ptr(qstats, cpu);
- for (i = 0 ; i < qstat_num; i++)
- WRITE_ONCE(ptr[i], 0);
- }
- return count;
- }
- static const struct file_operations fops_qstat = {
- .read = qstat_read,
- .write = qstat_write,
- .llseek = default_llseek,
- };
- static int __init init_qspinlock_stat(void)
- {
- struct dentry *d_qstat = debugfs_create_dir("qlockstat", NULL);
- int i;
- if (!d_qstat)
- goto out;
-
- for (i = 0; i < qstat_num; i++)
- if (!debugfs_create_file(qstat_names[i], 0400, d_qstat,
- (void *)(long)i, &fops_qstat))
- goto fail_undo;
- if (!debugfs_create_file(qstat_names[qstat_reset_cnts], 0200, d_qstat,
- (void *)(long)qstat_reset_cnts, &fops_qstat))
- goto fail_undo;
- return 0;
- fail_undo:
- debugfs_remove_recursive(d_qstat);
- out:
- pr_warn("Could not create 'qlockstat' debugfs entries\n");
- return -ENOMEM;
- }
- fs_initcall(init_qspinlock_stat);
- static inline void qstat_inc(enum qlock_stats stat, bool cond)
- {
- if (cond)
- this_cpu_inc(qstats[stat]);
- }
- static inline void qstat_hop(int hopcnt)
- {
- this_cpu_add(qstats[qstat_pv_hash_hops], hopcnt);
- }
- static inline void __pv_kick(int cpu)
- {
- u64 start = sched_clock();
- per_cpu(pv_kick_time, cpu) = start;
- pv_kick(cpu);
- this_cpu_add(qstats[qstat_pv_latency_kick], sched_clock() - start);
- }
- 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) {
- this_cpu_add(qstats[qstat_pv_latency_wake],
- sched_clock() - *pkick_time);
- qstat_inc(qstat_pv_kick_wake, true);
- }
- }
- #define pv_kick(c) __pv_kick(c)
- #define pv_wait(p, v) __pv_wait(p, v)
- #else
- static inline void qstat_inc(enum qlock_stats stat, bool cond) { }
- static inline void qstat_hop(int hopcnt) { }
- #endif
|