get_process_mem 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/.travis.yml +14 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +16 -0
- data/README.md +56 -0
- data/Rakefile +15 -0
- data/get_process_mem.gemspec +22 -0
- data/lib/get_process_mem.rb +87 -0
- data/lib/get_process_mem/version.rb +3 -0
- data/test/get_process_mem_test.rb +40 -0
- data/test/test_helper.rb +5 -0
- metadata +72 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5cca1e4bc8403f7fea51433ecdc8471f65fdf823
|
4
|
+
data.tar.gz: 3dfb46b8047e464e46f3bee70826b6ed9114522e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dbf95661e13258be18215581177f9f7f2beb949dd5f389ba75b18d60af72b877bfcb2668a53ac641c59fc2ab26b34a6e0320a451c1a346c5191b07596d5f6714
|
7
|
+
data.tar.gz: 77cbc7d341d3e8e40e573886f57d8be80801e37d8c55d813ca0853655b1ef9d205bf838cfda59015bfa761a9216a0896c5bbbdd63eb303c98618ae62a0ef78ac
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
*.gem
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# Get Process Memory
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/schneems/get_process_memory.png?branch=master)](https://travis-ci.org/schneems/get_process_memory)
|
4
|
+
|
5
|
+
Do you need to get the memory useage of a process? Great because this library does that.
|
6
|
+
|
7
|
+
## Install
|
8
|
+
|
9
|
+
In your `Gemfile` add
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'get_process_mem'
|
13
|
+
```
|
14
|
+
|
15
|
+
then run `$ bundle install`
|
16
|
+
|
17
|
+
|
18
|
+
## Use It
|
19
|
+
|
20
|
+
Get the current process memory usage:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
puts mem = GetProcessMem.new.inspect
|
24
|
+
#<GetProcessMem @mb=24.28125 @gb=0.023712158203125 @kb=24864.0 @bytes=25460736 >
|
25
|
+
mem.bytes # => 25460736
|
26
|
+
mem.kb # => 24864.0
|
27
|
+
mem.mb # => 24.28125
|
28
|
+
mem.gb # => 0.023712158203125
|
29
|
+
```
|
30
|
+
|
31
|
+
Note: all numeric values returned as a float except bytes which is an integer.
|
32
|
+
|
33
|
+
Get memory usage of another process:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
`echo 'nothing to see here' > tmplogf`
|
37
|
+
pid = Process.spawn('tail -f tmplog')
|
38
|
+
mem = GetProcessMem.new(pid)
|
39
|
+
puts mem.inspect
|
40
|
+
# => #<GetProcessMem @mb=0.48828125 @gb=0.000476837158203125 @kb=500.0 @bytes=512000 >
|
41
|
+
|
42
|
+
Process.kill('TERM', pid)
|
43
|
+
Process.wait(pid)
|
44
|
+
|
45
|
+
mem.inspect
|
46
|
+
# => "#<GetProcessMem @mb=0.0 @gb=0.0 @kb=0.0 @bytes=0>"
|
47
|
+
`rm tmplog`
|
48
|
+
```
|
49
|
+
|
50
|
+
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. It's all I needed for the project.
|
51
|
+
|
52
|
+
|
53
|
+
|
54
|
+
## License
|
55
|
+
|
56
|
+
MIT
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
|
5
|
+
require 'rake'
|
6
|
+
require 'rake/testtask'
|
7
|
+
|
8
|
+
task :default => [:test]
|
9
|
+
|
10
|
+
test_task = Rake::TestTask.new(:test) do |t|
|
11
|
+
t.libs << 'lib'
|
12
|
+
t.libs << 'test'
|
13
|
+
t.pattern = 'test/**/*_test.rb'
|
14
|
+
t.verbose = false
|
15
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'get_process_mem/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "get_process_mem"
|
8
|
+
gem.version = GetProcessMem::VERSION
|
9
|
+
gem.authors = ["Richard Schneeman"]
|
10
|
+
gem.email = ["richard.schneeman+rubygems@gmail.com"]
|
11
|
+
gem.description = %q{ Get memory usage of a process in Ruby }
|
12
|
+
gem.summary = %q{ Use GetProcessMem to find out the amount of RAM used by any process }
|
13
|
+
gem.homepage = "https://github.com/schneems/get_process_mem"
|
14
|
+
gem.license = "MIT"
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($/)
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
|
+
gem.require_paths = ["lib"]
|
20
|
+
|
21
|
+
gem.add_development_dependency "rake", "~> 10.1"
|
22
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
|
3
|
+
# Cribbed from Unicorn Worker Killer, thanks!
|
4
|
+
class GetProcessMem
|
5
|
+
KB_TO_BYTE = 1024 # 2**10 = 1024
|
6
|
+
MB_TO_BYTE = 1_048_576 # 1024**2 = 1_048_576
|
7
|
+
GB_TO_BYTE = 1_073_741_824 # 1024**3 = 1_073_741_824
|
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
|
+
attr_reader :pid
|
11
|
+
|
12
|
+
def initialize(pid = Process.pid, mem_type = MEM_TYPE)
|
13
|
+
@process_file = Pathname.new "/proc/#{pid}/status"
|
14
|
+
@pid = pid
|
15
|
+
@linux = @process_file.exist?
|
16
|
+
self.mem_type = mem_type
|
17
|
+
end
|
18
|
+
|
19
|
+
def linux?
|
20
|
+
@linux
|
21
|
+
end
|
22
|
+
|
23
|
+
def bytes
|
24
|
+
memory = linux_memory if linux?
|
25
|
+
memory ||= ps_memory
|
26
|
+
end
|
27
|
+
|
28
|
+
def kb(b = bytes)
|
29
|
+
b.to_f/KB_TO_BYTE
|
30
|
+
end
|
31
|
+
|
32
|
+
def mb(b = bytes)
|
33
|
+
b.to_f/MB_TO_BYTE
|
34
|
+
end
|
35
|
+
|
36
|
+
def gb(b = bytes)
|
37
|
+
b.to_f/GB_TO_BYTE
|
38
|
+
end
|
39
|
+
|
40
|
+
def refresh
|
41
|
+
@bytes = nil
|
42
|
+
end
|
43
|
+
|
44
|
+
def inspect
|
45
|
+
b = bytes
|
46
|
+
"#<#{self.class}:0x%08x @mb=#{mb b } @gb=#{gb b } @kb=#{kb b } @bytes=#{b}>" % (object_id * 2)
|
47
|
+
end
|
48
|
+
|
49
|
+
def mem_type
|
50
|
+
@mem_type
|
51
|
+
end
|
52
|
+
|
53
|
+
def mem_type=(mem_type)
|
54
|
+
@mem_type = mem_type.downcase
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
# Pull memory from `ps` command, takes more resources and can freeze
|
60
|
+
# in low memory situations
|
61
|
+
def ps_memory
|
62
|
+
KB_TO_BYTE * `ps -o #{mem_type_for_ps}= -p #{pid}`.to_i
|
63
|
+
end
|
64
|
+
|
65
|
+
def mem_type_for_ps
|
66
|
+
mem_type
|
67
|
+
end
|
68
|
+
|
69
|
+
def mem_type_for_linux
|
70
|
+
case mem_type
|
71
|
+
when 'rss'
|
72
|
+
'VmRSS'
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# linux stores memory info in a file "/proc/#{pid}/status"
|
77
|
+
# If it's available it uses less resources than shelling out to ps
|
78
|
+
def linux_memory
|
79
|
+
line = @process_file.read.each_line.detect {|line| line.include? mem_type_for_linux }
|
80
|
+
return unless line
|
81
|
+
return unless (name, value, unit = line.split(nil)).length == 3
|
82
|
+
multiplier = CONVERSION[unit.downcase]
|
83
|
+
multiplier * value.to_i
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
GetProcessMem
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class GetProcessMemTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@mem = GetProcessMem.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_seems_to_work
|
10
|
+
assert @mem.kb > 0
|
11
|
+
assert @mem.mb > 0
|
12
|
+
assert @mem.gb > 0
|
13
|
+
assert @mem.bytes > 0
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_conversions
|
17
|
+
bytes = 0
|
18
|
+
assert_equal 0.0, @mem.kb(bytes)
|
19
|
+
assert_equal 0.0, @mem.mb(bytes)
|
20
|
+
assert_equal 0.0, @mem.gb(bytes)
|
21
|
+
|
22
|
+
# kb
|
23
|
+
bytes = 1024
|
24
|
+
assert_equal 1.0, @mem.kb(bytes)
|
25
|
+
assert_equal 0.0009765625, @mem.mb(bytes)
|
26
|
+
assert_equal 9.5367431640625e-07, @mem.gb(bytes)
|
27
|
+
|
28
|
+
# mb
|
29
|
+
bytes = 1_048_576
|
30
|
+
assert_equal 1024.0, @mem.kb(bytes)
|
31
|
+
assert_equal 1.0, @mem.mb(bytes)
|
32
|
+
assert_equal 0.0009765625, @mem.gb(bytes)
|
33
|
+
|
34
|
+
# gb
|
35
|
+
bytes = 1_073_741_824
|
36
|
+
assert_equal 1048576.0, @mem.kb(bytes)
|
37
|
+
assert_equal 1024.0, @mem.mb(bytes)
|
38
|
+
assert_equal 1.0, @mem.gb(bytes)
|
39
|
+
end
|
40
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: get_process_mem
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Richard Schneeman
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-02-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '10.1'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '10.1'
|
27
|
+
description: " Get memory usage of a process in Ruby "
|
28
|
+
email:
|
29
|
+
- richard.schneeman+rubygems@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".gitignore"
|
35
|
+
- ".travis.yml"
|
36
|
+
- Gemfile
|
37
|
+
- Gemfile.lock
|
38
|
+
- README.md
|
39
|
+
- Rakefile
|
40
|
+
- get_process_mem.gemspec
|
41
|
+
- lib/get_process_mem.rb
|
42
|
+
- lib/get_process_mem/version.rb
|
43
|
+
- test/get_process_mem_test.rb
|
44
|
+
- test/test_helper.rb
|
45
|
+
homepage: https://github.com/schneems/get_process_mem
|
46
|
+
licenses:
|
47
|
+
- MIT
|
48
|
+
metadata: {}
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 2.2.0
|
66
|
+
signing_key:
|
67
|
+
specification_version: 4
|
68
|
+
summary: Use GetProcessMem to find out the amount of RAM used by any process
|
69
|
+
test_files:
|
70
|
+
- test/get_process_mem_test.rb
|
71
|
+
- test/test_helper.rb
|
72
|
+
has_rdoc:
|