plot_traces 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #!/usr/bin/env python3
  2. # Plot the network traces output when TRACE_SOCKIO is set to 1
  3. # ("make TRACE_SOCKIO=1" to build in this configuration)
  4. # Args: log_directory
  5. # Outputs trace.tex (and the files pdflatex builds, including the target
  6. # trace.pdf) into the log directory
  7. # Timescale (cm per second)
  8. timescale = .5
  9. # Nodescale (cm between nodes)
  10. nodescale = 1
  11. # Seconds between time label ticks
  12. time_tick = 2
  13. import glob
  14. import os
  15. import re
  16. import sys
  17. if len(sys.argv) != 2:
  18. print(f"Usage: {sys.argv[0]} log_directory", file=sys.stderr)
  19. sys.exit(1)
  20. # This will throw an exception if the directory does not exist
  21. os.chdir(sys.argv[1])
  22. nodelogs = glob.glob("s*.log")
  23. nodelogs.sort(key=lambda filename : int(filename[1:-4]))
  24. # Pass 1: For each sender and receiver, make a list of each message
  25. # queued from that sender to receiver, noting its queue start time,
  26. # queue end time, size, and type. Also gather all timestamp labels for
  27. # each node
  28. queued_messages = {}
  29. labels = {}
  30. min_ts = None
  31. max_ts = None
  32. for nodelog in nodelogs:
  33. node = nodelog[:-4]
  34. labels[node] = []
  35. with open(nodelog) as logf:
  36. queueing = {}
  37. for logline in logf:
  38. logline = logline.rstrip()
  39. if matches := re.match(
  40. r'(\d+\.\d+): RTE queueing (\d+) bytes to (\S+)',
  41. logline):
  42. [ts, size, recv] = matches.groups()
  43. assert(recv not in queueing)
  44. tsf = float(ts)
  45. queueing[recv] = \
  46. { 'queue_start': tsf, 'size': size }
  47. if min_ts is None or min_ts > tsf:
  48. min_ts = tsf
  49. elif matches := re.match(
  50. r'(\d+\.\d+): RTE queued (\d+) bytes to (\S+)',
  51. logline):
  52. [ts, size, recv] = matches.groups()
  53. assert(recv in queueing)
  54. assert(queueing[recv]['size'] == size)
  55. if (node, recv) not in queued_messages:
  56. queued_messages[(node, recv)] = []
  57. msg = {
  58. 'queue_start': queueing[recv]['queue_start'],
  59. 'queue_end': float(ts),
  60. 'size': size,
  61. 'type': 'RTE',
  62. }
  63. queued_messages[(node, recv)].append(msg)
  64. del queueing[recv]
  65. elif matches := re.match(
  66. r'(\d+\.\d+): Epoch \d+ (start|complete)',
  67. logline):
  68. [ts, typ] = matches.groups()
  69. tsf = float(ts)
  70. if typ == 'start':
  71. typ = 'S'
  72. elif typ == 'complete':
  73. typ = 'F'
  74. labels[node].append((tsf, typ))
  75. if min_ts is None or min_ts > tsf:
  76. min_ts = tsf
  77. if max_ts is None or max_ts < tsf:
  78. max_ts = tsf
  79. elif matches := re.match(
  80. r'(\d+\.\d+): Round (.*) complete',
  81. logline):
  82. [ts, rnd] = matches.groups()
  83. tsf = float(ts)
  84. if rnd == '11':
  85. rnd = 'A'
  86. elif rnd == '12':
  87. rnd = 'B'
  88. elif rnd == '13':
  89. rnd = 'C'
  90. labels[node].append((tsf, rnd))
  91. if max_ts is None or max_ts < tsf:
  92. max_ts = tsf
  93. elif matches := re.match(
  94. r'(\d+\.\d+): (Begin|End) Waksman networks precompute',
  95. logline):
  96. [ts, typ] = matches.groups()
  97. tsf = float(ts)
  98. if typ == 'Begin':
  99. typ = 'P'
  100. elif typ == 'End':
  101. typ = 'W'
  102. labels[node].append((tsf, typ))
  103. if min_ts is None or min_ts > tsf:
  104. min_ts = tsf
  105. if max_ts is None or max_ts < tsf:
  106. max_ts = tsf
  107. elif matches := re.match(
  108. r'(\d+\.\d+): Begin ingestion',
  109. logline):
  110. [ts] = matches.groups()
  111. tsf = float(ts)
  112. labels[node].append((tsf, 'i'))
  113. if max_ts is None or max_ts < tsf:
  114. max_ts = tsf
  115. elif matches := re.match(
  116. r'(\d+\.\d+): End ingestion of \d+ messages',
  117. logline):
  118. [ts] = matches.groups()
  119. tsf = float(ts)
  120. labels[node].append((tsf, 'I'))
  121. if max_ts is None or max_ts < tsf:
  122. max_ts = tsf
  123. # Pass 2: For each sender and receiver, note the receive start time and
  124. # receive end time for each message in the queued_messages list
  125. messages = {}
  126. for nodelog in nodelogs:
  127. node = nodelog[:-4]
  128. with open(nodelog) as logf:
  129. receiving = {}
  130. for logline in logf:
  131. logline = logline.rstrip()
  132. if matches := re.match(
  133. r'(\d+\.\d+): RTE receiving (\d+) bytes from (\S+)',
  134. logline):
  135. [ts, size, snd] = matches.groups()
  136. assert(snd not in receiving)
  137. receiving[snd] = \
  138. { 'recv_start': float(ts), 'size': size }
  139. elif matches := re.match(
  140. r'(\d+\.\d+): RTE received (\d+) bytes from (\S+)',
  141. logline):
  142. [ts, size, snd] = matches.groups()
  143. assert(snd in receiving)
  144. assert(receiving[snd]['size'] == size)
  145. assert(queued_messages[(snd, node)][0]['size'] == size)
  146. tsf = float(ts)
  147. if (snd, node) not in messages:
  148. messages[(snd, node)] = []
  149. msg = queued_messages[(snd, node)].pop(0)
  150. msg['recv_start'] = receiving[snd]['recv_start']
  151. msg['recv_end'] = tsf
  152. if max_ts is None or max_ts < tsf:
  153. max_ts = tsf
  154. messages[(snd, node)].append(msg)
  155. del receiving[snd]
  156. # Write a latex file that draws the messages
  157. with open("trace.tex", "w") as tf:
  158. print(r'''\documentclass{article}
  159. \usepackage[paperwidth=%fcm,paperheight=%fcm,margin=1cm]{geometry}
  160. \usepackage{tikz}
  161. \usepackage{times}
  162. \setlength\parindent{0pt}
  163. \pagestyle{empty}
  164. \begin{document}
  165. \begin{tikzpicture}''' % (((max_ts-min_ts)*timescale)+2.5,
  166. (len(nodelogs)+1)*nodescale+2.5), file=tf)
  167. nodenum = 0
  168. nodepos = {}
  169. print(r'''\draw[thick] (0,0) -- ++(%lf,0);''' %
  170. ((max_ts-min_ts)*timescale), file=tf)
  171. ts=0
  172. while ts<(max_ts-min_ts):
  173. print(r'''\draw [thick] (%lf,.1) node [anchor=south] { %s } -- ++(0,-.1);''' %
  174. (ts*timescale, str(ts)), file=tf)
  175. ts+=time_tick
  176. for nodelog in nodelogs:
  177. node = nodelog[:-4]
  178. nodenum += 1
  179. nodepos[node] = -nodenum * nodescale
  180. print(r'''\node [anchor=east] at (0,%lf) { %s };
  181. \draw[thick] (0,%lf) -- ++(%lf,0);''' %
  182. (nodepos[node], node, nodepos[node],
  183. (max_ts-min_ts)*timescale), file=tf)
  184. for (snd,recv) in messages:
  185. for msg in messages[(snd,recv)]:
  186. print(r'''%% %s %s %s %lf %lf %lf %lf''' % (snd, recv,
  187. msg['size'], msg['queue_start'], msg['queue_end'],
  188. msg['recv_start'], msg['recv_end']), file=tf)
  189. print(r'''\fill [fill=%s,fill opacity=.2] (%lf,%lf) --
  190. (%lf,%lf) -- (%lf,%lf) -- (%lf,%lf) -- cycle;''' %
  191. ('black',
  192. (msg['queue_start']-min_ts)*timescale, nodepos[snd],
  193. (msg['queue_end']-min_ts)*timescale, nodepos[snd],
  194. (msg['recv_end']-min_ts)*timescale, nodepos[recv],
  195. (msg['recv_start']-min_ts)*timescale, nodepos[recv]),
  196. file=tf)
  197. for node in labels:
  198. for (ts,label) in labels[node]:
  199. print(r'''%% %lf''' % ts, file=tf)
  200. print(r'''\draw [thick] (%lf,%lf) node [anchor=south] { %s } -- ++(0,-.1);''' %
  201. ((ts-min_ts)*timescale, nodepos[node]+.1, label),
  202. file=tf)
  203. print(r'''\end{tikzpicture}
  204. \end{document}''', file=tf)
  205. os.system("pdflatex trace")