qspinlock_stat.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. *
  12. * Authors: Waiman Long <waiman.long@hpe.com>
  13. */
  14. /*
  15. * When queued spinlock statistical counters are enabled, the following
  16. * debugfs files will be created for reporting the counter values:
  17. *
  18. * <debugfs>/qlockstat/
  19. * pv_hash_hops - average # of hops per hashing operation
  20. * pv_kick_unlock - # of vCPU kicks issued at unlock time
  21. * pv_kick_wake - # of vCPU kicks used for computing pv_latency_wake
  22. * pv_latency_kick - average latency (ns) of vCPU kick operation
  23. * pv_latency_wake - average latency (ns) from vCPU kick to wakeup
  24. * pv_lock_stealing - # of lock stealing operations
  25. * pv_spurious_wakeup - # of spurious wakeups
  26. * pv_wait_again - # of vCPU wait's that happened after a vCPU kick
  27. * pv_wait_early - # of early vCPU wait's
  28. * pv_wait_head - # of vCPU wait's at the queue head
  29. * pv_wait_node - # of vCPU wait's at a non-head queue node
  30. *
  31. * Writing to the "reset_counters" file will reset all the above counter
  32. * values.
  33. *
  34. * These statistical counters are implemented as per-cpu variables which are
  35. * summed and computed whenever the corresponding debugfs files are read. This
  36. * minimizes added overhead making the counters usable even in a production
  37. * environment.
  38. *
  39. * There may be slight difference between pv_kick_wake and pv_kick_unlock.
  40. */
  41. enum qlock_stats {
  42. qstat_pv_hash_hops,
  43. qstat_pv_kick_unlock,
  44. qstat_pv_kick_wake,
  45. qstat_pv_latency_kick,
  46. qstat_pv_latency_wake,
  47. qstat_pv_lock_stealing,
  48. qstat_pv_spurious_wakeup,
  49. qstat_pv_wait_again,
  50. qstat_pv_wait_early,
  51. qstat_pv_wait_head,
  52. qstat_pv_wait_node,
  53. qstat_num, /* Total number of statistical counters */
  54. qstat_reset_cnts = qstat_num,
  55. };
  56. #ifdef CONFIG_QUEUED_LOCK_STAT
  57. /*
  58. * Collect pvqspinlock statistics
  59. */
  60. #include <linux/debugfs.h>
  61. #include <linux/sched.h>
  62. #include <linux/fs.h>
  63. static const char * const qstat_names[qstat_num + 1] = {
  64. [qstat_pv_hash_hops] = "pv_hash_hops",
  65. [qstat_pv_kick_unlock] = "pv_kick_unlock",
  66. [qstat_pv_kick_wake] = "pv_kick_wake",
  67. [qstat_pv_spurious_wakeup] = "pv_spurious_wakeup",
  68. [qstat_pv_latency_kick] = "pv_latency_kick",
  69. [qstat_pv_latency_wake] = "pv_latency_wake",
  70. [qstat_pv_lock_stealing] = "pv_lock_stealing",
  71. [qstat_pv_wait_again] = "pv_wait_again",
  72. [qstat_pv_wait_early] = "pv_wait_early",
  73. [qstat_pv_wait_head] = "pv_wait_head",
  74. [qstat_pv_wait_node] = "pv_wait_node",
  75. [qstat_reset_cnts] = "reset_counters",
  76. };
  77. /*
  78. * Per-cpu counters
  79. */
  80. static DEFINE_PER_CPU(unsigned long, qstats[qstat_num]);
  81. static DEFINE_PER_CPU(u64, pv_kick_time);
  82. /*
  83. * Function to read and return the qlock statistical counter values
  84. *
  85. * The following counters are handled specially:
  86. * 1. qstat_pv_latency_kick
  87. * Average kick latency (ns) = pv_latency_kick/pv_kick_unlock
  88. * 2. qstat_pv_latency_wake
  89. * Average wake latency (ns) = pv_latency_wake/pv_kick_wake
  90. * 3. qstat_pv_hash_hops
  91. * Average hops/hash = pv_hash_hops/pv_kick_unlock
  92. */
  93. static ssize_t qstat_read(struct file *file, char __user *user_buf,
  94. size_t count, loff_t *ppos)
  95. {
  96. char buf[64];
  97. int cpu, counter, len;
  98. u64 stat = 0, kicks = 0;
  99. /*
  100. * Get the counter ID stored in file->f_inode->i_private
  101. */
  102. if (!file->f_inode) {
  103. WARN_ON_ONCE(1);
  104. return -EBADF;
  105. }
  106. counter = (long)(file->f_inode->i_private);
  107. if (counter >= qstat_num)
  108. return -EBADF;
  109. for_each_possible_cpu(cpu) {
  110. stat += per_cpu(qstats[counter], cpu);
  111. /*
  112. * Need to sum additional counter for some of them
  113. */
  114. switch (counter) {
  115. case qstat_pv_latency_kick:
  116. case qstat_pv_hash_hops:
  117. kicks += per_cpu(qstats[qstat_pv_kick_unlock], cpu);
  118. break;
  119. case qstat_pv_latency_wake:
  120. kicks += per_cpu(qstats[qstat_pv_kick_wake], cpu);
  121. break;
  122. }
  123. }
  124. if (counter == qstat_pv_hash_hops) {
  125. u64 frac;
  126. frac = 100ULL * do_div(stat, kicks);
  127. frac = DIV_ROUND_CLOSEST_ULL(frac, kicks);
  128. /*
  129. * Return a X.XX decimal number
  130. */
  131. len = snprintf(buf, sizeof(buf) - 1, "%llu.%02llu\n", stat, frac);
  132. } else {
  133. /*
  134. * Round to the nearest ns
  135. */
  136. if ((counter == qstat_pv_latency_kick) ||
  137. (counter == qstat_pv_latency_wake)) {
  138. stat = 0;
  139. if (kicks)
  140. stat = DIV_ROUND_CLOSEST_ULL(stat, kicks);
  141. }
  142. len = snprintf(buf, sizeof(buf) - 1, "%llu\n", stat);
  143. }
  144. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  145. }
  146. /*
  147. * Function to handle write request
  148. *
  149. * When counter = reset_cnts, reset all the counter values.
  150. * Since the counter updates aren't atomic, the resetting is done twice
  151. * to make sure that the counters are very likely to be all cleared.
  152. */
  153. static ssize_t qstat_write(struct file *file, const char __user *user_buf,
  154. size_t count, loff_t *ppos)
  155. {
  156. int cpu;
  157. /*
  158. * Get the counter ID stored in file->f_inode->i_private
  159. */
  160. if (!file->f_inode) {
  161. WARN_ON_ONCE(1);
  162. return -EBADF;
  163. }
  164. if ((long)(file->f_inode->i_private) != qstat_reset_cnts)
  165. return count;
  166. for_each_possible_cpu(cpu) {
  167. int i;
  168. unsigned long *ptr = per_cpu_ptr(qstats, cpu);
  169. for (i = 0 ; i < qstat_num; i++)
  170. WRITE_ONCE(ptr[i], 0);
  171. for (i = 0 ; i < qstat_num; i++)
  172. WRITE_ONCE(ptr[i], 0);
  173. }
  174. return count;
  175. }
  176. /*
  177. * Debugfs data structures
  178. */
  179. static const struct file_operations fops_qstat = {
  180. .read = qstat_read,
  181. .write = qstat_write,
  182. .llseek = default_llseek,
  183. };
  184. /*
  185. * Initialize debugfs for the qspinlock statistical counters
  186. */
  187. static int __init init_qspinlock_stat(void)
  188. {
  189. struct dentry *d_qstat = debugfs_create_dir("qlockstat", NULL);
  190. int i;
  191. if (!d_qstat) {
  192. pr_warn("Could not create 'qlockstat' debugfs directory\n");
  193. return 0;
  194. }
  195. /*
  196. * Create the debugfs files
  197. *
  198. * As reading from and writing to the stat files can be slow, only
  199. * root is allowed to do the read/write to limit impact to system
  200. * performance.
  201. */
  202. for (i = 0; i < qstat_num; i++)
  203. debugfs_create_file(qstat_names[i], 0400, d_qstat,
  204. (void *)(long)i, &fops_qstat);
  205. debugfs_create_file(qstat_names[qstat_reset_cnts], 0200, d_qstat,
  206. (void *)(long)qstat_reset_cnts, &fops_qstat);
  207. return 0;
  208. }
  209. fs_initcall(init_qspinlock_stat);
  210. /*
  211. * Increment the PV qspinlock statistical counters
  212. */
  213. static inline void qstat_inc(enum qlock_stats stat, bool cond)
  214. {
  215. if (cond)
  216. this_cpu_inc(qstats[stat]);
  217. }
  218. /*
  219. * PV hash hop count
  220. */
  221. static inline void qstat_hop(int hopcnt)
  222. {
  223. this_cpu_add(qstats[qstat_pv_hash_hops], hopcnt);
  224. }
  225. /*
  226. * Replacement function for pv_kick()
  227. */
  228. static inline void __pv_kick(int cpu)
  229. {
  230. u64 start = sched_clock();
  231. per_cpu(pv_kick_time, cpu) = start;
  232. pv_kick(cpu);
  233. this_cpu_add(qstats[qstat_pv_latency_kick], sched_clock() - start);
  234. }
  235. /*
  236. * Replacement function for pv_wait()
  237. */
  238. static inline void __pv_wait(u8 *ptr, u8 val)
  239. {
  240. u64 *pkick_time = this_cpu_ptr(&pv_kick_time);
  241. *pkick_time = 0;
  242. pv_wait(ptr, val);
  243. if (*pkick_time) {
  244. this_cpu_add(qstats[qstat_pv_latency_wake],
  245. sched_clock() - *pkick_time);
  246. qstat_inc(qstat_pv_kick_wake, true);
  247. }
  248. }
  249. #define pv_kick(c) __pv_kick(c)
  250. #define pv_wait(p, v) __pv_wait(p, v)
  251. /*
  252. * PV unfair trylock count tracking function
  253. */
  254. static inline int qstat_spin_steal_lock(struct qspinlock *lock)
  255. {
  256. int ret = pv_queued_spin_steal_lock(lock);
  257. qstat_inc(qstat_pv_lock_stealing, ret);
  258. return ret;
  259. }
  260. #undef queued_spin_trylock
  261. #define queued_spin_trylock(l) qstat_spin_steal_lock(l)
  262. #else /* CONFIG_QUEUED_LOCK_STAT */
  263. static inline void qstat_inc(enum qlock_stats stat, bool cond) { }
  264. static inline void qstat_hop(int hopcnt) { }
  265. #endif /* CONFIG_QUEUED_LOCK_STAT */