get_process_mem 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -1
- data/CHANGELOG.md +6 -2
- data/get_process_mem.gemspec +2 -0
- data/lib/get_process_mem.rb +21 -8
- 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
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
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
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,10 @@
|
|
1
|
+
## 0.2.4
|
2
|
+
|
3
|
+
- Use FFI to speed up memory lookup on Mac (darwin) by roughly 12x (#32)
|
4
|
+
|
1
5
|
## 0.2.3
|
2
6
|
|
3
|
-
-
|
7
|
+
- Silence BigDecimal deprecation warning under Ruby 2.5 (#26)
|
4
8
|
|
5
9
|
## 0.2.2
|
6
10
|
|
@@ -25,4 +29,4 @@
|
|
25
29
|
|
26
30
|
## 0.0.1
|
27
31
|
|
28
|
-
- Initial
|
32
|
+
- Initial
|
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,14 +3,8 @@ require 'bigdecimal'
|
|
3
3
|
|
4
4
|
# Cribbed from Unicorn Worker Killer, thanks!
|
5
5
|
class GetProcessMem
|
6
|
-
|
7
|
-
|
8
|
-
BigDecimal(value)
|
9
|
-
end
|
10
|
-
else
|
11
|
-
private_class_method def self.number_to_bigdecimal(value)
|
12
|
-
BigDecimal.new(value)
|
13
|
-
end
|
6
|
+
private_class_method def self.number_to_bigdecimal(value)
|
7
|
+
BigDecimal(value)
|
14
8
|
end
|
15
9
|
|
16
10
|
private def number_to_bigdecimal(value)
|
@@ -37,6 +31,20 @@ class GetProcessMem
|
|
37
31
|
include Sys
|
38
32
|
end
|
39
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
|
+
|
40
48
|
def initialize(pid = Process.pid)
|
41
49
|
@status_file = Pathname.new "/proc/#{pid}/status"
|
42
50
|
@process_file = Pathname.new "/proc/#{pid}/smaps"
|
@@ -50,6 +58,7 @@ class GetProcessMem
|
|
50
58
|
|
51
59
|
def bytes
|
52
60
|
memory = linux_status_memory if linux?
|
61
|
+
memory ||= darwin_memory if RUNS_ON_DARWIN
|
53
62
|
memory ||= ps_memory
|
54
63
|
end
|
55
64
|
|
@@ -108,4 +117,8 @@ class GetProcessMem
|
|
108
117
|
KB_TO_BYTE * number_to_bigdecimal(mem == "" ? 0 : mem)
|
109
118
|
end
|
110
119
|
end
|
120
|
+
|
121
|
+
def darwin_memory
|
122
|
+
Darwin.resident_size
|
123
|
+
end
|
111
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.7.6
|
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
|