direct-io.hg

view xen/common/trace.c @ 10690:77fe42f6442b

[IA64] Fix the ia64 build.
Signed-off-by: Alex Williamson <alex.williamson@hp.com>
author kfraser@localhost.localdomain
date Fri Jul 14 10:49:05 2006 +0100 (2006-07-14)
parents e05d60be5adb
children aa25666d4643
line source
1 /******************************************************************************
2 * common/trace.c
3 *
4 * Xen Trace Buffer
5 *
6 * Copyright (C) 2004 by Intel Research Cambridge
7 *
8 * Authors: Mark Williamson, mark.a.williamson@intel.com
9 * Rob Gardner, rob.gardner@hp.com
10 * Date: October 2005
11 *
12 * Copyright (C) 2005 Bin Ren
13 *
14 * The trace buffer code is designed to allow debugging traces of Xen to be
15 * generated on UP / SMP machines. Each trace entry is timestamped so that
16 * it's possible to reconstruct a chronological record of trace events.
17 *
18 * See also include/xen/trace.h and the dom0 op in
19 * include/public/dom0_ops.h
20 */
22 #include <xen/config.h>
23 #include <asm/types.h>
24 #include <asm/io.h>
25 #include <xen/lib.h>
26 #include <xen/sched.h>
27 #include <xen/smp.h>
28 #include <xen/trace.h>
29 #include <xen/errno.h>
30 #include <xen/event.h>
31 #include <xen/softirq.h>
32 #include <xen/init.h>
33 #include <xen/percpu.h>
34 #include <asm/atomic.h>
35 #include <public/dom0_ops.h>
37 /* opt_tbuf_size: trace buffer size (in pages) */
38 static unsigned int opt_tbuf_size = 0;
39 integer_param("tbuf_size", opt_tbuf_size);
41 /* Pointers to the meta-data objects for all system trace buffers */
42 static struct t_buf *t_bufs[NR_CPUS];
43 static struct t_rec *t_recs[NR_CPUS];
44 static int nr_recs;
46 /* High water mark for trace buffers; */
47 /* Send virtual interrupt when buffer level reaches this point */
48 static int t_buf_highwater;
50 /* Number of records lost due to per-CPU trace buffer being full. */
51 static DEFINE_PER_CPU(unsigned long, lost_records);
53 /* a flag recording whether initialization has been done */
54 /* or more properly, if the tbuf subsystem is enabled right now */
55 int tb_init_done;
57 /* which CPUs tracing is enabled on */
58 static unsigned long tb_cpu_mask = (~0UL);
60 /* which tracing events are enabled */
61 static u32 tb_event_mask = TRC_ALL;
63 static void trace_notify_guest(void)
64 {
65 send_guest_global_virq(dom0, VIRQ_TBUF);
66 }
69 /**
70 * alloc_trace_bufs - performs initialization of the per-cpu trace buffers.
71 *
72 * This function is called at start of day in order to initialize the per-cpu
73 * trace buffers. The trace buffers are then available for debugging use, via
74 * the %TRACE_xD macros exported in <xen/trace.h>.
75 *
76 * This function may also be called later when enabling trace buffers
77 * via the SET_SIZE hypercall.
78 */
79 static int alloc_trace_bufs(void)
80 {
81 int i, order;
82 unsigned long nr_pages;
83 char *rawbuf;
84 struct t_buf *buf;
86 if ( opt_tbuf_size == 0 )
87 return -EINVAL;
89 nr_pages = num_online_cpus() * opt_tbuf_size;
90 order = get_order_from_pages(nr_pages);
91 nr_recs = (opt_tbuf_size * PAGE_SIZE - sizeof(struct t_buf)) /
92 sizeof(struct t_rec);
94 if ( (rawbuf = alloc_xenheap_pages(order)) == NULL )
95 {
96 printk("Xen trace buffers: memory allocation failed\n");
97 opt_tbuf_size = 0;
98 return -EINVAL;
99 }
101 /* Share pages so that xentrace can map them. */
102 for ( i = 0; i < nr_pages; i++ )
103 share_xen_page_with_privileged_guests(
104 virt_to_page(rawbuf) + i, XENSHARE_writable);
106 for_each_online_cpu ( i )
107 {
108 buf = t_bufs[i] = (struct t_buf *)&rawbuf[i*opt_tbuf_size*PAGE_SIZE];
109 buf->cons = buf->prod = 0;
110 t_recs[i] = (struct t_rec *)(buf + 1);
111 }
113 t_buf_highwater = nr_recs >> 1; /* 50% high water */
114 open_softirq(TRACE_SOFTIRQ, trace_notify_guest);
116 return 0;
117 }
120 /**
121 * tb_set_size - handle the logic involved with dynamically
122 * allocating and deallocating tbufs
123 *
124 * This function is called when the SET_SIZE hypercall is done.
125 */
126 static int tb_set_size(int size)
127 {
128 /*
129 * Setting size is a one-shot operation. It can be done either at
130 * boot time or via control tools, but not by both. Once buffers
131 * are created they cannot be destroyed.
132 */
133 if ( (opt_tbuf_size != 0) || (size <= 0) )
134 {
135 DPRINTK("tb_set_size from %d to %d not implemented\n",
136 opt_tbuf_size, size);
137 return -EINVAL;
138 }
140 opt_tbuf_size = size;
141 if ( alloc_trace_bufs() != 0 )
142 return -EINVAL;
144 printk("Xen trace buffers: initialized\n");
145 return 0;
146 }
149 /**
150 * init_trace_bufs - performs initialization of the per-cpu trace buffers.
151 *
152 * This function is called at start of day in order to initialize the per-cpu
153 * trace buffers. The trace buffers are then available for debugging use, via
154 * the %TRACE_xD macros exported in <xen/trace.h>.
155 */
156 void init_trace_bufs(void)
157 {
158 if ( opt_tbuf_size == 0 )
159 {
160 printk("Xen trace buffers: disabled\n");
161 return;
162 }
164 if ( alloc_trace_bufs() == 0 )
165 {
166 printk("Xen trace buffers: initialised\n");
167 wmb(); /* above must be visible before tb_init_done flag set */
168 tb_init_done = 1;
169 }
170 }
173 /**
174 * tb_control - DOM0 operations on trace buffers.
175 * @tbc: a pointer to a dom0_tbufcontrol_t to be filled out
176 */
177 int tb_control(dom0_tbufcontrol_t *tbc)
178 {
179 static DEFINE_SPINLOCK(lock);
180 int rc = 0;
182 spin_lock(&lock);
184 switch ( tbc->op )
185 {
186 case DOM0_TBUF_GET_INFO:
187 tbc->cpu_mask = tb_cpu_mask;
188 tbc->evt_mask = tb_event_mask;
189 tbc->buffer_mfn = opt_tbuf_size ? virt_to_mfn(t_bufs[0]) : 0UL;
190 tbc->size = opt_tbuf_size * PAGE_SIZE;
191 break;
192 case DOM0_TBUF_SET_CPU_MASK:
193 tb_cpu_mask = tbc->cpu_mask;
194 break;
195 case DOM0_TBUF_SET_EVT_MASK:
196 tb_event_mask = tbc->evt_mask;
197 break;
198 case DOM0_TBUF_SET_SIZE:
199 rc = !tb_init_done ? tb_set_size(tbc->size) : -EINVAL;
200 break;
201 case DOM0_TBUF_ENABLE:
202 /* Enable trace buffers. Check buffers are already allocated. */
203 if ( opt_tbuf_size == 0 )
204 rc = -EINVAL;
205 else
206 tb_init_done = 1;
207 break;
208 case DOM0_TBUF_DISABLE:
209 /*
210 * Disable trace buffers. Just stops new records from being written,
211 * does not deallocate any memory.
212 */
213 tb_init_done = 0;
214 break;
215 default:
216 rc = -EINVAL;
217 break;
218 }
220 spin_unlock(&lock);
222 return rc;
223 }
225 /**
226 * trace - Enters a trace tuple into the trace buffer for the current CPU.
227 * @event: the event type being logged
228 * @d1...d5: the data items for the event being logged
229 *
230 * Logs a trace record into the appropriate buffer. Returns nonzero on
231 * failure, otherwise 0. Failure occurs only if the trace buffers are not yet
232 * initialised.
233 */
234 void trace(u32 event, unsigned long d1, unsigned long d2,
235 unsigned long d3, unsigned long d4, unsigned long d5)
236 {
237 struct t_buf *buf;
238 struct t_rec *rec;
239 unsigned long flags;
241 BUG_ON(!tb_init_done);
243 if ( (tb_event_mask & event) == 0 )
244 return;
246 /* match class */
247 if ( ((tb_event_mask >> TRC_CLS_SHIFT) & (event >> TRC_CLS_SHIFT)) == 0 )
248 return;
250 /* then match subclass */
251 if ( (((tb_event_mask >> TRC_SUBCLS_SHIFT) & 0xf )
252 & ((event >> TRC_SUBCLS_SHIFT) & 0xf )) == 0 )
253 return;
255 if ( (tb_cpu_mask & (1UL << smp_processor_id())) == 0 )
256 return;
258 /* Read tb_init_done /before/ t_bufs. */
259 rmb();
261 buf = t_bufs[smp_processor_id()];
263 local_irq_save(flags);
265 /* Check if space for two records (we write two if there are lost recs). */
266 if ( (buf->prod - buf->cons) >= (nr_recs - 1) )
267 {
268 this_cpu(lost_records)++;
269 local_irq_restore(flags);
270 return;
271 }
273 if ( unlikely(this_cpu(lost_records) != 0) )
274 {
275 rec = &t_recs[smp_processor_id()][buf->prod % nr_recs];
276 memset(rec, 0, sizeof(*rec));
277 rec->cycles = (u64)get_cycles();
278 rec->event = TRC_LOST_RECORDS;
279 rec->data[0] = this_cpu(lost_records);
280 this_cpu(lost_records) = 0;
282 wmb();
283 buf->prod++;
284 }
286 rec = &t_recs[smp_processor_id()][buf->prod % nr_recs];
287 rec->cycles = (u64)get_cycles();
288 rec->event = event;
289 rec->data[0] = d1;
290 rec->data[1] = d2;
291 rec->data[2] = d3;
292 rec->data[3] = d4;
293 rec->data[4] = d5;
295 wmb();
296 buf->prod++;
298 local_irq_restore(flags);
300 /*
301 * Notify trace buffer consumer that we've reached the high water mark.
302 *
303 */
304 if ( (buf->prod - buf->cons) == t_buf_highwater )
305 raise_softirq(TRACE_SOFTIRQ);
306 }
308 /*
309 * Local variables:
310 * mode: C
311 * c-set-style: "BSD"
312 * c-basic-offset: 4
313 * tab-width: 4
314 * indent-tabs-mode: nil
315 * End:
316 */