-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.py
More file actions
351 lines (256 loc) · 7.66 KB
/
common.py
File metadata and controls
351 lines (256 loc) · 7.66 KB
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
#!/usr/bin/env python
import os
from stat import *
def human_byte_sizes(value):
return human_value(value, 1024, "B")
def human_value(value, base = 1000, unit = ""):
if value == None:
return "-"
if value >= pow(base, 3):
return "%.1fG%s" % (value / pow(base, 3), unit)
if value >= pow(base, 2):
return "%.1fM%s" % (value / pow(base, 2), unit)
if value >= base:
return "%.1fK%s" % (value / base, unit)
if value >= 1:
return "%d%s" % (value, unit)
if value == 0:
return "0"
if base == 1000:
return "%dm%s" % (value * base, unit)
return "%2.2f%s" % (value, unit)
class min_avg_max_class:
def clear(self):
self.min = None
self.max = None
self.sum = 0
self.pushed = 0
def __init__(self, name, value = None, base = 1000, unit = "", thread_safe = False):
self.name = name
self.base = base
self.unit = unit
self.clear()
if thread_safe:
from threading import Lock
self._threadlock = Lock()
else:
self._threadlock = None
if value:
self.push(value)
def lock_acquire(self):
if self._threadlock:
self._threadlock.acquire()
def lock_release(self):
if self._threadlock:
self._threadlock.release()
def push(self, value):
if type(value) == list:
for x in value:
self.push(x)
return
value = float(value)
self.lock_acquire()
if self.min == None or value < self.min:
self.min = value
if self.max == None or value > self.max:
self.max = value
self.sum += value
self.pushed += 1
self.lock_release()
def avg(self):
try: return float(self.sum / self.pushed)
except: return None
def __str__(self):
return """%s(min/avg/max):%s/%s/%s""" % (self.name, human_value(self.min, self.base, self.unit), human_value(self.avg(), self.base, self.unit), human_value(self.max, self.base, self.unit))
OP_TYPE_OPEN = 10
OP_TYPE_DUP = 12
OP_TYPE_DUP2 = 14
OP_TYPE_FCNTL = 15
OP_TYPE_LSEEK = 16
OP_TYPE_READ = 18
OP_TYPE_WRITE = 20
OP_TYPE_CLOSE = 22
class appdump_file:
file_and_sizes = {}
def __init__(self, fname):
self.fp = open(fname, "r")
pos = os.stat(fname)[ST_SIZE]
step = 1024
while True:
pos -= step
self.fp.seek(pos)
buf = self.fp.read(step + 16)
tagpos = buf.find("%files")
if tagpos > 0:
self.fp.seek(pos + tagpos)
if not self.fp.readline().startswith("%files"):
raise "Error"
while True:
line = self.fp.readline().strip()
if len(line) == 0:
return
line = line.split("|", 1)
if len(line) != 2:
continue
self.file_and_sizes[line[0]] = int(line[1])
def convert_args_string_to_int(self, args):
tab = { "O_RDWR":os.O_RDWR, "O_WRONLY":os.O_WRONLY, "O_RDONLY":os.O_RDONLY, "O_NONBLOCK":os.O_NONBLOCK, "O_DIRECTORY":os.O_DIRECTORY }
toret = 0
for xarg in args.split("|"):
try: toret = toret | tab[xarg]
except KeyError:
print xarg
toret = toret | int(xarg, 16)
return toret
def walk_ops(self):
self.fp.seek(0)
while True:
line = self.fp.readline()
if line.startswith('%dump'):
break
if len(line) == 0:
raise "Wrong_Format"
while len(line):
line = self.fp.readline()
if line.startswith('#') or len(line) < 5:
continue
if line.startswith('%'):
raise StopIteration
line = line.strip().split(",")
op = io_op()
try: op.tstamp = float(line[0])
except: continue
try: op.pid = int(line[1])
except: pass
op.optype, args = line[2].split("|", 1)
args = args.split("|")
if op.optype == "open":
op.optype = OP_TYPE_OPEN
op.fname = args[0]
op.mode = self.convert_args_string_to_int(args[1])
elif op.optype == "dup":
op.optype = OP_TYPE_DUP
op.fd = int(args[0])
elif op.optype == "dup2":
op.optype = OP_TYPE_DUP2
op.fd = int(args[0])
op.newfd = int(args[1])
elif op.optype == "close":
op.optype = OP_TYPE_CLOSE
op.fd = int(args[0])
elif op.optype == "lseek":
op.optype = OP_TYPE_LSEEK
op.fd = int(args[0])
op.block = int(args[1])
if args[2] == "SEEK_SET": op.whence = 0
elif args[2] == "SEEK_CUR": op.whence = 1
elif args[2] == "SEEK_END": op.whence = 2
elif op.optype == "read":
op.optype = OP_TYPE_READ
op.fd = int(args[0])
op.size = int(args[2])
elif op.optype == "write":
op.optype = OP_TYPE_WRITE
op.fd = int(args[0])
op.size = int(args[2])
elif op.optype == "fcntl":
op.optype = OP_TYPE_FCNTL
op.fd = int(args[0])
op.cmd = args[1]
# op.arg = args[2]
if line[3].startswith("0x"):
op.retcode = int(line[3], 16)
else:
op.retcode = int(line[3])
op.time_elapsed = float(line[4])
yield op
class io_op:
pid = None
optype = None
tstamp = None
fd = None
size = None
fname = None
retvalue = None
time_elapsed = None
def time_finished(self):
return float(self.tstamp + self.time_elapsed)
def __str__(self):
return "%s = %s" % (self.print_call(), self.retcode)
def print_call(self):
if self.optype == OP_TYPE_OPEN:
return "open(%s)" % (self.fname)
if self.optype == OP_TYPE_READ:
return "read(%s,%s)" % (self.fd, self.size)
if self.optype == OP_TYPE_WRITE:
return "write(%s,%s)" % (self.fd, self.size)
if self.optype == OP_TYPE_DUP:
return "dup(%s)" % (self.fd)
if self.optype == OP_TYPE_CLOSE:
return "close(%s)" % (self.fd)
if self.optype == OP_TYPE_DUP2:
return "dup2(%s,%s)" % (self.fd, self.newfd)
if self.optype == OP_TYPE_LSEEK:
return "lseek(%s,%s,%s)" % (self.fd, self.block, self.whence)
if self.optype == OP_TYPE_FCNTL:
return "fcntl(%s,%s)" % (self.fd, self.cmd)
return "[unknown_syscall]"
def unique(s):
"""Return a list of the elements in s, but without duplicates.
For example, unique([1,2,3,1,2,3]) is some permutation of [1,2,3],
unique("abcabc") some permutation of ["a", "b", "c"], and
unique(([1, 2], [2, 3], [1, 2])) some permutation of
[[2, 3], [1, 2]].
For best speed, all sequence elements should be hashable. Then
unique() will usually work in linear time.
If not possible, the sequence elements should enjoy a total
ordering, and if list(s).sort() doesn't raise TypeError it's
assumed that they do enjoy a total ordering. Then unique() will
usually work in O(N*log2(N)) time.
If that's not possible either, the sequence elements must support
equality-testing. Then unique() will usually work in quadratic
time.
"""
n = len(s)
if n == 0:
return []
# Try using a dict first, as that's the fastest and will usually
# work. If it doesn't work, it will usually fail quickly, so it
# usually doesn't cost much to *try* it. It requires that all the
# sequence elements be hashable, and support equality comparison.
u = {}
try:
for x in s:
u[x] = 1
except TypeError:
del u # move on to the next method
else:
return u.keys()
# We can't hash all the elements. Second fastest is to sort,
# which brings the equal elements together; then duplicates are
# easy to weed out in a single pass.
# NOTE: Python's list.sort() was designed to be efficient in the
# presence of many duplicate elements. This isn't true of all
# sort functions in all languages or libraries, so this approach
# is more effective in Python than it may be elsewhere.
try:
t = list(s)
t.sort()
except TypeError:
del t # move on to the next method
else:
assert n > 0
last = t[0]
lasti = i = 1
while i < n:
if t[i] != last:
t[lasti] = last = t[i]
lasti += 1
i += 1
return t[:lasti]
# Brute force is all that's left.
u = []
for x in s:
if x not in u:
u.append(x)
return u