ktrace.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 = roundup_pow_of_two(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. Round the number of entries up to a
  45. * power of 2 so we can do fast masking to get the index from
  46. * the atomic index counter.
  47. */
  48. ktrace_t *
  49. ktrace_alloc(int nentries, unsigned int __nocast sleep)
  50. {
  51. ktrace_t *ktp;
  52. ktrace_entry_t *ktep;
  53. int entries;
  54. ktp = (ktrace_t*)kmem_zone_alloc(ktrace_hdr_zone, sleep);
  55. if (ktp == (ktrace_t*)NULL) {
  56. /*
  57. * KM_SLEEP callers don't expect failure.
  58. */
  59. if (sleep & KM_SLEEP)
  60. panic("ktrace_alloc: NULL memory on KM_SLEEP request!");
  61. return NULL;
  62. }
  63. /*
  64. * Special treatment for buffers with the ktrace_zentries entries
  65. */
  66. entries = roundup_pow_of_two(nentries);
  67. if (entries == ktrace_zentries) {
  68. ktep = (ktrace_entry_t*)kmem_zone_zalloc(ktrace_ent_zone,
  69. sleep);
  70. } else {
  71. ktep = (ktrace_entry_t*)kmem_zalloc((entries * sizeof(*ktep)),
  72. sleep | KM_LARGE);
  73. }
  74. if (ktep == NULL) {
  75. /*
  76. * KM_SLEEP callers don't expect failure.
  77. */
  78. if (sleep & KM_SLEEP)
  79. panic("ktrace_alloc: NULL memory on KM_SLEEP request!");
  80. kmem_free(ktp);
  81. return NULL;
  82. }
  83. ktp->kt_entries = ktep;
  84. ktp->kt_nentries = entries;
  85. ASSERT(is_power_of_2(entries));
  86. ktp->kt_index_mask = entries - 1;
  87. atomic_set(&ktp->kt_index, 0);
  88. ktp->kt_rollover = 0;
  89. return ktp;
  90. }
  91. /*
  92. * ktrace_free()
  93. *
  94. * Free up the ktrace header and buffer. It is up to the caller
  95. * to ensure that no-one is referencing it.
  96. */
  97. void
  98. ktrace_free(ktrace_t *ktp)
  99. {
  100. if (ktp == (ktrace_t *)NULL)
  101. return;
  102. /*
  103. * Special treatment for the Vnode trace buffer.
  104. */
  105. if (ktp->kt_nentries == ktrace_zentries)
  106. kmem_zone_free(ktrace_ent_zone, ktp->kt_entries);
  107. else
  108. kmem_free(ktp->kt_entries);
  109. kmem_zone_free(ktrace_hdr_zone, ktp);
  110. }
  111. /*
  112. * Enter the given values into the "next" entry in the trace buffer.
  113. * kt_index is always the index of the next entry to be filled.
  114. */
  115. void
  116. ktrace_enter(
  117. ktrace_t *ktp,
  118. void *val0,
  119. void *val1,
  120. void *val2,
  121. void *val3,
  122. void *val4,
  123. void *val5,
  124. void *val6,
  125. void *val7,
  126. void *val8,
  127. void *val9,
  128. void *val10,
  129. void *val11,
  130. void *val12,
  131. void *val13,
  132. void *val14,
  133. void *val15)
  134. {
  135. int index;
  136. ktrace_entry_t *ktep;
  137. ASSERT(ktp != NULL);
  138. /*
  139. * Grab an entry by pushing the index up to the next one.
  140. */
  141. index = atomic_add_return(1, &ktp->kt_index);
  142. index = (index - 1) & ktp->kt_index_mask;
  143. if (!ktp->kt_rollover && index == ktp->kt_nentries - 1)
  144. ktp->kt_rollover = 1;
  145. ASSERT((index >= 0) && (index < ktp->kt_nentries));
  146. ktep = &(ktp->kt_entries[index]);
  147. ktep->val[0] = val0;
  148. ktep->val[1] = val1;
  149. ktep->val[2] = val2;
  150. ktep->val[3] = val3;
  151. ktep->val[4] = val4;
  152. ktep->val[5] = val5;
  153. ktep->val[6] = val6;
  154. ktep->val[7] = val7;
  155. ktep->val[8] = val8;
  156. ktep->val[9] = val9;
  157. ktep->val[10] = val10;
  158. ktep->val[11] = val11;
  159. ktep->val[12] = val12;
  160. ktep->val[13] = val13;
  161. ktep->val[14] = val14;
  162. ktep->val[15] = val15;
  163. }
  164. /*
  165. * Return the number of entries in the trace buffer.
  166. */
  167. int
  168. ktrace_nentries(
  169. ktrace_t *ktp)
  170. {
  171. int index;
  172. if (ktp == NULL)
  173. return 0;
  174. index = atomic_read(&ktp->kt_index) & ktp->kt_index_mask;
  175. return (ktp->kt_rollover ? ktp->kt_nentries : index);
  176. }
  177. /*
  178. * ktrace_first()
  179. *
  180. * This is used to find the start of the trace buffer.
  181. * In conjunction with ktrace_next() it can be used to
  182. * iterate through the entire trace buffer. This code does
  183. * not do any locking because it is assumed that it is called
  184. * from the debugger.
  185. *
  186. * The caller must pass in a pointer to a ktrace_snap
  187. * structure in which we will keep some state used to
  188. * iterate through the buffer. This state must not touched
  189. * by any code outside of this module.
  190. */
  191. ktrace_entry_t *
  192. ktrace_first(ktrace_t *ktp, ktrace_snap_t *ktsp)
  193. {
  194. ktrace_entry_t *ktep;
  195. int index;
  196. int nentries;
  197. if (ktp->kt_rollover)
  198. index = atomic_read(&ktp->kt_index) & ktp->kt_index_mask;
  199. else
  200. index = 0;
  201. ktsp->ks_start = index;
  202. ktep = &(ktp->kt_entries[index]);
  203. nentries = ktrace_nentries(ktp);
  204. index++;
  205. if (index < nentries) {
  206. ktsp->ks_index = index;
  207. } else {
  208. ktsp->ks_index = 0;
  209. if (index > nentries)
  210. ktep = NULL;
  211. }
  212. return ktep;
  213. }
  214. /*
  215. * ktrace_next()
  216. *
  217. * This is used to iterate through the entries of the given
  218. * trace buffer. The caller must pass in the ktrace_snap_t
  219. * structure initialized by ktrace_first(). The return value
  220. * will be either a pointer to the next ktrace_entry or NULL
  221. * if all of the entries have been traversed.
  222. */
  223. ktrace_entry_t *
  224. ktrace_next(
  225. ktrace_t *ktp,
  226. ktrace_snap_t *ktsp)
  227. {
  228. int index;
  229. ktrace_entry_t *ktep;
  230. index = ktsp->ks_index;
  231. if (index == ktsp->ks_start) {
  232. ktep = NULL;
  233. } else {
  234. ktep = &ktp->kt_entries[index];
  235. }
  236. index++;
  237. if (index == ktrace_nentries(ktp)) {
  238. ktsp->ks_index = 0;
  239. } else {
  240. ktsp->ks_index = index;
  241. }
  242. return ktep;
  243. }
  244. /*
  245. * ktrace_skip()
  246. *
  247. * Skip the next "count" entries and return the entry after that.
  248. * Return NULL if this causes us to iterate past the beginning again.
  249. */
  250. ktrace_entry_t *
  251. ktrace_skip(
  252. ktrace_t *ktp,
  253. int count,
  254. ktrace_snap_t *ktsp)
  255. {
  256. int index;
  257. int new_index;
  258. ktrace_entry_t *ktep;
  259. int nentries = ktrace_nentries(ktp);
  260. index = ktsp->ks_index;
  261. new_index = index + count;
  262. while (new_index >= nentries) {
  263. new_index -= nentries;
  264. }
  265. if (index == ktsp->ks_start) {
  266. /*
  267. * We've iterated around to the start, so we're done.
  268. */
  269. ktep = NULL;
  270. } else if ((new_index < index) && (index < ktsp->ks_index)) {
  271. /*
  272. * We've skipped past the start again, so we're done.
  273. */
  274. ktep = NULL;
  275. ktsp->ks_index = ktsp->ks_start;
  276. } else {
  277. ktep = &(ktp->kt_entries[new_index]);
  278. new_index++;
  279. if (new_index == nentries) {
  280. ktsp->ks_index = 0;
  281. } else {
  282. ktsp->ks_index = new_index;
  283. }
  284. }
  285. return ktep;
  286. }