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
|
#!/usr/bin/python3
##################
# Imports #
##################
import sys
import struct
##################
# Trace class #
##################
class Trace:
"""Object representing a trace (i.e. set of records)"""
def __init__(self,files):
"""Initialize the Trace object with an iterator"""
self.iter = self.make_iter(files)
def make_iter(self, files):
"""Make the iterator for this Trace object"""
for file in files:
f = open(file,'rb')
while True:
data = f.read(24)
try:
typenum = struct.unpack_from('b',data)[0]
except struct.error:
#We read to the end of the file
f.close()
break
type = get_type(typenum)
try:
values = struct.unpack_from(StHeader.format +
type.format,data)
record = dict(zip(type.keys,values))
except struct.error:
f.close()
print("Invalid record detected, stopping.")
exit()
yield record
def filter(self, criteria):
"""Apply a filter to the trace"""
#Determine if they want to filter by >, <, !=, or ==
comp = None
seps = ['>','<','==','!=']
for sep in seps:
if criteria.find(sep) > 0:
comp = sep
if not comp:
print("Your filter is invalid: " + criteria)
exit()
#Apply the filter
field, comp, value = criteria.partition(comp)
test = "rec['" + field + "']" + comp + "int(" + value + ")"
def func(rec):
try:
if eval(test):
return True
else:
return False
except KeyError:
return False
self.iter = filter(func, self.iter)
def sort(self, key):
"""Return the records sorted by some key"""
def sortfunc(record):
if key in record:
return record[key]
else:
return 0
self.iter = sorted(self.iter, key=sortfunc)
def print_records(self):
"""Prints all records in the trace"""
for record in self.iter:
print(50*'=')
print(get_type(record['type']).message)
for k,v in record.items():
print(k +":", v)
def print_count(self,verbose=False):
"""Prints the number of records in the trace."""
print("Total Count: " + str(len(self.iter)))
if verbose:
counts = {1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0,9:0,10:0}
for record in self.iter:
type = record['type']
counts[type] += 1
for key in counts.keys():
print(get_type(key).__name__ +
': ' + str(counts[key]))
print("")
####################################
# Types for binary data conversion #
####################################
import struct
class StHeader:
format = '<bbhi'
formatStr = struct.Struct(format)
keys = ['type','cpu','pid','job']
message = 'The header.'
class StNameData:
format = '16s'
formatStr = struct.Struct(StHeader.format + format)
keys = StHeader.keys + ['name']
message = 'The name of the executable of this process.'
class StParamData:
format = 'IIIc'
#format = 'ccccc'
formatStr = struct.Struct(StHeader.format + format)
keys = StHeader.keys + ['wcet','period','phase','partition']
message = 'Regular parameters.'
class StReleaseData:
format = 'LL'
formatStr = struct.Struct(StHeader.format + format)
keys = StHeader.keys + ['release_time','deadline']
message = 'A job was/is going to be released.'
#Not yet used by Sched Trace
class StAssignedData:
format = 'Lc'
formatStr = struct.Struct(StHeader.format + format)
keys = StHeader.keys + ['when','target']
message = 'A job was assigned to a CPU.'
class StSwitchToData:
format = 'LI'
formatStr = struct.Struct(StHeader.format + format)
keys = StHeader.keys + ['when','exec_time']
message = 'A process was switched to on a given CPU.'
class StSwitchAwayData:
format = 'LI'
formatStr = struct.Struct(StHeader.format + format)
keys = StHeader.keys + ['when','exec_time']
message = 'A process was switched away on a given CPU.'
class StCompletionData:
format = 'L7x?c'
formatStr = struct.Struct(StHeader.format + format)
keys = StHeader.keys + ['when','forced?','flags']
message = 'A job completed.'
class StBlockData:
format = 'L'
formatStr = struct.Struct(StHeader.format + format)
keys = StHeader.keys + ['when']
message = 'A task blocks.'
class StResumeData:
format = 'L'
formatStr = struct.Struct(StHeader.format + format)
keys = StHeader.keys + ['when']
message = 'A task resumes.'
class StSysReleaseData:
format = 'LL'
formatStr = struct.Struct(StHeader.format + format)
keys = StHeader.keys + ['when','release']
message = 'StSysReleaseData (no details).'
def get_type(type_num):
"""Return the binary data type, given the type_num"""
types = [None,StNameData,StParamData,StReleaseData,StAssignedData,
StSwitchToData,StSwitchAwayData,StCompletionData,StBlockData,
StResumeData,StSysReleaseData]
return types[type_num]
|