process-metrics 0.12.0 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/context/getting-started.md +38 -1
- data/lib/process/metrics/general/linux.rb +6 -1
- data/lib/process/metrics/general/process_status.rb +1 -1
- data/lib/process/metrics/general.rb +1 -0
- data/lib/process/metrics/processor/linux.rb +89 -0
- data/lib/process/metrics/processor.rb +21 -1
- data/lib/process/metrics/version.rb +1 -1
- data/readme.md +8 -8
- data/releases.md +8 -0
- data.tar.gz.sig +0 -0
- metadata +2 -1
- metadata.gz.sig +2 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 86b26a801c95a764b0f98139e25ad1ac12b455b1b44b664722d1ff189bce8be4
|
|
4
|
+
data.tar.gz: 7591aff04fdbc4e9b6eabdd42ac8bcdf6ffbeea605f812b1d00255a57cba49c5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1a688f8a4db31df31adbbc5d5513716785ff61047f0b010aba506064d4c981dc5582fe99784eef0d244f877643034325d0996a323cc91e3ee1d6e6caa0e0f8e9
|
|
7
|
+
data.tar.gz: 72d8c7499461b35deafd4af20ba6c065de44f76d8868971e3ab70795febbb2863df98f9cb06c1e82e94fc1cdc7867275d58e1e03f9aa9ea890d50c5a37cf05bb
|
checksums.yaml.gz.sig
CHANGED
|
Binary file
|
data/context/getting-started.md
CHANGED
|
@@ -21,6 +21,7 @@ $ gem install process-metrics
|
|
|
21
21
|
The `process-metrics` gem provides a simple interface to collect and analyze process metrics.
|
|
22
22
|
|
|
23
23
|
- {ruby Process::Metrics::General} is the main entry point for process metrics. Use {ruby Process::Metrics::General.capture} to collect metrics for one or more processes.
|
|
24
|
+
- {ruby Process::Metrics::Processor} measures processor utilization over intervals between samples.
|
|
24
25
|
- {ruby Process::Metrics::Memory} provides additional methods for collecting memory metrics when the host operating system provides the necessary information.
|
|
25
26
|
|
|
26
27
|
## Usage
|
|
@@ -67,7 +68,7 @@ The {ruby Process::Metrics::General} struct contains the following fields:
|
|
|
67
68
|
- `process_id` - Process ID, a unique identifier for the process.
|
|
68
69
|
- `parent_process_id` - Parent Process ID, the process ID of the process that started this process.
|
|
69
70
|
- `process_group_id` - Process Group ID, the process group ID of the process, which can be shared by multiple processes.
|
|
70
|
-
- `processor_utilization` -
|
|
71
|
+
- `processor_utilization` - Average processor utilization in core units over the system's observation period. `1.0` represents one fully occupied CPU core, and multi-threaded processes can exceed `1.0`.
|
|
71
72
|
- `total_size` - Memory Size (bytes), the total size of the process's memory space (usually over-estimated as it doesn't take into account shared memory).
|
|
72
73
|
- `resident_size` - Resident (Set) Size (bytes), the amount of physical memory used by the process.
|
|
73
74
|
- `processor_time` - CPU Time (s), the amount of CPU time used by the process.
|
|
@@ -89,3 +90,39 @@ The {ruby Process::Metrics::Memory} struct contains the following fields:
|
|
|
89
90
|
- `proportional_swap_size` - Proportional Swap Memory Size (bytes), the amount of memory that has been swapped to disk, excluding shared memory.
|
|
90
91
|
|
|
91
92
|
In general, the interpretation of these fields is operating system specific. At best, they provide a rough estimate of the process's memory usage, but you should consult the documentation for your operating system for more details on exactly what each field represents.
|
|
93
|
+
|
|
94
|
+
## Interval Processor Utilization
|
|
95
|
+
|
|
96
|
+
The processor utilization reported by {ruby Process::Metrics::General} is a snapshot based on the operating system's observation period. Use {ruby Process::Metrics::Processor} when you need utilization over a specific sampling interval:
|
|
97
|
+
|
|
98
|
+
``` ruby
|
|
99
|
+
processor = Process::Metrics::Processor.new
|
|
100
|
+
|
|
101
|
+
# The first sample establishes a baseline:
|
|
102
|
+
processor.sample(Process.pid)
|
|
103
|
+
|
|
104
|
+
sleep(10)
|
|
105
|
+
sample = processor.sample(Process.pid).fetch(Process.pid)
|
|
106
|
+
|
|
107
|
+
sample.duration
|
|
108
|
+
# => approximately 10.0
|
|
109
|
+
|
|
110
|
+
sample.utilization
|
|
111
|
+
# => 1.0 means one fully occupied CPU core during the interval
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Both interfaces use the same core-unit scale. A utilization of `0.5` means half of one core on average, while `2.0` means two cores were fully occupied. Utilization is therefore not limited to the range `0.0..1.0`.
|
|
115
|
+
|
|
116
|
+
## Processor Capacity
|
|
117
|
+
|
|
118
|
+
Use {ruby Process::Metrics::Processor.count} to get the number of processors available to the current process. On Linux, this takes CPU affinity into account.
|
|
119
|
+
|
|
120
|
+
Use {ruby Process::Metrics::Processor.quota} to get the available processor capacity as a `Float`. On Linux, it also takes cgroup v2 CPU bandwidth limits into account. On other systems, when no finite quota is configured, or when the quota cannot be determined, it returns {ruby Process::Metrics::Processor.count} as a `Float`.
|
|
121
|
+
|
|
122
|
+
``` ruby
|
|
123
|
+
Process::Metrics::Processor.count
|
|
124
|
+
# => 8
|
|
125
|
+
|
|
126
|
+
Process::Metrics::Processor.quota
|
|
127
|
+
# => 1.5
|
|
128
|
+
```
|
|
@@ -73,6 +73,11 @@ module Process
|
|
|
73
73
|
end
|
|
74
74
|
processor_time = (utime + stime).to_f / CLK_TCK
|
|
75
75
|
elapsed_time = [(uptime_jiffies - start_time).to_f / CLK_TCK, 0.0].max
|
|
76
|
+
processor_utilization = if elapsed_time > 0.0
|
|
77
|
+
processor_time.fdiv(elapsed_time)
|
|
78
|
+
else
|
|
79
|
+
0.0
|
|
80
|
+
end
|
|
76
81
|
|
|
77
82
|
command = read_command(pid, executable_name)
|
|
78
83
|
|
|
@@ -80,7 +85,7 @@ module Process
|
|
|
80
85
|
pid,
|
|
81
86
|
parent_process_id,
|
|
82
87
|
process_group_id,
|
|
83
|
-
|
|
88
|
+
processor_utilization,
|
|
84
89
|
virtual_size,
|
|
85
90
|
resident_pages * PAGE_SIZE,
|
|
86
91
|
processor_time,
|
|
@@ -17,7 +17,7 @@ module Process
|
|
|
17
17
|
pid: ->(values){values.shift.to_i},
|
|
18
18
|
ppid: ->(values){values.shift.to_i},
|
|
19
19
|
pgid: ->(values){values.shift.to_i},
|
|
20
|
-
pcpu: ->(values){values.shift.to_f},
|
|
20
|
+
pcpu: ->(values){values.shift.to_f / 100.0},
|
|
21
21
|
vsz: ->(values){values.shift.to_i * 1024},
|
|
22
22
|
rss: ->(values){values.shift.to_i * 1024},
|
|
23
23
|
time: ->(values){Process::Metrics.duration(values.shift)},
|
|
@@ -35,6 +35,7 @@ module Process
|
|
|
35
35
|
end
|
|
36
36
|
|
|
37
37
|
# General process information.
|
|
38
|
+
# @attribute [Float] The processor utilization in core units, where `1.0` represents one fully occupied CPU core. Multi-threaded processes may report values greater than `1.0`.
|
|
38
39
|
class General < Struct.new(:process_id, :parent_process_id, :process_group_id, :processor_utilization, :virtual_size, :resident_size, :processor_time, :elapsed_time, :start_time, :command, :memory)
|
|
39
40
|
# Convert the object to a JSON serializable hash.
|
|
40
41
|
def as_json
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Released under the MIT License.
|
|
4
|
+
# Copyright, 2026, by Samuel Williams.
|
|
5
|
+
|
|
6
|
+
module Process
|
|
7
|
+
module Metrics
|
|
8
|
+
class Processor
|
|
9
|
+
# @private
|
|
10
|
+
module Linux
|
|
11
|
+
DEFAULT_CGROUP_ROOT = "/sys/fs/cgroup"
|
|
12
|
+
DEFAULT_CGROUP_PATH = "/proc/self/cgroup"
|
|
13
|
+
|
|
14
|
+
class << self
|
|
15
|
+
# Read the effective processor quota for the current cgroup.
|
|
16
|
+
# @parameter cgroup_root [String] The root of the cgroup v2 filesystem.
|
|
17
|
+
# @parameter cgroup_path [String] The process cgroup membership file.
|
|
18
|
+
# @returns [Float | Nil] The smallest finite quota in the cgroup hierarchy, if available.
|
|
19
|
+
def quota(cgroup_root: DEFAULT_CGROUP_ROOT, cgroup_path: DEFAULT_CGROUP_PATH)
|
|
20
|
+
unless relative_path = current_path(cgroup_path)
|
|
21
|
+
return nil
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
root = File.expand_path(cgroup_root)
|
|
25
|
+
directory = File.expand_path(relative_path.delete_prefix("/"), root)
|
|
26
|
+
return nil unless directory == root || directory.start_with?("#{root}/")
|
|
27
|
+
|
|
28
|
+
quota = nil
|
|
29
|
+
|
|
30
|
+
loop do
|
|
31
|
+
if current = read_quota(directory)
|
|
32
|
+
quota = [quota, current].compact.min
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
break if directory == root
|
|
36
|
+
directory = File.dirname(directory)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
return quota
|
|
40
|
+
rescue Errno::EACCES, Errno::EINVAL, Errno::ENOENT, Errno::ENOTDIR, ArgumentError
|
|
41
|
+
return nil
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
|
|
46
|
+
def current_path(path)
|
|
47
|
+
File.foreach(path) do |line|
|
|
48
|
+
hierarchy, controllers, relative_path = line.strip.split(":", 3)
|
|
49
|
+
|
|
50
|
+
if hierarchy == "0" && controllers == "" && relative_path
|
|
51
|
+
return relative_path
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
return nil
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def read_quota(directory)
|
|
59
|
+
maximum, period = File.read(File.join(directory, "cpu.max")).split
|
|
60
|
+
return nil if maximum == "max" || !maximum || !period
|
|
61
|
+
|
|
62
|
+
maximum = Integer(maximum)
|
|
63
|
+
period = Integer(period)
|
|
64
|
+
return nil unless maximum.positive? && period.positive?
|
|
65
|
+
|
|
66
|
+
return maximum.to_f / period
|
|
67
|
+
rescue Errno::EACCES, Errno::EINVAL, Errno::ENOENT, Errno::ENOTDIR, ArgumentError
|
|
68
|
+
return nil
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# The processor capacity available to the current process.
|
|
74
|
+
# This takes cgroup v2 CPU bandwidth limits into account, and otherwise returns {count} as a `Float`.
|
|
75
|
+
# @returns [Float] The available processor capacity in core units.
|
|
76
|
+
def self.quota
|
|
77
|
+
count = self.count.to_f
|
|
78
|
+
|
|
79
|
+
if quota = Linux.quota
|
|
80
|
+
if quota < count
|
|
81
|
+
return quota
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
return count
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
@@ -3,15 +3,31 @@
|
|
|
3
3
|
# Released under the MIT License.
|
|
4
4
|
# Copyright, 2026, by Samuel Williams.
|
|
5
5
|
|
|
6
|
+
require "etc"
|
|
7
|
+
|
|
6
8
|
module Process
|
|
7
9
|
module Metrics
|
|
8
10
|
# Computes interval CPU utilization from cumulative process metrics.
|
|
9
11
|
class Processor
|
|
12
|
+
# The number of processors available to the current process.
|
|
13
|
+
# On Linux, this takes the process CPU affinity into account.
|
|
14
|
+
# @returns [Integer] The number of available processors.
|
|
15
|
+
def self.count
|
|
16
|
+
Etc.nprocessors
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# The processor capacity available to the current process.
|
|
20
|
+
# On Linux, this takes cgroup v2 CPU bandwidth limits into account. Otherwise, it returns {count} as a `Float`.
|
|
21
|
+
# @returns [Float] The available processor capacity in core units.
|
|
22
|
+
def self.quota
|
|
23
|
+
return self.count.to_f
|
|
24
|
+
end
|
|
25
|
+
|
|
10
26
|
# An immutable measurement of process CPU usage over an interval.
|
|
11
27
|
# @attribute [Integer] The process ID.
|
|
12
28
|
# @attribute [Float] The elapsed monotonic time in seconds.
|
|
13
29
|
# @attribute [Float] The CPU time consumed during the interval in seconds.
|
|
14
|
-
# @attribute [Float] The CPU utilization, where one fully occupied core
|
|
30
|
+
# @attribute [Float] The CPU utilization in core units, where `1.0` represents one fully occupied core. Multi-threaded processes may report values greater than `1.0`.
|
|
15
31
|
class Sample < Struct.new(:process_id, :duration, :processor_time, :utilization)
|
|
16
32
|
end
|
|
17
33
|
|
|
@@ -79,3 +95,7 @@ module Process
|
|
|
79
95
|
end
|
|
80
96
|
end
|
|
81
97
|
end
|
|
98
|
+
|
|
99
|
+
if RUBY_PLATFORM.include?("linux")
|
|
100
|
+
require_relative "processor/linux"
|
|
101
|
+
end
|
data/readme.md
CHANGED
|
@@ -16,6 +16,14 @@ Please see the [project documentation](https://socketry.github.io/process-metric
|
|
|
16
16
|
|
|
17
17
|
Please see the [project releases](https://socketry.github.io/process-metrics/releases/index) for all releases.
|
|
18
18
|
|
|
19
|
+
### v0.14.0
|
|
20
|
+
|
|
21
|
+
- Add `Process::Metrics::Processor.count` and `.quota` for affinity-aware processor counts and cgroup v2-aware processor capacity.
|
|
22
|
+
|
|
23
|
+
### v0.13.0
|
|
24
|
+
|
|
25
|
+
- Normalize processor utilization to core units, where `1.0` represents one fully occupied CPU core, and restore Linux reporting.
|
|
26
|
+
|
|
19
27
|
### v0.12.0
|
|
20
28
|
|
|
21
29
|
- Add `Process::Metrics::Processor` for measuring per-process CPU utilization over an interval.
|
|
@@ -52,14 +60,6 @@ Please see the [project releases](https://socketry.github.io/process-metrics/rel
|
|
|
52
60
|
|
|
53
61
|
- Be more proactive about returning nil if memory capture failed.
|
|
54
62
|
|
|
55
|
-
### v0.6.1
|
|
56
|
-
|
|
57
|
-
- Handle `Errno::ESRCH: No such process @ io_fillbuf - fd:xxx /proc/xxx/smaps_rollup` by ignoring it.
|
|
58
|
-
|
|
59
|
-
### v0.6.0
|
|
60
|
-
|
|
61
|
-
- Add support for major and minor page faults on Linux: `Process::Metrics::Memory#major_faults` and `#minor_faults`. Unfortunately these metrics are not available on Darwin (macOS).
|
|
62
|
-
|
|
63
63
|
## Contributing
|
|
64
64
|
|
|
65
65
|
We welcome contributions to this project.
|
data/releases.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# Releases
|
|
2
2
|
|
|
3
|
+
## v0.14.0
|
|
4
|
+
|
|
5
|
+
- Add `Process::Metrics::Processor.count` and `.quota` for affinity-aware processor counts and cgroup v2-aware processor capacity.
|
|
6
|
+
|
|
7
|
+
## v0.13.0
|
|
8
|
+
|
|
9
|
+
- Normalize processor utilization to core units, where `1.0` represents one fully occupied CPU core, and restore Linux reporting.
|
|
10
|
+
|
|
3
11
|
## v0.12.0
|
|
4
12
|
|
|
5
13
|
- Add `Process::Metrics::Processor` for measuring per-process CPU utilization over an interval.
|
data.tar.gz.sig
CHANGED
|
Binary file
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: process-metrics
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.14.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Samuel Williams
|
|
@@ -89,6 +89,7 @@ files:
|
|
|
89
89
|
- lib/process/metrics/memory/darwin.rb
|
|
90
90
|
- lib/process/metrics/memory/linux.rb
|
|
91
91
|
- lib/process/metrics/processor.rb
|
|
92
|
+
- lib/process/metrics/processor/linux.rb
|
|
92
93
|
- lib/process/metrics/version.rb
|
|
93
94
|
- license.md
|
|
94
95
|
- readme.md
|
metadata.gz.sig
CHANGED
|
@@ -1,5 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
���m��l�iqp��Vk���C��)]�j�����Y�I�q5��Sj>D�Z��y�ִ�8ʋqr��ٓ���~���bV2���'(ױn:���q��?��,lTM"��d?*��g;a��P�ꯖ �
|
|
4
|
-
*;āz@*�%��YNvV�`��$��e8n������~g��^V�9͊O(�@�g@>M���oh^ޅA��;JfS=
|
|
5
|
-
���`�9���6!���B��+~��:Rd��\���Xha��&l��X�̔2��+#M�Lf�
|
|
1
|
+
l��U�H�3�>:���H�F#����#Q�)4�q&x�3���'����$�A�=4ߵ�Lj����%����y��YH�����ŕ���x>'*vĿ���CV���3,�Y�
|
|
2
|
+
��(����d�JY�&�c�+�gϖ�J�ܳ�WA�g ��mS0r)��y~
|