1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
#!/usr/bin/env python
#
# Collect and plot latency-profiling data from a running gpsd.
# Requires gnuplot.
#
import sys, os, time, getopt, gps, tempfile, time, socket
class Baton:
"Ship progress indication to stderr."
def __init__(self, prompt, endmsg=None):
self.stream = sys.stderr
self.stream.write(prompt + "... \010")
self.stream.flush()
self.count = 0
self.endmsg = endmsg
self.time = time.time()
return
def twirl(self, ch=None):
if self.stream is None:
return
if ch:
self.stream.write(ch)
else:
self.stream.write("-/|\\"[self.count % 4])
self.stream.write("\010")
self.count = self.count + 1
self.stream.flush()
return
def end(self, msg=None):
if msg == None:
msg = self.endmsg
if self.stream:
self.stream.write("...(%2.2f sec) %s.\n" % (time.time() - self.time, msg))
return
class uninstrumented:
"Total times wihout instrumentation, ignoring timezone skew."
name = "uninstrumented"
def header(self, fp):
fp.write("#Read\n")
fp.write("----------\n")
def formatter(self, session, fp):
fp.write("%2.6f\n" % ((time.time() - session.gps_time) % 3600,))
return True
def plot(self, file, title, session):
fmt = '''
set autoscale
set key below
set key title "Uninstrumented total latency, %s, %dN%d, cycle %ds"
plot "%s" using 0:1 title "Total time" with impulses
'''
return fmt % (title,
session.baudrate, session.stopbits, session.cycle,
file)
class rawplot:
"Print basic data, ignoring timezone skew."
name = "raw"
def header(self, fp):
fp.write("#GPS time Received Transmitted Read\n")
fp.write("#---------------- --------- ----------- ----------\n")
def formatter(self, session, fp):
fp.write("%.6f %2.6f %2.6f %2.6f # %s\n" \
% (session.rs232,
(session.recv_time - session.gps_time) % 3600,
(session.emit_time - session.gps_time) % 3600,
(time.time() - session.gps_time) % 3600,
session.tag))
return True
def plot(self, file, title, session):
fmt = '''
set autoscale
set key below
set key title "Raw latency data, %s, %dN%d, cycle %ds"
plot \
"%s" using 0:4 title "TCP/IP latency" with impulses, \
"%s" using 0:3 title "Decode time" with impulses, \
"%s" using 0:2 title "Other latency" with impulses, \
"%s" using 0:1 title "RS232 time" with impulses
'''
return fmt % (title,
session.baudrate, session.stopbits, session.cycle,
file, file, file, file)
class splitplot:
"Discard base time, use color to indicate different tags."
name = "split"
sentences = ("GPGGA", "GPRMC", "GPGLL")
def __init__(self):
self.found = {}
def header(self, fp):
fp.write("#")
for s in splitplot.sentences:
fp.write(s + " \t")
fp.write("Received Transmitted Read\n")
fp.write("#")
for s in splitplot.sentences:
fp.write("---------\t")
fp.write("--------- ----------- ----------\n")
def formatter(self, session, fp):
# Throw out total line latencies above 1 second, these are
# serial-buffering artifacts at the start of the session
line_latency = (session.recv_time - session.gps_time) % 3600
#if line_latency > session.cycle:
# return False
for s in splitplot.sentences:
if s == session.tag:
fp.write("%2.6f\t"% session.rs232)
self.found[s] = True
else:
fp.write("- \t")
fp.write("%2.6f\t%2.6f\t%2.6f\t# %s\n" \
% (line_latency,
(session.emit_time - session.gps_time) % 3600,
(time.time() - session.gps_time) % 3600,
session.tag))
return True
def plot(self, file, title, session):
fixed = '''
set autoscale
set key below
set key title "Filtered latency data, %s, %dN%d, cycle %ds"
plot \\
"%s" using 0:%d title "TCP/IP latency" with impulses, \\
"%s" using 0:%d title "Decode time" with impulses, \\
"%s" using 0:%d title "Other latency" with impulses, \\
'''
sc = len(splitplot.sentences)
fmt = fixed % (title,
session.baudrate, session.stopbits, session.cycle,
file, sc+3, file, sc+2, file, sc+1)
for i in range(sc):
if splitplot.sentences[i] in self.found:
fmt += ' "%s" using 0:%d title "%s" with impulses, \\\n' % \
(file, i+1, splitplot.sentences[i])
return fmt[:-4] + "\n"
formatters = (rawplot, splitplot, uninstrumented)
if __name__ == '__main__':
(options, arguments) = getopt.getopt(sys.argv[1:], "f:hn:o:rs:t:T:")
formatter = splitplot
raw = False
file = None
speed = 0
terminal = None
title = time.ctime()
await = 100
for (switch, val) in options:
if (switch == '-f'):
for formatter in formatters:
if formatter.name == val:
break
else:
sys.stderr.write("gpsprof: no such formatter.\n")
sys.exit(1)
elif (switch == '-n'):
await = int(val)
elif (switch == '-o'):
file = val
elif (switch == '-r'):
raw = True
elif (switch == '-s'):
speed = int(val)
elif (switch == '-t'):
title = val
elif (switch == '-T'):
terminal = val
elif (switch == '-h'):
sys.stderr.write("usage: gpsprof [-h] [-r] [-n samplecount] "
+ "[-f {" + "|".join(map(lambda x: x.name, formatters)) + "}] [-s speed] [-t title] [-o file]\n")
sys.exit(0)
plotter = formatter()
if file:
out = open(file, "w")
elif raw:
out = sys.stdout
else:
out = tempfile.NamedTemporaryFile()
try:
session = gps.gps()
except socket.error:
sys.stderr.write("gpsprof: gpsd unreachable.\n")
sys.exit(0)
try:
if speed:
session.query("b=%d" % speed)
if session.baudrate != speed:
sys.stderr.write("gpsprof: baud rate change failed.\n")
session.query("w+bc")
if formatter != uninstrumented:
session.query("z+")
#session.set_raw_hook(lambda x: sys.stdout.write(x))
plotter.header(out)
baton = Baton("gpsprof: gathering samples")
while await > 0:
session.poll()
baton.twirl()
# If timestamp is no good, skip it.
if not session.utc or session.utc == "?":
continue
session.gps_time = gps.isotime(session.utc)
session.rs232 = (session.length * (8.0+session.stopbits))/session.baudrate
if plotter.formatter(session, out):
await -= 1
baton.end()
finally:
session.query("w-z-")
out.flush()
if not raw:
command = plotter.plot(out.name, title, session)
if terminal:
command = "set terminal " + terminal + "\n" + command
pfp = os.popen("gnuplot -persist", "w")
pfp.write(command)
pfp.close()
del session
out.close()
|