Commit 9cc5fbbb authored by Jeremy Cline's avatar Jeremy Cline Committed by Paolo Bonzini

tools/kvm_stat: Add Python 3 support to kvm_stat

Make kvm_stat support Python 3 by changing the use of "print" to a
function rather than a statement, switching from "iteritems" and
"iterkeys" (removed in Python 3) to "items" and "keys" respectively,
and decoding bytes to strings when dealing with text.

With this change, kvm_stat is usable with Python 2.6 and greater.
Signed-off-by: default avatarJeremy Cline <jeremy@jcline.org>
Signed-off-by: default avatarRadim Krčmář <rkrcmar@redhat.com>
parent 0f107682
...@@ -19,9 +19,11 @@ Three different ways of output formatting are available: ...@@ -19,9 +19,11 @@ Three different ways of output formatting are available:
The data is sampled from the KVM's debugfs entries and its perf events. The data is sampled from the KVM's debugfs entries and its perf events.
""" """
from __future__ import print_function
import curses import curses
import sys import sys
import locale
import os import os
import time import time
import optparse import optparse
...@@ -225,6 +227,8 @@ IOCTL_NUMBERS = { ...@@ -225,6 +227,8 @@ IOCTL_NUMBERS = {
'RESET': 0x00002403, 'RESET': 0x00002403,
} }
ENCODING = locale.getpreferredencoding(False)
class Arch(object): class Arch(object):
"""Encapsulates global architecture specific data. """Encapsulates global architecture specific data.
...@@ -666,7 +670,7 @@ class TracepointProvider(Provider): ...@@ -666,7 +670,7 @@ class TracepointProvider(Provider):
"""Returns 'event name: current value' for all enabled events.""" """Returns 'event name: current value' for all enabled events."""
ret = defaultdict(int) ret = defaultdict(int)
for group in self.group_leaders: for group in self.group_leaders:
for name, val in group.read().iteritems(): for name, val in group.read().items():
if name in self._fields: if name in self._fields:
ret[name] += val ret[name] += val
return ret return ret
...@@ -955,7 +959,7 @@ class Tui(object): ...@@ -955,7 +959,7 @@ class Tui(object):
except: except:
raise Exception raise Exception
for line in child.stdout: for line in child.stdout:
line = line.lstrip().split(' ', 1) line = line.decode(ENCODING).lstrip().split(' ', 1)
# perform a sanity check before calling the more expensive # perform a sanity check before calling the more expensive
# function to possibly extract the guest name # function to possibly extract the guest name
if ' -name ' in line[1]: if ' -name ' in line[1]:
...@@ -1005,7 +1009,7 @@ class Tui(object): ...@@ -1005,7 +1009,7 @@ class Tui(object):
name = '' name = ''
try: try:
line = open('/proc/{}/cmdline' line = open('/proc/{}/cmdline'
.format(pid), 'rb').read().split('\0') .format(pid), 'r').read().split('\0')
parms = line[line.index('-name') + 1].split(',') parms = line[line.index('-name') + 1].split(',')
while '' in parms: while '' in parms:
# commas are escaped (i.e. ',,'), hence e.g. 'foo,bar' results # commas are escaped (i.e. ',,'), hence e.g. 'foo,bar' results
...@@ -1170,7 +1174,7 @@ class Tui(object): ...@@ -1170,7 +1174,7 @@ class Tui(object):
.format(self.stats.fields_filter)) .format(self.stats.fields_filter))
self.screen.addstr(3, 0, "New regex: ") self.screen.addstr(3, 0, "New regex: ")
curses.echo() curses.echo()
regex = self.screen.getstr() regex = self.screen.getstr().decode(ENCODING)
curses.noecho() curses.noecho()
if len(regex) == 0: if len(regex) == 0:
self.stats.fields_filter = DEFAULT_REGEX self.stats.fields_filter = DEFAULT_REGEX
...@@ -1204,7 +1208,7 @@ class Tui(object): ...@@ -1204,7 +1208,7 @@ class Tui(object):
curses.echo() curses.echo()
self.screen.addstr(3, 0, "Pid [0 or pid]: ") self.screen.addstr(3, 0, "Pid [0 or pid]: ")
pid = self.screen.getstr() pid = self.screen.getstr().decode(ENCODING)
curses.noecho() curses.noecho()
try: try:
...@@ -1233,7 +1237,7 @@ class Tui(object): ...@@ -1233,7 +1237,7 @@ class Tui(object):
self.screen.addstr(2, 0, 'Change delay from %.1fs to ' % self.screen.addstr(2, 0, 'Change delay from %.1fs to ' %
self._delay_regular) self._delay_regular)
curses.echo() curses.echo()
val = self.screen.getstr() val = self.screen.getstr().decode(ENCODING)
curses.noecho() curses.noecho()
try: try:
...@@ -1273,7 +1277,7 @@ class Tui(object): ...@@ -1273,7 +1277,7 @@ class Tui(object):
self.print_all_gnames(7) self.print_all_gnames(7)
curses.echo() curses.echo()
self.screen.addstr(3, 0, "Guest [ENTER or guest]: ") self.screen.addstr(3, 0, "Guest [ENTER or guest]: ")
gname = self.screen.getstr() gname = self.screen.getstr().decode(ENCODING)
curses.noecho() curses.noecho()
if not gname: if not gname:
...@@ -1369,25 +1373,25 @@ def batch(stats): ...@@ -1369,25 +1373,25 @@ def batch(stats):
s = stats.get() s = stats.get()
for key in sorted(s.keys()): for key in sorted(s.keys()):
values = s[key] values = s[key]
print '%-42s%10d%10d' % (key, values[0], values[1]) print('%-42s%10d%10d' % (key, values[0], values[1]))
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
def log(stats): def log(stats):
"""Prints statistics as reiterating key block, multiple value blocks.""" """Prints statistics as reiterating key block, multiple value blocks."""
keys = sorted(stats.get().iterkeys()) keys = sorted(stats.get().keys())
def banner(): def banner():
for k in keys: for k in keys:
print '%s' % k, print(k, end=' ')
print print()
def statline(): def statline():
s = stats.get() s = stats.get()
for k in keys: for k in keys:
print ' %9d' % s[k][1], print(' %9d' % s[k][1], end=' ')
print print()
line = 0 line = 0
banner_repeat = 20 banner_repeat = 20
while True: while True:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment