get_process_mem 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/CHANGELOG.md +8 -0
- data/README.md +9 -3
- data/lib/get_process_mem.rb +18 -10
- data/lib/get_process_mem/version.rb +1 -1
- metadata +4 -5
- data/Gemfile.lock +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1926d125b830fc4f0f99e8a4a74d68e98ff26e53
|
4
|
+
data.tar.gz: 49a31e4c798e3b8b9985ee2e7a5f0fbf347e1ec9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fce55634e47e6282a8fa42d2dd2df39a201c180612a70a6e87fce95dfbf313080fbbe46a80f00b21e62c40b99d9d304c35bcfa1adbb768b6b0e67d1409111bc0
|
7
|
+
data.tar.gz: cdcbe4cae20d5af9d53a79ec37f89cf2bfdb2d7d211114f78fa89b56cf732c3d39ca6ea39417da13ee1d393a772f8ecc63389ab7b7e26836907238088d29d0fd
|
data/.gitignore
CHANGED
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
[![Build Status](https://travis-ci.org/schneems/get_process_mem.png?branch=master)](https://travis-ci.org/schneems/get_process_mem)
|
4
4
|
|
5
|
-
Do you need to get the memory
|
5
|
+
Do you need to get the memory usage of a process? Great because this library does that.
|
6
6
|
|
7
7
|
## Install
|
8
8
|
|
@@ -28,7 +28,7 @@ mem.mb # => 24.28125
|
|
28
28
|
mem.gb # => 0.023712158203125
|
29
29
|
```
|
30
30
|
|
31
|
-
Note:
|
31
|
+
Note: All numeric values returned as a float except bytes which is an integer.
|
32
32
|
|
33
33
|
Get memory usage of another process:
|
34
34
|
|
@@ -47,7 +47,13 @@ mem.inspect
|
|
47
47
|
`rm tmplog`
|
48
48
|
```
|
49
49
|
|
50
|
-
|
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
|
+
```
|
51
57
|
|
52
58
|
|
53
59
|
|
data/lib/get_process_mem.rb
CHANGED
@@ -6,14 +6,14 @@ class GetProcessMem
|
|
6
6
|
MB_TO_BYTE = 1_048_576 # 1024**2 = 1_048_576
|
7
7
|
GB_TO_BYTE = 1_073_741_824 # 1024**3 = 1_073_741_824
|
8
8
|
CONVERSION = { "kb" => KB_TO_BYTE, "mb" => MB_TO_BYTE, "gb" => GB_TO_BYTE}
|
9
|
-
MEM_TYPE = "rss" # http://en.wikipedia.org/wiki/Resident_set_size
|
10
9
|
attr_reader :pid
|
11
10
|
|
12
|
-
def initialize(pid = Process.pid,
|
13
|
-
@process_file = Pathname.new "/proc/#{pid}/
|
11
|
+
def initialize(pid = Process.pid, options = {})
|
12
|
+
@process_file = Pathname.new "/proc/#{pid}/smaps"
|
14
13
|
@pid = pid
|
15
14
|
@linux = @process_file.exist?
|
16
|
-
|
15
|
+
options[:mem_type] ||= @linux ? 'pss' : 'rss'
|
16
|
+
self.mem_type = options[:mem_type]
|
17
17
|
end
|
18
18
|
|
19
19
|
def linux?
|
@@ -65,17 +65,25 @@ class GetProcessMem
|
|
65
65
|
def mem_type_for_linux
|
66
66
|
case mem_type
|
67
67
|
when 'rss'
|
68
|
-
'
|
68
|
+
'Rss'
|
69
|
+
when 'pss'
|
70
|
+
'Pss'
|
69
71
|
end
|
70
72
|
end
|
71
73
|
|
72
|
-
# linux stores memory info in a file "/proc/#{pid}/
|
74
|
+
# linux stores memory info in a file "/proc/#{pid}/smaps"
|
73
75
|
# If it's available it uses less resources than shelling out to ps
|
76
|
+
# It also allows us to use Pss (the process' proportional share of
|
77
|
+
# the mapping that is resident in RAM) as mem_type
|
74
78
|
def linux_memory
|
75
|
-
|
76
|
-
return unless
|
77
|
-
|
78
|
-
|
79
|
+
lines = @process_file.each_line.select {|line| line.include? mem_type_for_linux }
|
80
|
+
return unless lines.length > 0
|
81
|
+
lines.reduce(0) do |sum, line|
|
82
|
+
return unless (name, value, unit = line.split(nil)).length == 3
|
83
|
+
# The PSS value is truncated, and adding 0.5 should average this out
|
84
|
+
value = value.to_f + 0.5 if mem_type == 'pss'
|
85
|
+
sum += CONVERSION[unit.downcase] * value.to_f
|
86
|
+
end
|
79
87
|
end
|
80
88
|
end
|
81
89
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: get_process_mem
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
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-
|
11
|
+
date: 2014-04-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -33,8 +33,8 @@ extra_rdoc_files: []
|
|
33
33
|
files:
|
34
34
|
- ".gitignore"
|
35
35
|
- ".travis.yml"
|
36
|
+
- CHANGELOG.md
|
36
37
|
- Gemfile
|
37
|
-
- Gemfile.lock
|
38
38
|
- README.md
|
39
39
|
- Rakefile
|
40
40
|
- get_process_mem.gemspec
|
@@ -62,11 +62,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
62
62
|
version: '0'
|
63
63
|
requirements: []
|
64
64
|
rubyforge_project:
|
65
|
-
rubygems_version: 2.2.
|
65
|
+
rubygems_version: 2.2.2
|
66
66
|
signing_key:
|
67
67
|
specification_version: 4
|
68
68
|
summary: Use GetProcessMem to find out the amount of RAM used by any process
|
69
69
|
test_files:
|
70
70
|
- test/get_process_mem_test.rb
|
71
71
|
- test/test_helper.rb
|
72
|
-
has_rdoc:
|