ktrace.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <xfs.h>
  19. static kmem_zone_t *ktrace_hdr_zone;
  20. static kmem_zone_t *ktrace_ent_zone;
  21. static int ktrace_zentries;
  22. void __init
  23. ktrace_init(int zentries)
  24. {
  25. ktrace_zentries = zentries;
  26. ktrace_hdr_zone = kmem_zone_init(sizeof(ktrace_t),
  27. "ktrace_hdr");
  28. ASSERT(ktrace_hdr_zone);
  29. ktrace_ent_zone = kmem_zone_init(ktrace_zentries
  30. * sizeof(ktrace_entry_t),
  31. "ktrace_ent");
  32. ASSERT(ktrace_ent_zone);
  33. }
  34. void __exit
  35. ktrace_uninit(void)
  36. {
  37. kmem_zone_destroy(ktrace_hdr_zone);
  38. kmem_zone_destroy(ktrace_ent_zone);
  39. }
  40. /*
  41. * ktrace_alloc()
  42. *
  43. * Allocate a ktrace header and enough buffering for the given
  44. * number of entries.
  45. */
  46. ktrace_t *
  47. ktrace_alloc(int nentries, unsigned int __nocast sleep)
  48. {
  49. ktrace_t *ktp;
  50. ktrace_entry_t *ktep;
  51. ktp = (ktrace_t*)kmem_zone_alloc(ktrace_hdr_zone, sleep);
  52. if (ktp == (ktrace_t*)NULL) {
  53. /*
  54. * KM_SLEEP callers don't expect failure.
  55. */
  56. if (sleep & KM_SLEEP)
  57. panic("ktrace_alloc: NULL memory on KM_SLEEP request!");
  58. return NULL;
  59. }
  60. /*
  61. * Special treatment for buffers with the ktrace_zentries entries
  62. */
  63. if (nentries == ktrace_zentries) {
  64. ktep = (ktrace_entry_t*)kmem_zone_zalloc(ktrace_ent_zone,
  65. sleep);
  66. } else {
  67. ktep = (ktrace_entry_t*)kmem_zalloc((nentries * sizeof(*ktep)),
  68. sleep | KM_LARGE);
  69. }
  70. if (ktep == NULL) {
  71. /*
  72. * KM_SLEEP callers don't expect failure.
  73. */
  74. if (sleep & KM_SLEEP)
  75. panic("ktrace_alloc: NULL memory on KM_SLEEP request!");
  76. kmem_free(ktp, sizeof(*ktp));
  77. return NULL;
  78. }
  79. ktp->kt_entries = ktep;
  80. ktp->kt_nentries = nentries;
  81. ktp->kt_index = 0;
  82. ktp->kt_rollover = 0;
  83. return ktp;
  84. }
  85. /*
  86. * ktrace_free()
  87. *
  88. * Free up the ktrace header and buffer. It is up to the caller
  89. * to ensure that no-one is referencing it.
  90. */
  91. void
  92. ktrace_free(ktrace_t *ktp)
  93. {
  94. int entries_size;
  95. if (ktp == (ktrace_t *)NULL)
  96. return;
  97. /*
  98. * Special treatment for the Vnode trace buffer.
  99. */
  100. if (ktp->kt_nentries == ktrace_zentries) {
  101. kmem_zone_free(ktrace_ent_zone, ktp->kt_entries);
  102. } else {
  103. entries_size = (int)(ktp->kt_nentries * sizeof(ktrace_entry_t));
  104. kmem_free(ktp->kt_entries, entries_size);
  105. }
  106. kmem_zone_free(ktrace_hdr_zone, ktp);
  107. }
  108. /*
  109. * Enter the given values into the "next" entry in the trace buffer.
  110. * kt_index is always the index of the next entry to be filled.
  111. */
  112. void
  113. ktrace_enter(
  114. ktrace_t *ktp,
  115. void *val0,
  116. void *val1,
  117. void *val2,
  118. void *val3,
  119. void *val4,
  120. void *val5,
  121. void *val6,
  122. void *val7,
  123. void *val8,
  124. void *val9,
  125. void *val10,
  126. void *val11,
  127. void *val12,
  128. void *val13,
  129. void *val14,
  130. void *val15)
  131. {
  132. static DEFINE_SPINLOCK(wrap_lock);
  133. unsigned long flags;
  134. int index;
  135. ktrace_entry_t *ktep;
  136. ASSERT(ktp != NULL);
  137. /*
  138. * Grab an entry by pushing the index up to the next one.
  139. */
  140. spin_lock_irqsave(&wrap_lock, flags);
  141. index = ktp->kt_index;
  142. if (++ktp->kt_index == ktp->kt_nentries)
  143. ktp->kt_index = 0;
  144. spin_unlock_irqrestore(&wrap_lock, flags);
  145. if (!ktp->kt_rollover && index == ktp->kt_nentries - 1)
  146. ktp->kt_rollover = 1;
  147. ASSERT((index >= 0) && (index < ktp->kt_nentries));
  148. ktep = &(ktp->kt_entries[index]);
  149. ktep->val[0] = val0;
  150. ktep->val[1] = val1;
  151. ktep->val[2] = val2;
  152. ktep->val[3] = val3;
  153. ktep->val[4] = val4;
  154. ktep->val[5] = val5;
  155. ktep->val[6] = val6;
  156. ktep->val[7] = val7;
  157. ktep->val[8] = val8;
  158. ktep->val[9] = val9;
  159. ktep->val[10] = val10;
  160. ktep->val[11] = val11;
  161. ktep->val[12] = val12;
  162. ktep->val[13] = val13;
  163. ktep->val[14] = val14;
  164. ktep->val[15] = val15;
  165. }
  166. /*
  167. * Return the number of entries in the trace buffer.
  168. */
  169. int
  170. ktrace_nentries(
  171. ktrace_t *ktp)
  172. {
  173. if (ktp == NULL) {
  174. return 0;
  175. }
  176. return (ktp->kt_rollover ? ktp->kt_nentries : ktp->kt_index);
  177. }
  178. /*
  179. * ktrace_first()
  180. *
  181. * This is used to find the start of the trace buffer.
  182. * In conjunction with ktrace_next() it can be used to
  183. * iterate through the entire trace buffer. This code does
  184. * not do any locking because it is assumed that it is called
  185. * from the debugger.
  186. *
  187. * The caller must pass in a pointer to a ktrace_snap
  188. * structure in which we will keep some state used to
  189. * iterate through the buffer. This state must not touched
  190. * by any code outside of this module.
  191. */
  192. ktrace_entry_t *
  193. ktrace_first(ktrace_t *ktp, ktrace_snap_t *ktsp)
  194. {
  195. ktrace_entry_t *ktep;
  196. int index;
  197. int nentries;
  198. if (ktp->kt_rollover)
  199. index = ktp->kt_index;
  200. else
  201. index = 0;
  202. ktsp->ks_start = index;
  203. ktep = &(ktp->kt_entries[index]);
  204. nentries = ktrace_nentries(ktp);
  205. index++;
  206. if (index < nentries) {
  207. ktsp->ks_index = index;
  208. } else {
  209. ktsp->ks_index = 0;
  210. if (index > nentries)
  211. ktep = NULL;
  212. }
  213. return ktep;
  214. }
  215. /*
  216. * ktrace_next()
  217. *
  218. * This is used to iterate through the entries of the given
  219. * trace buffer. The caller must pass in the ktrace_snap_t
  220. * structure initialized by ktrace_first(). The return value
  221. * will be either a pointer to the next ktrace_entry or NULL
  222. * if all of the entries have been traversed.
  223. */
  224. ktrace_entry_t *
  225. ktrace_next(
  226. ktrace_t *ktp,
  227. ktrace_snap_t *ktsp)
  228. {
  229. int index;
  230. ktrace_entry_t *ktep;
  231. index = ktsp->ks_index;
  232. if (index == ktsp->ks_start) {
  233. ktep = NULL;
  234. } else {
  235. ktep = &ktp->kt_entries[index];
  236. }
  237. index++;
  238. if (index == ktrace_nentries(ktp)) {
  239. ktsp->ks_index = 0;
  240. } else {
  241. ktsp->ks_index = index;
  242. }
  243. return ktep;
  244. }
  245. /*
  246. * ktrace_skip()
  247. *
  248. * Skip the next "count" entries and return the entry after that.
  249. * Return NULL if this causes us to iterate past the beginning again.
  250. */
  251. ktrace_entry_t *
  252. ktrace_skip(
  253. ktrace_t *ktp,
  254. int count,
  255. ktrace_snap_t *ktsp)
  256. {
  257. int index;
  258. int new_index;
  259. ktrace_entry_t *ktep;
  260. int nentries = ktrace_nentries(ktp);
  261. index = ktsp->ks_index;
  262. new_index = index + count;
  263. while (new_index >= nentries) {
  264. new_index -= nentries;
  265. }
  266. if (index == ktsp->ks_start) {
  267. /*
  268. * We've iterated around to the start, so we're done.
  269. */
  270. ktep = NULL;
  271. } else if ((new_index < index) && (index < ktsp->ks_index)) {
  272. /*
  273. * We've skipped past the start again, so we're done.
  274. */
  275. ktep = NULL;
  276. ktsp->ks_index = ktsp->ks_start;
  277. } else {
  278. ktep = &(ktp->kt_entries[new_index]);
  279. new_index++;
  280. if (new_index == nentries) {
  281. ktsp->ks_index = 0;
  282. } else {
  283. ktsp->ks_index = new_index;
  284. }
  285. }
  286. return ktep;
  287. }