summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMac Mollison <mollison@cs.unc.edu>2009-01-29 00:17:54 -0500
committerMac Mollison <mollison@cs.unc.edu>2009-01-29 00:17:54 -0500
commite5583136c038cdb004cdfe4cb3d7f1bdbd82f701 (patch)
tree50c2b3406476d3fb4e3c6203529b6a36e1dadd65
parent90aff3291a0d95b8597bb057433963251ff0ae96 (diff)
Factored out code into 3 .py files, did some cleanup, removed main()
Works fine
-rwxr-xr-xsta.py174
-rw-r--r--sta_output.py15
-rw-r--r--sta_types.py88
3 files changed, 112 insertions, 165 deletions
diff --git a/sta.py b/sta.py
index d8c883a..f97154d 100755
--- a/sta.py
+++ b/sta.py
@@ -1,3 +1,5 @@
1#!/usr/bin/python3
2
1#Note to self: I stopped while working on implementing an iterator so I could 3#Note to self: I stopped while working on implementing an iterator so I could
2# do a filter() operation to get only events of a certain job so I could compare 4# do a filter() operation to get only events of a certain job so I could compare
3# to Bjoern's pdf 5# to Bjoern's pdf
@@ -10,93 +12,16 @@
10 # - and /maybe/ allow for saving /loading previously saved traces. 12 # - and /maybe/ allow for saving /loading previously saved traces.
11 13
12 14
13#!/usr/bin/python3
14
15# Imports.
16import struct
17import sys
18
19
20####################################
21# Types for binary data conversion #
22####################################
23
24st_record_size = 192
25
26
27class StHeader:
28 format = '<bbhi'
29 formatStr = struct.Struct(format)
30 keys = ['type','cpu','pid','job']
31 message = 'The header.'
32
33
34class StNameData:
35 format = '16s'
36 formatStr = struct.Struct(StHeader.format + format)
37 keys = StHeader.keys + ['name']
38 message = 'The name of the executable of this process.'
39
40# Untested because this never appears in my sample files
41# ACTUALLY: None in 0, none in 1, a bunch in 2, and none in 3
42class StParamData:
43 format = 'III8?'
44 formatStr = struct.Struct(StHeader.format + format)
45 keys = StHeader.keys + ['wcet','period','phase','partition']
46 message = 'Regular parameters.'
47
48
49class StReleaseData:
50 format = 'LL'
51 formatStr = struct.Struct(StHeader.format + format)
52 keys = StHeader.keys + ['release_time','deadline']
53 message = 'A job was/is going to be released.'
54
55
56class AssignedData:
57 format = 'Lh'
58 formatStr = struct.Struct(StHeader.format + format)
59 keys = StHeader.keys + ['when','target']
60 message = 'A job was assigned to a CPU.'
61
62class StSwitchToData:
63 format = 'LI'
64 formatStr = struct.Struct(StHeader.format + format)
65 keys = StHeader.keys + ['when','exec_time']
66 message = 'A process was switched to on a given CPU.'
67
68
69class StSwitchAwayData:
70 format = 'LI'
71 formatStr = struct.Struct(StHeader.format + format)
72 keys = StHeader.keys + ['when','exec_time']
73
74class StCompletionData:
75 format = 'L7x?c'
76 formatStr = struct.Struct(StHeader.format + format)
77 keys = StHeader.keys + ['when','forced?','flags']
78 message = 'A job completed.'
79
80 15
81class StBlockData: 16##################
82 format = 'L' 17# Starting stuff #
83 formatStr = struct.Struct(StHeader.format + format) 18##################
84 keys = StHeader.keys + ['when']
85 message = 'A task blocks.'
86
87
88class StResumeData:
89 format = 'L'
90 formatStr = struct.Struct(StHeader.format + format)
91 keys = StHeader.keys + ['when']
92 message = 'A task resumes.'
93 19
20# Imports
21import sys
22from sta_output import *
23from sta_types import *
94 24
95class StSysReleaseData:
96 format = 'LL'
97 formatStr = struct.Struct(StHeader.format + format)
98 keys = StHeader.keys + ['when','release']
99 message = 'StSysReleaseData (no details).'
100 25
101 26
102 27
@@ -104,46 +29,6 @@ class StSysReleaseData:
104# Business logic # 29# Business logic #
105################## 30##################
106 31
107
108def get_type(type_num):
109 """Return the class needed to read a record, given its numerical type"""
110 type = None
111 if type_num == 1:
112 return StNameData
113 elif type_num == 2:
114 return StParamData
115 elif type_num == 3:
116 return StReleaseData
117 elif type_num == 4:
118 return StAssignedData
119 elif type_num == 5:
120 return StSwitchToData
121 elif type_num == 6:
122 return StSwitchAwayData
123 elif type_num == 7:
124 return StCompletionData
125 elif type_num == 8:
126 return StBlockData
127 elif type_num == 9:
128 return StResumeData
129 elif type_num == 10:
130 return StSysReleaseData
131 return type
132
133
134def print_record(record):
135 """Prints a record, given the record as a dict."""
136 print(record)
137
138
139def print_record_verbose(record):
140 """Prints a record verbosely, given the record as a dict."""
141 print(50*'=')
142 print(get_type(record['type']).message)
143 for k,v in record.items():
144 print(k +":", v)
145
146
147def trace_iter(*files): 32def trace_iter(*files):
148 """Returns an iterator for iterating over trace(s) and producing records.""" 33 """Returns an iterator for iterating over trace(s) and producing records."""
149 for file in files: 34 for file in files:
@@ -159,44 +44,3 @@ def trace_iter(*files):
159 except struct.error: 44 except struct.error:
160 f.close() 45 f.close()
161 break 46 break
162
163
164def trace(*files):
165 obj = "hi"
166 iter = trace_iter(*files)
167 def filter():
168 print("Hi!")
169 obj.filter = filter
170 obj.iter = iter
171 return obj
172
173
174########
175# Main #
176########
177
178
179def main(args):
180 """Main function, called if the program is executed."""
181 if args[1] == '-v':
182 printfunc = print_record_verbose
183 files = sys.argv[2:]
184 else:
185 printfunc = print_record
186 files = args[1:]
187 for file in files:
188 data = open(file,'rb').read()
189 offset = 0
190 try:
191 while True:
192 record = get_record(data,offset)
193 printfunc(record)
194 offset += st_record_size
195 #input("Press Enter")
196 except struct.error: # We've reached the EOF.
197 continue
198
199
200# Call main() if the program is executed directly.
201if __name__ == "__main__":
202 main(sys.argv)
diff --git a/sta_output.py b/sta_output.py
new file mode 100644
index 0000000..d2235b7
--- /dev/null
+++ b/sta_output.py
@@ -0,0 +1,15 @@
1####################################
2# Output formatting functions #
3####################################
4
5def print_record(record):
6 """Prints a record, given the record as a dict."""
7 print(record)
8
9
10def print_record_verbose(record):
11 """Prints a record verbosely, given the record as a dict."""
12 print(50*'=')
13 print(get_type(record['type']).message)
14 for k,v in record.items():
15 print(k +":", v)
diff --git a/sta_types.py b/sta_types.py
new file mode 100644
index 0000000..d837559
--- /dev/null
+++ b/sta_types.py
@@ -0,0 +1,88 @@
1####################################
2# Types for binary data conversion #
3####################################
4
5import struct
6
7st_record_size = 192
8
9class StHeader:
10 format = '<bbhi'
11 formatStr = struct.Struct(format)
12 keys = ['type','cpu','pid','job']
13 message = 'The header.'
14
15
16class StNameData:
17 format = '16s'
18 formatStr = struct.Struct(StHeader.format + format)
19 keys = StHeader.keys + ['name']
20 message = 'The name of the executable of this process.'
21
22# Untested because this never appears in my sample files
23# ACTUALLY: None in 0, none in 1, a bunch in 2, and none in 3
24class StParamData:
25 format = 'III8?'
26 formatStr = struct.Struct(StHeader.format + format)
27 keys = StHeader.keys + ['wcet','period','phase','partition']
28 message = 'Regular parameters.'
29
30
31class StReleaseData:
32 format = 'LL'
33 formatStr = struct.Struct(StHeader.format + format)
34 keys = StHeader.keys + ['release_time','deadline']
35 message = 'A job was/is going to be released.'
36
37
38class StAssignedData:
39 format = 'Lh'
40 formatStr = struct.Struct(StHeader.format + format)
41 keys = StHeader.keys + ['when','target']
42 message = 'A job was assigned to a CPU.'
43
44class StSwitchToData:
45 format = 'LI'
46 formatStr = struct.Struct(StHeader.format + format)
47 keys = StHeader.keys + ['when','exec_time']
48 message = 'A process was switched to on a given CPU.'
49
50
51class StSwitchAwayData:
52 format = 'LI'
53 formatStr = struct.Struct(StHeader.format + format)
54 keys = StHeader.keys + ['when','exec_time']
55
56class StCompletionData:
57 format = 'L7x?c'
58 formatStr = struct.Struct(StHeader.format + format)
59 keys = StHeader.keys + ['when','forced?','flags']
60 message = 'A job completed.'
61
62
63class StBlockData:
64 format = 'L'
65 formatStr = struct.Struct(StHeader.format + format)
66 keys = StHeader.keys + ['when']
67 message = 'A task blocks.'
68
69
70class StResumeData:
71 format = 'L'
72 formatStr = struct.Struct(StHeader.format + format)
73 keys = StHeader.keys + ['when']
74 message = 'A task resumes.'
75
76
77class StSysReleaseData:
78 format = 'LL'
79 formatStr = struct.Struct(StHeader.format + format)
80 keys = StHeader.keys + ['when','release']
81 message = 'StSysReleaseData (no details).'
82
83def get_type(type_num):
84 """Return the binary data type, given the type_num"""
85 types = [None,StNameData,StParamData,StReleaseData,StAssignedData,
86 StSwitchToData,StSwitchAwayData,StCompletionData,StBlockData,
87 StResumeData,StSysReleaseData]
88 return types[type_num]