Skip to content

Commit

Permalink
Print "?" for a calculation if a previous value for an instance is no…
Browse files Browse the repository at this point in the history
…t found. Fix related tests

As a convention in PCP values that are not found/valid should be printed as "?". This commit will make the calculation method to return a None value
if any of the required previous value is not found. If a none value is returned the Reporter class would print "?" at its place.
  • Loading branch information
sitaramshelke committed Jun 28, 2016
1 parent 7f85c02 commit 8cfa801
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 21 deletions.
69 changes: 52 additions & 17 deletions pcp-pidstat.py
Expand Up @@ -88,19 +88,31 @@ def __init__(self, instance, delta_time, metrics_repository):
self.__metric_repository = metrics_repository

def user_percent(self):
percent_of_time = 100 * float(self.__metric_repository.current_value('proc.psinfo.utime', self.instance) - self.__metric_repository.previous_value('proc.psinfo.utime', self.instance)) / float(1000 * self.__delta_time)
return float("%.2f"%percent_of_time)
if self.__metric_repository.previous_value('proc.psinfo.utime', self.instance) is not None:
percent_of_time = 100 * float(self.__metric_repository.current_value('proc.psinfo.utime', self.instance) - self.__metric_repository.previous_value('proc.psinfo.utime', self.instance)) / float(1000 * self.__delta_time)
return float("%.2f"%percent_of_time)
else:
return None

def guest_percent(self):
percent_of_time = 100 * float(self.__metric_repository.current_value('proc.psinfo.guest_time', self.instance) - self.__metric_repository.previous_value('proc.psinfo.guest_time', self.instance)) / float(1000 * self.__delta_time)
return float("%.2f"%percent_of_time)
if self.__metric_repository.previous_value('proc.psinfo.guest_time', self.instance) is not None:
percent_of_time = 100 * float(self.__metric_repository.current_value('proc.psinfo.guest_time', self.instance) - self.__metric_repository.previous_value('proc.psinfo.guest_time', self.instance)) / float(1000 * self.__delta_time)
return float("%.2f"%percent_of_time)
else:
return None

def system_percent(self):
percent_of_time = 100 * float(self.__metric_repository.current_value('proc.psinfo.stime', self.instance) - self.__metric_repository.previous_value('proc.psinfo.stime', self.instance)) / float(1000 * self.__delta_time)
return float("%.2f"%percent_of_time)
if self.__metric_repository.previous_value('proc.psinfo.stime', self.instance) is not None:
percent_of_time = 100 * float(self.__metric_repository.current_value('proc.psinfo.stime', self.instance) - self.__metric_repository.previous_value('proc.psinfo.stime', self.instance)) / float(1000 * self.__delta_time)
return float("%.2f"%percent_of_time)
else:
return None

def total_percent(self):
return self.user_percent()+self.guest_percent()+self.system_percent()
if self.user_percent() is not None and self.guest_percent() is not None and self.system_percent() is not None:
return self.user_percent()+self.guest_percent()+self.system_percent()
else:
return None

def pid(self):
return self.__metric_repository.current_value('proc.psinfo.pid', self.instance)
Expand Down Expand Up @@ -184,14 +196,18 @@ def process_name(self):
def minflt(self):
c_min_flt = self.__metric_repository.current_value('proc.psinfo.minflt', self.instance)
p_min_flt = self.__metric_repository.previous_value('proc.psinfo.minflt', self.instance)

return float("%.2f" % ((c_min_flt - p_min_flt)/self.delta_time))
if p_min_flt is not None:
return float("%.2f" % ((c_min_flt - p_min_flt)/self.delta_time))
else:
return None

def majflt(self):
c_maj_flt = self.__metric_repository.current_value('proc.psinfo.maj_flt', self.instance)
p_maj_flt = self.__metric_repository.previous_value('proc.psinfo.maj_flt', self.instance)

return float("%.2f" % ((c_maj_flt - p_maj_flt)/self.delta_time))
if p_maj_flt is not None:
return float("%.2f" % ((c_maj_flt - p_maj_flt)/self.delta_time))
else:
return None

def vsize(self):
return self.__metric_repository.current_value('proc.psinfo.vsize', self.instance)
Expand Down Expand Up @@ -309,13 +325,26 @@ def print_report(self, timestamp, ncpu):
self.printer ("Timestamp\tUID\tPID\tusr\tsystem\tguest\t%CPU\tCPU\tCommand")
processes = self.process_filter.filter_processes(self.cpu_usage.get_processes(self.delta_time))
for process in processes:
user_percent = process.user_percent()
guest_percent = process.guest_percent()
system_percent = process.system_percent()
total_percent = process.total_percent()
if user_percent is None:
user_percent = "?"
if guest_percent is None:
guest_percent = "?"
if system_percent is None:
system_percent = "?"
if total_percent is None:
total_percent = "?"

if self.pidstat_options.per_processor_usage:
total_percent /= ncpu
total_percent = float("%.2f"%(total_percent/ncpu))

if self.pidstat_options.filtered_process_user is not None:
self.printer("%s\t%s\t%d\t%.2f\t%.2f\t%.2f\t%.2f\t%d\t%s" % (timestamp,process.user_name(),process.pid(),process.user_percent(),process.system_percent(),process.guest_percent(),total_percent,process.cpu_number(),process.process_name()))
self.printer("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s" % (timestamp,process.user_name(),process.pid(),user_percent,process.system_percent(),process.guest_percent(),total_percent,process.cpu_number(),process.process_name()))
else:
self.printer("%s\t%d\t%d\t%.2f\t%.2f\t%.2f\t%.2f\t%d\t%s" % (timestamp,process.user_id(),process.pid(),process.user_percent(),process.system_percent(),process.guest_percent(),total_percent,process.cpu_number(),process.process_name()))
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,process.system_percent(),process.guest_percent(),total_percent,process.cpu_number(),process.process_name()))

class CpuProcessPrioritiesReporter:
def __init__(self, process_priority, process_filter, printer):
Expand All @@ -327,7 +356,7 @@ 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%d\t%d\t%d\t%s\t%s" % (timestamp,process.user_id(),process.pid(),process.priority(),process.policy(),process.process_name()))
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):
Expand All @@ -340,7 +369,13 @@ def print_report(self, timestamp):
self.printer ("Timestamp\tUID\tPID\tMinFlt/s\tMajFlt/s\tVSize\tRSS\t%Mem\tCommand")
processes = self.process_filter.filter_processes(self.process_memory_util.get_processes(self.delta_time))
for process in processes:
self.printer("%s\t%d\t%d\t%.2f\t\t%.2f\t\t%d\t%d\t%.2f\t%s" % (timestamp,process.user_id(),process.pid(),process.minflt(),process.majflt(),process.vsize(),process.rss(),process.mem(),process.process_name()))
maj_flt = process.majflt()
min_flt = process.minflt()
if maj_flt is None:
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()))

class CpuProcessStackUtilReporter:
def __init__(self, process_stack_util, process_filter, printer):
Expand All @@ -352,7 +387,7 @@ 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%d\t%d\t%d\t%s" % (timestamp,process.user_id(),process.pid(),process.stack_size(),process.process_name()))
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):
Expand Down
6 changes: 3 additions & 3 deletions test/cpu_usage_reporter_test.py
Expand Up @@ -29,7 +29,7 @@ def test_print_report_without_filtering(self):

reporter.print_report(123, 4)

printer.assert_called_with("123\t1000\t1\t2.43\t1.24\t0.00\t3.67\t1\tprocess_1")
printer.assert_called_with("123\t1000\t1\t2.43\t1.24\t0.0\t3.67\t1\tprocess_1")

def test_print_report_with_user_name(self):
self.options.filtered_process_user = 'pcp'
Expand All @@ -41,7 +41,7 @@ def test_print_report_with_user_name(self):

reporter.print_report(123, 4)

printer.assert_called_with("123\tpcp\t1\t2.43\t1.24\t0.00\t3.67\t1\tprocess_1")
printer.assert_called_with("123\tpcp\t1\t2.43\t1.24\t0.0\t3.67\t1\tprocess_1")

def test_print_report_with_per_processor_usage(self):
self.options.per_processor_usage = True
Expand All @@ -53,7 +53,7 @@ def test_print_report_with_per_processor_usage(self):

reporter.print_report(123, 4)

printer.assert_called_with("123\t1000\t1\t2.43\t1.24\t0.00\t0.92\t1\tprocess_1")
printer.assert_called_with("123\t1000\t1\t2.43\t1.24\t0.0\t0.92\t1\tprocess_1")

if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion test/process_memory_util_reporter_test.py
Expand Up @@ -25,7 +25,7 @@ def test_print_report_without_filtering(self):

reporter.print_report(123)

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

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

0 comments on commit 8cfa801

Please sign in to comment.