get_process_mem 0.2.1 → 0.2.4
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 +5 -5
- data/.travis.yml +6 -4
- data/CHANGELOG.md +14 -2
- data/README.md +1 -0
- data/get_process_mem.gemspec +2 -0
- data/lib/get_process_mem.rb +38 -10
- data/lib/get_process_mem/darwin.rb +53 -0
- data/lib/get_process_mem/version.rb +1 -1
- data/test/get_process_mem_test.rb +2 -2
- metadata +18 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 87eabfa8d0c83dd5448c3418805623e0322eac365a31effc157e37d96dcdb205
|
4
|
+
data.tar.gz: 689724dac0774a37b97899f85fa2f7eedd5effe1d3e01cae5e4606047c38df89
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4a4a209a463bb2076a671c96db52c0e5e0a12ad12e3915508d496eb89a18f2be305d729883ee0e64aba9050ed8c7cb3a42531067915428163e325c2f1401759c
|
7
|
+
data.tar.gz: 884c8aab69d1e2a90ef6c1c8f98985d920adb89eb692ddb39363d0f7765e86073aa08d3382d0759a070bf6a20916164de6945f71421ce377533f963c1cf8fb5f
|
data/.travis.yml
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
language: ruby
|
2
|
+
os:
|
3
|
+
- linux
|
4
|
+
- osx
|
2
5
|
|
3
6
|
before_install:
|
4
7
|
- gem install bundler -v 1.12.5
|
5
8
|
|
6
9
|
rvm:
|
7
|
-
-
|
8
|
-
- 2.
|
9
|
-
- 2.1.0
|
10
|
+
- 2.2.0
|
11
|
+
- 2.5.0
|
10
12
|
- ruby-head
|
11
13
|
- jruby-19mode
|
12
14
|
- rbx-19mode
|
@@ -15,4 +17,4 @@ matrix:
|
|
15
17
|
allow_failures:
|
16
18
|
- rvm: ruby-head
|
17
19
|
- rvm: rbx-19mode
|
18
|
-
- rvm: jruby-19mode
|
20
|
+
- rvm: jruby-19mode
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,18 @@
|
|
1
|
+
## 0.2.4
|
2
|
+
|
3
|
+
- Use FFI to speed up memory lookup on Mac (darwin) by roughly 12x (#32)
|
4
|
+
|
5
|
+
## 0.2.3
|
6
|
+
|
7
|
+
- Silence BigDecimal deprecation warning under Ruby 2.5 (#26)
|
8
|
+
|
9
|
+
## 0.2.2
|
10
|
+
|
11
|
+
- Fix support for Ruby 2.4 (#23)
|
12
|
+
|
1
13
|
## 0.2.1
|
2
14
|
|
3
|
-
-
|
15
|
+
- ???
|
4
16
|
|
5
17
|
## 0.2.0
|
6
18
|
|
@@ -17,4 +29,4 @@
|
|
17
29
|
|
18
30
|
## 0.0.1
|
19
31
|
|
20
|
-
- Initial
|
32
|
+
- Initial
|
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# Get Process Memory
|
2
2
|
|
3
3
|
[](https://travis-ci.org/schneems/get_process_mem)
|
4
|
+
[](https://www.codetriage.com/schneems/get_process_mem)
|
4
5
|
|
5
6
|
Do you need to get the memory usage of a process? Great because this library does that.
|
6
7
|
|
data/get_process_mem.gemspec
CHANGED
@@ -18,6 +18,8 @@ Gem::Specification.new do |gem|
|
|
18
18
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
19
|
gem.require_paths = ["lib"]
|
20
20
|
|
21
|
+
gem.add_dependency "ffi", "~> 1.0"
|
22
|
+
|
21
23
|
gem.add_development_dependency "sys-proctable", "~> 1.0"
|
22
24
|
gem.add_development_dependency "rake", "~> 10.1"
|
23
25
|
gem.add_development_dependency "test-unit", "~> 3.1.0"
|
data/lib/get_process_mem.rb
CHANGED
@@ -3,11 +3,19 @@ require 'bigdecimal'
|
|
3
3
|
|
4
4
|
# Cribbed from Unicorn Worker Killer, thanks!
|
5
5
|
class GetProcessMem
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
private_class_method def self.number_to_bigdecimal(value)
|
7
|
+
BigDecimal(value)
|
8
|
+
end
|
9
|
+
|
10
|
+
private def number_to_bigdecimal(value)
|
11
|
+
self.class.send(:number_to_bigdecimal, value)
|
12
|
+
end
|
13
|
+
|
14
|
+
KB_TO_BYTE = number_to_bigdecimal 1024 # 2**10 = 1024
|
15
|
+
MB_TO_BYTE = number_to_bigdecimal 1_048_576 # 1024**2 = 1_048_576
|
16
|
+
GB_TO_BYTE = number_to_bigdecimal 1_073_741_824 # 1024**3 = 1_073_741_824
|
9
17
|
CONVERSION = { "kb" => KB_TO_BYTE, "mb" => MB_TO_BYTE, "gb" => GB_TO_BYTE }
|
10
|
-
ROUND_UP =
|
18
|
+
ROUND_UP = number_to_bigdecimal "0.5"
|
11
19
|
attr_reader :pid
|
12
20
|
|
13
21
|
RUNS_ON_WINDOWS = Gem.win_platform?
|
@@ -23,6 +31,20 @@ class GetProcessMem
|
|
23
31
|
include Sys
|
24
32
|
end
|
25
33
|
|
34
|
+
RUNS_ON_DARWIN = Gem.platforms.detect do |p|
|
35
|
+
p.is_a?(Gem::Platform) && p.os == 'darwin'
|
36
|
+
end
|
37
|
+
|
38
|
+
if RUNS_ON_DARWIN
|
39
|
+
begin
|
40
|
+
require 'get_process_mem/darwin'
|
41
|
+
rescue LoadError => e
|
42
|
+
message = "Please add `ffi` to your Gemfile for darwin (macos) machines\n"
|
43
|
+
message << e.message
|
44
|
+
raise e, message
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
26
48
|
def initialize(pid = Process.pid)
|
27
49
|
@status_file = Pathname.new "/proc/#{pid}/status"
|
28
50
|
@process_file = Pathname.new "/proc/#{pid}/smaps"
|
@@ -36,19 +58,20 @@ class GetProcessMem
|
|
36
58
|
|
37
59
|
def bytes
|
38
60
|
memory = linux_status_memory if linux?
|
61
|
+
memory ||= darwin_memory if RUNS_ON_DARWIN
|
39
62
|
memory ||= ps_memory
|
40
63
|
end
|
41
64
|
|
42
65
|
def kb(b = bytes)
|
43
|
-
(b/
|
66
|
+
(b/KB_TO_BYTE).to_f
|
44
67
|
end
|
45
68
|
|
46
69
|
def mb(b = bytes)
|
47
|
-
(b/
|
70
|
+
(b/MB_TO_BYTE).to_f
|
48
71
|
end
|
49
72
|
|
50
73
|
def gb(b = bytes)
|
51
|
-
(b/
|
74
|
+
(b/GB_TO_BYTE).to_f
|
52
75
|
end
|
53
76
|
|
54
77
|
def inspect
|
@@ -73,7 +96,7 @@ class GetProcessMem
|
|
73
96
|
return if lines.empty?
|
74
97
|
lines.reduce(0) do |sum, line|
|
75
98
|
line.match(/(?<value>(\d*\.{0,1}\d+))\s+(?<unit>\w\w)/) do |m|
|
76
|
-
value =
|
99
|
+
value = number_to_bigdecimal(m[:value]) + ROUND_UP
|
77
100
|
unit = m[:unit].downcase
|
78
101
|
sum += CONVERSION[unit] * value
|
79
102
|
end
|
@@ -88,9 +111,14 @@ class GetProcessMem
|
|
88
111
|
def ps_memory
|
89
112
|
if RUNS_ON_WINDOWS
|
90
113
|
size = ProcTable.ps(pid).working_set_size
|
91
|
-
|
114
|
+
number_to_bigdecimal(size)
|
92
115
|
else
|
93
|
-
|
116
|
+
mem = `ps -o rss= -p #{pid}`
|
117
|
+
KB_TO_BYTE * number_to_bigdecimal(mem == "" ? 0 : mem)
|
94
118
|
end
|
95
119
|
end
|
120
|
+
|
121
|
+
def darwin_memory
|
122
|
+
Darwin.resident_size
|
123
|
+
end
|
96
124
|
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'ffi'
|
2
|
+
|
3
|
+
class GetProcessMem
|
4
|
+
class Darwin
|
5
|
+
extend FFI::Library
|
6
|
+
ffi_lib 'c'
|
7
|
+
attach_function :mach_task_self, [], :__darwin_mach_port_t
|
8
|
+
attach_function :task_info,
|
9
|
+
[
|
10
|
+
:__darwin_mach_port_t,
|
11
|
+
:int, # return selector
|
12
|
+
:pointer, #pointer to task info
|
13
|
+
:pointer, #pointer to int (size of structure / bytes filled out)
|
14
|
+
],
|
15
|
+
:int
|
16
|
+
|
17
|
+
class IntPtr < FFI::Struct
|
18
|
+
layout :value, :int
|
19
|
+
end
|
20
|
+
|
21
|
+
class TaskInfo < FFI::Struct
|
22
|
+
layout :suspend_count, :int32,
|
23
|
+
:virtual_size, :uint64,
|
24
|
+
:resident_size, :uint64,
|
25
|
+
:user_time, :uint64,
|
26
|
+
:system_time, :uint64,
|
27
|
+
:policy, :int32
|
28
|
+
end
|
29
|
+
|
30
|
+
MACH_TASK_BASIC_INFO = 20
|
31
|
+
MACH_TASK_BASIC_INFO_COUNT = TaskInfo.size / FFI.type_size(:uint)
|
32
|
+
|
33
|
+
class << self
|
34
|
+
def resident_size
|
35
|
+
mach_task_info[:resident_size]
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def mach_task_info
|
41
|
+
data = TaskInfo.new
|
42
|
+
out_count = IntPtr.new
|
43
|
+
out_count[:value] = MACH_TASK_BASIC_INFO_COUNT
|
44
|
+
result = task_info(mach_task_self, MACH_TASK_BASIC_INFO, data, out_count)
|
45
|
+
if result == 0
|
46
|
+
data
|
47
|
+
else
|
48
|
+
raise "task_info returned #{result}"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -16,7 +16,7 @@ class GetProcessMemTest < Test::Unit::TestCase
|
|
16
16
|
def test_linux_smap
|
17
17
|
delta = 1
|
18
18
|
bytes = @mem.linux_memory(fixture_path("heroku-bash-smap"))
|
19
|
-
assert_in_delta BigDecimal
|
19
|
+
assert_in_delta BigDecimal("2122240.0"), bytes, delta
|
20
20
|
end
|
21
21
|
|
22
22
|
def test_linux_status
|
@@ -26,7 +26,7 @@ class GetProcessMemTest < Test::Unit::TestCase
|
|
26
26
|
|
27
27
|
def test_conversions
|
28
28
|
bytes = 0
|
29
|
-
delta = BigDecimal
|
29
|
+
delta = BigDecimal("0.0000001")
|
30
30
|
assert_in_delta 0.0, @mem.kb(bytes), delta
|
31
31
|
assert_in_delta 0.0, @mem.mb(bytes), delta
|
32
32
|
assert_in_delta 0.0, @mem.gb(bytes), delta
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: get_process_mem
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Schneeman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-07-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: ffi
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: sys-proctable
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -68,6 +82,7 @@ files:
|
|
68
82
|
- appveyor.yml
|
69
83
|
- get_process_mem.gemspec
|
70
84
|
- lib/get_process_mem.rb
|
85
|
+
- lib/get_process_mem/darwin.rb
|
71
86
|
- lib/get_process_mem/version.rb
|
72
87
|
- test/fixtures/heroku-bash-smap
|
73
88
|
- test/fixtures/heroku-bash-status
|
@@ -92,8 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
107
|
- !ruby/object:Gem::Version
|
93
108
|
version: '0'
|
94
109
|
requirements: []
|
95
|
-
|
96
|
-
rubygems_version: 2.5.1
|
110
|
+
rubygems_version: 3.0.3
|
97
111
|
signing_key:
|
98
112
|
specification_version: 4
|
99
113
|
summary: Use GetProcessMem to find out the amount of RAM used by any process
|