@@ -43,15 +43,19 @@ def iter(self):
43
43
return self .data .islice (start = 0 )
44
44
45
45
def read_entry (f ):
46
+ """ Read a single entry of trace file f.
47
+ """
46
48
kinds = {0 :'loads' , 1 :'stores' , 2 :'atomics' }
47
49
mark = f .read (1 )
50
+ # kernel start mark
48
51
if mark == b'\x00 ' :
49
52
buf = f .read (2 )
50
53
size , = struct .unpack ('H' , buf )
51
54
kernelname = f .read (size ).decode ('utf-8' )
52
55
return 'kernel' , {
53
56
'name' : kernelname ,
54
57
}
58
+ # single access mark
55
59
elif mark == b'\xff ' :
56
60
buf = f .read (24 )
57
61
f1 , f2 , f3 = struct .unpack ('QQQ' , buf )
@@ -71,6 +75,7 @@ def read_entry(f):
71
75
'cta' : (ctax , ctay , ctaz ),
72
76
'count' : 1 ,
73
77
}
78
+ # run-length encoded access mark
74
79
elif mark == b'\xfe ' :
75
80
buf = f .read (24 + 2 )
76
81
f1 , f2 , f3 , count = struct .unpack ('QQQH' , buf )
@@ -90,15 +95,21 @@ def read_entry(f):
90
95
'cta' : (ctax , ctay , ctaz ),
91
96
'count' : count ,
92
97
}
98
+ # eof reached
93
99
elif mark == b'' :
94
100
return 'eof' , None
95
101
else :
96
102
return 'invalid' , { 'byte' : mark , 'pos' : f .tell () }
97
103
98
104
def open_database (path ):
105
+ """Open SQLite database specified by path, configure connection and
106
+ create tables if they don't exist.
107
+ """
99
108
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.
100
112
db .execute ('PRAGMA synchronous = OFF;' )
101
- db .execute ('PRAGMA foreign_keys = ON;' )
102
113
#db.set_trace_callback(print)
103
114
104
115
db .execute ("""CREATE TABLE IF NOT EXISTS kernels
@@ -122,6 +133,8 @@ def open_database(path):
122
133
return db
123
134
124
135
def write_kernel (db , kernel , tag ):
136
+ """Serialize kernel object into database under specified tag.
137
+ """
125
138
id = db .execute ("INSERT INTO kernels (tag, kernel, launch)"
126
139
" VALUES(?, ?, ?);" , (tag , kernel ['name' ], kernel ['launch' ])).lastrowid
127
140
for (x , y , z ) in kernel ['ctas' ]:
@@ -133,8 +146,18 @@ def write_kernel(db, kernel, tag):
133
146
(id , x ,y ,z , k , start , stop ))
134
147
135
148
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
+
138
161
def handle_kernel (kernel , entry ):
139
162
launch = 0 if kernel == None else kernel ['launch' ] + 1
140
163
return {
@@ -181,7 +204,9 @@ def main(argv):
181
204
while True :
182
205
kind , entry = read_entry (trace )
183
206
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 ()
185
210
kernel = handle_kernel (kernel , entry )
186
211
elif kind == 'record' :
187
212
kernel = handle_record (kernel , entry )
@@ -191,8 +216,9 @@ def main(argv):
191
216
else :
192
217
print ('invalid entry in trace file' )
193
218
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 ()
196
222
197
223
if __name__ == "__main__" :
198
224
sys .exit (main (sys .argv ))
0 commit comments