get_process_mem 0.2.2 → 0.2.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/check_changelog.yml +12 -0
- data/.travis.yml +12 -8
- data/CHANGELOG.md +23 -1
- data/README.md +1 -1
- data/get_process_mem.gemspec +5 -3
- data/lib/get_process_mem.rb +40 -11
- data/lib/get_process_mem/darwin.rb +63 -0
- data/lib/get_process_mem/version.rb +1 -1
- data/test/get_process_mem_test.rb +12 -3
- metadata +28 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 06a96e8d978fd95bcf546a1e5e8bbc01cc10635d50b2f87639eb9c1f8c7f4f76
|
4
|
+
data.tar.gz: 6c941ae4f0301ff698b9be4ba75597537ed3ae5a11bf9295ab81144ef8435047
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3cdb2baad08676298249f006b12f0c61bd0fdc1e13902211e2e0c3ea965c39fee2edb2ad3d2ceb617dcf450738e765c60c7e748830d85c52b044fa0ad8a3c8db
|
7
|
+
data.tar.gz: 35d717cc39f85a902adaa8ea6028d969b40b89465bcc0d1578de4e97308fe890e77ce9ff06b68855253030b3ab26fa130e2e44be06b041368a39a4cb4bc300f8
|
@@ -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
|
data/.travis.yml
CHANGED
@@ -1,18 +1,22 @@
|
|
1
1
|
language: ruby
|
2
|
+
os:
|
3
|
+
- linux
|
4
|
+
- osx
|
2
5
|
|
3
6
|
before_install:
|
4
|
-
- gem install bundler -v 1.
|
7
|
+
- gem install bundler -v 1.17.3
|
5
8
|
|
6
9
|
rvm:
|
7
|
-
-
|
8
|
-
- 2.
|
9
|
-
- 2.1.0
|
10
|
+
- 2.2
|
11
|
+
- 2.6
|
10
12
|
- ruby-head
|
11
|
-
- jruby-
|
12
|
-
-
|
13
|
+
- jruby-head
|
14
|
+
- truffleruby
|
13
15
|
|
14
16
|
matrix:
|
15
17
|
allow_failures:
|
18
|
+
- rvm: 2.2
|
19
|
+
if: os = osx
|
16
20
|
- rvm: ruby-head
|
17
|
-
- rvm:
|
18
|
-
- rvm:
|
21
|
+
- rvm: jruby-head
|
22
|
+
- rvm: truffleruby
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,25 @@
|
|
1
|
+
## HEAD (unreleased)
|
2
|
+
|
3
|
+
## 0.2.7
|
4
|
+
|
5
|
+
- Native (faster) support for returning memory from different PIDs on mac (https://github.com/schneems/get_process_mem/pull/42)
|
6
|
+
|
7
|
+
## 0.2.6
|
8
|
+
|
9
|
+
- Support returning memory from different PIDs on mac (https://github.com/schneems/get_process_mem/pull/41)
|
10
|
+
|
11
|
+
## 0.2.5
|
12
|
+
|
13
|
+
- Use new sys-proctable interface (https://github.com/schneems/get_process_mem/pull/36)
|
14
|
+
|
15
|
+
## 0.2.4
|
16
|
+
|
17
|
+
- Use FFI to speed up memory lookup on Mac (darwin) by roughly 12x (#32)
|
18
|
+
|
19
|
+
## 0.2.3
|
20
|
+
|
21
|
+
- Silence BigDecimal deprecation warning under Ruby 2.5 (#26)
|
22
|
+
|
1
23
|
## 0.2.2
|
2
24
|
|
3
25
|
- Fix support for Ruby 2.4 (#23)
|
@@ -21,4 +43,4 @@
|
|
21
43
|
|
22
44
|
## 0.0.1
|
23
45
|
|
24
|
-
- Initial
|
46
|
+
- Initial
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Get Process Memory
|
2
2
|
|
3
|
-
[![Build Status](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
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)
|
5
5
|
|
6
6
|
Do you need to get the memory usage of a process? Great because this library does that.
|
data/get_process_mem.gemspec
CHANGED
@@ -18,7 +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.
|
22
|
-
|
23
|
-
gem.add_development_dependency "
|
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"
|
24
26
|
end
|
data/lib/get_process_mem.rb
CHANGED
@@ -3,11 +3,19 @@ require 'bigdecimal'
|
|
3
3
|
|
4
4
|
# Cribbed from Unicorn Worker Killer, thanks!
|
5
5
|
class GetProcessMem
|
6
|
-
|
7
|
-
|
8
|
-
|
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
|
9
17
|
CONVERSION = { "kb" => KB_TO_BYTE, "mb" => MB_TO_BYTE, "gb" => GB_TO_BYTE }
|
10
|
-
ROUND_UP =
|
18
|
+
ROUND_UP = number_to_bigdecimal "0.5"
|
11
19
|
attr_reader :pid
|
12
20
|
|
13
21
|
RUNS_ON_WINDOWS = Gem.win_platform?
|
@@ -23,6 +31,20 @@ class GetProcessMem
|
|
23
31
|
include Sys
|
24
32
|
end
|
25
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
|
+
|
26
48
|
def initialize(pid = Process.pid)
|
27
49
|
@status_file = Pathname.new "/proc/#{pid}/status"
|
28
50
|
@process_file = Pathname.new "/proc/#{pid}/smaps"
|
@@ -36,19 +58,20 @@ class GetProcessMem
|
|
36
58
|
|
37
59
|
def bytes
|
38
60
|
memory = linux_status_memory if linux?
|
61
|
+
memory ||= darwin_memory if RUNS_ON_DARWIN
|
39
62
|
memory ||= ps_memory
|
40
63
|
end
|
41
64
|
|
42
65
|
def kb(b = bytes)
|
43
|
-
(b/
|
66
|
+
(b/KB_TO_BYTE).to_f
|
44
67
|
end
|
45
68
|
|
46
69
|
def mb(b = bytes)
|
47
|
-
(b/
|
70
|
+
(b/MB_TO_BYTE).to_f
|
48
71
|
end
|
49
72
|
|
50
73
|
def gb(b = bytes)
|
51
|
-
(b/
|
74
|
+
(b/GB_TO_BYTE).to_f
|
52
75
|
end
|
53
76
|
|
54
77
|
def inspect
|
@@ -73,7 +96,7 @@ class GetProcessMem
|
|
73
96
|
return if lines.empty?
|
74
97
|
lines.reduce(0) do |sum, line|
|
75
98
|
line.match(/(?<value>(\d*\.{0,1}\d+))\s+(?<unit>\w\w)/) do |m|
|
76
|
-
value =
|
99
|
+
value = number_to_bigdecimal(m[:value]) + ROUND_UP
|
77
100
|
unit = m[:unit].downcase
|
78
101
|
sum += CONVERSION[unit] * value
|
79
102
|
end
|
@@ -87,11 +110,17 @@ class GetProcessMem
|
|
87
110
|
# in low memory situations
|
88
111
|
def ps_memory
|
89
112
|
if RUNS_ON_WINDOWS
|
90
|
-
size = ProcTable.ps(pid).working_set_size
|
91
|
-
|
113
|
+
size = ProcTable.ps(pid: pid).working_set_size
|
114
|
+
number_to_bigdecimal(size)
|
92
115
|
else
|
93
116
|
mem = `ps -o rss= -p #{pid}`
|
94
|
-
KB_TO_BYTE *
|
117
|
+
KB_TO_BYTE * number_to_bigdecimal(mem == "" ? 0 : mem)
|
95
118
|
end
|
96
119
|
end
|
120
|
+
|
121
|
+
def darwin_memory
|
122
|
+
Darwin.resident_size(pid)
|
123
|
+
rescue Errno::EPERM
|
124
|
+
nil
|
125
|
+
end
|
97
126
|
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'ffi'
|
2
|
+
|
3
|
+
class GetProcessMem
|
4
|
+
class Darwin
|
5
|
+
extend FFI::Library
|
6
|
+
ffi_lib 'proc'
|
7
|
+
|
8
|
+
|
9
|
+
class TaskInfo < FFI::Struct
|
10
|
+
layout :pti_virtual_size, :uint64,
|
11
|
+
:pti_resident_size, :uint64,
|
12
|
+
:pti_total_user, :uint64,
|
13
|
+
:pti_total_system, :uint64,
|
14
|
+
:pti_threads_user, :uint64,
|
15
|
+
:pti_threads_system, :uint64,
|
16
|
+
:pti_policy, :int32,
|
17
|
+
:pti_faults, :int32,
|
18
|
+
:pti_pageins, :int32,
|
19
|
+
:pti_cow_faults, :int32,
|
20
|
+
:pti_messages_sent, :int32,
|
21
|
+
:pti_messages_received, :int32,
|
22
|
+
:pti_syscalls_mach, :int32,
|
23
|
+
:pti_syscalls_unix, :int32,
|
24
|
+
:pti_csw, :int32,
|
25
|
+
:pti_threadnum, :int32,
|
26
|
+
:pti_numrunning, :int32,
|
27
|
+
:pti_priority, :int32
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
attach_function :proc_pidinfo,
|
33
|
+
[
|
34
|
+
:int, #pid
|
35
|
+
:int, # flavour
|
36
|
+
:uint64, #arg, not needed for this selector
|
37
|
+
TaskInfo.by_ref, #output buffer
|
38
|
+
:int, #size of buffer
|
39
|
+
],
|
40
|
+
:int
|
41
|
+
|
42
|
+
|
43
|
+
PROC_PIDTASKINFO = 4 #from sys/proc_info.h
|
44
|
+
|
45
|
+
class << self
|
46
|
+
def resident_size(pid)
|
47
|
+
get_proc_pidinfo(pid)[:pti_resident_size]
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def get_proc_pidinfo(pid)
|
53
|
+
data = TaskInfo.new
|
54
|
+
result = proc_pidinfo(pid, PROC_PIDTASKINFO, 0, data, TaskInfo.size)
|
55
|
+
if result == TaskInfo.size
|
56
|
+
data
|
57
|
+
else
|
58
|
+
raise SystemCallError.new("proc_pidinfo returned #{result}", FFI.errno);
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -1,11 +1,20 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class GetProcessMemTest < Test::Unit::TestCase
|
4
|
-
|
5
4
|
def setup
|
6
5
|
@mem = GetProcessMem.new
|
7
6
|
end
|
8
7
|
|
8
|
+
def test_different_pid_returns_different_memory
|
9
|
+
pid = Process.spawn("tail -f Gemfile")
|
10
|
+
|
11
|
+
other_mem = GetProcessMem.new(pid)
|
12
|
+
assert @mem.kb > other_mem.kb
|
13
|
+
ensure
|
14
|
+
Process.kill('TERM', pid) if pid
|
15
|
+
Process.wait(pid) if pid
|
16
|
+
end
|
17
|
+
|
9
18
|
def test_seems_to_work
|
10
19
|
assert @mem.kb > 0
|
11
20
|
assert @mem.mb > 0
|
@@ -16,7 +25,7 @@ class GetProcessMemTest < Test::Unit::TestCase
|
|
16
25
|
def test_linux_smap
|
17
26
|
delta = 1
|
18
27
|
bytes = @mem.linux_memory(fixture_path("heroku-bash-smap"))
|
19
|
-
assert_in_delta BigDecimal
|
28
|
+
assert_in_delta BigDecimal("2122240.0"), bytes, delta
|
20
29
|
end
|
21
30
|
|
22
31
|
def test_linux_status
|
@@ -26,7 +35,7 @@ class GetProcessMemTest < Test::Unit::TestCase
|
|
26
35
|
|
27
36
|
def test_conversions
|
28
37
|
bytes = 0
|
29
|
-
delta = BigDecimal
|
38
|
+
delta = BigDecimal("0.0000001")
|
30
39
|
assert_in_delta 0.0, @mem.kb(bytes), delta
|
31
40
|
assert_in_delta 0.0, @mem.mb(bytes), delta
|
32
41
|
assert_in_delta 0.0, @mem.gb(bytes), delta
|
metadata
CHANGED
@@ -1,57 +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.
|
4
|
+
version: 0.2.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Schneeman
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-08-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: ffi
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.0'
|
20
|
-
type: :
|
20
|
+
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
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'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rake
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
30
44
|
requirements:
|
31
45
|
- - "~>"
|
32
46
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
47
|
+
version: '12'
|
34
48
|
type: :development
|
35
49
|
prerelease: false
|
36
50
|
version_requirements: !ruby/object:Gem::Requirement
|
37
51
|
requirements:
|
38
52
|
- - "~>"
|
39
53
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
54
|
+
version: '12'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: test-unit
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
44
58
|
requirements:
|
45
59
|
- - "~>"
|
46
60
|
- !ruby/object:Gem::Version
|
47
|
-
version: 3
|
61
|
+
version: '3'
|
48
62
|
type: :development
|
49
63
|
prerelease: false
|
50
64
|
version_requirements: !ruby/object:Gem::Requirement
|
51
65
|
requirements:
|
52
66
|
- - "~>"
|
53
67
|
- !ruby/object:Gem::Version
|
54
|
-
version: 3
|
68
|
+
version: '3'
|
55
69
|
description: " Get memory usage of a process in Ruby "
|
56
70
|
email:
|
57
71
|
- richard.schneeman+rubygems@gmail.com
|
@@ -59,6 +73,7 @@ executables: []
|
|
59
73
|
extensions: []
|
60
74
|
extra_rdoc_files: []
|
61
75
|
files:
|
76
|
+
- ".github/workflows/check_changelog.yml"
|
62
77
|
- ".gitignore"
|
63
78
|
- ".travis.yml"
|
64
79
|
- CHANGELOG.md
|
@@ -68,6 +83,7 @@ files:
|
|
68
83
|
- appveyor.yml
|
69
84
|
- get_process_mem.gemspec
|
70
85
|
- lib/get_process_mem.rb
|
86
|
+
- lib/get_process_mem/darwin.rb
|
71
87
|
- lib/get_process_mem/version.rb
|
72
88
|
- test/fixtures/heroku-bash-smap
|
73
89
|
- test/fixtures/heroku-bash-status
|
@@ -77,7 +93,7 @@ homepage: https://github.com/schneems/get_process_mem
|
|
77
93
|
licenses:
|
78
94
|
- MIT
|
79
95
|
metadata: {}
|
80
|
-
post_install_message:
|
96
|
+
post_install_message:
|
81
97
|
rdoc_options: []
|
82
98
|
require_paths:
|
83
99
|
- lib
|
@@ -92,9 +108,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
108
|
- !ruby/object:Gem::Version
|
93
109
|
version: '0'
|
94
110
|
requirements: []
|
95
|
-
|
96
|
-
|
97
|
-
signing_key:
|
111
|
+
rubygems_version: 3.1.2
|
112
|
+
signing_key:
|
98
113
|
specification_version: 4
|
99
114
|
summary: Use GetProcessMem to find out the amount of RAM used by any process
|
100
115
|
test_files:
|