import operator
from .command import Command
from ..core import cputop
-from ..ascii_graph import Pyasciigraph
from . import mi
+from . import termgraph
class Cputop(Command):
return result_table
def _print_per_tid_usage(self, result_table):
- graph = Pyasciigraph()
- values = []
-
- for row in result_table.rows:
- process_do = row.process
- migration_count = row.migrations.value
- if row.priority.value is not None:
- prio_str = 'prio: %d' % row.priority.value
- else:
- prio_str = 'prio: ?'
- output_str = '%s (%d) (%s)' % (process_do.name, process_do.tid,
- prio_str)
-
- if migration_count > 0:
- output_str += ', %d migrations' % (migration_count)
+ row_format = '{:<25} {:>10} {}'
+ label_header = row_format.format('Process', 'Migrations', 'Priorities')
+
+ def format_label(row):
+ return row_format.format(
+ '%s (%d)' % (row.process.name, row.process.tid),
+ row.migrations.value,
+ row.prio_list.value,
+ )
- values.append((output_str, row.usage.to_percentage()))
+ graph = termgraph.BarGraph(
+ title='Per-TID Usage',
+ unit='%',
+ get_value=lambda row: row.usage.to_percentage(),
+ get_label=format_label,
+ label_header=label_header,
+ data=result_table.rows
+ )
- for line in graph.graph('Per-TID CPU Usage', values, unit=' %'):
- print(line)
+ graph.print_graph()
def _print_per_cpu_usage(self, result_table):
- graph = Pyasciigraph()
- values = []
-
- for row in result_table.rows:
- cpu = row.cpu
- values.append(('CPU %d' % cpu.id, row.usage.to_percentage()))
+ graph = termgraph.BarGraph(
+ title='Per-CPU Usage',
+ unit='%',
+ get_value=lambda row: row.usage.to_percentage(),
+ get_label=lambda row: 'CPU %d' % row.cpu.id,
+ data=result_table.rows
+ )
- for line in graph.graph('Per-CPU Usage', values, unit=' %'):
- print(line)
+ graph.print_graph()
def _print_total_cpu_usage(self, result_table):
usage_percent = result_table.rows[0].usage.to_percentage()
cputopcmd.run()
-# entry point (human)
def run():
_run(mi_mode=False)
-# entry point (MI)
def run_mi():
_run(mi_mode=True)