Skip to content

Commit

Permalink
Add -U support to -R,-k and -r options
Browse files Browse the repository at this point in the history
  • Loading branch information
sitaramshelke committed Jul 2, 2016
1 parent 98db302 commit 7cebe46
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 15 deletions.
31 changes: 21 additions & 10 deletions pcp-pidstat.py
Expand Up @@ -347,23 +347,28 @@ def print_report(self, timestamp, ncpu):
self.printer("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s" % (timestamp,process.user_id(),process.pid(),user_percent,system_percent,guest_percent,total_percent,process.cpu_number(),process.process_name()))

class CpuProcessPrioritiesReporter:
def __init__(self, process_priority, process_filter, printer):
def __init__(self, process_priority, process_filter, printer, pidstat_options):
self.process_priority = process_priority
self.process_filter = process_filter
self.printer = printer
self.pidstat_options = pidstat_options

def print_report(self, timestamp):
self.printer ("Timestamp\tUID\tPID\tprio\tpolicy\tCommand")
processes = self.process_filter.filter_processes(self.process_priority.get_processes())
for process in processes:
self.printer("%s\t%s\t%s\t%s\t%s\t%s" % (timestamp,process.user_id(),process.pid(),process.priority(),process.policy(),process.process_name()))
if self.pidstat_options.show_process_user:
self.printer("%s\t%s\t%s\t%s\t%s\t%s" % (timestamp,process.user_name(),process.pid(),process.priority(),process.policy(),process.process_name()))
else:
self.printer("%s\t%s\t%s\t%s\t%s\t%s" % (timestamp,process.user_id(),process.pid(),process.priority(),process.policy(),process.process_name()))

class CpuProcessMemoryUtilReporter:
def __init__(self, process_memory_util, process_filter, delta_time, printer):
def __init__(self, process_memory_util, process_filter, delta_time, printer, pidstat_options):
self.process_memory_util = process_memory_util
self.process_filter = process_filter
self.printer = printer
self.delta_time = delta_time
self.pidstat_options = pidstat_options

def print_report(self, timestamp):
self.printer ("Timestamp\tUID\tPID\tMinFlt/s\tMajFlt/s\tVSize\tRSS\t%Mem\tCommand")
Expand All @@ -375,20 +380,26 @@ def print_report(self, timestamp):
maj_flt = "?"
if min_flt is None:
min_flt = "?"
self.printer("%s\t%s\t%s\t%s\t\t%s\t\t%s\t%s\t%s\t%s" % (timestamp,process.user_id(),process.pid(),min_flt,maj_flt,process.vsize(),process.rss(),process.mem(),process.process_name()))
if self.pidstat_options.show_process_user:
self.printer("%s\t%s\t%s\t%s\t\t%s\t\t%s\t%s\t%s\t%s" % (timestamp,process.user_name(),process.pid(),min_flt,maj_flt,process.vsize(),process.rss(),process.mem(),process.process_name()))
else:
self.printer("%s\t%s\t%s\t%s\t\t%s\t\t%s\t%s\t%s\t%s" % (timestamp,process.user_id(),process.pid(),min_flt,maj_flt,process.vsize(),process.rss(),process.mem(),process.process_name()))

class CpuProcessStackUtilReporter:
def __init__(self, process_stack_util, process_filter, printer):
def __init__(self, process_stack_util, process_filter, printer, pidstat_options):
self.process_stack_util = process_stack_util
self.process_filter = process_filter
self.printer = printer
self.pidstat_options = pidstat_options

def print_report(self, timestamp):
self.printer ("Timestamp\tUID\tPID\tStkSize\tCommand")
processes = self.process_filter.filter_processes(self.process_stack_util.get_processes())
for process in processes:
self.printer("%s\t%s\t%s\t%s\t%s" % (timestamp,process.user_id(),process.pid(),process.stack_size(),process.process_name()))

if self.pidstat_options.show_process_user:
self.printer("%s\t%s\t%s\t%s\t%s" % (timestamp,process.user_name(),process.pid(),process.stack_size(),process.process_name()))
else:
self.printer("%s\t%s\t%s\t%s\t%s" % (timestamp,process.user_id(),process.pid(),process.stack_size(),process.process_name()))

class PidstatOptions(pmapi.pmOptions):
process_name = None
Expand Down Expand Up @@ -493,21 +504,21 @@ def report(self,manager):
process_stack_util = CpuProcessStackUtil(metric_repository)
process_filter = ProcessFilter(PidstatOptions)
stdout = StdoutPrinter()
report = CpuProcessStackUtilReporter(process_stack_util, process_filter, stdout.Print)
report = CpuProcessStackUtilReporter(process_stack_util, process_filter, stdout.Print, PidstatOptions)

report.print_report(timestamp[3])
elif(PidstatOptions.show_process_memory_util):
process_memory_util = CpuProcessMemoryUtil(metric_repository)
process_filter = ProcessFilter(PidstatOptions)
stdout = StdoutPrinter()
report = CpuProcessMemoryUtilReporter(process_memory_util, process_filter, interval_in_seconds, stdout.Print)
report = CpuProcessMemoryUtilReporter(process_memory_util, process_filter, interval_in_seconds, stdout.Print, PidstatOptions)

report.print_report(timestamp[3])
elif(PidstatOptions.show_process_priority):
process_priority = CpuProcessPriorities(metric_repository)
process_filter = ProcessFilter(PidstatOptions)
stdout = StdoutPrinter()
report = CpuProcessPrioritiesReporter(process_priority, process_filter, stdout.Print)
report = CpuProcessPrioritiesReporter(process_priority, process_filter, stdout.Print, PidstatOptions)

report.print_report(timestamp[3])
else:
Expand Down
21 changes: 18 additions & 3 deletions test/process_memory_util_reporter_test.py
Expand Up @@ -4,6 +4,9 @@

class TestProcessMemoryUtilReporter(unittest.TestCase):
def setUp(self):
self.options = Mock(
show_process_user = None)

process_1 = Mock(pid = Mock(return_value = 1),
process_name = Mock(return_value = "process_1"),
user_name = Mock(return_value='pcp'),
Expand All @@ -21,7 +24,7 @@ def test_print_report_without_filtering(self):
process_filter = Mock()
printer = Mock()
process_filter.filter_processes = Mock(return_value=self.processes)
reporter = CpuProcessMemoryUtilReporter(process_memory_util, process_filter, 1, printer)
reporter = CpuProcessMemoryUtilReporter(process_memory_util, process_filter, 1, printer, self.options)

reporter.print_report(123)

Expand All @@ -33,7 +36,7 @@ def test_print_report_with_min_flt_None(self):
printer = Mock()
self.processes[0].minflt = Mock(return_value=None)
process_filter.filter_processes = Mock(return_value=self.processes)
reporter = CpuProcessMemoryUtilReporter(process_memory_util, process_filter, 1, printer)
reporter = CpuProcessMemoryUtilReporter(process_memory_util, process_filter, 1, printer, self.options)

reporter.print_report(123)

Expand All @@ -45,11 +48,23 @@ def test_print_report_with_maj_flt_None(self):
printer = Mock()
self.processes[0].majflt = Mock(return_value=None)
process_filter.filter_processes = Mock(return_value=self.processes)
reporter = CpuProcessMemoryUtilReporter(process_memory_util, process_filter, 1, printer)
reporter = CpuProcessMemoryUtilReporter(process_memory_util, process_filter, 1, printer, self.options)

reporter.print_report(123)

printer.assert_called_with("123\t1000\t1\t9.1\t\t?\t\t100\t200\t1.23\tprocess_1")

def test_print_report_with_user_name(self):
self.options.show_process_user = 'pcp'
process_memory_util = Mock()
process_filter = Mock()
printer = Mock()
process_filter.filter_processes = Mock(return_value=self.processes)
reporter = CpuProcessMemoryUtilReporter(process_memory_util, process_filter, 1, printer, self.options)

reporter.print_report(123)

printer.assert_called_with('123\tpcp\t1\t9.1\t\t5.34\t\t100\t200\t1.23\tprocess_1')

if __name__ == "__main__":
unittest.main()
17 changes: 16 additions & 1 deletion test/process_priority_reporter_test.py
Expand Up @@ -4,6 +4,9 @@

class TestProcessPriorityReporter(unittest.TestCase):
def setUp(self):
self.options = Mock(
show_process_user = None)

process_1 = Mock(pid = Mock(return_value = 1),
process_name = Mock(return_value = "process_1"),
user_name = Mock(return_value='pcp'),
Expand All @@ -18,11 +21,23 @@ def test_print_report_without_filtering(self):
process_filter = Mock()
printer = Mock()
process_filter.filter_processes = Mock(return_value=self.processes)
reporter = CpuProcessPrioritiesReporter(process_priority, process_filter, printer)
reporter = CpuProcessPrioritiesReporter(process_priority, process_filter, printer, self.options)

reporter.print_report(123)

printer.assert_called_with("123\t1000\t1\t99\tFIFO\tprocess_1")

def test_print_report_with_user_name(self):
self.options.show_process_user = 'pcp'
process_priority = Mock()
process_filter = Mock()
printer = Mock()
process_filter.filter_processes = Mock(return_value=self.processes)
reporter = CpuProcessPrioritiesReporter(process_priority, process_filter, printer, self.options)

reporter.print_report(123)

printer.assert_called_with("123\tpcp\t1\t99\tFIFO\tprocess_1")

if __name__ == "__main__":
unittest.main()
17 changes: 16 additions & 1 deletion test/process_stack_util_reporter_test.py
Expand Up @@ -4,6 +4,9 @@

class TestProcessStackUtilReporter(unittest.TestCase):
def setUp(self):
self.options = Mock(
show_process_user = None)

process_1 = Mock(pid = Mock(return_value = 1),
process_name = Mock(return_value = "process_1"),
user_name = Mock(return_value='pcp'),
Expand All @@ -17,11 +20,23 @@ def test_print_report_without_filtering(self):
process_filter = Mock()
printer = Mock()
process_filter.filter_processes = Mock(return_value=self.processes)
reporter = CpuProcessStackUtilReporter(process_stack_util, process_filter, printer)
reporter = CpuProcessStackUtilReporter(process_stack_util, process_filter, printer, self.options)

reporter.print_report(123)

printer.assert_called_with("123\t1000\t1\t136\tprocess_1")

def test_print_report_with_user_name(self):
self.options.show_process_user = 'pcp'
process_stack_util = Mock()
process_filter = Mock()
printer = Mock()
process_filter.filter_processes = Mock(return_value=self.processes)
reporter = CpuProcessStackUtilReporter(process_stack_util, process_filter, printer, self.options)

reporter.print_report(123)

printer.assert_called_with("123\tpcp\t1\t136\tprocess_1")

if __name__ == "__main__":
unittest.main()

0 comments on commit 7cebe46

Please sign in to comment.