sputrace.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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_used(void)
  39. {
  40. return (sputrace_head - sputrace_tail) % bufsize;
  41. }
  42. static inline int sputrace_avail(void)
  43. {
  44. return bufsize - sputrace_used();
  45. }
  46. static int sputrace_sprint(char *tbuf, int n)
  47. {
  48. const struct sputrace *t = sputrace_log + sputrace_tail % bufsize;
  49. struct timespec tv =
  50. ktime_to_timespec(ktime_sub(t->tstamp, sputrace_start));
  51. return snprintf(tbuf, n,
  52. "[%lu.%09lu] %d: %s (ctxthread = %d, spu = %d)\n",
  53. (unsigned long) tv.tv_sec,
  54. (unsigned long) tv.tv_nsec,
  55. t->curr_tid,
  56. t->name,
  57. t->owner_tid,
  58. t->number);
  59. }
  60. static ssize_t sputrace_read(struct file *file, char __user *buf,
  61. size_t len, loff_t *ppos)
  62. {
  63. int error = 0, cnt = 0;
  64. if (!buf || len < 0)
  65. return -EINVAL;
  66. while (cnt < len) {
  67. char tbuf[128];
  68. int width;
  69. error = wait_event_interruptible(sputrace_wait,
  70. sputrace_used() > 0);
  71. if (error)
  72. break;
  73. spin_lock(&sputrace_lock);
  74. if (sputrace_head == sputrace_tail) {
  75. spin_unlock(&sputrace_lock);
  76. continue;
  77. }
  78. width = sputrace_sprint(tbuf, sizeof(tbuf));
  79. if (width < len)
  80. sputrace_tail = (sputrace_tail + 1) % bufsize;
  81. spin_unlock(&sputrace_lock);
  82. if (width >= len)
  83. break;
  84. error = copy_to_user(buf + cnt, tbuf, width);
  85. if (error)
  86. break;
  87. cnt += width;
  88. }
  89. return cnt == 0 ? error : cnt;
  90. }
  91. static int sputrace_open(struct inode *inode, struct file *file)
  92. {
  93. spin_lock(&sputrace_lock);
  94. sputrace_head = sputrace_tail = 0;
  95. sputrace_start = ktime_get();
  96. spin_unlock(&sputrace_lock);
  97. return 0;
  98. }
  99. static const struct file_operations sputrace_fops = {
  100. .owner = THIS_MODULE,
  101. .open = sputrace_open,
  102. .read = sputrace_read,
  103. };
  104. static void sputrace_log_item(const char *name, struct spu_context *ctx,
  105. struct spu *spu)
  106. {
  107. spin_lock(&sputrace_lock);
  108. if (sputrace_avail() > 1) {
  109. struct sputrace *t = sputrace_log + sputrace_head;
  110. t->tstamp = ktime_get();
  111. t->owner_tid = ctx->tid;
  112. t->name = name;
  113. t->curr_tid = current->pid;
  114. t->number = spu ? spu->number : -1;
  115. sputrace_head = (sputrace_head + 1) % bufsize;
  116. } else {
  117. printk(KERN_WARNING
  118. "sputrace: lost samples due to full buffer.\n");
  119. }
  120. spin_unlock(&sputrace_lock);
  121. wake_up(&sputrace_wait);
  122. }
  123. static void spu_context_event(void *probe_private, void *call_data,
  124. const char *format, va_list *args)
  125. {
  126. struct spu_probe *p = probe_private;
  127. struct spu_context *ctx;
  128. struct spu *spu;
  129. ctx = va_arg(*args, struct spu_context *);
  130. spu = va_arg(*args, struct spu *);
  131. sputrace_log_item(p->name, ctx, spu);
  132. }
  133. static void spu_context_nospu_event(void *probe_private, void *call_data,
  134. const char *format, va_list *args)
  135. {
  136. struct spu_probe *p = probe_private;
  137. struct spu_context *ctx;
  138. ctx = va_arg(*args, struct spu_context *);
  139. sputrace_log_item(p->name, ctx, NULL);
  140. }
  141. struct spu_probe spu_probes[] = {
  142. { "spu_bind_context__enter", "ctx %p spu %p", spu_context_event },
  143. { "spu_unbind_context__enter", "ctx %p spu %p", spu_context_event },
  144. { "spu_get_idle__enter", "ctx %p", spu_context_nospu_event },
  145. { "spu_get_idle__found", "ctx %p spu %p", spu_context_event },
  146. { "spu_get_idle__not_found", "ctx %p", spu_context_nospu_event },
  147. { "spu_find_victim__enter", "ctx %p", spu_context_nospu_event },
  148. { "spusched_tick__preempt", "ctx %p spu %p", spu_context_event },
  149. { "spusched_tick__newslice", "ctx %p", spu_context_nospu_event },
  150. { "spu_yield__enter", "ctx %p", spu_context_nospu_event },
  151. { "spu_deactivate__enter", "ctx %p", spu_context_nospu_event },
  152. { "__spu_deactivate__unload", "ctx %p spu %p", spu_context_event },
  153. { "spufs_ps_fault__enter", "ctx %p", spu_context_nospu_event },
  154. { "spufs_ps_fault__sleep", "ctx %p", spu_context_nospu_event },
  155. { "spufs_ps_fault__wake", "ctx %p spu %p", spu_context_event },
  156. { "spufs_ps_fault__insert", "ctx %p spu %p", spu_context_event },
  157. { "spu_acquire_saved__enter", "ctx %p", spu_context_nospu_event },
  158. { "destroy_spu_context__enter", "ctx %p", spu_context_nospu_event },
  159. { "spufs_stop_callback__enter", "ctx %p spu %p", spu_context_event },
  160. };
  161. static int __init sputrace_init(void)
  162. {
  163. struct proc_dir_entry *entry;
  164. int i, error = -ENOMEM;
  165. sputrace_log = kcalloc(bufsize, sizeof(struct sputrace), GFP_KERNEL);
  166. if (!sputrace_log)
  167. goto out;
  168. entry = proc_create("sputrace", S_IRUSR, NULL, &sputrace_fops);
  169. if (!entry)
  170. goto out_free_log;
  171. for (i = 0; i < ARRAY_SIZE(spu_probes); i++) {
  172. struct spu_probe *p = &spu_probes[i];
  173. error = marker_probe_register(p->name, p->format,
  174. p->probe_func, p);
  175. if (error)
  176. printk(KERN_INFO "Unable to register probe %s\n",
  177. p->name);
  178. }
  179. return 0;
  180. out_free_log:
  181. kfree(sputrace_log);
  182. out:
  183. return -ENOMEM;
  184. }
  185. static void __exit sputrace_exit(void)
  186. {
  187. int i;
  188. for (i = 0; i < ARRAY_SIZE(spu_probes); i++)
  189. marker_probe_unregister(spu_probes[i].name,
  190. spu_probes[i].probe_func, &spu_probes[i]);
  191. remove_proc_entry("sputrace", NULL);
  192. kfree(sputrace_log);
  193. }
  194. module_init(sputrace_init);
  195. module_exit(sputrace_exit);
  196. MODULE_LICENSE("GPL");