Skip to content

Commit 098c548

Browse files
author
Alexander Matz
committed
comments, more commits, no foreign keys
1 parent 6db83ce commit 098c548

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

tools/cta-sets.py

+32-6
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,19 @@ def iter(self):
4343
return self.data.islice(start = 0)
4444

4545
def read_entry(f):
46+
""" Read a single entry of trace file f.
47+
"""
4648
kinds={0:'loads', 1:'stores', 2:'atomics'}
4749
mark = f.read(1)
50+
# kernel start mark
4851
if mark == b'\x00':
4952
buf = f.read(2)
5053
size, = struct.unpack('H', buf)
5154
kernelname = f.read(size).decode('utf-8')
5255
return 'kernel', {
5356
'name': kernelname,
5457
}
58+
# single access mark
5559
elif mark == b'\xff':
5660
buf = f.read(24)
5761
f1, f2, f3 = struct.unpack('QQQ', buf)
@@ -71,6 +75,7 @@ def read_entry(f):
7175
'cta': (ctax, ctay, ctaz),
7276
'count': 1,
7377
}
78+
# run-length encoded access mark
7479
elif mark == b'\xfe':
7580
buf = f.read(24 + 2)
7681
f1, f2, f3, count = struct.unpack('QQQH', buf)
@@ -90,15 +95,21 @@ def read_entry(f):
9095
'cta': (ctax, ctay, ctaz),
9196
'count': count,
9297
}
98+
# eof reached
9399
elif mark == b'':
94100
return 'eof', None
95101
else:
96102
return 'invalid', { 'byte': mark, 'pos': f.tell() }
97103

98104
def open_database(path):
105+
"""Open SQLite database specified by path, configure connection and
106+
create tables if they don't exist.
107+
"""
99108
db = sqlite3.connect(path, timeout = 600)
109+
# massively improves commit performance, introduces risk of corrupt
110+
# Database on machine/OS crash or power out. Application crashes still
111+
# don't corrupt database.
100112
db.execute('PRAGMA synchronous = OFF;')
101-
db.execute('PRAGMA foreign_keys = ON;')
102113
#db.set_trace_callback(print)
103114

104115
db.execute("""CREATE TABLE IF NOT EXISTS kernels
@@ -122,6 +133,8 @@ def open_database(path):
122133
return db
123134

124135
def write_kernel(db, kernel, tag):
136+
"""Serialize kernel object into database under specified tag.
137+
"""
125138
id = db.execute("INSERT INTO kernels (tag, kernel, launch)"
126139
" VALUES(?, ?, ?);", (tag, kernel['name'], kernel['launch'])).lastrowid
127140
for (x, y, z) in kernel['ctas']:
@@ -133,8 +146,18 @@ def write_kernel(db, kernel, tag):
133146
(id, x,y,z, k, start, stop))
134147

135148

136-
# structure: { name: string, launch: num, ctas: { (x,y,z) : {loads, stores, atomics} } }
137-
# (db, tag, kernel, entry) -> kernel
149+
# Kernel object : {
150+
# 'name': string,
151+
# 'launch': num,
152+
# 'ctas': {
153+
# (x, y, z) : {
154+
# 'loads': RangeList,
155+
# 'stores': RangeList,
156+
# 'atomics': RangeList
157+
# }
158+
# }
159+
# }
160+
138161
def handle_kernel(kernel, entry):
139162
launch = 0 if kernel == None else kernel['launch'] + 1
140163
return {
@@ -181,7 +204,9 @@ def main(argv):
181204
while True:
182205
kind, entry = read_entry(trace)
183206
if kind == 'kernel':
184-
if kernel != None: write_kernel(db, kernel, tag)
207+
if kernel != None:
208+
write_kernel(db, kernel, tag)
209+
db.commit()
185210
kernel = handle_kernel(kernel, entry)
186211
elif kind == 'record':
187212
kernel = handle_record(kernel, entry)
@@ -191,8 +216,9 @@ def main(argv):
191216
else:
192217
print('invalid entry in trace file')
193218
return 1
194-
if kernel != None: write_kernel(db, kernel, tag)
195-
db.commit()
219+
if kernel != None:
220+
write_kernel(db, kernel, tag)
221+
db.commit()
196222

197223
if __name__ == "__main__":
198224
sys.exit(main(sys.argv))

0 commit comments

Comments
 (0)