From 2c81d80d872a9396153128dcd5c904cdf8f5d447 Mon Sep 17 00:00:00 2001 From: Johan Walles Date: Thu, 28 Mar 2024 06:49:09 +0100 Subject: [PATCH] The word is "aggregated" --- px/px_process.py | 17 +++++++---------- px/px_sort_order.py | 4 ++-- px/px_terminal.py | 24 ++++++++++++------------ px/px_top.py | 30 +++++++++++++++--------------- tests/px_top_test.py | 4 ++-- 5 files changed, 38 insertions(+), 41 deletions(-) diff --git a/px/px_process.py b/px/px_process.py index 9b16e47..fe65aba 100644 --- a/px/px_process.py +++ b/px/px_process.py @@ -91,7 +91,7 @@ def __init__( memory_percent: Optional[float] = None, cpu_percent: Optional[float] = None, cpu_time: Optional[float] = None, - cumulative_cpu_time: Optional[float] = None, + aggregated_cpu_time: Optional[float] = None, ) -> None: self.pid: int = pid self.ppid: Optional[int] = ppid @@ -142,7 +142,7 @@ def __init__( self.cpu_percent_s = f"{cpu_percent:.0f}%" self.set_cpu_time_seconds(cpu_time) - self.set_cumulative_cpu_time_seconds(cumulative_cpu_time) + self.set_aggregated_cpu_time_seconds(aggregated_cpu_time) self.children: List[PxProcess] = [] self.parent: Optional[PxProcess] = None @@ -178,12 +178,12 @@ def set_cpu_time_seconds(self, seconds: Optional[float]) -> None: self.cpu_time_s = seconds_to_str(seconds) self.cpu_time_seconds = seconds - def set_cumulative_cpu_time_seconds(self, seconds: Optional[float]) -> None: - self.cumulative_cpu_time_s: str = "--" - self.cumulative_cpu_time_seconds = None + def set_aggregated_cpu_time_seconds(self, seconds: Optional[float]) -> None: + self.aggregated_cpu_time_s: str = "--" + self.aggregated_cpu_time_seconds = None if seconds is not None: - self.cumulative_cpu_time_s = seconds_to_str(seconds) - self.cumulative_cpu_time_seconds = seconds + self.aggregated_cpu_time_s = seconds_to_str(seconds) + self.aggregated_cpu_time_seconds = seconds def match(self, string, require_exact_user=True): """ @@ -408,9 +408,6 @@ def resolve_links(processes: Dict[int, PxProcess], now: datetime.datetime) -> No process.parent.children.append(process) -# FIXME: Make the tree look like a tree in cumulative CPU time mode - - def remove_process_and_descendants(processes: Dict[int, PxProcess], pid: int) -> None: process = processes[pid] if process.parent is not None: diff --git a/px/px_sort_order.py b/px/px_sort_order.py index 583c28d..0c5c2df 100644 --- a/px/px_sort_order.py +++ b/px/px_sort_order.py @@ -4,11 +4,11 @@ class SortOrder(enum.Enum): CPU = 1 MEMORY = 2 - CUMULATIVE_CPU = 3 + AGGREGATED_CPU = 3 def next(self): if self == SortOrder.CPU: return SortOrder.MEMORY if self == SortOrder.MEMORY: - return SortOrder.CUMULATIVE_CPU + return SortOrder.AGGREGATED_CPU return SortOrder.CPU diff --git a/px/px_terminal.py b/px/px_terminal.py index 21e42b9..0cce65e 100644 --- a/px/px_terminal.py +++ b/px/px_terminal.py @@ -326,8 +326,8 @@ def to_screen_lines( """ cputime_name = "CPUTIME" - if sort_order == px_sort_order.SortOrder.CUMULATIVE_CPU: - cputime_name = "CUMLCPU" + if sort_order == px_sort_order.SortOrder.AGGREGATED_CPU: + cputime_name = "AGGRCPU" headings = [ "PID", "COMMAND", @@ -342,9 +342,9 @@ def to_screen_lines( highlight_column = 5 # "RAM" elif sort_order in [ px_sort_order.SortOrder.CPU, - px_sort_order.SortOrder.CUMULATIVE_CPU, + px_sort_order.SortOrder.AGGREGATED_CPU, ]: - highlight_column = 4 # "CPUTIME" or "CUMLCPU" + highlight_column = 4 # "CPUTIME" or "AGGRCPU" # Compute widest width for pid, command, user, cpu and memory usage columns pid_width = len(headings[0]) @@ -360,8 +360,8 @@ def to_screen_lines( cpu_width = max(cpu_width, len(proc.cpu_percent_s)) cputime_s = proc.cpu_time_s - if sort_order == px_sort_order.SortOrder.CUMULATIVE_CPU: - cputime_s = proc.cumulative_cpu_time_s + if sort_order == px_sort_order.SortOrder.AGGREGATED_CPU: + cputime_s = proc.aggregated_cpu_time_s cputime_width = max(cputime_width, len(cputime_s)) mem_width = max(mem_width, len(proc.memory_percent_s)) @@ -406,14 +406,14 @@ def to_screen_lines( max_memory_percent_s = proc.memory_percent_s cpu_time_seconds = proc.cpu_time_seconds - if sort_order == px_sort_order.SortOrder.CUMULATIVE_CPU: + if sort_order == px_sort_order.SortOrder.AGGREGATED_CPU: if proc.pid <= 1: # Both the kernel (PID 0) and the init process (PID 1) will just # have contain the total time of all other processes. Since we # only use this max value for highlighting (see below), if we # include these only they will be highlighted. So we skip them. continue - cpu_time_seconds = proc.cumulative_cpu_time_seconds + cpu_time_seconds = proc.aggregated_cpu_time_seconds if cpu_time_seconds is not None and cpu_time_seconds > max_cpu_time_seconds: max_cpu_time_seconds = cpu_time_seconds @@ -433,9 +433,9 @@ def to_screen_lines( cpu_time_s = proc.cpu_time_s cpu_time_seconds = proc.cpu_time_seconds - if sort_order == px_sort_order.SortOrder.CUMULATIVE_CPU: - cpu_time_s = proc.cumulative_cpu_time_s - cpu_time_seconds = proc.cumulative_cpu_time_seconds + if sort_order == px_sort_order.SortOrder.AGGREGATED_CPU: + cpu_time_s = proc.aggregated_cpu_time_s + cpu_time_seconds = proc.aggregated_cpu_time_seconds if not cpu_time_seconds: # Zero or undefined @@ -455,7 +455,7 @@ def to_screen_lines( owner = bold(owner) indent = "" - if sort_order == px_sort_order.SortOrder.CUMULATIVE_CPU: + if sort_order == px_sort_order.SortOrder.AGGREGATED_CPU: indent = " " * proc.level * 2 columns = [ diff --git a/px/px_top.py b/px/px_top.py index 5f67c30..835ace8 100644 --- a/px/px_top.py +++ b/px/px_top.py @@ -96,9 +96,9 @@ def adjust_cpu_times( return list(pid2proc.values()) -def compute_cumulative_cpu_times(toplist: List[px_process.PxProcess]) -> None: +def compute_aggregated_cpu_times(toplist: List[px_process.PxProcess]) -> None: """ - Compute cumulative CPU times for all processes in the toplist. + Compute aggregated CPU times for all processes in the toplist. This function modifies the toplist in place. """ @@ -108,12 +108,12 @@ def compute_cumulative_cpu_times(toplist: List[px_process.PxProcess]) -> None: while root_process.parent is not None: root_process = root_process.parent - # Now, walk the process tree and compute cumulative CPU times + # Now, walk the process tree and compute aggregated CPU times def walk_tree(proc: px_process.PxProcess) -> float: sum = proc.cpu_time_seconds or 0 for child in proc.children: sum += walk_tree(child) - proc.set_cumulative_cpu_time_seconds(sum) + proc.set_aggregated_cpu_time_seconds(sum) return sum walk_tree(root_process) @@ -126,8 +126,8 @@ def get_notnone_cpu_time_seconds(proc: px_process.PxProcess) -> float: return 0 -def get_notnone_cumulative_cpu_time_seconds(proc: px_process.PxProcess) -> float: - seconds = proc.cumulative_cpu_time_seconds +def get_notnone_aggregated_cpu_time_seconds(proc: px_process.PxProcess) -> float: + seconds = proc.aggregated_cpu_time_seconds if seconds is not None: return seconds return 0 @@ -166,7 +166,7 @@ def sort_by_cpu_usage_tree( toplist: List[px_process.PxProcess], ) -> List[px_process.PxProcess]: """ - Sort the process list by cumulative CPU time, but keep the tree structure. + Sort the process list by aggregated CPU time, but keep the tree structure. """ root_process = toplist[0] while root_process.parent is not None: @@ -174,7 +174,7 @@ def sort_by_cpu_usage_tree( def sort_children(proc: px_process.PxProcess) -> None: proc.children = sorted( - proc.children, key=get_notnone_cumulative_cpu_time_seconds, reverse=True + proc.children, key=get_notnone_aggregated_cpu_time_seconds, reverse=True ) for child in proc.children: sort_children(child) @@ -201,7 +201,7 @@ def get_toplist( sort_order=px_sort_order.SortOrder.CPU, ) -> List[px_process.PxProcess]: toplist = adjust_cpu_times(baseline, current) - compute_cumulative_cpu_times(toplist) + compute_aggregated_cpu_times(toplist) # Sort by interestingness last toplist = px_process.order_best_first(toplist) @@ -209,7 +209,7 @@ def get_toplist( toplist = sorted(toplist, key=get_notnone_memory_percent, reverse=True) elif sort_order == px_sort_order.SortOrder.CPU: toplist = sort_by_cpu_usage(toplist) - elif sort_order == px_sort_order.SortOrder.CUMULATIVE_CPU: + elif sort_order == px_sort_order.SortOrder.AGGREGATED_CPU: toplist = sort_by_cpu_usage_tree(toplist) return toplist @@ -425,12 +425,12 @@ def get_screen_lines( # number of processes toplist_table_lines += screen_rows * [""] - top_what = "CPU" + top_line = "Top processes by CPU usage" if sort_order == px_sort_order.SortOrder.MEMORY: - top_what = "memory" - elif sort_order == px_sort_order.SortOrder.CUMULATIVE_CPU: - top_what = "cumulative CPU" - lines += [px_terminal.bold("Top " + top_what + " using processes")] + top_line = "Top processes by memory usage" + elif sort_order == px_sort_order.SortOrder.AGGREGATED_CPU: + top_line = "Process tree ordered by aggregated CPU time" + lines += [px_terminal.bold(top_line)] if top_mode == MODE_SEARCH: lines += [SEARCH_PROMPT_ACTIVE + px_terminal.bold(search or "") + SEARCH_CURSOR] diff --git a/tests/px_top_test.py b/tests/px_top_test.py index e71e864..c894a9e 100644 --- a/tests/px_top_test.py +++ b/tests/px_top_test.py @@ -61,8 +61,8 @@ def test_get_toplist(): baseline = {p.pid: (p.start_time, p.cpu_time_seconds or 0.0) for p in current} toplist = px_top.get_toplist(baseline, px_process.get_all()) for process in toplist: - assert process.cumulative_cpu_time_seconds is not None - assert process.cumulative_cpu_time_s != "--" + assert process.aggregated_cpu_time_seconds is not None + assert process.aggregated_cpu_time_s != "--" def test_get_command():