Skip to content

Commit 2043e75

Browse files
committed
100% documentation coverage.
1 parent 2a9bd05 commit 2043e75

File tree

8 files changed

+68
-2
lines changed

8 files changed

+68
-2
lines changed

lib/process/metrics/command.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77

88
module Process
99
module Metrics
10+
# @namespace
1011
module Command
12+
# Call the default command (Top).
13+
# @parameter arguments [Array] Command-line arguments to pass through.
1114
def self.call(*arguments)
1215
Top.call(*arguments)
1316
end

lib/process/metrics/command/summary.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
module Process
1313
module Metrics
1414
module Command
15+
# Helper module for rendering horizontal progress bars using Unicode block characters.
1516
module Bar
1617
BLOCK = [
1718
" ",
@@ -25,6 +26,10 @@ module Bar
2526
"█",
2627
]
2728

29+
# Format a fractional value as a horizontal bar.
30+
# @parameter value [Float] A value between 0.0 and 1.0 representing the fill level.
31+
# @parameter width [Integer] The width of the bar in characters.
32+
# @returns [String] A string of Unicode block characters representing the filled bar.
2833
def self.format(value, width)
2934
blocks = width * value
3035
full_blocks = blocks.floor
@@ -38,6 +43,7 @@ def self.format(value, width)
3843
end
3944
end
4045

46+
# Command that displays a formatted summary of memory usage statistics for processes.
4147
class Summary < Samovar::Command
4248
self.description = "Display a summary of memory usage statistics."
4349

@@ -48,6 +54,8 @@ class Summary < Samovar::Command
4854
option "--total-memory <integer>", "Set the total memory relative to the usage (MiB).", type: Integer
4955
end
5056

57+
# Get the configured terminal for styled output.
58+
# @returns [Console::Terminal] A terminal object with color/style definitions.
5159
def terminal
5260
terminal = Console::Terminal.for($stdout)
5361

@@ -62,6 +70,9 @@ def terminal
6270
return terminal
6371
end
6472

73+
# Format a processor utilization percentage with color-coded bar.
74+
# @parameter value [Float] The CPU utilization percentage (0.0-100.0).
75+
# @parameter terminal [Console::Terminal] The terminal to output styled text.
6576
def format_processor_utilization(value, terminal)
6677
if value > 80.0
6778
intensity = :high
@@ -78,6 +89,10 @@ def format_processor_utilization(value, terminal)
7889

7990
UNITS = ["KiB", "MiB", "GiB"]
8091

92+
# Format a memory size value in human-readable units.
93+
# @parameter value [Numeric] The size value in kilobytes.
94+
# @parameter units [Array(String)] The unit labels to use for scaling.
95+
# @returns [String] A formatted string with value and unit (e.g., "512KiB", "1.5MiB").
8196
def format_size(value, units: UNITS)
8297
unit = 0
8398

@@ -89,6 +104,10 @@ def format_size(value, units: UNITS)
89104
return "#{value.round(unit)}#{units[unit]}"
90105
end
91106

107+
# Format a memory value with a horizontal bar showing utilization relative to total.
108+
# @parameter value [Numeric] The memory value in kilobytes.
109+
# @parameter total [Numeric] The total memory available in kilobytes.
110+
# @parameter terminal [Console::Terminal] The terminal to output styled text.
92111
def format_memory(value, total, terminal)
93112
if value > (total * 0.8)
94113
intensity = :high
@@ -103,6 +122,8 @@ def format_memory(value, total, terminal)
103122
terminal.print(formatted, intensity, "[", Bar.format(value / total.to_f, 60), "]", :reset)
104123
end
105124

125+
# Get the total memory to use for percentage calculations.
126+
# @returns [Integer] Total memory in kilobytes.
106127
def total_memory
107128
if total_memory = @options[:total_memory]
108129
return total_memory * 1024
@@ -111,6 +132,7 @@ def total_memory
111132
end
112133
end
113134

135+
# Execute the summary command, capturing and displaying process metrics.
114136
def call
115137
# Validate required arguments: at least one of --pid or --ppid must be provided:
116138
unless @options[:pid] || @options[:ppid]

lib/process/metrics/command/top.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
module Process
1212
module Metrics
1313
module Command
14+
# Top-level command entry point for the process-metrics CLI.
1415
class Top < Samovar::Command
1516
self.description = "Collect memory usage statistics."
1617

@@ -23,6 +24,7 @@ class Top < Samovar::Command
2324
"summary" => Summary,
2425
}, default: "summary"
2526

27+
# Execute the top command, dispatching to nested commands.
2628
def call
2729
if @options[:version]
2830
puts "#{self.name} v#{VERSION}"

lib/process/metrics/general.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,20 @@ def total_size
8484

8585
alias memory_usage total_size
8686

87+
# Recursively expand a set of child PIDs into a collection.
88+
# @parameter children [Array<Integer>] The list of child process IDs to expand.
89+
# @parameter hierarchy [Hash<Integer, Array<Integer>>] The parent-to-children process hierarchy.
90+
# @parameter pids [Set<Integer>] The set to populate with process IDs.
8791
def self.expand_children(children, hierarchy, pids)
8892
children.each do |pid|
8993
self.expand(pid, hierarchy, pids)
9094
end
9195
end
9296

97+
# Recursively expand a process and its descendants into a collection.
98+
# @parameter pid [Integer] The process ID to expand.
99+
# @parameter hierarchy [Hash<Integer, Array<Integer>>] The parent-to-children process hierarchy.
100+
# @parameter pids [Set<Integer>] The set to populate with process IDs.
93101
def self.expand(pid, hierarchy, pids)
94102
unless pids.include?(pid)
95103
pids << pid
@@ -100,6 +108,9 @@ def self.expand(pid, hierarchy, pids)
100108
end
101109
end
102110

111+
# Build a parent-to-children process hierarchy from a set of processes.
112+
# @parameter processes [Hash<Integer, General>] A hash mapping PIDs to General instances.
113+
# @returns [Hash<Integer, Array<Integer>>] A hash mapping each parent PID to an array of child PIDs.
103114
def self.build_tree(processes)
104115
hierarchy = Hash.new{|h,k| h[k] = []}
105116

@@ -112,6 +123,8 @@ def self.build_tree(processes)
112123
return hierarchy
113124
end
114125

126+
# Capture detailed memory metrics for each process in the given collection.
127+
# @parameter processes [Hash<Integer, General>] A hash mapping PIDs to General instances.
115128
def self.capture_memory(processes)
116129
count = processes.size
117130

lib/process/metrics/memory.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ def unique_size
2727
self.private_clean_size + self.private_dirty_size
2828
end
2929

30+
# Create a zero-initialized Memory instance.
31+
# @returns [Memory] A new Memory object with all fields set to zero.
3032
def self.zero
3133
self.new(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
3234
end

lib/process/metrics/memory/darwin.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
module Process
77
module Metrics
8+
# Darwin (macOS) implementation of memory metrics using vmmap.
89
class Memory::Darwin
910
VMMAP = "/usr/bin/vmmap"
1011

@@ -25,7 +26,9 @@ def self.total_size
2526
end
2627
end
2728

28-
# Parse a size string into kilobytes.
29+
# Parse a size string from vmmap output into kilobytes.
30+
# @parameter string [String | Nil] The size string (e.g., "4K", "1.5M", "2G").
31+
# @returns [Integer] The size in kilobytes.
2932
def self.parse_size(string)
3033
return 0 unless string
3134

@@ -93,14 +96,22 @@ def self.capture(pid, count: 1, **options)
9396

9497
if Memory::Darwin.supported?
9598
class << Memory
99+
# Whether memory capture is supported on this platform.
100+
# @returns [Boolean] True if vmmap is available.
96101
def supported?
97102
return true
98103
end
99104

105+
# Get total system memory size.
106+
# @returns [Integer] Total memory in kilobytes.
100107
def total_size
101108
return Memory::Darwin.total_size
102109
end
103110

111+
# Capture memory metrics for a process.
112+
# @parameter pid [Integer] The process ID.
113+
# @parameter options [Hash] Additional options (e.g., count for proportional estimates).
114+
# @returns [Memory] A Memory instance with captured metrics.
104115
def capture(...)
105116
return Memory::Darwin.capture(...)
106117
end

lib/process/metrics/memory/linux.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55

66
module Process
77
module Metrics
8+
# Linux implementation of memory metrics using `/proc/[pid]/smaps` and `/proc/[pid]/stat`.
89
class Memory::Linux
9-
# Extract minor/major page fault counters from /proc/[pid]/stat and assign to usage.
10+
# Extract minor/major page fault counters from `/proc/[pid]/stat` and assign to usage.
11+
# @parameter pid [Integer] The process ID.
12+
# @parameter usage [Memory] The Memory instance to populate with fault counters.
1013
def self.capture_faults(pid, usage)
1114
begin
1215
stat = File.read("/proc/#{pid}/stat")
@@ -116,14 +119,22 @@ def self.supported?
116119

117120
if Memory::Linux.supported?
118121
class << Memory
122+
# Whether memory capture is supported on this platform.
123+
# @returns [Boolean] True if /proc/[pid]/smaps or smaps_rollup is readable.
119124
def supported?
120125
return true
121126
end
122127

128+
# Get total system memory size.
129+
# @returns [Integer] Total memory in kilobytes.
123130
def total_size
124131
return Memory::Linux.total_size
125132
end
126133

134+
# Capture memory metrics for a process.
135+
# @parameter pid [Integer] The process ID.
136+
# @parameter options [Hash] Additional options.
137+
# @returns [Memory] A Memory instance with captured metrics.
127138
def capture(...)
128139
return Memory::Linux.capture(...)
129140
end

lib/process/metrics/version.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
# Released under the MIT License.
44
# Copyright, 2019-2025, by Samuel Williams.
55

6+
# @namespace
67
module Process
8+
# @namespace
79
module Metrics
810
VERSION = "0.5.2"
911
end

0 commit comments

Comments
 (0)