msm_perf.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (C) 2013 Red Hat
  3. * Author: Rob Clark <robdclark@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /* For profiling, userspace can:
  18. *
  19. * tail -f /sys/kernel/debug/dri/<minor>/gpu
  20. *
  21. * This will enable performance counters/profiling to track the busy time
  22. * and any gpu specific performance counters that are supported.
  23. */
  24. #ifdef CONFIG_DEBUG_FS
  25. #include <linux/debugfs.h>
  26. #include "msm_drv.h"
  27. #include "msm_gpu.h"
  28. struct msm_perf_state {
  29. struct drm_device *dev;
  30. bool open;
  31. int cnt;
  32. struct mutex read_lock;
  33. char buf[256];
  34. int buftot, bufpos;
  35. unsigned long next_jiffies;
  36. };
  37. #define SAMPLE_TIME (HZ/4)
  38. /* wait for next sample time: */
  39. static int wait_sample(struct msm_perf_state *perf)
  40. {
  41. unsigned long start_jiffies = jiffies;
  42. if (time_after(perf->next_jiffies, start_jiffies)) {
  43. unsigned long remaining_jiffies =
  44. perf->next_jiffies - start_jiffies;
  45. int ret = schedule_timeout_interruptible(remaining_jiffies);
  46. if (ret > 0) {
  47. /* interrupted */
  48. return -ERESTARTSYS;
  49. }
  50. }
  51. perf->next_jiffies += SAMPLE_TIME;
  52. return 0;
  53. }
  54. static int refill_buf(struct msm_perf_state *perf)
  55. {
  56. struct msm_drm_private *priv = perf->dev->dev_private;
  57. struct msm_gpu *gpu = priv->gpu;
  58. char *ptr = perf->buf;
  59. int rem = sizeof(perf->buf);
  60. int i, n;
  61. if ((perf->cnt++ % 32) == 0) {
  62. /* Header line: */
  63. n = snprintf(ptr, rem, "%%BUSY");
  64. ptr += n;
  65. rem -= n;
  66. for (i = 0; i < gpu->num_perfcntrs; i++) {
  67. const struct msm_gpu_perfcntr *perfcntr = &gpu->perfcntrs[i];
  68. n = snprintf(ptr, rem, "\t%s", perfcntr->name);
  69. ptr += n;
  70. rem -= n;
  71. }
  72. } else {
  73. /* Sample line: */
  74. uint32_t activetime = 0, totaltime = 0;
  75. uint32_t cntrs[5];
  76. uint32_t val;
  77. int ret;
  78. /* sleep until next sample time: */
  79. ret = wait_sample(perf);
  80. if (ret)
  81. return ret;
  82. ret = msm_gpu_perfcntr_sample(gpu, &activetime, &totaltime,
  83. ARRAY_SIZE(cntrs), cntrs);
  84. if (ret < 0)
  85. return ret;
  86. val = totaltime ? 1000 * activetime / totaltime : 0;
  87. n = snprintf(ptr, rem, "%3d.%d%%", val / 10, val % 10);
  88. ptr += n;
  89. rem -= n;
  90. for (i = 0; i < ret; i++) {
  91. /* cycle counters (I think).. convert to MHz.. */
  92. val = cntrs[i] / 10000;
  93. n = snprintf(ptr, rem, "\t%5d.%02d",
  94. val / 100, val % 100);
  95. ptr += n;
  96. rem -= n;
  97. }
  98. }
  99. n = snprintf(ptr, rem, "\n");
  100. ptr += n;
  101. rem -= n;
  102. perf->bufpos = 0;
  103. perf->buftot = ptr - perf->buf;
  104. return 0;
  105. }
  106. static ssize_t perf_read(struct file *file, char __user *buf,
  107. size_t sz, loff_t *ppos)
  108. {
  109. struct msm_perf_state *perf = file->private_data;
  110. int n = 0, ret = 0;
  111. mutex_lock(&perf->read_lock);
  112. if (perf->bufpos >= perf->buftot) {
  113. ret = refill_buf(perf);
  114. if (ret)
  115. goto out;
  116. }
  117. n = min((int)sz, perf->buftot - perf->bufpos);
  118. if (copy_to_user(buf, &perf->buf[perf->bufpos], n)) {
  119. ret = -EFAULT;
  120. goto out;
  121. }
  122. perf->bufpos += n;
  123. *ppos += n;
  124. out:
  125. mutex_unlock(&perf->read_lock);
  126. if (ret)
  127. return ret;
  128. return n;
  129. }
  130. static int perf_open(struct inode *inode, struct file *file)
  131. {
  132. struct msm_perf_state *perf = inode->i_private;
  133. struct drm_device *dev = perf->dev;
  134. struct msm_drm_private *priv = dev->dev_private;
  135. struct msm_gpu *gpu = priv->gpu;
  136. int ret = 0;
  137. mutex_lock(&dev->struct_mutex);
  138. if (perf->open || !gpu) {
  139. ret = -EBUSY;
  140. goto out;
  141. }
  142. file->private_data = perf;
  143. perf->open = true;
  144. perf->cnt = 0;
  145. perf->buftot = 0;
  146. perf->bufpos = 0;
  147. msm_gpu_perfcntr_start(gpu);
  148. perf->next_jiffies = jiffies + SAMPLE_TIME;
  149. out:
  150. mutex_unlock(&dev->struct_mutex);
  151. return ret;
  152. }
  153. static int perf_release(struct inode *inode, struct file *file)
  154. {
  155. struct msm_perf_state *perf = inode->i_private;
  156. struct msm_drm_private *priv = perf->dev->dev_private;
  157. msm_gpu_perfcntr_stop(priv->gpu);
  158. perf->open = false;
  159. return 0;
  160. }
  161. static const struct file_operations perf_debugfs_fops = {
  162. .owner = THIS_MODULE,
  163. .open = perf_open,
  164. .read = perf_read,
  165. .llseek = no_llseek,
  166. .release = perf_release,
  167. };
  168. int msm_perf_debugfs_init(struct drm_minor *minor)
  169. {
  170. struct msm_drm_private *priv = minor->dev->dev_private;
  171. struct msm_perf_state *perf;
  172. struct dentry *ent;
  173. /* only create on first minor: */
  174. if (priv->perf)
  175. return 0;
  176. perf = kzalloc(sizeof(*perf), GFP_KERNEL);
  177. if (!perf)
  178. return -ENOMEM;
  179. perf->dev = minor->dev;
  180. mutex_init(&perf->read_lock);
  181. priv->perf = perf;
  182. ent = debugfs_create_file("perf", S_IFREG | S_IRUGO,
  183. minor->debugfs_root, perf, &perf_debugfs_fops);
  184. if (!ent) {
  185. DRM_ERROR("Cannot create /sys/kernel/debug/dri/%pd/perf\n",
  186. minor->debugfs_root);
  187. goto fail;
  188. }
  189. return 0;
  190. fail:
  191. msm_perf_debugfs_cleanup(priv);
  192. return -1;
  193. }
  194. void msm_perf_debugfs_cleanup(struct msm_drm_private *priv)
  195. {
  196. struct msm_perf_state *perf = priv->perf;
  197. if (!perf)
  198. return;
  199. priv->perf = NULL;
  200. mutex_destroy(&perf->read_lock);
  201. kfree(perf);
  202. }
  203. #endif