summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Wu <peter@lekensteyn.nl>2014-06-18 12:06:18 +0200
committerPeter Wu <peter@lekensteyn.nl>2014-06-18 12:06:18 +0200
commit120f967196c4ce76fb9836162ac6e237a4327166 (patch)
treefb06188e01fb5a5915adbd09873c53ce0ffc949b
parent450bf70af0761e6d2c4b59ba413cc2043a936e79 (diff)
downloadscripts-120f967196c4ce76fb9836162ac6e237a4327166.tar.gz
interrupts-graph.py: prepare for writing / reading to file
-rwxr-xr-xrare/interrupts-graph.py48
1 files changed, 38 insertions, 10 deletions
diff --git a/rare/interrupts-graph.py b/rare/interrupts-graph.py
index e4286c3..eb4c5c4 100755
--- a/rare/interrupts-graph.py
+++ b/rare/interrupts-graph.py
@@ -38,6 +38,7 @@ ALT_COLORS = ['#023FA5', '#7D87B9', '#BEC1D4', '#D6BCC0', '#BB7784', '#FFFFFF',
'#8DD593', '#C6DEC7', '#EAD3C6', '#F0B98D', '#EF9708', '#0FCFC0', '#9CDED6',
'#D5EAE7', '#F3E1EB', '#F6C4E1', '#F79CD4']
+input_filename, output_filename = '', ''
def is_line_ok(name, yvalues):
"""Returns True if a line should be displayed for this name."""
@@ -65,18 +66,45 @@ def synchronized(lock):
return newFunction
return wrap
+if not input_filename:
+ input_filename = '/proc/interrupts'
+input_is_proc = input_filename == '/proc/interrupts'
+input_file = open(input_filename)
+output_file = open(output_filename, 'a') if output_filename else None
+output_writer = output_file.write if output_file else None
+
def get_numbers():
# TODO: may break the graph if a line disappears
- with open('/proc/interrupts') as pi:
- ncpus = len(pi.readline().split())
- for line in pi:
- name, values = line.split(':', 1)
- name = name.strip()
- values = values.strip().split(None, ncpus)
- if len(values) >= ncpus:
- # Name is ID + description for uniqueness
- name += ':' + values[-1];
- yield name, sum(int(values[i]) for i in range(0, ncpus))
+ if input_is_proc:
+ # Rewind stream
+ input_file.seek(0)
+ return parse_raw(input_file, output_writer)
+ else:
+ return parse_raw(input_file, output_writer)
+
+def parse_raw(pi, line_callback=None):
+ ncpus = None
+ for line in pi:
+ # Treat empty lines as boundary
+ if not line.strip():
+ break
+ if line_callback:
+ line_callback(line)
+
+ if ncpus is None:
+ ncpus = len(line.split())
+ continue
+ name, values = line.split(':', 1)
+ name = name.strip()
+ values = values.strip().split(None, ncpus)
+ if len(values) >= ncpus:
+ # Name is ID + description for uniqueness
+ name += ':' + values[-1];
+ yield name, sum(int(values[i]) for i in range(0, ncpus))
+
+ # Signal end of entry
+ if line_callback:
+ line_callback('\n')
prev = OrderedDict()
def get_diffs():