plot_traces 7.3 KB

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