sputrace.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Copyright (C) 2007 IBM Deutschland Entwicklung GmbH
  3. * Released under GPL v2.
  4. *
  5. * Partially based on net/ipv4/tcp_probe.c.
  6. *
  7. * Simple tracing facility for spu contexts.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/marker.h>
  13. #include <linux/proc_fs.h>
  14. #include <linux/wait.h>
  15. #include <asm/atomic.h>
  16. #include <asm/uaccess.h>
  17. #include "spufs.h"
  18. struct spu_probe {
  19. const char *name;
  20. const char *format;
  21. marker_probe_func *probe_func;
  22. };
  23. struct sputrace {
  24. ktime_t tstamp;
  25. int owner_tid; /* owner */
  26. int curr_tid;
  27. const char *name;
  28. int number;
  29. };
  30. static int bufsize __read_mostly = 16384;
  31. MODULE_PARM_DESC(bufsize, "Log buffer size (number of records)");
  32. module_param(bufsize, int, 0);
  33. static DEFINE_SPINLOCK(sputrace_lock);
  34. static DECLARE_WAIT_QUEUE_HEAD(sputrace_wait);
  35. static ktime_t sputrace_start;
  36. static unsigned long sputrace_head, sputrace_tail;
  37. static struct sputrace *sputrace_log;
  38. static int sputrace_logging;
  39. static int sputrace_used(void)
  40. {
  41. return (sputrace_head - sputrace_tail) % bufsize;
  42. }
  43. static inline int sputrace_avail(void)
  44. {
  45. return bufsize - sputrace_used();
  46. }
  47. static int sputrace_sprint(char *tbuf, int n)
  48. {
  49. const struct sputrace *t = sputrace_log + sputrace_tail % bufsize;
  50. struct timespec tv =
  51. ktime_to_timespec(ktime_sub(t->tstamp, sputrace_start));
  52. return snprintf(tbuf, n,
  53. "[%lu.%09lu] %d: %s (ctxthread = %d, spu = %d)\n",
  54. (unsigned long) tv.tv_sec,
  55. (unsigned long) tv.tv_nsec,
  56. t->curr_tid,
  57. t->name,
  58. t->owner_tid,
  59. t->number);
  60. }
  61. static ssize_t sputrace_read(struct file *file, char __user *buf,
  62. size_t len, loff_t *ppos)
  63. {
  64. int error = 0, cnt = 0;
  65. if (!buf || len < 0)
  66. return -EINVAL;
  67. while (cnt < len) {
  68. char tbuf[128];
  69. int width;
  70. /* If we have data ready to return, don't block waiting
  71. * for more */
  72. if (cnt > 0 && sputrace_used() == 0)
  73. break;
  74. error = wait_event_interruptible(sputrace_wait,
  75. sputrace_used() > 0);
  76. if (error)
  77. break;
  78. spin_lock(&sputrace_lock);
  79. if (sputrace_head == sputrace_tail) {
  80. spin_unlock(&sputrace_lock);
  81. continue;
  82. }
  83. width = sputrace_sprint(tbuf, sizeof(tbuf));
  84. if (width < len)
  85. sputrace_tail = (sputrace_tail + 1) % bufsize;
  86. spin_unlock(&sputrace_lock);
  87. if (width >= len)
  88. break;
  89. error = copy_to_user(buf + cnt, tbuf, width);
  90. if (error)
  91. break;
  92. cnt += width;
  93. }
  94. return cnt == 0 ? error : cnt;
  95. }
  96. static int sputrace_open(struct inode *inode, struct file *file)
  97. {
  98. int rc;
  99. spin_lock(&sputrace_lock);
  100. if (sputrace_logging) {
  101. rc = -EBUSY;
  102. goto out;
  103. }
  104. sputrace_logging = 1;
  105. sputrace_head = sputrace_tail = 0;
  106. sputrace_start = ktime_get();
  107. rc = 0;
  108. out:
  109. spin_unlock(&sputrace_lock);
  110. return rc;
  111. }
  112. static int sputrace_release(struct inode *inode, struct file *file)
  113. {
  114. spin_lock(&sputrace_lock);
  115. sputrace_logging = 0;
  116. spin_unlock(&sputrace_lock);
  117. return 0;
  118. }
  119. static const struct file_operations sputrace_fops = {
  120. .owner = THIS_MODULE,
  121. .open = sputrace_open,
  122. .read = sputrace_read,
  123. .release = sputrace_release,
  124. };
  125. static void sputrace_log_item(const char *name, struct spu_context *ctx,
  126. struct spu *spu)
  127. {
  128. spin_lock(&sputrace_lock);
  129. if (!sputrace_logging) {
  130. spin_unlock(&sputrace_lock);
  131. return;
  132. }
  133. if (sputrace_avail() > 1) {
  134. struct sputrace *t = sputrace_log + sputrace_head;
  135. t->tstamp = ktime_get();
  136. t->owner_tid = ctx->tid;
  137. t->name = name;
  138. t->curr_tid = current->pid;
  139. t->number = spu ? spu->number : -1;
  140. sputrace_head = (sputrace_head + 1) % bufsize;
  141. } else {
  142. printk(KERN_WARNING
  143. "sputrace: lost samples due to full buffer.\n");
  144. }
  145. spin_unlock(&sputrace_lock);
  146. wake_up(&sputrace_wait);
  147. }
  148. static void spu_context_event(void *probe_private, void *call_data,
  149. const char *format, va_list *args)
  150. {
  151. struct spu_probe *p = probe_private;
  152. struct spu_context *ctx;
  153. struct spu *spu;
  154. ctx = va_arg(*args, struct spu_context *);
  155. spu = va_arg(*args, struct spu *);
  156. sputrace_log_item(p->name, ctx, spu);
  157. }
  158. static void spu_context_nospu_event(void *probe_private, void *call_data,
  159. const char *format, va_list *args)
  160. {
  161. struct spu_probe *p = probe_private;
  162. struct spu_context *ctx;
  163. ctx = va_arg(*args, struct spu_context *);
  164. sputrace_log_item(p->name, ctx, NULL);
  165. }
  166. struct spu_probe spu_probes[] = {
  167. { "spu_bind_context__enter", "ctx %p spu %p", spu_context_event },
  168. { "spu_unbind_context__enter", "ctx %p spu %p", spu_context_event },
  169. { "spu_get_idle__enter", "ctx %p", spu_context_nospu_event },
  170. { "spu_get_idle__found", "ctx %p spu %p", spu_context_event },
  171. { "spu_get_idle__not_found", "ctx %p", spu_context_nospu_event },
  172. { "spu_find_victim__enter", "ctx %p", spu_context_nospu_event },
  173. { "spusched_tick__preempt", "ctx %p spu %p", spu_context_event },
  174. { "spusched_tick__newslice", "ctx %p", spu_context_nospu_event },
  175. { "spu_yield__enter", "ctx %p", spu_context_nospu_event },
  176. { "spu_deactivate__enter", "ctx %p", spu_context_nospu_event },
  177. { "__spu_deactivate__unload", "ctx %p spu %p", spu_context_event },
  178. { "spufs_ps_fault__enter", "ctx %p", spu_context_nospu_event },
  179. { "spufs_ps_fault__sleep", "ctx %p", spu_context_nospu_event },
  180. { "spufs_ps_fault__wake", "ctx %p spu %p", spu_context_event },
  181. { "spufs_ps_fault__insert", "ctx %p spu %p", spu_context_event },
  182. { "spu_acquire_saved__enter", "ctx %p", spu_context_nospu_event },
  183. { "destroy_spu_context__enter", "ctx %p", spu_context_nospu_event },
  184. { "spufs_stop_callback__enter", "ctx %p spu %p", spu_context_event },
  185. };
  186. static int __init sputrace_init(void)
  187. {
  188. struct proc_dir_entry *entry;
  189. int i, error = -ENOMEM;
  190. sputrace_log = kcalloc(bufsize, sizeof(struct sputrace), GFP_KERNEL);
  191. if (!sputrace_log)
  192. goto out;
  193. entry = proc_create("sputrace", S_IRUSR, NULL, &sputrace_fops);
  194. if (!entry)
  195. goto out_free_log;
  196. for (i = 0; i < ARRAY_SIZE(spu_probes); i++) {
  197. struct spu_probe *p = &spu_probes[i];
  198. error = marker_probe_register(p->name, p->format,
  199. p->probe_func, p);
  200. if (error)
  201. printk(KERN_INFO "Unable to register probe %s\n",
  202. p->name);
  203. }
  204. return 0;
  205. out_free_log:
  206. kfree(sputrace_log);
  207. out:
  208. return -ENOMEM;
  209. }
  210. static void __exit sputrace_exit(void)
  211. {
  212. int i;
  213. for (i = 0; i < ARRAY_SIZE(spu_probes); i++)
  214. marker_probe_unregister(spu_probes[i].name,
  215. spu_probes[i].probe_func, &spu_probes[i]);
  216. remove_proc_entry("sputrace", NULL);
  217. kfree(sputrace_log);
  218. marker_synchronize_unregister();
  219. }
  220. module_init(sputrace_init);
  221. module_exit(sputrace_exit);
  222. MODULE_LICENSE("GPL");