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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 40e47a8db99d1a0aa017234b19201977d0d5ffd7
4
- data.tar.gz: e6f6c78ed5e072ba3e1c61ca8485e04f470e19e0
3
+ metadata.gz: 1926d125b830fc4f0f99e8a4a74d68e98ff26e53
4
+ data.tar.gz: 49a31e4c798e3b8b9985ee2e7a5f0fbf347e1ec9
5
5
  SHA512:
6
- metadata.gz: 35a0c27762550925839a2c6722d3e759bb77568e1a7badf46ddaa81c5907ee1c7e4c122b9e9bc0b772ce573c3d4b08b5626ec40bf71f5491c6a5250f1ac05893
7
- data.tar.gz: 1ff60c90b5f3b47526b86a5ea7bcb4d40068a4aa67a424246bcde6bc25a2cfb2b43dd03de2806c6e8b8be2943e28d3fdbce4eaeff565496d2f1ee6aa15e045d9
6
+ metadata.gz: fce55634e47e6282a8fa42d2dd2df39a201c180612a70a6e87fce95dfbf313080fbbe46a80f00b21e62c40b99d9d304c35bcfa1adbb768b6b0e67d1409111bc0
7
+ data.tar.gz: cdcbe4cae20d5af9d53a79ec37f89cf2bfdb2d7d211114f78fa89b56cf732c3d39ca6ea39417da13ee1d393a772f8ecc63389ab7b7e26836907238088d29d0fd
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
1
  *.gem
2
+ Gemfile.lock
data/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ ## 0.1.0
2
+
3
+ - Default to using PSS for linux (#2 & #3) @ksoderstrom
4
+ - Correct for bit shifting truncation (#4) @ksoderstrom
5
+
6
+ ## 0.0.1
7
+
8
+ - Initial
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 useage of a process? Great because this library does that.
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: all numeric values returned as a float except bytes which is an integer.
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
- 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.
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
 
@@ -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, mem_type = MEM_TYPE)
13
- @process_file = Pathname.new "/proc/#{pid}/status"
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
- self.mem_type = mem_type
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
- 'VmRSS'
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}/status"
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
- line = @process_file.each_line.detect {|line| line.include? mem_type_for_linux }
76
- return unless line
77
- return unless (name, value, unit = line.split(nil)).length == 3
78
- CONVERSION[unit.downcase] * value.to_i
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
 
@@ -1,3 +1,3 @@
1
1
  class GetProcessMem
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
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.2
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-02-10 00:00:00.000000000 Z
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.0
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:
data/Gemfile.lock DELETED
@@ -1,16 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- get_process_mem (0.0.1)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- rake (10.1.1)
10
-
11
- PLATFORMS
12
- ruby
13
-
14
- DEPENDENCIES
15
- get_process_mem!
16
- rake (~> 10.1)