parse_log.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. import os, sys, time
  2. # usage: parse_log.py log-file [socket-index to focus on]
  3. socket_filter = None
  4. if len(sys.argv) >= 3:
  5. socket_filter = sys.argv[2].strip()
  6. if socket_filter == None:
  7. print "scanning for socket with the most packets"
  8. file = open(sys.argv[1], 'rb')
  9. sockets = {}
  10. for l in file:
  11. if not 'our_delay' in l: continue
  12. try:
  13. a = l.strip().split(" ")
  14. socket_index = a[1][:-1]
  15. except:
  16. continue
  17. # msvc's runtime library doesn't prefix pointers
  18. # with '0x'
  19. # if socket_index[:2] != '0x':
  20. # continue
  21. if socket_index in sockets:
  22. sockets[socket_index] += 1
  23. else:
  24. sockets[socket_index] = 1
  25. items = sockets.items()
  26. items.sort(lambda x, y: y[1] - x[1])
  27. count = 0
  28. for i in items:
  29. print '%s: %d' % (i[0], i[1])
  30. count += 1
  31. if count > 5: break
  32. file.close()
  33. socket_filter = items[0][0]
  34. print '\nfocusing on socket %s' % socket_filter
  35. file = open(sys.argv[1], 'rb')
  36. out_file = 'utp.out%s' % socket_filter;
  37. out = open(out_file, 'wb')
  38. delay_samples = 'dots lc rgb "blue"'
  39. delay_base = 'steps lw 2 lc rgb "purple"'
  40. target_delay = 'steps lw 2 lc rgb "red"'
  41. off_target = 'dots lc rgb "blue"'
  42. cwnd = 'steps lc rgb "green"'
  43. window_size = 'steps lc rgb "sea-green"'
  44. rtt = 'lines lc rgb "light-blue"'
  45. metrics = {
  46. 'our_delay':['our delay (ms)', 'x1y2', delay_samples],
  47. 'upload_rate':['send rate (B/s)', 'x1y1', 'lines'],
  48. 'max_window':['cwnd (B)', 'x1y1', cwnd],
  49. 'target_delay':['target delay (ms)', 'x1y2', target_delay],
  50. 'cur_window':['bytes in-flight (B)', 'x1y1', window_size],
  51. 'cur_window_packets':['number of packets in-flight', 'x1y2', 'steps'],
  52. 'packet_size':['current packet size (B)', 'x1y2', 'steps'],
  53. 'rtt':['rtt (ms)', 'x1y2', rtt],
  54. 'off_target':['off-target (ms)', 'x1y2', off_target],
  55. 'delay_sum':['delay sum (ms)', 'x1y2', 'steps'],
  56. 'their_delay':['their delay (ms)', 'x1y2', delay_samples],
  57. 'get_microseconds':['clock (us)', 'x1y1', 'steps'],
  58. 'wnduser':['advertised window size (B)', 'x1y1', 'steps'],
  59. 'delay_base':['delay base (us)', 'x1y1', delay_base],
  60. 'their_delay_base':['their delay base (us)', 'x1y1', delay_base],
  61. 'their_actual_delay':['their actual delay (us)', 'x1y1', delay_samples],
  62. 'actual_delay':['actual_delay (us)', 'x1y1', delay_samples]
  63. }
  64. histogram_quantization = 1
  65. socket_index = None
  66. columns = []
  67. begin = None
  68. title = "-"
  69. packet_loss = 0
  70. packet_timeout = 0
  71. delay_histogram = {}
  72. window_size = {'0': 0, '1': 0}
  73. # [35301484] 0x00ec1190: actual_delay:1021583 our_delay:102 their_delay:-1021345 off_target:297 max_window:2687 upload_rate:18942 delay_base:1021481154 delay_sum:-1021242 target_delay:400 acked_bytes:1441 cur_window:2882 scaled_gain:2.432
  74. counter = 0
  75. print "reading log file"
  76. for l in file:
  77. if "UTP_Connect" in l:
  78. title = l[:-2]
  79. if socket_filter != None:
  80. title += ' socket: %s' % socket_filter
  81. else:
  82. title += ' sum of all sockets'
  83. continue
  84. try:
  85. a = l.strip().split(" ")
  86. t = a[0][1:-1]
  87. socket_index = a[1][:-1]
  88. except:
  89. continue
  90. # if socket_index[:2] != '0x':
  91. # continue
  92. if socket_filter != None and socket_index != socket_filter:
  93. continue
  94. counter += 1
  95. if (counter % 300 == 0):
  96. print "\r%d " % counter,
  97. if "lost." in l:
  98. packet_loss = packet_loss + 1
  99. continue
  100. if "Packet timeout" in l:
  101. packet_timeout = packet_timeout + 1
  102. continue
  103. if "our_delay:" not in l:
  104. continue
  105. # used for Logf timestamps
  106. # t, m = t.split(".")
  107. # t = time.strptime(t, "%H:%M:%S")
  108. # t = list(t)
  109. # t[0] += 107
  110. # t = tuple(t)
  111. # m = float(m)
  112. # m /= 1000.0
  113. # t = time.mktime(t) + m
  114. # used for tick count timestamps
  115. t = int(t)
  116. if begin is None:
  117. begin = t
  118. t = t - begin
  119. # print time. Convert from milliseconds to seconds
  120. print >>out, '%f\t' % (float(t)/1000.),
  121. #if t > 200000:
  122. # break
  123. fill_columns = not columns
  124. for i in a[2:]:
  125. try:
  126. n, v = i.split(':')
  127. except:
  128. continue
  129. v = float(v)
  130. if n == "our_delay":
  131. bucket = v / histogram_quantization
  132. delay_histogram[bucket] = 1 + delay_histogram.get(bucket, 0)
  133. if not n in metrics: continue
  134. if fill_columns:
  135. columns.append(n)
  136. if n == "max_window":
  137. window_size[socket_index] = v
  138. print >>out, '%f\t' % int(reduce(lambda a,b: a+b, window_size.values())),
  139. else:
  140. print >>out, '%f\t' % v,
  141. print >>out, float(packet_loss * 8000), float(packet_timeout * 8000)
  142. packet_loss = 0
  143. packet_timeout = 0
  144. out.close()
  145. out = open('%s.histogram' % out_file, 'wb')
  146. for d,f in delay_histogram.iteritems():
  147. print >>out, float(d*histogram_quantization) + histogram_quantization / 2, f
  148. out.close()
  149. plot = [
  150. {
  151. 'data': ['upload_rate', 'max_window', 'cur_window', 'wnduser', 'cur_window_packets', 'packet_size', 'rtt'],
  152. 'title': 'send-packet-size',
  153. 'y1': 'Bytes',
  154. 'y2': 'Time (ms)'
  155. },
  156. {
  157. 'data': ['our_delay', 'max_window', 'target_delay', 'cur_window', 'wnduser', 'cur_window_packets'],
  158. 'title': 'uploading',
  159. 'y1': 'Bytes',
  160. 'y2': 'Time (ms)'
  161. },
  162. {
  163. 'data': ['our_delay', 'max_window', 'target_delay', 'cur_window', 'cur_window_packets'],
  164. 'title': 'uploading_packets',
  165. 'y1': 'Bytes',
  166. 'y2': 'Time (ms)'
  167. },
  168. {
  169. 'data': ['get_microseconds'],
  170. 'title': 'timer',
  171. 'y1': 'Time microseconds',
  172. 'y2': 'Time (ms)'
  173. },
  174. {
  175. 'data': ['their_delay', 'target_delay', 'rtt'],
  176. 'title': 'their_delay',
  177. 'y1': '',
  178. 'y2': 'Time (ms)'
  179. },
  180. {
  181. 'data': ['their_actual_delay','their_delay_base'],
  182. 'title': 'their_delay_base',
  183. 'y1': 'Time (us)',
  184. 'y2': ''
  185. },
  186. {
  187. 'data': ['our_delay', 'target_delay', 'rtt'],
  188. 'title': 'our-delay',
  189. 'y1': '',
  190. 'y2': 'Time (ms)'
  191. },
  192. {
  193. 'data': ['actual_delay', 'delay_base'],
  194. 'title': 'our_delay_base',
  195. 'y1': 'Time (us)',
  196. 'y2': ''
  197. }
  198. ]
  199. out = open('utp.gnuplot', 'w+')
  200. files = ''
  201. #print >>out, 'set xtics 0, 20'
  202. print >>out, "set term png size 1280,800"
  203. print >>out, 'set output "%s.delays.png"' % out_file
  204. print >>out, 'set xrange [0:250]'
  205. print >>out, 'set xlabel "delay (ms)"'
  206. print >>out, 'set boxwidth 1'
  207. print >>out, 'set style fill solid'
  208. print >>out, 'set ylabel "number of packets"'
  209. print >>out, 'plot "%s.histogram" using 1:2 with boxes' % out_file
  210. print >>out, "set style data steps"
  211. #print >>out, "set yrange [0:*]"
  212. print >>out, "set y2range [*:*]"
  213. files += out_file + '.delays.png '
  214. #set hidden3d
  215. #set title "Peer bandwidth distribution"
  216. #set xlabel "Ratio"
  217. for p in plot:
  218. print >>out, 'set title "%s %s"' % (p['title'], title)
  219. print >>out, 'set xlabel "time (s)"'
  220. print >>out, 'set ylabel "%s"' % p['y1']
  221. print >>out, "set tics nomirror"
  222. print >>out, 'set y2tics'
  223. print >>out, 'set y2label "%s"' % p['y2']
  224. print >>out, 'set xrange [0:*]'
  225. print >>out, "set key box"
  226. print >>out, "set term png size 1280,800"
  227. print >>out, 'set output "%s-%s.png"' % (out_file, p['title'])
  228. files += '%s-%s.png ' % (out_file, p['title'])
  229. comma = ''
  230. print >>out, "plot",
  231. for c in p['data']:
  232. if not c in metrics: continue
  233. i = columns.index(c)
  234. print >>out, '%s"%s" using 1:%d title "%s-%s" axes %s with %s' % (comma, out_file, i + 2, metrics[c][0], metrics[c][1], metrics[c][1], metrics[c][2]),
  235. comma = ', '
  236. print >>out, ''
  237. out.close()
  238. os.system("gnuplot utp.gnuplot")
  239. os.system("open %s" % files)