debuggers.hg

view tools/xentrace/xentrace_format @ 21067:b4a1832a916f

Update Xen version to 4.0.0-rc6
author Keir Fraser <keir.fraser@citrix.com>
date Tue Mar 09 18:18:05 2010 +0000 (2010-03-09)
parents 4e7c5eb71774
children
line source
1 #!/usr/bin/env python
3 # by Mark Williamson, (C) 2004 Intel Research Cambridge
5 # Program for reformatting trace buffer output according to user-supplied rules
7 import re, sys, string, signal, struct, os, getopt
9 def usage():
10 print >> sys.stderr, \
11 "Usage: " + sys.argv[0] + """ defs-file
12 Parses trace data in binary format, as output by Xentrace and
13 reformats it according to the rules in a file of definitions. The
14 rules in this file should have the format ({ and } show grouping
15 and are not part of the syntax):
17 {event_id}{whitespace}{text format string}
19 The textual format string may include format specifiers, such as:
20 %(cpu)d, %(tsc)d, %(event)d, %(1)d, %(2)d, %(3)d, %(4)d, ...
21 [ the 'd' format specifier outputs in decimal, alternatively 'x'
22 will output in hexadecimal and 'o' will output in octal ]
24 Which correspond to the CPU number, event ID, timestamp counter and
25 the 7 data fields from the trace record. There should be one such
26 rule for each type of event.
28 Depending on your system and the volume of trace buffer data,
29 this script may not be able to keep up with the output of xentrace
30 if it is piped directly. In these circumstances you should have
31 xentrace output to a file for processing off-line.
32 """
33 sys.exit(1)
35 def read_defs(defs_file):
36 defs = {}
38 fd = open(defs_file)
40 reg = re.compile('(\S+)\s+(\S.*)')
42 while True:
43 line = fd.readline()
44 if not line:
45 break
47 if line[0] == '#' or line[0] == '\n':
48 continue
50 m = reg.match(line)
52 if not m: print >> sys.stderr, "Bad format file" ; sys.exit(1)
54 defs[str(eval(m.group(1)))] = m.group(2)
56 return defs
58 def sighand(x,y):
59 global interrupted
60 interrupted = 1
62 ##### Main code
64 mhz = 0
66 if len(sys.argv) < 2:
67 usage()
69 try:
70 opts, arg = getopt.getopt(sys.argv[1:], "c:" )
72 for opt in opts:
73 if opt[0] == '-c' : mhz = int(opt[1])
75 except getopt.GetoptError:
76 usage()
78 signal.signal(signal.SIGTERM, sighand)
79 signal.signal(signal.SIGHUP, sighand)
80 signal.signal(signal.SIGINT, sighand)
82 interrupted = 0
84 try:
85 defs = read_defs(arg[0])
86 except IOError, exn:
87 print exn
88 sys.exit(1)
90 # structure of trace record (as output by xentrace):
91 # HDR(I) {TSC(Q)} D1(I) D2(I) D3(I) D4(I) D5(I) D6(I) D7(I)
92 #
93 # HDR consists of EVENT:28:, n_data:3:, tsc_in:1:
94 # EVENT means Event ID
95 # n_data means number of data (like D1, D2, ...)
96 # tsc_in means TSC data exists(1) or not(0).
97 # if tsc_in == 0, TSC(Q) does not exists.
98 #
99 # CPU ID exists on trace data of EVENT=0x0001f003
100 #
101 HDRREC = "I"
102 TSCREC = "Q"
103 D1REC = "I"
104 D2REC = "II"
105 D3REC = "III"
106 D4REC = "IIII"
107 D5REC = "IIIII"
108 D6REC = "IIIIII"
109 D7REC = "IIIIIII"
111 last_tsc = [0]
113 TRC_TRACE_IRQ = 0x1f004
114 NR_VECTORS = 256
115 irq_measure = [{'count':0, 'tot_cycles':0, 'max_cycles':0}] * NR_VECTORS
117 i=0
119 while not interrupted:
120 try:
121 i=i+1
122 line = sys.stdin.read(struct.calcsize(HDRREC))
123 if not line:
124 break
125 event = struct.unpack(HDRREC, line)[0]
126 n_data = event >> 28 & 0x7
127 tsc_in = event >> 31
129 d1 = 0
130 d2 = 0
131 d3 = 0
132 d4 = 0
133 d5 = 0
134 d6 = 0
135 d7 = 0
137 tsc = 0
139 if tsc_in == 1:
140 line = sys.stdin.read(struct.calcsize(TSCREC))
141 if not line:
142 break
143 tsc = struct.unpack(TSCREC, line)[0]
145 if n_data == 1:
146 line = sys.stdin.read(struct.calcsize(D1REC))
147 if not line:
148 break
149 d1 = struct.unpack(D1REC, line)[0]
150 if n_data == 2:
151 line = sys.stdin.read(struct.calcsize(D2REC))
152 if not line:
153 break
154 (d1, d2) = struct.unpack(D2REC, line)
155 if n_data == 3:
156 line = sys.stdin.read(struct.calcsize(D3REC))
157 if not line:
158 break
159 (d1, d2, d3) = struct.unpack(D3REC, line)
160 if n_data == 4:
161 line = sys.stdin.read(struct.calcsize(D4REC))
162 if not line:
163 break
164 (d1, d2, d3, d4) = struct.unpack(D4REC, line)
165 if n_data == 5:
166 line = sys.stdin.read(struct.calcsize(D5REC))
167 if not line:
168 break
169 (d1, d2, d3, d4, d5) = struct.unpack(D5REC, line)
170 if n_data == 6:
171 line = sys.stdin.read(struct.calcsize(D6REC))
172 if not line:
173 break
174 (d1, d2, d3, d4, d5, d6) = struct.unpack(D6REC, line)
175 if n_data == 7:
176 line = sys.stdin.read(struct.calcsize(D7REC))
177 if not line:
178 break
179 (d1, d2, d3, d4, d5, d6, d7) = struct.unpack(D7REC, line)
181 # Event field is 28bit of 'uint32_t' in header, not 'long'.
182 event &= 0x0fffffff
183 if event == 0x1f003:
184 cpu = d1
186 if event == TRC_TRACE_IRQ:
187 # IN - d1:vector, d2:tsc_in, d3:tsc_out
188 # OUT - d1:vector, d2:count, d3:tot_cycles, d4:max_cycles
189 tsc_diff = d3 - d2
190 if tsc_diff < 0:
191 break
192 irq_measure[d1]['count'] += 1
193 irq_measure[d1]['tot_cycles'] += tsc_diff
194 if irq_measure[d1]['max_cycles'] < tsc_diff:
195 irq_measure[d1]['max_cycles'] = tsc_diff
196 d2 = irq_measure[d1]['count']
197 d3 = irq_measure[d1]['tot_cycles']
198 d4 = irq_measure[d1]['max_cycles']
200 #tsc = (tscH<<32) | tscL
202 #print i, tsc
204 if cpu >= len(last_tsc):
205 last_tsc += [0] * (cpu - len(last_tsc) + 1)
206 elif tsc < last_tsc[cpu] and tsc_in == 1:
207 print "TSC stepped backward cpu %d ! %d %d" % (cpu,tsc,last_tsc[cpu])
209 # provide relative TSC
210 if last_tsc[cpu] > 0 and tsc_in == 1:
211 reltsc = tsc - last_tsc[cpu]
212 else:
213 reltsc = 0
215 if tsc_in == 1:
216 last_tsc[cpu] = tsc
218 if mhz:
219 tsc = tsc / (mhz*1000000.0)
221 args = {'cpu' : cpu,
222 'tsc' : tsc,
223 'event' : event,
224 'reltsc': reltsc,
225 '1' : d1,
226 '2' : d2,
227 '3' : d3,
228 '4' : d4,
229 '5' : d5,
230 '6' : d6,
231 '7' : d7 }
233 try:
235 if defs.has_key(str(event)):
236 print defs[str(event)] % args
237 else:
238 if defs.has_key(str(0)): print defs[str(0)] % args
239 except TypeError:
240 if defs.has_key(str(event)):
241 print defs[str(event)]
242 print args
243 else:
244 if defs.has_key(str(0)):
245 print defs[str(0)]
246 print args
249 except IOError, struct.error: sys.exit()