From f2f1f48d0654dde0fc7734e80857487e83181f61 Mon Sep 17 00:00:00 2001 From: Peter Portante Date: Tue, 3 May 2022 21:02:59 -0400 Subject: [PATCH] Emit multiple reports like `pidstat` does --- src/pcp/pidstat/pcp-pidstat.py | 120 +++++++++++++-------------------- 1 file changed, 47 insertions(+), 73 deletions(-) diff --git a/src/pcp/pidstat/pcp-pidstat.py b/src/pcp/pidstat/pcp-pidstat.py index e9e889d982..aae67ea6e7 100755 --- a/src/pcp/pidstat/pcp-pidstat.py +++ b/src/pcp/pidstat/pcp-pidstat.py @@ -795,27 +795,9 @@ class PidstatOptions(pmapi.pmOptions): timefmt = "%H:%M:%S" def checkOptions(self): - if self.show_process_priority and self.show_process_memory_util: - print("Error: -R is incompatible with -r") - return False - elif self.show_process_priority and self.show_process_stack_util: - print("Error: -R is incompatible with -k") - return False - elif self.show_process_priority and self.show_process_cpu_util: - print("Error: -R is incompatible with -u") - return False - elif self.show_process_memory_util and self.show_process_stack_util: - print("Error: -r is incompatible with -k") - return False - elif self.show_process_memory_util and self.show_process_cpu_util: - print("Error: -r is incompatible with -u") - return False - elif self.show_process_cpu_util and self.show_process_stack_util: - print("Error: -u is incompatible with -k") - return False - elif (self.show_process_memory_util or self.show_process_stack_util or - self.show_process_priority or self.show_process_cpu_util) and - self.show_process_state: + if ((self.show_process_memory_util or self.show_process_stack_util or + self.show_process_priority or self.show_process_cpu_util) and + self.show_process_state): print("Error: Incompatible flags provided") return False elif self.flag_error: @@ -824,12 +806,12 @@ def checkOptions(self): elif self.ps_args_flag: print("Error: Incorrect usage of the -l flag") return False - - if not self.show_process_state and not self.show_process_cpu_util: - # Determine if we are defaulting to -u. - if not any(self.show_process_memory_util, self.show_process_stack_util, - self.show_process_priority): - self.show_process_cpu_util = True + elif self.show_process_state or self.show_process_cpu_util: + return True + # Determine if we are defaulting to -u. + if not any(self.show_process_memory_util, self.show_process_stack_util, + self.show_process_priority): + self.show_process_cpu_util = True return True def extraOptions(self, opt, optarg, index): @@ -989,41 +971,7 @@ def report(self,manager): metric_repository = ReportingMetricRepository(group) - if PidstatOptions.show_process_stack_util: - process_stack_util = CpuProcessStackUtil(metric_repository) - process_filter = ProcessFilter(PidstatOptions) - stdout = StdoutPrinter() - printdecorator = NoneHandlingPrinterDecorator(stdout) - report = CpuProcessStackUtilReporter(process_stack_util, process_filter, - printdecorator.Print, PidstatOptions) - - report.print_report(timestamp, header_indentation, value_indentation) - - #=========================================================================================================== - elif PidstatOptions.show_process_memory_util: - process_memory_util = CpuProcessMemoryUtil(metric_repository) - process_filter = ProcessFilter(PidstatOptions) - stdout = StdoutPrinter() - printdecorator = NoneHandlingPrinterDecorator(stdout) - report = CpuProcessMemoryUtilReporter(process_memory_util, process_filter, - interval_in_seconds, - printdecorator.Print, PidstatOptions) - - report.print_report(timestamp, header_indentation, value_indentation) - - #=========================================================================================================== - elif PidstatOptions.show_process_priority: - process_priority = CpuProcessPriorities(metric_repository) - process_filter = ProcessFilter(PidstatOptions) - stdout = StdoutPrinter() - printdecorator = NoneHandlingPrinterDecorator(stdout) - report = CpuProcessPrioritiesReporter(process_priority, process_filter, - printdecorator.Print, PidstatOptions) - - report.print_report(timestamp, header_indentation, value_indentation) - - #=========================================================================================================== - elif PidstatOptions.show_process_state: + if PidstatOptions.show_process_state: process_state = CpuProcessState(metric_repository) process_filter = ProcessFilter(PidstatOptions) stdout = StdoutPrinter() @@ -1031,18 +979,44 @@ def report(self,manager): report = CpuProcessStateReporter(process_state, process_filter, interval_in_seconds, printdecorator.Print, PidstatOptions) - report.print_report(timestamp, header_indentation, value_indentation) - - #=========================================================================================================== - elif PidstatOptions.show_process_cpu_util: - cpu_usage = CpuUsage(metric_repository) - process_filter = ProcessFilter(PidstatOptions) - stdout = StdoutPrinter() - printdecorator = NoneHandlingPrinterDecorator(stdout) - report = CpuUsageReporter(cpu_usage, process_filter, interval_in_seconds, - printdecorator.Print, PidstatOptions) - report.print_report(timestamp, ncpu, header_indentation, value_indentation) + else: + if PidstatOptions.show_process_stack_util: + process_stack_util = CpuProcessStackUtil(metric_repository) + process_filter = ProcessFilter(PidstatOptions) + stdout = StdoutPrinter() + printdecorator = NoneHandlingPrinterDecorator(stdout) + report = CpuProcessStackUtilReporter(process_stack_util, process_filter, + printdecorator.Print, PidstatOptions) + report.print_report(timestamp, header_indentation, value_indentation) + + if PidstatOptions.show_process_memory_util: + process_memory_util = CpuProcessMemoryUtil(metric_repository) + process_filter = ProcessFilter(PidstatOptions) + stdout = StdoutPrinter() + printdecorator = NoneHandlingPrinterDecorator(stdout) + report = CpuProcessMemoryUtilReporter(process_memory_util, process_filter, + interval_in_seconds, + printdecorator.Print, PidstatOptions) + report.print_report(timestamp, header_indentation, value_indentation) + + if PidstatOptions.show_process_priority: + process_priority = CpuProcessPriorities(metric_repository) + process_filter = ProcessFilter(PidstatOptions) + stdout = StdoutPrinter() + printdecorator = NoneHandlingPrinterDecorator(stdout) + report = CpuProcessPrioritiesReporter(process_priority, process_filter, + printdecorator.Print, PidstatOptions) + report.print_report(timestamp, header_indentation, value_indentation) + + if PidstatOptions.show_process_cpu_util: + cpu_usage = CpuUsage(metric_repository) + process_filter = ProcessFilter(PidstatOptions) + stdout = StdoutPrinter() + printdecorator = NoneHandlingPrinterDecorator(stdout) + report = CpuUsageReporter(cpu_usage, process_filter, interval_in_seconds, + printdecorator.Print, PidstatOptions) + report.print_report(timestamp, ncpu, header_indentation, value_indentation) if __name__ == "__main__":