diff options
| author | Gary Bressler <garybressler@nc.rr.com> | 2010-03-09 13:33:54 -0500 |
|---|---|---|
| committer | Gary Bressler <garybressler@nc.rr.com> | 2010-03-09 13:33:54 -0500 |
| commit | 883f9dfe38ab081025220aafbf47f722b540d003 (patch) | |
| tree | 6ab638add46bfd8e622301c2d704b3d6dfdba9b5 /reader | |
| parent | b97f0447b302746ab054aba0fd7417224ba73d86 (diff) | |
Preliminary, fairly workable version of unit_trace, including the visualizer.
Diffstat (limited to 'reader')
| -rwxr-xr-x | reader/sample_script.py | 8 | ||||
| -rw-r--r-- | reader/trace_reader.py | 32 |
2 files changed, 27 insertions, 13 deletions
diff --git a/reader/sample_script.py b/reader/sample_script.py index f7e9297..676cfac 100755 --- a/reader/sample_script.py +++ b/reader/sample_script.py | |||
| @@ -14,10 +14,10 @@ import stdout_printer | |||
| 14 | 14 | ||
| 15 | # Specify your trace files | 15 | # Specify your trace files |
| 16 | g6 = [ | 16 | g6 = [ |
| 17 | '../sample_traces/st-g6-0.bin', | 17 | '../traces/st-g6-0.bin', |
| 18 | '../sample_traces/st-g6-1.bin', | 18 | '../traces/st-g6-1.bin', |
| 19 | '../sample_traces/st-g6-2.bin', | 19 | '../traces/st-g6-2.bin', |
| 20 | '../sample_traces/st-g6-3.bin', | 20 | '../traces/st-g6-3.bin', |
| 21 | ] | 21 | ] |
| 22 | 22 | ||
| 23 | # Here is an example of a custom filter function. | 23 | # Here is an example of a custom filter function. |
diff --git a/reader/trace_reader.py b/reader/trace_reader.py index a4ff964..831a06e 100644 --- a/reader/trace_reader.py +++ b/reader/trace_reader.py | |||
| @@ -27,7 +27,12 @@ | |||
| 27 | 27 | ||
| 28 | import struct | 28 | import struct |
| 29 | 29 | ||
| 30 | 30 | ############################################################################### | |
| 31 | # Class definitions | ||
| 32 | ############################################################################### | ||
| 33 | class InvalidRecordError(Exception): | ||
| 34 | pass | ||
| 35 | |||
| 31 | ############################################################################### | 36 | ############################################################################### |
| 32 | # Public functions | 37 | # Public functions |
| 33 | ############################################################################### | 38 | ############################################################################### |
| @@ -49,15 +54,19 @@ def trace_reader(files): | |||
| 49 | for file in files: | 54 | for file in files: |
| 50 | file_iter = _get_file_iter(file) | 55 | file_iter = _get_file_iter(file) |
| 51 | file_iters.append(file_iter) | 56 | file_iters.append(file_iter) |
| 52 | file_iter_buff.append([file_iter.next()]) | 57 | file_iter_buff.append([]) |
| 53 | 58 | ||
| 54 | # We keep 100 records in each buffer and then keep the buffer sorted | 59 | # We keep 100 records in each buffer and then keep the buffer sorted |
| 55 | # This is because records may have been recorded slightly out of order | 60 | # This is because records may have been recorded slightly out of order |
| 56 | # This cannot guarantee records are produced in order, but it makes it | 61 | # This cannot guarantee records are produced in order, but it makes it |
| 57 | # overwhelmingly probably. | 62 | # overwhelmingly probably. |
| 58 | for x in range(0,len(file_iter_buff)): | 63 | for x in range(0,len(file_iter_buff)): |
| 59 | for y in range(0,100): | 64 | for y in range(0,100): |
| 60 | file_iter_buff[x].append(file_iters[x].next()) | 65 | try: |
| 66 | file_iter_buff[x].append(file_iters[x].next()) | ||
| 67 | except StopIteration: | ||
| 68 | break | ||
| 69 | |||
| 61 | for x in range(0,len(file_iter_buff)): | 70 | for x in range(0,len(file_iter_buff)): |
| 62 | file_iter_buff[x] = sorted(file_iter_buff[x],key=lambda rec: rec.when) | 71 | file_iter_buff[x] = sorted(file_iter_buff[x],key=lambda rec: rec.when) |
| 63 | 72 | ||
| @@ -96,7 +105,7 @@ def trace_reader(files): | |||
| 96 | 105 | ||
| 97 | # Check for monotonically increasing time | 106 | # Check for monotonically increasing time |
| 98 | if last_time is not None and earliest.when < last_time: | 107 | if last_time is not None and earliest.when < last_time: |
| 99 | exit("FATAL ERROR: trace_reader.py: out-of-order record produced") | 108 | raise InvalidRecordError("out-of-order record produced") |
| 100 | else: | 109 | else: |
| 101 | last_time = earliest.when | 110 | last_time = earliest.when |
| 102 | 111 | ||
| @@ -112,10 +121,12 @@ def _get_file_iter(file): | |||
| 112 | f = open(file,'rb') | 121 | f = open(file,'rb') |
| 113 | while True: | 122 | while True: |
| 114 | data = f.read(RECORD_HEAD_SIZE) | 123 | data = f.read(RECORD_HEAD_SIZE) |
| 124 | if data == '': | ||
| 125 | break | ||
| 115 | try: | 126 | try: |
| 116 | type_num = struct.unpack_from('b',data)[0] | 127 | type_num = struct.unpack_from('b',data)[0] |
| 117 | except struct.error: | 128 | except struct.error: |
| 118 | break #We read to the end of the file | 129 | raise InvalidRecordError("Invalid record detected, stopping.") |
| 119 | type = _get_type(type_num) | 130 | type = _get_type(type_num) |
| 120 | try: | 131 | try: |
| 121 | values = struct.unpack_from(StHeader.format + | 132 | values = struct.unpack_from(StHeader.format + |
| @@ -123,8 +134,7 @@ def _get_file_iter(file): | |||
| 123 | record_dict = dict(zip(type.keys,values)) | 134 | record_dict = dict(zip(type.keys,values)) |
| 124 | except struct.error: | 135 | except struct.error: |
| 125 | f.close() | 136 | f.close() |
| 126 | print "Invalid record detected, stopping." | 137 | raise InvalidRecordError("Invalid record detected, stopping.") |
| 127 | exit() | ||
| 128 | 138 | ||
| 129 | # Convert the record_dict into an object | 139 | # Convert the record_dict into an object |
| 130 | record = _dict2obj(record_dict) | 140 | record = _dict2obj(record_dict) |
| @@ -139,7 +149,7 @@ def _get_file_iter(file): | |||
| 139 | # If there is no timestamp, set the time to 0 | 149 | # If there is no timestamp, set the time to 0 |
| 140 | if 'when' not in record.__dict__.keys(): | 150 | if 'when' not in record.__dict__.keys(): |
| 141 | record.when = 0 | 151 | record.when = 0 |
| 142 | 152 | ||
| 143 | yield record | 153 | yield record |
| 144 | 154 | ||
| 145 | # Convert a dict into an object | 155 | # Convert a dict into an object |
| @@ -235,6 +245,8 @@ def _get_type(type_num): | |||
| 235 | types = [None,StNameData,StParamData,StReleaseData,StAssignedData, | 245 | types = [None,StNameData,StParamData,StReleaseData,StAssignedData, |
| 236 | StSwitchToData,StSwitchAwayData,StCompletionData,StBlockData, | 246 | StSwitchToData,StSwitchAwayData,StCompletionData,StBlockData, |
| 237 | StResumeData,StSysReleaseData] | 247 | StResumeData,StSysReleaseData] |
| 248 | if type_num >= len(types) or type_num < 0: | ||
| 249 | raise InvalidRecordError("Invalid record detected, stopping.") | ||
| 238 | return types[type_num] | 250 | return types[type_num] |
| 239 | 251 | ||
| 240 | # Return the type name, given the type_num (this is simply a convenience to | 252 | # Return the type name, given the type_num (this is simply a convenience to |
| @@ -242,4 +254,6 @@ def _get_type(type_num): | |||
| 242 | def _get_type_name(type_num): | 254 | def _get_type_name(type_num): |
| 243 | type_names = [None,"name","params","release","assign","switch_to", | 255 | type_names = [None,"name","params","release","assign","switch_to", |
| 244 | "switch_away","completion","block","resume","sys_release"] | 256 | "switch_away","completion","block","resume","sys_release"] |
| 257 | if type_num >= len(type_names) or type_num < 0: | ||
| 258 | raise InvalidRecordError("Invalid record detected, stopping.") | ||
| 245 | return type_names[type_num] | 259 | return type_names[type_num] |
