kvm: qemu: provide a reset method for virtio
[kvm-userspace.git] / user / kvmtrace_format
blob9e7cfd4a04490ddf90fd3c6232f1af308a9228ca
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 kvmtrace 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 %(tsc)d, %(event)d, %(pid)d %(vcpu)d %(1)d, %(2)d,
21 %(3)d, %(4)d, %(5)d
22 [ the 'd' format specifier outputs in decimal, alternatively 'x'
23 will output in hexadecimal and 'o' will output in octal ]
25 Which correspond to the event ID, timestamp counter, pid
26 , vcpu and the 5 data fields from the trace record. There should be
27 one such 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 kvmtrace
30 if it is piped directly. In these circumstances you should have
31 kvmtrace 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 defs = read_defs(arg[0])
86 # structure of trace record (as output by kvmtrace):
87 # HDR(I) {TSC(Q)} D1(I) D2(I) D3(I) D4(I) D5(I)
89 # HDR consists of EVENT:28:, n_data:3:, tsc_in:1:
90 # pid:32, vcpu_id:32
91 # EVENT means Event ID
92 # n_data means number of data (like D1, D2, ...)
93 # tsc_in means TSC data exists(1) or not(0).
94 # if tsc_in == 0, TSC(Q) does not exists.
96 HDRREC = "III"
97 TSCREC = "Q"
98 D1REC = "I"
99 D2REC = "II"
100 D3REC = "III"
101 D4REC = "IIII"
102 D5REC = "IIIII"
104 last_tsc = 0
108 while not interrupted:
109 try:
110 i=i+1
111 line = sys.stdin.read(struct.calcsize(HDRREC))
112 if not line:
113 break
114 (event, pid, vcpu_id) = struct.unpack(HDRREC, line)
116 n_data = event >> 28 & 0x7
117 tsc_in = event >> 31
119 d1 = 0
120 d2 = 0
121 d3 = 0
122 d4 = 0
123 d5 = 0
125 tsc = 0
127 if tsc_in == 1:
128 line = sys.stdin.read(struct.calcsize(TSCREC))
129 if not line:
130 break
131 tsc = struct.unpack(TSCREC, line)[0]
132 if n_data == 1:
133 line = sys.stdin.read(struct.calcsize(D1REC))
134 if not line:
135 break
136 d1 = struct.unpack(D1REC, line)[0]
137 if n_data == 2:
138 line = sys.stdin.read(struct.calcsize(D2REC))
139 if not line:
140 break
141 (d1, d2) = struct.unpack(D2REC, line)
142 if n_data == 3:
143 line = sys.stdin.read(struct.calcsize(D3REC))
144 if not line:
145 break
146 (d1, d2, d3) = struct.unpack(D3REC, line)
147 if n_data == 4:
148 line = sys.stdin.read(struct.calcsize(D4REC))
149 if not line:
150 break
151 (d1, d2, d3, d4) = struct.unpack(D4REC, line)
152 if n_data == 5:
153 line = sys.stdin.read(struct.calcsize(D5REC))
154 if not line:
155 break
156 (d1, d2, d3, d4, d5) = struct.unpack(D5REC, line)
158 event &= 0x0fffffff
160 # provide relative TSC
162 if last_tsc > 0 and tsc_in == 1:
163 reltsc = tsc - last_tsc
164 else:
165 reltsc = 0
167 if tsc_in == 1:
168 last_tsc = tsc
170 if mhz:
171 tsc = tsc / (mhz*1000000.0)
173 args = {'tsc' : tsc,
174 'event' : event,
175 'reltsc': reltsc,
176 'pid' : pid,
177 'vcpu' : vcpu_id,
178 '1' : d1,
179 '2' : d2,
180 '3' : d3,
181 '4' : d4,
182 '5' : d5 }
184 try:
186 if defs.has_key(str(event)):
187 print defs[str(event)] % args
188 else:
189 if defs.has_key(str(0)): print defs[str(0)] % args
190 except TypeError:
191 if defs.has_key(str(event)):
192 print defs[str(event)]
193 print args
194 else:
195 if defs.has_key(str(0)):
196 print defs[str(0)]
197 print args
200 except IOError, struct.error: sys.exit()