msm_perf.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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;
  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. ret = copy_to_user(buf, &perf->buf[perf->bufpos], n);
  121. if (ret)
  122. goto out;
  123. perf->bufpos += n;
  124. *ppos += n;
  125. out:
  126. mutex_unlock(&perf->read_lock);
  127. if (ret)
  128. return ret;
  129. return n;
  130. }
  131. static int perf_open(struct inode *inode, struct file *file)
  132. {
  133. struct msm_perf_state *perf = inode->i_private;
  134. struct drm_device *dev = perf->dev;
  135. struct msm_drm_private *priv = dev->dev_private;
  136. struct msm_gpu *gpu = priv->gpu;
  137. int ret = 0;
  138. mutex_lock(&dev->struct_mutex);
  139. if (perf->open || !gpu) {
  140. ret = -EBUSY;
  141. goto out;
  142. }
  143. file->private_data = perf;
  144. perf->open = true;
  145. perf->cnt = 0;
  146. perf->buftot = 0;
  147. perf->bufpos = 0;
  148. msm_gpu_perfcntr_start(gpu);
  149. perf->next_jiffies = jiffies + SAMPLE_TIME;
  150. out:
  151. mutex_unlock(&dev->struct_mutex);
  152. return ret;
  153. }
  154. static int perf_release(struct inode *inode, struct file *file)
  155. {
  156. struct msm_perf_state *perf = inode->i_private;
  157. struct msm_drm_private *priv = perf->dev->dev_private;
  158. msm_gpu_perfcntr_stop(priv->gpu);
  159. perf->open = false;
  160. return 0;
  161. }
  162. static const struct file_operations perf_debugfs_fops = {
  163. .owner = THIS_MODULE,
  164. .open = perf_open,
  165. .read = perf_read,
  166. .llseek = no_llseek,
  167. .release = perf_release,
  168. };
  169. int msm_perf_debugfs_init(struct drm_minor *minor)
  170. {
  171. struct msm_drm_private *priv = minor->dev->dev_private;
  172. struct msm_perf_state *perf;
  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. perf->node = kzalloc(sizeof(*perf->node), GFP_KERNEL);
  183. if (!perf->node)
  184. goto fail;
  185. perf->ent = debugfs_create_file("perf", S_IFREG | S_IRUGO,
  186. minor->debugfs_root, perf, &perf_debugfs_fops);
  187. if (!perf->ent) {
  188. DRM_ERROR("Cannot create /sys/kernel/debug/dri/%s/perf\n",
  189. minor->debugfs_root->d_name.name);
  190. goto fail;
  191. }
  192. perf->node->minor = minor;
  193. perf->node->dent = perf->ent;
  194. perf->node->info_ent = NULL;
  195. mutex_lock(&minor->debugfs_lock);
  196. list_add(&perf->node->list, &minor->debugfs_list);
  197. mutex_unlock(&minor->debugfs_lock);
  198. return 0;
  199. fail:
  200. msm_perf_debugfs_cleanup(minor);
  201. return -1;
  202. }
  203. void msm_perf_debugfs_cleanup(struct drm_minor *minor)
  204. {
  205. struct msm_drm_private *priv = minor->dev->dev_private;
  206. struct msm_perf_state *perf = priv->perf;
  207. if (!perf)
  208. return;
  209. priv->perf = NULL;
  210. debugfs_remove(perf->ent);
  211. if (perf->node) {
  212. mutex_lock(&minor->debugfs_lock);
  213. list_del(&perf->node->list);
  214. mutex_unlock(&minor->debugfs_lock);
  215. kfree(perf->node);
  216. }
  217. mutex_destroy(&perf->read_lock);
  218. kfree(perf);
  219. }
  220. #endif