ia64/xen-unstable

view xen/common/trace.c @ 10287:8c6ff337d164

Allow all trace-buffer hypercalls to be executed even when tracing is currently disabled.
From: George Dunlap
Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Thu Jun 01 21:34:47 2006 +0100 (2006-06-01)
parents 0f162a135140
children 808430428622
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 <asm/atomic.h>
34 #include <public/dom0_ops.h>
36 /* opt_tbuf_size: trace buffer size (in pages) */
37 static unsigned int opt_tbuf_size = 0;
38 integer_param("tbuf_size", opt_tbuf_size);
40 /* Pointers to the meta-data objects for all system trace buffers */
41 static struct t_buf *t_bufs[NR_CPUS];
42 static struct t_rec *t_recs[NR_CPUS];
43 static int nr_recs;
45 /* High water mark for trace buffers; */
46 /* Send virtual interrupt when buffer level reaches this point */
47 static int t_buf_highwater;
50 /* a flag recording whether initialization has been done */
51 /* or more properly, if the tbuf subsystem is enabled right now */
52 int tb_init_done;
54 /* which CPUs tracing is enabled on */
55 static unsigned long tb_cpu_mask = (~0UL);
57 /* which tracing events are enabled */
58 static u32 tb_event_mask = TRC_ALL;
60 static void trace_notify_guest(void)
61 {
62 send_guest_global_virq(dom0, VIRQ_TBUF);
63 }
66 /**
67 * alloc_trace_bufs - performs initialization of the per-cpu trace buffers.
68 *
69 * This function is called at start of day in order to initialize the per-cpu
70 * trace buffers. The trace buffers are then available for debugging use, via
71 * the %TRACE_xD macros exported in <xen/trace.h>.
72 *
73 * This function may also be called later when enabling trace buffers
74 * via the SET_SIZE hypercall.
75 */
76 static int alloc_trace_bufs(void)
77 {
78 int i, order;
79 unsigned long nr_pages;
80 char *rawbuf;
81 struct t_buf *buf;
83 if ( opt_tbuf_size == 0 )
84 return -EINVAL;
86 nr_pages = num_online_cpus() * opt_tbuf_size;
87 order = get_order_from_pages(nr_pages);
88 nr_recs = (opt_tbuf_size * PAGE_SIZE - sizeof(struct t_buf)) /
89 sizeof(struct t_rec);
91 if ( (rawbuf = alloc_xenheap_pages(order)) == NULL )
92 {
93 printk("Xen trace buffers: memory allocation failed\n");
94 opt_tbuf_size = 0;
95 return -EINVAL;
96 }
98 /* Share pages so that xentrace can map them. */
99 for ( i = 0; i < nr_pages; i++ )
100 share_xen_page_with_privileged_guests(
101 virt_to_page(rawbuf) + i, XENSHARE_writable);
103 for_each_online_cpu ( i )
104 {
105 buf = t_bufs[i] = (struct t_buf *)&rawbuf[i*opt_tbuf_size*PAGE_SIZE];
106 buf->cons = buf->prod = 0;
107 t_recs[i] = (struct t_rec *)(buf + 1);
108 }
110 t_buf_highwater = nr_recs >> 1; /* 50% high water */
111 open_softirq(TRACE_SOFTIRQ, trace_notify_guest);
113 return 0;
114 }
117 /**
118 * tb_set_size - handle the logic involved with dynamically
119 * allocating and deallocating tbufs
120 *
121 * This function is called when the SET_SIZE hypercall is done.
122 */
123 static int tb_set_size(int size)
124 {
125 /*
126 * Setting size is a one-shot operation. It can be done either at
127 * boot time or via control tools, but not by both. Once buffers
128 * are created they cannot be destroyed.
129 */
130 if ( (opt_tbuf_size != 0) || (size <= 0) )
131 {
132 DPRINTK("tb_set_size from %d to %d not implemented\n",
133 opt_tbuf_size, size);
134 return -EINVAL;
135 }
137 opt_tbuf_size = size;
138 if ( alloc_trace_bufs() != 0 )
139 return -EINVAL;
141 printk("Xen trace buffers: initialized\n");
142 return 0;
143 }
146 /**
147 * init_trace_bufs - performs initialization of the per-cpu trace buffers.
148 *
149 * This function is called at start of day in order to initialize the per-cpu
150 * trace buffers. The trace buffers are then available for debugging use, via
151 * the %TRACE_xD macros exported in <xen/trace.h>.
152 */
153 void init_trace_bufs(void)
154 {
155 if ( opt_tbuf_size == 0 )
156 {
157 printk("Xen trace buffers: disabled\n");
158 return;
159 }
161 if ( alloc_trace_bufs() == 0 )
162 {
163 printk("Xen trace buffers: initialised\n");
164 wmb(); /* above must be visible before tb_init_done flag set */
165 tb_init_done = 1;
166 }
167 }
170 /**
171 * tb_control - DOM0 operations on trace buffers.
172 * @tbc: a pointer to a dom0_tbufcontrol_t to be filled out
173 */
174 int tb_control(dom0_tbufcontrol_t *tbc)
175 {
176 static spinlock_t lock = SPIN_LOCK_UNLOCKED;
177 int rc = 0;
179 spin_lock(&lock);
181 switch ( tbc->op )
182 {
183 case DOM0_TBUF_GET_INFO:
184 tbc->cpu_mask = tb_cpu_mask;
185 tbc->evt_mask = tb_event_mask;
186 tbc->buffer_mfn = opt_tbuf_size ? virt_to_mfn(t_bufs[0]) : 0UL;
187 tbc->size = opt_tbuf_size * PAGE_SIZE;
188 break;
189 case DOM0_TBUF_SET_CPU_MASK:
190 tb_cpu_mask = tbc->cpu_mask;
191 break;
192 case DOM0_TBUF_SET_EVT_MASK:
193 tb_event_mask = tbc->evt_mask;
194 break;
195 case DOM0_TBUF_SET_SIZE:
196 rc = !tb_init_done ? tb_set_size(tbc->size) : -EINVAL;
197 break;
198 case DOM0_TBUF_ENABLE:
199 /* Enable trace buffers. Check buffers are already allocated. */
200 if ( opt_tbuf_size == 0 )
201 rc = -EINVAL;
202 else
203 tb_init_done = 1;
204 break;
205 case DOM0_TBUF_DISABLE:
206 /*
207 * Disable trace buffers. Just stops new records from being written,
208 * does not deallocate any memory.
209 */
210 tb_init_done = 0;
211 break;
212 default:
213 rc = -EINVAL;
214 break;
215 }
217 spin_unlock(&lock);
219 return rc;
220 }
222 /**
223 * trace - Enters a trace tuple into the trace buffer for the current CPU.
224 * @event: the event type being logged
225 * @d1...d5: the data items for the event being logged
226 *
227 * Logs a trace record into the appropriate buffer. Returns nonzero on
228 * failure, otherwise 0. Failure occurs only if the trace buffers are not yet
229 * initialised.
230 */
231 void trace(u32 event, unsigned long d1, unsigned long d2,
232 unsigned long d3, unsigned long d4, unsigned long d5)
233 {
234 struct t_buf *buf;
235 struct t_rec *rec;
236 unsigned long flags;
238 BUG_ON(!tb_init_done);
240 if ( (tb_event_mask & event) == 0 )
241 return;
243 /* match class */
244 if ( ((tb_event_mask >> TRC_CLS_SHIFT) & (event >> TRC_CLS_SHIFT)) == 0 )
245 return;
247 /* then match subclass */
248 if ( (((tb_event_mask >> TRC_SUBCLS_SHIFT) & 0xf )
249 & ((event >> TRC_SUBCLS_SHIFT) & 0xf )) == 0 )
250 return;
252 if ( (tb_cpu_mask & (1UL << smp_processor_id())) == 0 )
253 return;
255 /* Read tb_init_done /before/ t_bufs. */
256 rmb();
258 buf = t_bufs[smp_processor_id()];
260 local_irq_save(flags);
262 if ( (buf->prod - buf->cons) >= nr_recs )
263 {
264 local_irq_restore(flags);
265 return;
266 }
268 rec = &t_recs[smp_processor_id()][buf->prod % nr_recs];
269 rec->cycles = (u64)get_cycles();
270 rec->event = event;
271 rec->data[0] = d1;
272 rec->data[1] = d2;
273 rec->data[2] = d3;
274 rec->data[3] = d4;
275 rec->data[4] = d5;
277 wmb();
278 buf->prod++;
280 local_irq_restore(flags);
282 /*
283 * Notify trace buffer consumer that we've reached the high water mark.
284 *
285 */
286 if ( (buf->prod - buf->cons) == t_buf_highwater )
287 raise_softirq(TRACE_SOFTIRQ);
288 }
290 /*
291 * Local variables:
292 * mode: C
293 * c-set-style: "BSD"
294 * c-basic-offset: 4
295 * tab-width: 4
296 * indent-tabs-mode: nil
297 * End:
298 */