msm_perf.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. struct dentry *ent;
  37. struct drm_info_node *node;
  38. };
  39. #define SAMPLE_TIME (HZ/4)
  40. /* wait for next sample time: */
  41. static int wait_sample(struct msm_perf_state *perf)
  42. {
  43. unsigned long start_jiffies = jiffies;
  44. if (time_after(perf->next_jiffies, start_jiffies)) {
  45. unsigned long remaining_jiffies =
  46. perf->next_jiffies - start_jiffies;
  47. int ret = schedule_timeout_interruptible(remaining_jiffies);
  48. if (ret > 0) {
  49. /* interrupted */
  50. return -ERESTARTSYS;
  51. }
  52. }
  53. perf->next_jiffies += SAMPLE_TIME;
  54. return 0;
  55. }
  56. static int refill_buf(struct msm_perf_state *perf)
  57. {
  58. struct msm_drm_private *priv = perf->dev->dev_private;
  59. struct msm_gpu *gpu = priv->gpu;
  60. char *ptr = perf->buf;
  61. int rem = sizeof(perf->buf);
  62. int i, n;
  63. if ((perf->cnt++ % 32) == 0) {
  64. /* Header line: */
  65. n = snprintf(ptr, rem, "%%BUSY");
  66. ptr += n;
  67. rem -= n;
  68. for (i = 0; i < gpu->num_perfcntrs; i++) {
  69. const struct msm_gpu_perfcntr *perfcntr = &gpu->perfcntrs[i];
  70. n = snprintf(ptr, rem, "\t%s", perfcntr->name);
  71. ptr += n;
  72. rem -= n;
  73. }
  74. } else {
  75. /* Sample line: */
  76. uint32_t activetime = 0, totaltime = 0;
  77. uint32_t cntrs[5];
  78. uint32_t val;
  79. int ret;
  80. /* sleep until next sample time: */
  81. ret = wait_sample(perf);
  82. if (ret)
  83. return ret;
  84. ret = msm_gpu_perfcntr_sample(gpu, &activetime, &totaltime,
  85. ARRAY_SIZE(cntrs), cntrs);
  86. if (ret < 0)
  87. return ret;
  88. val = totaltime ? 1000 * activetime / totaltime : 0;
  89. n = snprintf(ptr, rem, "%3d.%d%%", val / 10, val % 10);
  90. ptr += n;
  91. rem -= n;
  92. for (i = 0; i < ret; i++) {
  93. /* cycle counters (I think).. convert to MHz.. */
  94. val = cntrs[i] / 10000;
  95. n = snprintf(ptr, rem, "\t%5d.%02d",
  96. val / 100, val % 100);
  97. ptr += n;
  98. rem -= n;
  99. }
  100. }
  101. n = snprintf(ptr, rem, "\n");
  102. ptr += n;
  103. rem -= n;
  104. perf->bufpos = 0;
  105. perf->buftot = ptr - perf->buf;
  106. return 0;
  107. }
  108. static ssize_t perf_read(struct file *file, char __user *buf,
  109. size_t sz, loff_t *ppos)
  110. {
  111. struct msm_perf_state *perf = file->private_data;
  112. int n = 0, ret = 0;
  113. mutex_lock(&perf->read_lock);
  114. if (perf->bufpos >= perf->buftot) {
  115. ret = refill_buf(perf);
  116. if (ret)
  117. goto out;
  118. }
  119. n = min((int)sz, perf->buftot - perf->bufpos);
  120. if (copy_to_user(buf, &perf->buf[perf->bufpos], n)) {
  121. ret = -EFAULT;
  122. goto out;
  123. }
  124. perf->bufpos += n;
  125. *ppos += n;
  126. out:
  127. mutex_unlock(&perf->read_lock);
  128. if (ret)
  129. return ret;
  130. return n;
  131. }
  132. static int perf_open(struct inode *inode, struct file *file)
  133. {
  134. struct msm_perf_state *perf = inode->i_private;
  135. struct drm_device *dev = perf->dev;
  136. struct msm_drm_private *priv = dev->dev_private;
  137. struct msm_gpu *gpu = priv->gpu;
  138. int ret = 0;
  139. mutex_lock(&dev->struct_mutex);
  140. if (perf->open || !gpu) {
  141. ret = -EBUSY;
  142. goto out;
  143. }
  144. file->private_data = perf;
  145. perf->open = true;
  146. perf->cnt = 0;
  147. perf->buftot = 0;
  148. perf->bufpos = 0;
  149. msm_gpu_perfcntr_start(gpu);
  150. perf->next_jiffies = jiffies + SAMPLE_TIME;
  151. out:
  152. mutex_unlock(&dev->struct_mutex);
  153. return ret;
  154. }
  155. static int perf_release(struct inode *inode, struct file *file)
  156. {
  157. struct msm_perf_state *perf = inode->i_private;
  158. struct msm_drm_private *priv = perf->dev->dev_private;
  159. msm_gpu_perfcntr_stop(priv->gpu);
  160. perf->open = false;
  161. return 0;
  162. }
  163. static const struct file_operations perf_debugfs_fops = {
  164. .owner = THIS_MODULE,
  165. .open = perf_open,
  166. .read = perf_read,
  167. .llseek = no_llseek,
  168. .release = perf_release,
  169. };
  170. int msm_perf_debugfs_init(struct drm_minor *minor)
  171. {
  172. struct msm_drm_private *priv = minor->dev->dev_private;
  173. struct msm_perf_state *perf;
  174. /* only create on first minor: */
  175. if (priv->perf)
  176. return 0;
  177. perf = kzalloc(sizeof(*perf), GFP_KERNEL);
  178. if (!perf)
  179. return -ENOMEM;
  180. perf->dev = minor->dev;
  181. mutex_init(&perf->read_lock);
  182. priv->perf = perf;
  183. perf->node = kzalloc(sizeof(*perf->node), GFP_KERNEL);
  184. if (!perf->node)
  185. goto fail;
  186. perf->ent = debugfs_create_file("perf", S_IFREG | S_IRUGO,
  187. minor->debugfs_root, perf, &perf_debugfs_fops);
  188. if (!perf->ent) {
  189. DRM_ERROR("Cannot create /sys/kernel/debug/dri/%pd/perf\n",
  190. minor->debugfs_root);
  191. goto fail;
  192. }
  193. perf->node->minor = minor;
  194. perf->node->dent = perf->ent;
  195. perf->node->info_ent = NULL;
  196. mutex_lock(&minor->debugfs_lock);
  197. list_add(&perf->node->list, &minor->debugfs_list);
  198. mutex_unlock(&minor->debugfs_lock);
  199. return 0;
  200. fail:
  201. msm_perf_debugfs_cleanup(minor);
  202. return -1;
  203. }
  204. void msm_perf_debugfs_cleanup(struct drm_minor *minor)
  205. {
  206. struct msm_drm_private *priv = minor->dev->dev_private;
  207. struct msm_perf_state *perf = priv->perf;
  208. if (!perf)
  209. return;
  210. priv->perf = NULL;
  211. debugfs_remove(perf->ent);
  212. if (perf->node) {
  213. mutex_lock(&minor->debugfs_lock);
  214. list_del(&perf->node->list);
  215. mutex_unlock(&minor->debugfs_lock);
  216. kfree(perf->node);
  217. }
  218. mutex_destroy(&perf->read_lock);
  219. kfree(perf);
  220. }
  221. #endif