get_process_mem 0.2.7 → 1.0.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
  SHA256:
3
- metadata.gz: 06a96e8d978fd95bcf546a1e5e8bbc01cc10635d50b2f87639eb9c1f8c7f4f76
4
- data.tar.gz: 6c941ae4f0301ff698b9be4ba75597537ed3ae5a11bf9295ab81144ef8435047
3
+ metadata.gz: '0497c51fa11f95206b380559a93991c3d22bc7b42c3c50e6c2455412bd518357'
4
+ data.tar.gz: 41f4d4dfce94cdabf6ec78a54665924867efd3c7b014693b75166793f8ae607b
5
5
  SHA512:
6
- metadata.gz: 3cdb2baad08676298249f006b12f0c61bd0fdc1e13902211e2e0c3ea965c39fee2edb2ad3d2ceb617dcf450738e765c60c7e748830d85c52b044fa0ad8a3c8db
7
- data.tar.gz: 35d717cc39f85a902adaa8ea6028d969b40b89465bcc0d1578de4e97308fe890e77ce9ff06b68855253030b3ab26fa130e2e44be06b041368a39a4cb4bc300f8
6
+ metadata.gz: 139c6ab3e913d199c8aa64fdfb4525f63e619e19f2410376bb0b19cf25930cfa849a3e1cd3f540363801bafa97c54cef75c720d7bdb5a701b5a732cf65797c13
7
+ data.tar.gz: 9d9c4d60f2fc78cfb2e3977857686286c6327cb32bf9e0ba7d4bafb4edb9fb76461ad11305d327aed2eacea437488622fff7a9a3b40ba55c14a0872832e55fb2
@@ -0,0 +1,33 @@
1
+ name: CI
2
+
3
+ on:
4
+ - push
5
+ - pull_request
6
+
7
+ jobs:
8
+ test:
9
+ runs-on: ubuntu-latest
10
+ strategy:
11
+ fail-fast: false
12
+ matrix:
13
+ ruby:
14
+ - 2.6
15
+ - 2.7
16
+ - 3.0
17
+ - 3.1
18
+ - 3.2
19
+ - 3.3
20
+ - head
21
+ steps:
22
+ - name: Checkout code
23
+ uses: actions/checkout@v4
24
+ - name: Set up Ruby
25
+ uses: ruby/setup-ruby@v1
26
+ with:
27
+ ruby-version: ${{ matrix.ruby }}
28
+ bundler-cache: true
29
+ - name: Ruby linting
30
+ run: bundle exec standardrb
31
+ - name: test
32
+ run: bundle exec rake test
33
+ continue-on-error: ${{ matrix.ruby == 'head' }}
data/.standard.yml ADDED
@@ -0,0 +1 @@
1
+ ruby_version: 2.6
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  ## HEAD (unreleased)
2
2
 
3
+ ## 1.0.0
4
+
5
+ - Ensure PID is an integer (https://github.com/schneems/get_process_mem/pull/46)
6
+ - Minimum Ruby version is now Ruby 2.6
7
+ - Add `bigdecimal` as a dependency.
8
+
3
9
  ## 0.2.7
4
10
 
5
11
  - Native (faster) support for returning memory from different PIDs on mac (https://github.com/schneems/get_process_mem/pull/42)
data/Gemfile CHANGED
@@ -1,4 +1,6 @@
1
1
  source "https://rubygems.org"
2
- gem 'sys-proctable'
2
+ gem "sys-proctable"
3
3
 
4
4
  gemspec
5
+
6
+ gem "standard"
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Get Process Memory
2
2
 
3
- [![Build Status](https://travis-ci.org/schneems/get_process_mem.svg?branch=master)](https://travis-ci.org/schneems/get_process_mem)
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)
3
+ [![CI](https://github.com/zombocom/get_process_mem/actions/workflows/ci.yml/badge.svg)](https://github.com/zombocom/get_process_mem/actions/workflows/ci.yml)
4
+ [![Help Contribute to Open Source](https://www.codetriage.com/zombocom/get_process_mem/badges/users.svg)](https://www.codetriage.com/zombocom/get_process_mem)
5
5
 
6
6
  Do you need to get the memory usage of a process? Great because this library does that.
7
7
 
data/Rakefile CHANGED
@@ -1,15 +1,13 @@
1
- # encoding: UTF-8
1
+ require "bundler/gem_tasks"
2
2
 
3
- require 'bundler/gem_tasks'
3
+ require "rake"
4
+ require "rake/testtask"
4
5
 
5
- require 'rake'
6
- require 'rake/testtask'
6
+ task default: [:test]
7
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'
8
+ Rake::TestTask.new(:test) do |t|
9
+ t.libs << "lib"
10
+ t.libs << "test"
11
+ t.pattern = "test/**/*_test.rb"
14
12
  t.verbose = false
15
13
  end
@@ -1,24 +1,25 @@
1
- # -*- encoding: utf-8 -*-
2
- lib = File.expand_path('../lib', __FILE__)
1
+ lib = File.expand_path("../lib", __FILE__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'get_process_mem/version'
3
+ require "get_process_mem/version"
5
4
 
6
5
  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"
6
+ gem.name = "get_process_mem"
7
+ gem.version = GetProcessMem::VERSION
8
+ gem.authors = ["Richard Schneeman"]
9
+ gem.email = ["richard.schneeman+rubygems@gmail.com"]
10
+ gem.description = " Get memory usage of a process in Ruby "
11
+ gem.summary = " Use GetProcessMem to find out the amount of RAM used by any process "
12
+ gem.homepage = "https://github.com/schneems/get_process_mem"
13
+ gem.license = "MIT"
15
14
 
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)/})
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
19
17
  gem.require_paths = ["lib"]
20
18
 
19
+ gem.required_ruby_version = ">= 2.6"
20
+
21
21
  gem.add_dependency "ffi", "~> 1.0"
22
+ gem.add_dependency "bigdecimal", ">= 2.0"
22
23
 
23
24
  gem.add_development_dependency "sys-proctable", "~> 1.2"
24
25
  gem.add_development_dependency "rake", "~> 12"
@@ -1,46 +1,42 @@
1
- require 'ffi'
1
+ require "ffi"
2
2
 
3
3
  class GetProcessMem
4
4
  class Darwin
5
5
  extend FFI::Library
6
- ffi_lib 'proc'
7
-
6
+ ffi_lib "proc"
8
7
 
9
8
  class TaskInfo < FFI::Struct
10
9
  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
-
10
+ :pti_resident_size, :uint64,
11
+ :pti_total_user, :uint64,
12
+ :pti_total_system, :uint64,
13
+ :pti_threads_user, :uint64,
14
+ :pti_threads_system, :uint64,
15
+ :pti_policy, :int32,
16
+ :pti_faults, :int32,
17
+ :pti_pageins, :int32,
18
+ :pti_cow_faults, :int32,
19
+ :pti_messages_sent, :int32,
20
+ :pti_messages_received, :int32,
21
+ :pti_syscalls_mach, :int32,
22
+ :pti_syscalls_unix, :int32,
23
+ :pti_csw, :int32,
24
+ :pti_threadnum, :int32,
25
+ :pti_numrunning, :int32,
26
+ :pti_priority, :int32
29
27
  end
30
28
 
31
-
32
29
  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
-
30
+ [
31
+ :int, # pid
32
+ :int, # flavour
33
+ :uint64, # arg, not needed for this selector
34
+ TaskInfo.by_ref, # output buffer
35
+ :int # size of buffer
36
+ ],
37
+ :int
42
38
 
43
- PROC_PIDTASKINFO = 4 #from sys/proc_info.h
39
+ PROC_PIDTASKINFO = 4 # from sys/proc_info.h
44
40
 
45
41
  class << self
46
42
  def resident_size(pid)
@@ -55,7 +51,7 @@ class GetProcessMem
55
51
  if result == TaskInfo.size
56
52
  data
57
53
  else
58
- raise SystemCallError.new("proc_pidinfo returned #{result}", FFI.errno);
54
+ raise SystemCallError.new("proc_pidinfo returned #{result}", FFI.errno)
59
55
  end
60
56
  end
61
57
  end
@@ -1,3 +1,3 @@
1
1
  class GetProcessMem
2
- VERSION = "0.2.7"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -1,5 +1,5 @@
1
- require 'pathname'
2
- require 'bigdecimal'
1
+ require "pathname"
2
+ require "bigdecimal"
3
3
 
4
4
  # Cribbed from Unicorn Worker Killer, thanks!
5
5
  class GetProcessMem
@@ -14,15 +14,15 @@ class GetProcessMem
14
14
  KB_TO_BYTE = number_to_bigdecimal 1024 # 2**10 = 1024
15
15
  MB_TO_BYTE = number_to_bigdecimal 1_048_576 # 1024**2 = 1_048_576
16
16
  GB_TO_BYTE = number_to_bigdecimal 1_073_741_824 # 1024**3 = 1_073_741_824
17
- CONVERSION = { "kb" => KB_TO_BYTE, "mb" => MB_TO_BYTE, "gb" => GB_TO_BYTE }
18
- ROUND_UP = number_to_bigdecimal "0.5"
17
+ CONVERSION = {"kb" => KB_TO_BYTE, "mb" => MB_TO_BYTE, "gb" => GB_TO_BYTE}
18
+ ROUND_UP = number_to_bigdecimal "0.5"
19
19
  attr_reader :pid
20
20
 
21
21
  RUNS_ON_WINDOWS = Gem.win_platform?
22
22
 
23
23
  if RUNS_ON_WINDOWS
24
24
  begin
25
- require 'sys/proctable'
25
+ require "sys/proctable"
26
26
  rescue LoadError => e
27
27
  message = "Please add `sys-proctable` to your Gemfile for windows machines\n"
28
28
  message << e.message
@@ -31,13 +31,13 @@ class GetProcessMem
31
31
  include Sys
32
32
  end
33
33
 
34
- RUNS_ON_DARWIN = Gem.platforms.detect do |p|
35
- p.is_a?(Gem::Platform) && p.os == 'darwin'
36
- end
34
+ RUNS_ON_DARWIN = Gem.platforms.detect do |p|
35
+ p.is_a?(Gem::Platform) && p.os == "darwin"
36
+ end
37
37
 
38
38
  if RUNS_ON_DARWIN
39
39
  begin
40
- require 'get_process_mem/darwin'
40
+ require "get_process_mem/darwin"
41
41
  rescue LoadError => e
42
42
  message = "Please add `ffi` to your Gemfile for darwin (macos) machines\n"
43
43
  message << e.message
@@ -46,10 +46,10 @@ class GetProcessMem
46
46
  end
47
47
 
48
48
  def initialize(pid = Process.pid)
49
- @status_file = Pathname.new "/proc/#{pid}/status"
49
+ @status_file = Pathname.new "/proc/#{pid}/status"
50
50
  @process_file = Pathname.new "/proc/#{pid}/smaps"
51
- @pid = pid
52
- @linux = @status_file.exist?
51
+ @pid = Integer(pid)
52
+ @linux = @status_file.exist?
53
53
  end
54
54
 
55
55
  def linux?
@@ -57,32 +57,33 @@ class GetProcessMem
57
57
  end
58
58
 
59
59
  def bytes
60
- memory = linux_status_memory if linux?
60
+ memory = linux_status_memory if linux?
61
61
  memory ||= darwin_memory if RUNS_ON_DARWIN
62
62
  memory ||= ps_memory
63
+ memory
63
64
  end
64
65
 
65
66
  def kb(b = bytes)
66
- (b/KB_TO_BYTE).to_f
67
+ (b / KB_TO_BYTE).to_f
67
68
  end
68
69
 
69
70
  def mb(b = bytes)
70
- (b/MB_TO_BYTE).to_f
71
+ (b / MB_TO_BYTE).to_f
71
72
  end
72
73
 
73
74
  def gb(b = bytes)
74
- (b/GB_TO_BYTE).to_f
75
+ (b / GB_TO_BYTE).to_f
75
76
  end
76
77
 
77
78
  def inspect
78
79
  b = bytes
79
- "#<#{self.class}:0x%08x @mb=#{ mb b } @gb=#{ gb b } @kb=#{ kb b } @bytes=#{b}>" % (object_id * 2)
80
+ "#<#{self.class}:0x%08x @mb=#{mb b} @gb=#{gb b} @kb=#{kb b} @bytes=#{b}>" % (object_id * 2)
80
81
  end
81
82
 
82
83
  # linux stores memory info in a file "/proc/#{pid}/status"
83
84
  # If it's available it uses less resources than shelling out to ps
84
85
  def linux_status_memory(file = @status_file)
85
- line = file.each_line.detect {|line| line.start_with? 'VmRSS'.freeze }
86
+ line = file.each_line.detect { |line| line.start_with? "VmRSS".freeze }
86
87
  return unless line
87
88
  return unless (_name, value, unit = line.split(nil)).length == 3
88
89
  CONVERSION[unit.downcase!] * value.to_i
@@ -92,13 +93,13 @@ class GetProcessMem
92
93
 
93
94
  # linux stores detailed memory info in a file "/proc/#{pid}/smaps"
94
95
  def linux_memory(file = @process_file)
95
- lines = file.each_line.select {|line| line.match(/^Rss/) }
96
+ lines = file.each_line.select { |line| line.match(/^Rss/) }
96
97
  return if lines.empty?
97
98
  lines.reduce(0) do |sum, line|
98
- line.match(/(?<value>(\d*\.{0,1}\d+))\s+(?<unit>\w\w)/) do |m|
99
+ line.match(/(?<value>\d*\.{0,1}\d+)\s+(?<unit>\w\w)/) do |m|
99
100
  value = number_to_bigdecimal(m[:value]) + ROUND_UP
100
- unit = m[:unit].downcase
101
- sum += CONVERSION[unit] * value
101
+ unit = m[:unit].downcase
102
+ sum += CONVERSION[unit] * value
102
103
  end
103
104
  sum
104
105
  end
@@ -114,7 +115,7 @@ class GetProcessMem
114
115
  number_to_bigdecimal(size)
115
116
  else
116
117
  mem = `ps -o rss= -p #{pid}`
117
- KB_TO_BYTE * number_to_bigdecimal(mem == "" ? 0 : mem)
118
+ KB_TO_BYTE * number_to_bigdecimal((mem == "") ? 0 : mem)
118
119
  end
119
120
  end
120
121
 
@@ -1,4 +1,4 @@
1
- require 'test_helper'
1
+ require "test_helper"
2
2
 
3
3
  class GetProcessMemTest < Test::Unit::TestCase
4
4
  def setup
@@ -11,14 +11,23 @@ class GetProcessMemTest < Test::Unit::TestCase
11
11
  other_mem = GetProcessMem.new(pid)
12
12
  assert @mem.kb > other_mem.kb
13
13
  ensure
14
- Process.kill('TERM', pid) if pid
14
+ Process.kill("TERM", pid) if pid
15
15
  Process.wait(pid) if pid
16
16
  end
17
17
 
18
+ def test_invalid_pid
19
+ raised_exception = false
20
+ GetProcessMem.new("ls")
21
+ rescue ArgumentError
22
+ raised_exception = true
23
+ ensure
24
+ assert raised_exception
25
+ end
26
+
18
27
  def test_seems_to_work
19
- assert @mem.kb > 0
20
- assert @mem.mb > 0
21
- assert @mem.gb > 0
28
+ assert @mem.kb > 0
29
+ assert @mem.mb > 0
30
+ assert @mem.gb > 0
22
31
  assert @mem.bytes > 0
23
32
  end
24
33
 
@@ -36,26 +45,26 @@ class GetProcessMemTest < Test::Unit::TestCase
36
45
  def test_conversions
37
46
  bytes = 0
38
47
  delta = BigDecimal("0.0000001")
39
- assert_in_delta 0.0, @mem.kb(bytes), delta
40
- assert_in_delta 0.0, @mem.mb(bytes), delta
41
- assert_in_delta 0.0, @mem.gb(bytes), delta
48
+ assert_in_delta 0.0, @mem.kb(bytes), delta
49
+ assert_in_delta 0.0, @mem.mb(bytes), delta
50
+ assert_in_delta 0.0, @mem.gb(bytes), delta
42
51
 
43
52
  # kb
44
53
  bytes = 1024
45
- assert_in_delta 1.0, @mem.kb(bytes), delta
46
- assert_in_delta 0.0009765625, @mem.mb(bytes), delta
47
- assert_in_delta 9.5367431640625e-07, @mem.gb(bytes), delta
54
+ assert_in_delta 1.0, @mem.kb(bytes), delta
55
+ assert_in_delta 0.0009765625, @mem.mb(bytes), delta
56
+ assert_in_delta 9.5367431640625e-07, @mem.gb(bytes), delta
48
57
 
49
58
  # mb
50
59
  bytes = 1_048_576
51
- assert_in_delta 1024.0, @mem.kb(bytes), delta
52
- assert_in_delta 1.0, @mem.mb(bytes), delta
53
- assert_in_delta 0.0009765625, @mem.gb(bytes), delta
60
+ assert_in_delta 1024.0, @mem.kb(bytes), delta
61
+ assert_in_delta 1.0, @mem.mb(bytes), delta
62
+ assert_in_delta 0.0009765625, @mem.gb(bytes), delta
54
63
 
55
64
  # gb
56
65
  bytes = 1_073_741_824
57
- assert_in_delta 1048576.0, @mem.kb(bytes), delta
58
- assert_in_delta 1024.0, @mem.mb(bytes), delta
59
- assert_in_delta 1.0, @mem.gb(bytes), delta
66
+ assert_in_delta 1048576.0, @mem.kb(bytes), delta
67
+ assert_in_delta 1024.0, @mem.mb(bytes), delta
68
+ assert_in_delta 1.0, @mem.gb(bytes), delta
60
69
  end
61
70
  end
data/test/test_helper.rb CHANGED
@@ -1,10 +1,10 @@
1
1
  Bundler.require
2
2
 
3
- require 'get_process_mem'
4
- require 'test/unit'
3
+ require "get_process_mem"
4
+ require "test/unit"
5
5
 
6
6
  def fixture_path(name = nil)
7
7
  path = Pathname.new(File.expand_path("../fixtures", __FILE__))
8
8
  return path.join(name) if name
9
9
  path
10
- end
10
+ 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.2.7
4
+ version: 1.0.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: 2020-08-27 00:00:00.000000000 Z
11
+ date: 2024-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bigdecimal
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: sys-proctable
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -74,7 +88,9 @@ extensions: []
74
88
  extra_rdoc_files: []
75
89
  files:
76
90
  - ".github/workflows/check_changelog.yml"
91
+ - ".github/workflows/ci.yml"
77
92
  - ".gitignore"
93
+ - ".standard.yml"
78
94
  - ".travis.yml"
79
95
  - CHANGELOG.md
80
96
  - Gemfile
@@ -101,19 +117,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
101
117
  requirements:
102
118
  - - ">="
103
119
  - !ruby/object:Gem::Version
104
- version: '0'
120
+ version: '2.6'
105
121
  required_rubygems_version: !ruby/object:Gem::Requirement
106
122
  requirements:
107
123
  - - ">="
108
124
  - !ruby/object:Gem::Version
109
125
  version: '0'
110
126
  requirements: []
111
- rubygems_version: 3.1.2
127
+ rubygems_version: 3.5.9
112
128
  signing_key:
113
129
  specification_version: 4
114
130
  summary: Use GetProcessMem to find out the amount of RAM used by any process
115
- test_files:
116
- - test/fixtures/heroku-bash-smap
117
- - test/fixtures/heroku-bash-status
118
- - test/get_process_mem_test.rb
119
- - test/test_helper.rb
131
+ test_files: []