get_process_mem 0.2.0 → 0.2.5

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
- SHA1:
3
- metadata.gz: d66e96644c3f8d171fd8533346150dc2a653badc
4
- data.tar.gz: faaa15b6c510b50ced777bc2163042ac2934b9a6
2
+ SHA256:
3
+ metadata.gz: 1d0fb74ff83b646fc0abdbbad624257304311fa98ea5204cce96d12957572432
4
+ data.tar.gz: 8a51db8a8638d7d160b6a34d924244d4a28056168f3aea4341abf9d6fe890576
5
5
  SHA512:
6
- metadata.gz: 03f334783b312689fa9f7adcb28cd692285a2ffe181ebd5753662cb6b8b326249a57391beeea26a0198eddc2c86d8692f20bd70a02d8ab67c1d45be8bd6fc78d
7
- data.tar.gz: 865fba383f18073a1bf9d7d9cfa543ad56e57544347aa3e8374899aea1f4144a8ddc7b66dc22f2762e94a55ce04970a5d9c865395577436058791bedaa87673a
6
+ metadata.gz: e3291519f57f2b44c5d8794098c5b87331bb49599f43ea1d06bc269560759e0e9ef0bb2318a4f1d261a4709634024cd00d0bb73bca559ff23440fb194cb25964
7
+ data.tar.gz: 4c16b205f3a20b0f1a7a2110bb3ee370918107f2a4e509a044f9ab0dc01cf8285a3042c5cc656c65a6bd50d6e5f5d68c5bd2e980915d42632bf0fbed3faccfa3
@@ -0,0 +1,12 @@
1
+ name: Check Changelog
2
+
3
+ on: [pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v1
10
+ - name: Check that CHANGELOG is touched
11
+ run: |
12
+ cat $GITHUB_EVENT_PATH | jq .pull_request.title | grep -i '\[\(\(changelog skip\)\|\(ci skip\)\)\]' || git diff remotes/origin/${{ github.base_ref }} --name-only | grep CHANGELOG.md
@@ -1,14 +1,20 @@
1
1
  language: ruby
2
+ os:
3
+ - linux
4
+ - osx
5
+
6
+ before_install:
7
+ - gem install bundler -v 1.17.3
8
+
2
9
  rvm:
3
- - 1.9.3
4
- - 2.0.0
5
- - 2.1.0
10
+ - 2.2
11
+ - 2.6
6
12
  - ruby-head
7
- - jruby-19mode
8
- - rbx-19mode
13
+ - jruby-head
14
+ - truffleruby
9
15
 
10
16
  matrix:
11
17
  allow_failures:
12
18
  - rvm: ruby-head
13
- - rvm: rbx-19mode
14
- - rvm: jruby-19mode
19
+ - rvm: jruby-head
20
+ - rvm: truffleruby
@@ -1,3 +1,25 @@
1
+ ## Master - unreleased
2
+
3
+ ## 0.2.5
4
+
5
+ - Use new sys-proctable interface (https://github.com/schneems/get_process_mem/pull/36)
6
+
7
+ ## 0.2.4
8
+
9
+ - Use FFI to speed up memory lookup on Mac (darwin) by roughly 12x (#32)
10
+
11
+ ## 0.2.3
12
+
13
+ - Silence BigDecimal deprecation warning under Ruby 2.5 (#26)
14
+
15
+ ## 0.2.2
16
+
17
+ - Fix support for Ruby 2.4 (#23)
18
+
19
+ ## 0.2.1
20
+
21
+ - ???
22
+
1
23
  ## 0.2.0
2
24
 
3
25
  - Reverting back to RSS for linux until (#7) is resolved
@@ -13,4 +35,4 @@
13
35
 
14
36
  ## 0.0.1
15
37
 
16
- - Initial
38
+ - Initial
data/Gemfile CHANGED
@@ -1,3 +1,4 @@
1
1
  source "https://rubygems.org"
2
+ gem 'sys-proctable'
2
3
 
3
4
  gemspec
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # Get Process Memory
2
2
 
3
- [![Build Status](https://travis-ci.org/schneems/get_process_mem.png?branch=master)](https://travis-ci.org/schneems/get_process_mem)
3
+ [![Build Status](https://travis-ci.org/schneems/get_process_mem.svg?branch=master)](https://travis-ci.org/schneems/get_process_mem)
4
+ [![Help Contribute to Open Source](https://www.codetriage.com/schneems/get_process_mem/badges/users.svg)](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
 
@@ -12,15 +13,17 @@ In your `Gemfile` add
12
13
  gem 'get_process_mem'
13
14
  ```
14
15
 
15
- then run `$ bundle install`
16
+ then run `$ bundle install`.
16
17
 
18
+ If you're using Windows you'll also need to have the `sys-proctable` gem.
17
19
 
18
20
  ## Use It
19
21
 
20
22
  Get the current process memory usage:
21
23
 
22
24
  ```ruby
23
- puts mem = GetProcessMem.new.inspect
25
+ mem = GetProcessMem.new
26
+ puts mem.inspect
24
27
  #<GetProcessMem @mb=24.28125 @gb=0.023712158203125 @kb=24864.0 @bytes=25460736 >
25
28
  mem.bytes # => 25460736
26
29
  mem.kb # => 24864.0
@@ -47,14 +50,7 @@ mem.inspect
47
50
  `rm tmplog`
48
51
  ```
49
52
 
50
- On Linux, which provides `/proc/<pid>/smaps`, the default memory type returned is PSS, or "proportional set size", where shared memory is divided by the number of processes sharing it. On other platforms, the size returned is the RSS or the [Resident Set Size](http://en.wikipedia.org/wiki/Resident_set_size), basically how much memory the program takes up in RAM at the time, including all the shared memory.
51
-
52
- The memory type can be specified by passing an options hash:
53
-
54
- ```ruby
55
- GetProcessMem.new(Process.pid, mem_type: 'rss')
56
- ```
57
-
53
+ On Linux, for memory size we return the RSS or the [Resident Set Size](http://en.wikipedia.org/wiki/Resident_set_size), basically how much memory the program takes up in RAM at the time, including all the shared memory.
58
54
 
59
55
 
60
56
  ## License
@@ -0,0 +1,20 @@
1
+ install:
2
+ - set PATH=C:\Ruby22\bin;%PATH%
3
+ - bundle install
4
+
5
+ build: off
6
+
7
+ before_test:
8
+ - ruby -v
9
+ - gem -v
10
+ - bundle -v
11
+
12
+ test_script:
13
+ - bundle exec rake
14
+
15
+ environment:
16
+ matrix:
17
+ - RUBY_VERSION: 23
18
+ - RUBY_VERSION: 22
19
+ - RUBY_VERSION: 21
20
+ - RUBY_VERSION: 200
@@ -18,5 +18,9 @@ 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_development_dependency "rake", "~> 10.1"
21
+ gem.add_dependency "ffi", "~> 1.0"
22
+
23
+ gem.add_development_dependency "sys-proctable", "~> 1.2"
24
+ gem.add_development_dependency "rake", "~> 12"
25
+ gem.add_development_dependency "test-unit", "~> 3"
22
26
  end
@@ -1,20 +1,55 @@
1
1
  require 'pathname'
2
2
  require 'bigdecimal'
3
3
 
4
-
5
4
  # Cribbed from Unicorn Worker Killer, thanks!
6
5
  class GetProcessMem
7
- KB_TO_BYTE = 1024 # 2**10 = 1024
8
- MB_TO_BYTE = 1_048_576 # 1024**2 = 1_048_576
9
- GB_TO_BYTE = 1_073_741_824 # 1024**3 = 1_073_741_824
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
10
17
  CONVERSION = { "kb" => KB_TO_BYTE, "mb" => MB_TO_BYTE, "gb" => GB_TO_BYTE }
11
- ROUND_UP = BigDecimal.new("0.5")
18
+ ROUND_UP = number_to_bigdecimal "0.5"
12
19
  attr_reader :pid
13
20
 
21
+ RUNS_ON_WINDOWS = Gem.win_platform?
22
+
23
+ if RUNS_ON_WINDOWS
24
+ begin
25
+ require 'sys/proctable'
26
+ rescue LoadError => e
27
+ message = "Please add `sys-proctable` to your Gemfile for windows machines\n"
28
+ message << e.message
29
+ raise e, message
30
+ end
31
+ include Sys
32
+ end
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
+
14
48
  def initialize(pid = Process.pid)
49
+ @status_file = Pathname.new "/proc/#{pid}/status"
15
50
  @process_file = Pathname.new "/proc/#{pid}/smaps"
16
51
  @pid = pid
17
- @linux = @process_file.exist?
52
+ @linux = @status_file.exist?
18
53
  end
19
54
 
20
55
  def linux?
@@ -22,20 +57,21 @@ class GetProcessMem
22
57
  end
23
58
 
24
59
  def bytes
25
- memory = linux_memory if linux?
60
+ memory = linux_status_memory if linux?
61
+ memory ||= darwin_memory if RUNS_ON_DARWIN
26
62
  memory ||= ps_memory
27
63
  end
28
64
 
29
65
  def kb(b = bytes)
30
- (b/BigDecimal.new(KB_TO_BYTE)).to_f
66
+ (b/KB_TO_BYTE).to_f
31
67
  end
32
68
 
33
69
  def mb(b = bytes)
34
- (b/BigDecimal.new(MB_TO_BYTE)).to_f
70
+ (b/MB_TO_BYTE).to_f
35
71
  end
36
72
 
37
73
  def gb(b = bytes)
38
- (b/BigDecimal.new(GB_TO_BYTE)).to_f
74
+ (b/GB_TO_BYTE).to_f
39
75
  end
40
76
 
41
77
  def inspect
@@ -43,22 +79,24 @@ class GetProcessMem
43
79
  "#<#{self.class}:0x%08x @mb=#{ mb b } @gb=#{ gb b } @kb=#{ kb b } @bytes=#{b}>" % (object_id * 2)
44
80
  end
45
81
 
46
- def mem_type
47
- @mem_type
48
- end
49
-
50
- def mem_type=(mem_type)
51
- @mem_type = mem_type.downcase
82
+ # linux stores memory info in a file "/proc/#{pid}/status"
83
+ # If it's available it uses less resources than shelling out to ps
84
+ def linux_status_memory(file = @status_file)
85
+ line = file.each_line.detect {|line| line.start_with? 'VmRSS'.freeze }
86
+ return unless line
87
+ return unless (_name, value, unit = line.split(nil)).length == 3
88
+ CONVERSION[unit.downcase!] * value.to_i
89
+ rescue Errno::EACCES, Errno::ENOENT
90
+ 0
52
91
  end
53
92
 
54
- # linux stores memory info in a file "/proc/#{pid}/smaps"
55
- # If it's available it uses less resources than shelling out to ps
93
+ # linux stores detailed memory info in a file "/proc/#{pid}/smaps"
56
94
  def linux_memory(file = @process_file)
57
- lines = file.each_line.select {|line| line.match /^Rss/ }
95
+ lines = file.each_line.select {|line| line.match(/^Rss/) }
58
96
  return if lines.empty?
59
97
  lines.reduce(0) do |sum, line|
60
98
  line.match(/(?<value>(\d*\.{0,1}\d+))\s+(?<unit>\w\w)/) do |m|
61
- value = BigDecimal.new(m[:value]) + ROUND_UP
99
+ value = number_to_bigdecimal(m[:value]) + ROUND_UP
62
100
  unit = m[:unit].downcase
63
101
  sum += CONVERSION[unit] * value
64
102
  end
@@ -68,11 +106,19 @@ class GetProcessMem
68
106
  0
69
107
  end
70
108
 
71
- private
72
-
73
109
  # Pull memory from `ps` command, takes more resources and can freeze
74
110
  # in low memory situations
75
111
  def ps_memory
76
- KB_TO_BYTE * BigDecimal.new(`ps -o rss= -p #{pid}`)
112
+ if RUNS_ON_WINDOWS
113
+ size = ProcTable.ps(pid: pid).working_set_size
114
+ number_to_bigdecimal(size)
115
+ else
116
+ mem = `ps -o rss= -p #{pid}`
117
+ KB_TO_BYTE * number_to_bigdecimal(mem == "" ? 0 : mem)
118
+ end
119
+ end
120
+
121
+ def darwin_memory
122
+ Darwin.resident_size
77
123
  end
78
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.0"
3
- end
2
+ VERSION = "0.2.5"
3
+ end
@@ -0,0 +1,41 @@
1
+ Name: bash
2
+ State: S (sleeping)
3
+ Tgid: 3
4
+ Ngid: 0
5
+ Pid: 3
6
+ PPid: 1
7
+ TracerPid: 0
8
+ Uid: 58790 58790 58790 58790
9
+ Gid: 58790 58790 58790 58790
10
+ FDSize: 256
11
+ Groups:
12
+ VmPeak: 109120 kB
13
+ VmSize: 109120 kB
14
+ VmLck: 0 kB
15
+ VmPin: 0 kB
16
+ VmHWM: 2032 kB
17
+ VmRSS: 2032 kB
18
+ VmData: 356 kB
19
+ VmStk: 136 kB
20
+ VmExe: 956 kB
21
+ VmLib: 2288 kB
22
+ VmPTE: 64 kB
23
+ VmSwap: 0 kB
24
+ Threads: 1
25
+ SigQ: 0/274188
26
+ SigPnd: 0000000000000000
27
+ ShdPnd: 0000000000000000
28
+ SigBlk: 0000000000010000
29
+ SigIgn: 0000000000380004
30
+ SigCgt: 000000004b817efb
31
+ CapInh: 0000000000000000
32
+ CapPrm: 0000000000000000
33
+ CapEff: 0000000000000000
34
+ CapBnd: 0000001ff598cffe
35
+ Seccomp: 2
36
+ Cpus_allowed: f
37
+ Cpus_allowed_list: 0-3
38
+ Mems_allowed: 00000000,00000001
39
+ Mems_allowed_list: 0
40
+ voluntary_ctxt_switches: 34
41
+ nonvoluntary_ctxt_switches: 35
@@ -16,12 +16,17 @@ 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
+ end
21
+
22
+ def test_linux_status
23
+ bytes = @mem.linux_status_memory(fixture_path("heroku-bash-status"))
24
+ assert_equal bytes, 2080768
20
25
  end
21
26
 
22
27
  def test_conversions
23
28
  bytes = 0
24
- delta = BigDecimal.new("0.0000001")
29
+ delta = BigDecimal("0.0000001")
25
30
  assert_in_delta 0.0, @mem.kb(bytes), delta
26
31
  assert_in_delta 0.0, @mem.mb(bytes), delta
27
32
  assert_in_delta 0.0, @mem.gb(bytes), delta
metadata CHANGED
@@ -1,29 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: get_process_mem
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Schneeman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-16 00:00:00.000000000 Z
11
+ date: 2019-11-05 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'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sys-proctable
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.2'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.2'
13
41
  - !ruby/object:Gem::Dependency
14
42
  name: rake
15
43
  requirement: !ruby/object:Gem::Requirement
16
44
  requirements:
17
45
  - - "~>"
18
46
  - !ruby/object:Gem::Version
19
- version: '10.1'
47
+ version: '12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: test-unit
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3'
20
62
  type: :development
21
63
  prerelease: false
22
64
  version_requirements: !ruby/object:Gem::Requirement
23
65
  requirements:
24
66
  - - "~>"
25
67
  - !ruby/object:Gem::Version
26
- version: '10.1'
68
+ version: '3'
27
69
  description: " Get memory usage of a process in Ruby "
28
70
  email:
29
71
  - richard.schneeman+rubygems@gmail.com
@@ -31,16 +73,20 @@ executables: []
31
73
  extensions: []
32
74
  extra_rdoc_files: []
33
75
  files:
76
+ - ".github/workflows/check_changelog.yml"
34
77
  - ".gitignore"
35
78
  - ".travis.yml"
36
79
  - CHANGELOG.md
37
80
  - Gemfile
38
81
  - README.md
39
82
  - Rakefile
83
+ - appveyor.yml
40
84
  - get_process_mem.gemspec
41
85
  - lib/get_process_mem.rb
86
+ - lib/get_process_mem/darwin.rb
42
87
  - lib/get_process_mem/version.rb
43
88
  - test/fixtures/heroku-bash-smap
89
+ - test/fixtures/heroku-bash-status
44
90
  - test/get_process_mem_test.rb
45
91
  - test/test_helper.rb
46
92
  homepage: https://github.com/schneems/get_process_mem
@@ -62,13 +108,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
108
  - !ruby/object:Gem::Version
63
109
  version: '0'
64
110
  requirements: []
65
- rubyforge_project:
66
- rubygems_version: 2.2.2
111
+ rubygems_version: 3.0.3
67
112
  signing_key:
68
113
  specification_version: 4
69
114
  summary: Use GetProcessMem to find out the amount of RAM used by any process
70
115
  test_files:
71
116
  - test/fixtures/heroku-bash-smap
117
+ - test/fixtures/heroku-bash-status
72
118
  - test/get_process_mem_test.rb
73
119
  - test/test_helper.rb
74
- has_rdoc: