get_process_mem 0.2.0 → 0.2.1
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 -0
- data/CHANGELOG.md +4 -0
- data/Gemfile +1 -0
- data/README.md +5 -10
- data/appveyor.yml +20 -0
- data/get_process_mem.gemspec +2 -0
- data/lib/get_process_mem.rb +33 -15
- data/lib/get_process_mem/version.rb +2 -2
- data/test/fixtures/heroku-bash-status +41 -0
- data/test/get_process_mem_test.rb +5 -0
- metadata +34 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1ec90ef55de8687d6be515c6191755d5411ee3ab
|
4
|
+
data.tar.gz: 610e87016aed28ddd40ec448b7fcd42e754cb1a4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be1f8d9ddc800b00e67054d2d218e2cc6bbdc9a5972c5491941f481ff8e693d9d423315dad04ef1de9e5ab61a372e02fb5a409fbbcae10a3b4f2b23791849e58
|
7
|
+
data.tar.gz: c8b6e1fb05a95d651e60f86255e0b9c72089374922e1a434ed047489a800217f7d512f00188542b1b0f59e1c9b190ad97afb05cc8d53429a18a72b120e1481ed
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -12,15 +12,17 @@ In your `Gemfile` add
|
|
12
12
|
gem 'get_process_mem'
|
13
13
|
```
|
14
14
|
|
15
|
-
then run `$ bundle install
|
15
|
+
then run `$ bundle install`.
|
16
16
|
|
17
|
+
If you're using Windows you'll also need to have the `sys-proctable` gem.
|
17
18
|
|
18
19
|
## Use It
|
19
20
|
|
20
21
|
Get the current process memory usage:
|
21
22
|
|
22
23
|
```ruby
|
23
|
-
|
24
|
+
mem = GetProcessMem.new
|
25
|
+
puts mem.inspect
|
24
26
|
#<GetProcessMem @mb=24.28125 @gb=0.023712158203125 @kb=24864.0 @bytes=25460736 >
|
25
27
|
mem.bytes # => 25460736
|
26
28
|
mem.kb # => 24864.0
|
@@ -47,14 +49,7 @@ mem.inspect
|
|
47
49
|
`rm tmplog`
|
48
50
|
```
|
49
51
|
|
50
|
-
On Linux,
|
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
|
-
|
52
|
+
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
53
|
|
59
54
|
|
60
55
|
## License
|
data/appveyor.yml
ADDED
@@ -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
|
data/get_process_mem.gemspec
CHANGED
@@ -18,5 +18,7 @@ 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 "sys-proctable", "~> 1.0"
|
21
22
|
gem.add_development_dependency "rake", "~> 10.1"
|
23
|
+
gem.add_development_dependency "test-unit", "~> 3.1.0"
|
22
24
|
end
|
data/lib/get_process_mem.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'pathname'
|
2
2
|
require 'bigdecimal'
|
3
3
|
|
4
|
-
|
5
4
|
# Cribbed from Unicorn Worker Killer, thanks!
|
6
5
|
class GetProcessMem
|
7
6
|
KB_TO_BYTE = 1024 # 2**10 = 1024
|
@@ -11,10 +10,24 @@ class GetProcessMem
|
|
11
10
|
ROUND_UP = BigDecimal.new("0.5")
|
12
11
|
attr_reader :pid
|
13
12
|
|
13
|
+
RUNS_ON_WINDOWS = Gem.win_platform?
|
14
|
+
|
15
|
+
if RUNS_ON_WINDOWS
|
16
|
+
begin
|
17
|
+
require 'sys/proctable'
|
18
|
+
rescue LoadError => e
|
19
|
+
message = "Please add `sys-proctable` to your Gemfile for windows machines\n"
|
20
|
+
message << e.message
|
21
|
+
raise e, message
|
22
|
+
end
|
23
|
+
include Sys
|
24
|
+
end
|
25
|
+
|
14
26
|
def initialize(pid = Process.pid)
|
27
|
+
@status_file = Pathname.new "/proc/#{pid}/status"
|
15
28
|
@process_file = Pathname.new "/proc/#{pid}/smaps"
|
16
29
|
@pid = pid
|
17
|
-
@linux = @
|
30
|
+
@linux = @status_file.exist?
|
18
31
|
end
|
19
32
|
|
20
33
|
def linux?
|
@@ -22,7 +35,7 @@ class GetProcessMem
|
|
22
35
|
end
|
23
36
|
|
24
37
|
def bytes
|
25
|
-
memory =
|
38
|
+
memory = linux_status_memory if linux?
|
26
39
|
memory ||= ps_memory
|
27
40
|
end
|
28
41
|
|
@@ -43,18 +56,20 @@ class GetProcessMem
|
|
43
56
|
"#<#{self.class}:0x%08x @mb=#{ mb b } @gb=#{ gb b } @kb=#{ kb b } @bytes=#{b}>" % (object_id * 2)
|
44
57
|
end
|
45
58
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
59
|
+
# linux stores memory info in a file "/proc/#{pid}/status"
|
60
|
+
# If it's available it uses less resources than shelling out to ps
|
61
|
+
def linux_status_memory(file = @status_file)
|
62
|
+
line = file.each_line.detect {|line| line.start_with? 'VmRSS'.freeze }
|
63
|
+
return unless line
|
64
|
+
return unless (_name, value, unit = line.split(nil)).length == 3
|
65
|
+
CONVERSION[unit.downcase!] * value.to_i
|
66
|
+
rescue Errno::EACCES, Errno::ENOENT
|
67
|
+
0
|
52
68
|
end
|
53
69
|
|
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
|
70
|
+
# linux stores detailed memory info in a file "/proc/#{pid}/smaps"
|
56
71
|
def linux_memory(file = @process_file)
|
57
|
-
lines = file.each_line.select {|line| line.match
|
72
|
+
lines = file.each_line.select {|line| line.match(/^Rss/) }
|
58
73
|
return if lines.empty?
|
59
74
|
lines.reduce(0) do |sum, line|
|
60
75
|
line.match(/(?<value>(\d*\.{0,1}\d+))\s+(?<unit>\w\w)/) do |m|
|
@@ -68,11 +83,14 @@ class GetProcessMem
|
|
68
83
|
0
|
69
84
|
end
|
70
85
|
|
71
|
-
private
|
72
|
-
|
73
86
|
# Pull memory from `ps` command, takes more resources and can freeze
|
74
87
|
# in low memory situations
|
75
88
|
def ps_memory
|
76
|
-
|
89
|
+
if RUNS_ON_WINDOWS
|
90
|
+
size = ProcTable.ps(pid).working_set_size
|
91
|
+
BigDecimal.new(size)
|
92
|
+
else
|
93
|
+
KB_TO_BYTE * BigDecimal.new(`ps -o rss= -p #{pid}`)
|
94
|
+
end
|
77
95
|
end
|
78
96
|
end
|
@@ -1,3 +1,3 @@
|
|
1
1
|
class GetProcessMem
|
2
|
-
VERSION = "0.2.
|
3
|
-
end
|
2
|
+
VERSION = "0.2.1"
|
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
|
@@ -19,6 +19,11 @@ class GetProcessMemTest < Test::Unit::TestCase
|
|
19
19
|
assert_in_delta BigDecimal.new("2122240.0"), bytes, delta
|
20
20
|
end
|
21
21
|
|
22
|
+
def test_linux_status
|
23
|
+
bytes = @mem.linux_status_memory(fixture_path("heroku-bash-status"))
|
24
|
+
assert_equal bytes, 2080768
|
25
|
+
end
|
26
|
+
|
22
27
|
def test_conversions
|
23
28
|
bytes = 0
|
24
29
|
delta = BigDecimal.new("0.0000001")
|
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.1
|
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: 2016-06-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sys-proctable
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
type: :development
|
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: rake
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -24,6 +38,20 @@ dependencies:
|
|
24
38
|
- - "~>"
|
25
39
|
- !ruby/object:Gem::Version
|
26
40
|
version: '10.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: test-unit
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.1.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.1.0
|
27
55
|
description: " Get memory usage of a process in Ruby "
|
28
56
|
email:
|
29
57
|
- richard.schneeman+rubygems@gmail.com
|
@@ -37,10 +65,12 @@ files:
|
|
37
65
|
- Gemfile
|
38
66
|
- README.md
|
39
67
|
- Rakefile
|
68
|
+
- appveyor.yml
|
40
69
|
- get_process_mem.gemspec
|
41
70
|
- lib/get_process_mem.rb
|
42
71
|
- lib/get_process_mem/version.rb
|
43
72
|
- test/fixtures/heroku-bash-smap
|
73
|
+
- test/fixtures/heroku-bash-status
|
44
74
|
- test/get_process_mem_test.rb
|
45
75
|
- test/test_helper.rb
|
46
76
|
homepage: https://github.com/schneems/get_process_mem
|
@@ -63,12 +93,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
63
93
|
version: '0'
|
64
94
|
requirements: []
|
65
95
|
rubyforge_project:
|
66
|
-
rubygems_version: 2.
|
96
|
+
rubygems_version: 2.5.1
|
67
97
|
signing_key:
|
68
98
|
specification_version: 4
|
69
99
|
summary: Use GetProcessMem to find out the amount of RAM used by any process
|
70
100
|
test_files:
|
71
101
|
- test/fixtures/heroku-bash-smap
|
102
|
+
- test/fixtures/heroku-bash-status
|
72
103
|
- test/get_process_mem_test.rb
|
73
104
|
- test/test_helper.rb
|
74
|
-
has_rdoc:
|