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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7b60fae7d8331f5398f41d526da4b9fa66259afc4c6241565aac397b2bc55fc9
4
- data.tar.gz: 64b2eec9b3ab7ad3c34c139cab0b40160e893849e4d556b502a46eba3e958dff
3
+ metadata.gz: 87eabfa8d0c83dd5448c3418805623e0322eac365a31effc157e37d96dcdb205
4
+ data.tar.gz: 689724dac0774a37b97899f85fa2f7eedd5effe1d3e01cae5e4606047c38df89
5
5
  SHA512:
6
- metadata.gz: 21462917fdd94d4e3c155a6aa6f2ef209e1556a653aa245b8d721938b91c6de0adef11cd026ec83fda36934fe32e6cf7223a31c3b127c64e26042e0b8df3c71e
7
- data.tar.gz: 868292113b42ed941431a05977b6590c72a8a7415db1d56cdde5d5243212200a91c2a36ad788588fe8d48614107f66a47f28d803f044daed89e8306ef6530043
6
+ metadata.gz: 4a4a209a463bb2076a671c96db52c0e5e0a12ad12e3915508d496eb89a18f2be305d729883ee0e64aba9050ed8c7cb3a42531067915428163e325c2f1401759c
7
+ data.tar.gz: 884c8aab69d1e2a90ef6c1c8f98985d920adb89eb692ddb39363d0f7765e86073aa08d3382d0759a070bf6a20916164de6945f71421ce377533f963c1cf8fb5f
@@ -1,4 +1,7 @@
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
@@ -14,4 +17,4 @@ matrix:
14
17
  allow_failures:
15
18
  - rvm: ruby-head
16
19
  - rvm: rbx-19mode
17
- - rvm: jruby-19mode
20
+ - rvm: jruby-19mode
@@ -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
- - Silence BigDecimal deprecation warning under Ruby 2.5 (#26)
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
@@ -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"
@@ -3,14 +3,8 @@ require 'bigdecimal'
3
3
 
4
4
  # Cribbed from Unicorn Worker Killer, thanks!
5
5
  class GetProcessMem
6
- if Kernel.respond_to?(:BigDecimal)
7
- private_class_method def self.number_to_bigdecimal(value)
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
@@ -1,3 +1,3 @@
1
1
  class GetProcessMem
2
- VERSION = "0.2.3"
2
+ VERSION = "0.2.4"
3
3
  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.new("2122240.0"), bytes, delta
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.new("0.0000001")
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.3
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: 2018-10-18 00:00:00.000000000 Z
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
- rubyforge_project:
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