diff options
| author | Mac Mollison <mollison@cs.unc.edu> | 2009-01-29 00:17:54 -0500 |
|---|---|---|
| committer | Mac Mollison <mollison@cs.unc.edu> | 2009-01-29 00:17:54 -0500 |
| commit | e5583136c038cdb004cdfe4cb3d7f1bdbd82f701 (patch) | |
| tree | 50c2b3406476d3fb4e3c6203529b6a36e1dadd65 | |
| parent | 90aff3291a0d95b8597bb057433963251ff0ae96 (diff) | |
Factored out code into 3 .py files, did some cleanup, removed main()
Works fine
| -rwxr-xr-x | sta.py | 174 | ||||
| -rw-r--r-- | sta_output.py | 15 | ||||
| -rw-r--r-- | sta_types.py | 88 |
3 files changed, 112 insertions, 165 deletions
| @@ -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. | ||
| 16 | import struct | ||
| 17 | import sys | ||
| 18 | |||
| 19 | |||
| 20 | #################################### | ||
| 21 | # Types for binary data conversion # | ||
| 22 | #################################### | ||
| 23 | |||
| 24 | st_record_size = 192 | ||
| 25 | |||
| 26 | |||
| 27 | class StHeader: | ||
| 28 | format = '<bbhi' | ||
| 29 | formatStr = struct.Struct(format) | ||
| 30 | keys = ['type','cpu','pid','job'] | ||
| 31 | message = 'The header.' | ||
| 32 | |||
| 33 | |||
| 34 | class 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 | ||
| 42 | class 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 | |||
| 49 | class 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 | |||
| 56 | class 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 | |||
| 62 | class 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 | |||
| 69 | class StSwitchAwayData: | ||
| 70 | format = 'LI' | ||
| 71 | formatStr = struct.Struct(StHeader.format + format) | ||
| 72 | keys = StHeader.keys + ['when','exec_time'] | ||
| 73 | |||
| 74 | class 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 | ||
| 81 | class 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 | |||
| 88 | class 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 | ||
| 21 | import sys | ||
| 22 | from sta_output import * | ||
| 23 | from sta_types import * | ||
| 94 | 24 | ||
| 95 | class 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 | |||
| 108 | def 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 | |||
| 134 | def print_record(record): | ||
| 135 | """Prints a record, given the record as a dict.""" | ||
| 136 | print(record) | ||
| 137 | |||
| 138 | |||
| 139 | def 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 | |||
| 147 | def trace_iter(*files): | 32 | def 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 | |||
| 164 | def 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 | |||
| 179 | def 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. | ||
| 201 | if __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 | |||
| 5 | def print_record(record): | ||
| 6 | """Prints a record, given the record as a dict.""" | ||
| 7 | print(record) | ||
| 8 | |||
| 9 | |||
| 10 | def 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 | |||
| 5 | import struct | ||
| 6 | |||
| 7 | st_record_size = 192 | ||
| 8 | |||
| 9 | class StHeader: | ||
| 10 | format = '<bbhi' | ||
| 11 | formatStr = struct.Struct(format) | ||
| 12 | keys = ['type','cpu','pid','job'] | ||
| 13 | message = 'The header.' | ||
| 14 | |||
| 15 | |||
| 16 | class 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 | ||
| 24 | class 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 | |||
| 31 | class 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 | |||
| 38 | class 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 | |||
| 44 | class 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 | |||
| 51 | class StSwitchAwayData: | ||
| 52 | format = 'LI' | ||
| 53 | formatStr = struct.Struct(StHeader.format + format) | ||
| 54 | keys = StHeader.keys + ['when','exec_time'] | ||
| 55 | |||
| 56 | class 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 | |||
| 63 | class StBlockData: | ||
| 64 | format = 'L' | ||
| 65 | formatStr = struct.Struct(StHeader.format + format) | ||
| 66 | keys = StHeader.keys + ['when'] | ||
| 67 | message = 'A task blocks.' | ||
| 68 | |||
| 69 | |||
| 70 | class StResumeData: | ||
| 71 | format = 'L' | ||
| 72 | formatStr = struct.Struct(StHeader.format + format) | ||
| 73 | keys = StHeader.keys + ['when'] | ||
| 74 | message = 'A task resumes.' | ||
| 75 | |||
| 76 | |||
| 77 | class StSysReleaseData: | ||
| 78 | format = 'LL' | ||
| 79 | formatStr = struct.Struct(StHeader.format + format) | ||
| 80 | keys = StHeader.keys + ['when','release'] | ||
| 81 | message = 'StSysReleaseData (no details).' | ||
| 82 | |||
| 83 | def 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] | ||
