-
Notifications
You must be signed in to change notification settings - Fork 0
/
state_visualizer.py
230 lines (204 loc) · 7.3 KB
/
state_visualizer.py
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
224
225
226
227
228
229
230
#!env python3
import sys
import os
import re
from functools import cmp_to_key
column_width = 20
created_re = re.compile("^<CreateLog> (.*) was created.*")
monitor_enter_re = re.compile("^<MonitorLog> (.*) enters .*state '(.*)'.]")
state_enter_re = re.compile("^<StateLog> (.*) enters state '(.*)'[.]")
monitor_exit_re = re.compile("^<MonitorLog> (.*) exits .*state '(.*)'.]")
state_exit_re = re.compile("^<StateLog> (.*) exits state '(.*)'[.]")
send_re = re.compile("^<SendLog> '(.*)' in state .* sent event '([^ ']*).* to (.*)([(][^)]*[)])")
dequeue_re = re.compile("^<DequeueLog> '(.*)' dequeued event '([^ ']*)[ '].*")
pop_re = re.compile("^<PopLog> '(.*)' popped with unhandled event '([^']*)'")
halt_re = re.compile("^<HaltLog> (.*) halted")
error_re = re.compile("^<ErrorLog> ([^.]*)([.].*)?")
black = "\x1b[40m"
red = "\x1b[41m"
green = "\x1b[42m"
yellow = "\x1b[43m"
blue = "\x1b[44m"
magenta = "\x1b[45m"
cyan = "\x1b[46m"
white = "\x1b[47m"
def inc_machine_count(counts, mach):
if not mach in counts:
counts[mach] = 1
else:
counts[mach] += 1
def get_machines(lines):
machine_counts = {}
for line in lines:
m = created_re.match(line)
if m is not None:
machine = m.group(1)
if not machine.startswith("Plang.CSharpRuntime"):
inc_machine_count(machine_counts, machine)
continue
m = state_enter_re.match(line)
if m is not None:
machine = m.group(1)
if not machine.startswith("Plang.CSharpRuntime"):
inc_machine_count(machine_counts, machine)
continue
m = state_exit_re.match(line)
if m is not None:
machine = m.group(1)
if not machine.startswith("Plang.CSharpRuntime"):
inc_machine_count(machine_counts, machine)
continue
m = send_re.match(line)
if m is not None:
machine = m.group(1)
if not machine.startswith("Plang.CSharpRuntime"):
inc_machine_count(machine_counts, machine)
continue
m = dequeue_re.match(line)
if m is not None:
machine = m.group(1)
if not machine.startswith("Plang.CSharpRuntime"):
inc_machine_count(machine_counts, machine)
continue
m = monitor_enter_re.match(line)
if m is not None:
machine = m.group(1)
if not machine.startswith("Plang.CSharpRuntime"):
inc_machine_count(machine_counts, machine)
continue
m = monitor_exit_re.match(line)
if m is not None:
machine = m.group(1)
if not machine.startswith("Plang.CSharpRuntime"):
inc_machine_count(machine_counts, machine)
continue
machines = list(machine_counts.keys())
machines = sorted(machines, key=cmp_to_key(lambda x,y: machine_counts[y] - machine_counts[x]))
return machines
def print_header(machines):
first = True
for machine in machines:
if not first:
print("|", end='')
first = False
print(("%%-%d.%ds" % (column_width, column_width)) % machine, end='')
print("")
def create_machine_map(machines):
machine_map = {}
for machine in machines:
machine_map[machine] = { "state": "", "action": "", "color": black }
return machine_map
def process_log_line(line, machine_map):
for k in machine_map.keys():
machine_map[k]["action"] = ""
machine_map[k]["color"] = black
m = monitor_enter_re.match(line)
if m is not None:
machine = m.group(1)
state = m.group(2)
machine_map[machine]["action"] = state
machine_map[machine]["color"] = green
machine_map[machine]["state"] = state
return True
m = state_enter_re.match(line)
if m is not None:
machine = m.group(1)
state = m.group(2)
machine_map[machine]["action"] = state
machine_map[machine]["color"] = green
machine_map[machine]["state"] = state
return True
m = monitor_exit_re.match(line)
if m is not None:
machine = m.group(1)
state = m.group(2)
machine_map[machine]["action"] = state
machine_map[machine]["color"] = red
machine_map[machine]["state"] = ""
return True
m = state_exit_re.match(line)
if m is not None:
machine = m.group(1)
state = m.group(2)
machine_map[machine]["action"] = state
machine_map[machine]["color"] = red
machine_map[machine]["state"] = ""
return True
m = send_re.match(line)
if m is not None:
machine = m.group(1)
event = m.group(2)
machine_num = m.group(4)
machine_map[machine]["action"] = event+"-->"+machine_num
machine_map[machine]["color"] = blue
return True
m = dequeue_re.match(line)
if m is not None:
machine = m.group(1)
event = m.group(2)
machine_map[machine]["action"] = "-->"+event
machine_map[machine]["color"] = magenta
return True
elif line.startswith("<DequeueLog>"):
print("Failed to match: "+line)
m = pop_re.match(line)
if m is not None:
machine = m.group(1)
event = m.group(2)
machine_map[machine]["action"] = "<<<"+event+">>>"
machine_map[machine]["color"] = yellow
return True
m = halt_re.match(line)
if m is not None:
machine = m.group(1)
machine_map[machine]["action"] = "!!! Halted !!!"
machine_map[machine]["color"] = red
machine_map[machine]["state"] = "dead"
return True
m = error_re.match(line)
if m is not None:
print("%s%s%s" % (red, line, black))
return False
return False
filenames = []
pos = 1
while pos < len(sys.argv):
if sys.argv[pos].startswith("--column-width"):
if sys.argv[pos].startswith("--column-width="):
column_width = int(sys.argv[pos][15:])
pos = pos + 1
elif pos < len(sys.argv)-1:
column_width = int(sys.argv[pos+1])
pos = pos + 2
else:
print("Column with is specified as --column-width=nn or --column-width nn")
quit()
else:
filenames.append(sys.argv[pos])
pos = pos + 1
for filename in filenames:
file_in = open(filename, "r")
log_lines = []
for line in file_in:
log_lines.append(line.strip())
machines = get_machines(log_lines)
print_header(machines)
machine_map = create_machine_map(machines)
lines_processed = 0
for line in log_lines:
if process_log_line(line, machine_map):
lines_processed = lines_processed + 1
if lines_processed >= 20:
print_header(machines)
lines_processed = 0
first = True
for machine in machines:
if not first:
print("|",end="")
first = False
if machine_map[machine]["action"] != "":
print(("%%s%%-%d.%ds%%s" % (column_width, column_width)) % (machine_map[machine]["color"], machine_map[machine]["action"], black), end='')
else:
print(("%%s%%-%d.%ds" % (column_width, column_width)) % (black, machine_map[machine]["state"]), end='')
print("")
file_in.close()