2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
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.
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.
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
20 static kmem_zone_t *ktrace_hdr_zone;
21 static kmem_zone_t *ktrace_ent_zone;
22 static int ktrace_zentries;
25 ktrace_init(int zentries)
27 ktrace_zentries = zentries;
29 ktrace_hdr_zone = kmem_zone_init(sizeof(ktrace_t),
31 ASSERT(ktrace_hdr_zone);
33 ktrace_ent_zone = kmem_zone_init(ktrace_zentries
34 * sizeof(ktrace_entry_t),
36 ASSERT(ktrace_ent_zone);
42 kmem_cache_destroy(ktrace_hdr_zone);
43 kmem_cache_destroy(ktrace_ent_zone);
49 * Allocate a ktrace header and enough buffering for the given
53 ktrace_alloc(int nentries, unsigned int __nocast sleep)
58 ktp = (ktrace_t*)kmem_zone_alloc(ktrace_hdr_zone, sleep);
60 if (ktp == (ktrace_t*)NULL) {
62 * KM_SLEEP callers don't expect failure.
65 panic("ktrace_alloc: NULL memory on KM_SLEEP request!");
71 * Special treatment for buffers with the ktrace_zentries entries
73 if (nentries == ktrace_zentries) {
74 ktep = (ktrace_entry_t*)kmem_zone_zalloc(ktrace_ent_zone,
77 ktep = (ktrace_entry_t*)kmem_zalloc((nentries * sizeof(*ktep)),
83 * KM_SLEEP callers don't expect failure.
86 panic("ktrace_alloc: NULL memory on KM_SLEEP request!");
88 kmem_free(ktp, sizeof(*ktp));
93 spinlock_init(&(ktp->kt_lock), "kt_lock");
95 ktp->kt_entries = ktep;
96 ktp->kt_nentries = nentries;
106 * Free up the ktrace header and buffer. It is up to the caller
107 * to ensure that no-one is referencing it.
110 ktrace_free(ktrace_t *ktp)
114 if (ktp == (ktrace_t *)NULL)
117 spinlock_destroy(&ktp->kt_lock);
120 * Special treatment for the Vnode trace buffer.
122 if (ktp->kt_nentries == ktrace_zentries) {
123 kmem_zone_free(ktrace_ent_zone, ktp->kt_entries);
125 entries_size = (int)(ktp->kt_nentries * sizeof(ktrace_entry_t));
127 kmem_free(ktp->kt_entries, entries_size);
130 kmem_zone_free(ktrace_hdr_zone, ktp);
135 * Enter the given values into the "next" entry in the trace buffer.
136 * kt_index is always the index of the next entry to be filled.
158 static DEFINE_SPINLOCK(wrap_lock);
161 ktrace_entry_t *ktep;
166 * Grab an entry by pushing the index up to the next one.
168 spin_lock_irqsave(&wrap_lock, flags);
169 index = ktp->kt_index;
170 if (++ktp->kt_index == ktp->kt_nentries)
172 spin_unlock_irqrestore(&wrap_lock, flags);
174 if (!ktp->kt_rollover && index == ktp->kt_nentries - 1)
175 ktp->kt_rollover = 1;
177 ASSERT((index >= 0) && (index < ktp->kt_nentries));
179 ktep = &(ktp->kt_entries[index]);
191 ktep->val[10] = val10;
192 ktep->val[11] = val11;
193 ktep->val[12] = val12;
194 ktep->val[13] = val13;
195 ktep->val[14] = val14;
196 ktep->val[15] = val15;
200 * Return the number of entries in the trace buffer.
210 return (ktp->kt_rollover ? ktp->kt_nentries : ktp->kt_index);
216 * This is used to find the start of the trace buffer.
217 * In conjunction with ktrace_next() it can be used to
218 * iterate through the entire trace buffer. This code does
219 * not do any locking because it is assumed that it is called
222 * The caller must pass in a pointer to a ktrace_snap
223 * structure in which we will keep some state used to
224 * iterate through the buffer. This state must not touched
225 * by any code outside of this module.
228 ktrace_first(ktrace_t *ktp, ktrace_snap_t *ktsp)
230 ktrace_entry_t *ktep;
234 if (ktp->kt_rollover)
235 index = ktp->kt_index;
239 ktsp->ks_start = index;
240 ktep = &(ktp->kt_entries[index]);
242 nentries = ktrace_nentries(ktp);
244 if (index < nentries) {
245 ktsp->ks_index = index;
248 if (index > nentries)
257 * This is used to iterate through the entries of the given
258 * trace buffer. The caller must pass in the ktrace_snap_t
259 * structure initialized by ktrace_first(). The return value
260 * will be either a pointer to the next ktrace_entry or NULL
261 * if all of the entries have been traversed.
269 ktrace_entry_t *ktep;
271 index = ktsp->ks_index;
272 if (index == ktsp->ks_start) {
275 ktep = &ktp->kt_entries[index];
279 if (index == ktrace_nentries(ktp)) {
282 ktsp->ks_index = index;
291 * Skip the next "count" entries and return the entry after that.
292 * Return NULL if this causes us to iterate past the beginning again.
302 ktrace_entry_t *ktep;
303 int nentries = ktrace_nentries(ktp);
305 index = ktsp->ks_index;
306 new_index = index + count;
307 while (new_index >= nentries) {
308 new_index -= nentries;
310 if (index == ktsp->ks_start) {
312 * We've iterated around to the start, so we're done.
315 } else if ((new_index < index) && (index < ktsp->ks_index)) {
317 * We've skipped past the start again, so we're done.
320 ktsp->ks_index = ktsp->ks_start;
322 ktep = &(ktp->kt_entries[new_index]);
324 if (new_index == nentries) {
327 ktsp->ks_index = new_index;