-
Notifications
You must be signed in to change notification settings - Fork 6
/
ProfileHandler.py
380 lines (334 loc) · 11.1 KB
/
ProfileHandler.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# -*- coding: utf-8 -*-
######################################################
# PROJECT : numaprof #
# VERSION : 1.1.2 #
# DATE : 11/2020 #
# AUTHOR : Valat Sébastien - CERN #
# LICENSE : CeCILL-C #
######################################################
######################################################
import json
import subprocess
import os
######################################################
class ProfileHandler:
metrics = [ "firstTouch", "unpinnedFirstTouch", "unpinnedPageAccess", "unpinnedThreadAccess", "unpinnedBothAccess", "localAccess", "remoteAccess", "localMcdramAccess", "remoteMcdramAccess", "nonAlloc" ]
def __init__(self,filepath):
self.filepath = filepath
self.load()
def load(self):
json_data=open(self.filepath).read()
self.data = json.loads(json_data)
version = self.data["infos"]["formatVersion"]
if version != 2:
raise Exception("Invalid format version, as {version} expected 2 !")
self.prepare()
def prepare(self):
self.prepareFileFilter();
self.prepareFileNameAndLine();
def prepareFileNameAndLine(self):
#do it for instructions
for instr in self.data["instructions"]:
fname = self.getFuncFileName(instr)
func = self.getFuncName(instr)
line = self.getLine(instr)
binary = self.getBinary(instr)
self.data["instructions"][instr]["binary"] = binary
self.data["instructions"][instr]["file"] = fname
self.data["instructions"][instr]["func"] = func
self.data["instructions"][instr]["line"] = line
#do it for allocs
for instr in self.data["allocs"]:
fname = self.getFuncFileName(instr)
func = self.getFuncName(instr)
line = self.getLine(instr)
binary = self.getBinary(instr)
self.data["allocs"][instr]["binary"] = binary
self.data["allocs"][instr]["file"] = fname
self.data["allocs"][instr]["func"] = func
self.data["allocs"][instr]["line"] = line
def getFileName(self):
return self.filepath.split('/')[-1]
def getInfos(self):
return self.data["infos"]
def getNumaTopo(self):
return self.data["topo"]
def getThreadInfos(self):
return self.data["threads"]
def getProcessSummary(self):
#init
total = {}
for m in self.metrics:
total[m] = 0
#sum all threads
for thread in self.data["threads"]:
for m in thread["stats"]:
total[m] += thread["stats"][m]
#ret
return {
"metrics": total
}
def getNumaNodes(self):
cnt = 0
for topo in self.data["topo"]:
cnt += 1
return cnt
def getNumaPageStats(self):
return self.data["process"]["maxAllocatedPages"]
def getProcessAccessMatrix(self):
out = {}
#init
for node in range(-1,self.getNumaNodes()):
out[str(node)] = []
for i in range(0,self.getNumaNodes()):
out[str(node)].append(0)
#merge
for thread in self.data["threads"]:
matrix = thread["accessMatrix"]
for node in range(-1,self.getNumaNodes()):
for i in range(0,self.getNumaNodes()):
out[str(node)][i] += matrix[str(node)][i]
#ret
return out
def getProcessDistanceCnt(self):
out = []
#init
for i in self.data["threads"][0]["distanceCnt"]:
out.append(0)
#merge
for thread in self.data["threads"]:
idx = 0
for val in thread["distanceCnt"]:
out[idx] += val
idx += 1
return out;
def prepareFileFilter(self):
out1 = {}
out2 = {}
for instr in self.data["symbols"]["instr"]:
if instr in self.data["symbols"]["instr"] and "file" in self.data["symbols"]["instr"][instr]:
fid = self.data["symbols"]["instr"][instr]["file"]
fname = os.path.normpath(self.data["symbols"]["strings"][fid])
out1[fname] = True
bid = self.data["symbols"]["instr"][instr]["binary"]
bname = os.path.normpath(self.data["symbols"]["strings"][bid])
out2[bname] = True
self.fileFilter = out1
self.binaryFilter = out2
def hasFile(self,fname):
#print self.fileFilter
return fname in self.fileFilter
def hasBinary(self,fname):
return fname in self.binaryFilter
def getFuncName(self,instr):
id = self.data["symbols"]["instr"][instr]["function"]
return self.data["symbols"]["strings"][id]
def getFuncFileName(self,instr):
if "file" in self.data["symbols"]["instr"][instr]:
id = self.data["symbols"]["instr"][instr]["file"]
return os.path.normpath(self.data["symbols"]["strings"][id])
else:
return "??"
def getLine(self,instr):
if "line" in self.data["symbols"]["instr"][instr]:
return self.data["symbols"]["instr"][instr]["line"]
else:
return "??"
def getBinary(self,instr):
if "binary" in self.data["symbols"]["instr"][instr]:
id = self.data["symbols"]["instr"][instr]["binary"]
return os.path.normpath(self.data["symbols"]["strings"][id])
else:
return "??"
def merge(self,out,inData):
for entry in inData:
if entry == "line" or entry == "func" or entry == "file" or entry == "binary":
continue;
if entry in out:
out[entry] += inData[entry]
else:
out[entry] = inData[entry]
def getDefault(self):
out = {"access":{},"alloc":{}};
for i in self.metrics:
out["access"][i] = 0
out["alloc"][i] = 0
return out
def getFuncList(self):
out = {}
#do it for instructions
instructions = self.data["instructions"]
for instr in instructions:
#fname = self.getFuncName(instr)
selectedInstructions = instructions[instr]
fname = selectedInstructions["func"]
if not fname in out:
out[fname] = self.getDefault()
#out[fname]["file"] = self.getFuncFileName(instr)
out[fname]["file"] = selectedInstructions["file"]
if not "access" in out[fname]:
out[fname]["access"] = {}
self.merge(out[fname]["access"],selectedInstructions)
#do it for allocs
allocs = self.data["allocs"]
for instr in allocs:
#fname = self.getFuncName(instr)
allocInstr = allocs[instr]
fname = allocInstr["func"]
if not fname in out:
out[fname] = self.getDefault()
out[fname]["file"] = allocInstr["file"]
if not "alloc" in out[fname]:
out[fname]["alloc"] = {}
self.merge(out[fname]["alloc"],allocInstr)
return out;
def getAsmFuncList(self):
out = {}
#do it for instructions
instructions = self.data["instructions"]
for instr in instructions:
#fname = self.getFuncName(instr)
selectedInstr = instructions[instr]
fname = selectedInstr["func"]
if not fname in out:
out[fname] = self.getDefault()
#out[fname]["file"] = self.getFuncFileName(instr)
out[fname]["file"] = selectedInstr["binary"]
if not "access" in out[fname]:
out[fname]["access"] = {}
self.merge(out[fname]["access"],selectedInstr)
#do it for allocs
allocs = self.data["allocs"]
for instr in allocs:
#fname = self.getFuncName(instr)
allocInstr = allocs[instr]
fname = allocInstr["func"]
if not fname in out:
out[fname] = self.getDefault()
out[fname]["file"] = allocInstr["binary"]
if not "alloc" in out[fname]:
out[fname]["alloc"] = {}
self.merge(out[fname]["alloc"],allocInstr)
return out;
def getFileStats(self,path):
out = {}
#do it for instructions
for instr in self.data["instructions"]:
#fname = self.getFuncFileName(instr)
fname = self.data["instructions"][instr]["file"]
if fname == path:
#line = self.getLine(instr)
line = self.data["instructions"][instr]["line"]
if not line in out:
out[line] = self.getDefault()
out[line]["func"] = self.data["instructions"][instr]["func"]
if not "access" in out[line]:
out[line]["access"] = {}
self.merge(out[line]["access"],self.data["instructions"][instr])
#do it for allocs
for instr in self.data["allocs"]:
#fname = self.getFuncFileName(instr)
fname = self.data["allocs"][instr]["file"]
if fname == path:
#line = self.getLine(instr)
line = self.data["allocs"][instr]["line"]
if not line in out:
out[line] = self.getDefault()
out[line]["func"] = self.data["allocs"][instr]["func"]
if not "alloc" in out[line]:
out[line]["alloc"] = {}
self.merge(out[line]["alloc"],self.data["allocs"][instr])
return out;
def getOneAddrOfSymbol(self,path,func):
for instr in self.data["instructions"]:
#fname = self.getFuncFileName(instr)
fname = self.data["instructions"][instr]["binary"]
f = self.data["instructions"][instr]["func"]
if path == fname and f == func:
return "0x"+self.getSoRelativeAddr(instr)
for instr in self.data["allocs"]:
#fname = self.getFuncFileName(instr)
fname = self.data["allocs"][instr]["binary"]
f = self.data["allocs"][instr]["func"]
if path == fname and f == func:
return "0x"+self.getSoRelativeAddr(instr)
def loadBinaryDesass(self,path,func):
addr = self.getOneAddrOfSymbol(path,func)[2:]
data = subprocess.check_output(['objdump', '-d',path]).decode("utf-8")
lines = data.split('\n')
out = []
status = False
for line in lines:
if status and line == "":
out.append("")
break
if line == "":
out = []
else:
caddr = line.split(":")[0].strip()
if caddr == addr:
status = True
out.append(line)
return "\n".join(out)
def loadBinaryDesassMap(self,path,func):
data = self.loadBinaryDesass(path,func)
lines = data.split('\n')
cnt = 1
out = {}
for line in lines:
if len(line) > 0 and line[0] == ' ':
addr = line.split(':')[0].strip()
out[addr] = cnt
cnt += 1
return out
def getSoRelativeAddr(self,addr):
addrInt = int(addr[2:],16)
for sym in self.data["symbols"]["map"]:
lower = int(sym["lower"][2:],16)
upper = int(sym["upper"][2:],16)
if addrInt >= lower and addrInt < upper and (".so" in sym["file"] or lower != int('00400000',16)):
baseAddr = int(self.data["symbols"]["lowAddrMap"][sym["file"]])
#print "Replace in %s : %s => 0x%x"%(sym["file"],addr,(addrInt - lower))
return "%x" % (addrInt - baseAddr)
#print "Addr not found in memory map, keep addr : %s"%addr
return addr[2:]
def getAsmFileStats(self,path,realPath,func):
out = {}
asmMap = self.loadBinaryDesassMap(realPath,func)
#do it for instructions
for instr in self.data["instructions"]:
#fname = self.getFuncFileName(instr)
fname = self.data["instructions"][instr]["binary"]
f = self.data["instructions"][instr]["func"]
if fname == path and f == func:
#line = self.getLine(instr)
#addr = instr[2:]
addr = self.getSoRelativeAddr(instr)
#print addr, asmMap[addr]
line = asmMap[addr]
if not line in out:
out[line] = self.getDefault()
out[line]["func"] = f
if not "access" in out[line]:
out[line]["access"] = {}
self.merge(out[line]["access"],self.data["instructions"][instr])
out[line]["addr"] = addr
#do it for allocs
for instr in self.data["allocs"]:
#fname = self.getFuncFileName(instr)
fname = self.data["allocs"][instr]["binary"]
f = self.data["allocs"][instr]["func"]
if fname == path and f == func:
#line = self.getLine(instr)
#addr = instr[2:]
addr = self.getSoRelativeAddr(instr)
#print addr, asmMap[addr]
line = asmMap[addr]
if not line in out:
out[line] = self.getDefault()
out[line]["func"] = f
if not "alloc" in out[line]:
out[line]["alloc"] = {}
self.merge(out[line]["alloc"],self.data["allocs"][instr])
out[line]["addr"] = addr
return out;