diff options
author | Gary Bressler <garybressler@nc.rr.com> | 2010-02-25 12:04:02 -0500 |
---|---|---|
committer | Gary Bressler <garybressler@nc.rr.com> | 2010-02-25 12:04:02 -0500 |
commit | d7dfc575f9a1a5a0117cab591c6284b1f1844dcf (patch) | |
tree | 0ed3ebbe43138ecaf67d070bfd4c9614dd511f08 /trace_reader.py | |
parent | 75dedea2014c9ea703bb4000fbfda45c20b196e5 (diff) |
This commit of unit-trace converts it to Python2 from Python3, so that it
can operate with the visualizer, which uses Python2.
Diffstat (limited to 'trace_reader.py')
-rw-r--r-- | trace_reader.py | 75 |
1 files changed, 38 insertions, 37 deletions
diff --git a/trace_reader.py b/trace_reader.py index e18547e..a4ff964 100644 --- a/trace_reader.py +++ b/trace_reader.py | |||
@@ -49,7 +49,7 @@ def trace_reader(files): | |||
49 | for file in files: | 49 | for file in files: |
50 | file_iter = _get_file_iter(file) | 50 | file_iter = _get_file_iter(file) |
51 | file_iters.append(file_iter) | 51 | file_iters.append(file_iter) |
52 | file_iter_buff.append([next(file_iter)]) | 52 | file_iter_buff.append([file_iter.next()]) |
53 | 53 | ||
54 | # We keep 100 records in each buffer and then keep the buffer sorted | 54 | # 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 | 55 | # This is because records may have been recorded slightly out of order |
@@ -57,7 +57,7 @@ def trace_reader(files): | |||
57 | # overwhelmingly probably. | 57 | # overwhelmingly probably. |
58 | for x in range(0,len(file_iter_buff)): | 58 | for x in range(0,len(file_iter_buff)): |
59 | for y in range(0,100): | 59 | for y in range(0,100): |
60 | file_iter_buff[x].append(next(file_iters[x])) | 60 | file_iter_buff[x].append(file_iters[x].next()) |
61 | for x in range(0,len(file_iter_buff)): | 61 | for x in range(0,len(file_iter_buff)): |
62 | file_iter_buff[x] = sorted(file_iter_buff[x],key=lambda rec: rec.when) | 62 | file_iter_buff[x] = sorted(file_iter_buff[x],key=lambda rec: rec.when) |
63 | 63 | ||
@@ -83,7 +83,7 @@ def trace_reader(files): | |||
83 | # Try to append a new record to the buffer (if there is another) and | 83 | # Try to append a new record to the buffer (if there is another) and |
84 | # then keep the buffer sorted | 84 | # then keep the buffer sorted |
85 | try: | 85 | try: |
86 | file_iter_buff[buff_to_refill].append(next(file_iters[buff_to_refill])) | 86 | file_iter_buff[buff_to_refill].append(file_iters[buff_to_refill].next()) |
87 | file_iter_buff[buff_to_refill] = sorted(file_iter_buff[buff_to_refill], | 87 | file_iter_buff[buff_to_refill] = sorted(file_iter_buff[buff_to_refill], |
88 | key=lambda rec: rec.when) | 88 | key=lambda rec: rec.when) |
89 | 89 | ||
@@ -109,42 +109,42 @@ def trace_reader(files): | |||
109 | 109 | ||
110 | # Returns an iterator to pull records from a file | 110 | # Returns an iterator to pull records from a file |
111 | def _get_file_iter(file): | 111 | def _get_file_iter(file): |
112 | with open(file,'rb') as f: | 112 | f = open(file,'rb') |
113 | while True: | 113 | while True: |
114 | data = f.read(RECORD_HEAD_SIZE) | 114 | data = f.read(RECORD_HEAD_SIZE) |
115 | try: | 115 | try: |
116 | type_num = struct.unpack_from('b',data)[0] | 116 | type_num = struct.unpack_from('b',data)[0] |
117 | except struct.error: | 117 | except struct.error: |
118 | break #We read to the end of the file | 118 | break #We read to the end of the file |
119 | type = _get_type(type_num) | 119 | type = _get_type(type_num) |
120 | try: | 120 | try: |
121 | values = struct.unpack_from(StHeader.format + | 121 | values = struct.unpack_from(StHeader.format + |
122 | type.format,data) | 122 | type.format,data) |
123 | record_dict = dict(zip(type.keys,values)) | 123 | record_dict = dict(zip(type.keys,values)) |
124 | except struct.error: | 124 | except struct.error: |
125 | f.close() | 125 | f.close() |
126 | print("Invalid record detected, stopping.") | 126 | print "Invalid record detected, stopping." |
127 | exit() | 127 | exit() |
128 | 128 | ||
129 | # Convert the record_dict into an object | 129 | # Convert the record_dict into an object |
130 | record = _dict2obj(record_dict) | 130 | record = _dict2obj(record_dict) |
131 | 131 | ||
132 | # Give it a type name (easier to work with than type number) | 132 | # Give it a type name (easier to work with than type number) |
133 | record.type_name = _get_type_name(type_num) | 133 | record.type_name = _get_type_name(type_num) |
134 | 134 | ||
135 | # All records should have a 'record type' field. | 135 | # All records should have a 'record type' field. |
136 | # e.g. these are 'event's as opposed to 'error's | 136 | # e.g. these are 'event's as opposed to 'error's |
137 | record.record_type = "event" | 137 | record.record_type = "event" |
138 | 138 | ||
139 | # If there is no timestamp, set the time to 0 | 139 | # If there is no timestamp, set the time to 0 |
140 | if 'when' not in record.__dict__.keys(): | 140 | if 'when' not in record.__dict__.keys(): |
141 | record.when = 0 | 141 | record.when = 0 |
142 | 142 | ||
143 | yield record | 143 | yield record |
144 | 144 | ||
145 | # Convert a dict into an object | 145 | # Convert a dict into an object |
146 | def _dict2obj(d): | 146 | def _dict2obj(d): |
147 | class Obj: pass | 147 | class Obj(object): pass |
148 | o = Obj() | 148 | o = Obj() |
149 | for key in d.keys(): | 149 | for key in d.keys(): |
150 | o.__dict__[key] = d[key] | 150 | o.__dict__[key] = d[key] |
@@ -206,7 +206,8 @@ class StSwitchAwayData: | |||
206 | message = 'A process was switched away on a given CPU.' | 206 | message = 'A process was switched away on a given CPU.' |
207 | 207 | ||
208 | class StCompletionData: | 208 | class StCompletionData: |
209 | format = 'Q3x?c' | 209 | #format = 'Q3x?c' |
210 | format = 'Q3xcc' | ||
210 | formatStr = struct.Struct(StHeader.format + format) | 211 | formatStr = struct.Struct(StHeader.format + format) |
211 | keys = StHeader.keys + ['when','forced?','flags'] | 212 | keys = StHeader.keys + ['when','forced?','flags'] |
212 | message = 'A job completed.' | 213 | message = 'A job completed.' |