linux_proc_mem 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b711c603c2e49c96540e5c7504e7efc1cb4acf7f
4
+ data.tar.gz: 7d865a3cd96dab21f3880c1892f0bcaf7d47d8b8
5
+ SHA512:
6
+ metadata.gz: 669d96f74dee8c55f79e8d757a7eb00b8d214d5c7e624e07cdbf01ee05910caacfc69a666df7b48c4bd65234580877744e9b9f6943d1f7185965dfc107958452
7
+ data.tar.gz: 302a8adf2e8fa4a18dbc4f0801df87f80df59aae4133417ab4fa76ab8efee5e3935f570a9466c9b3807f8fad302f54c604ebecead99cf684162584db602b91b2
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ proc-mem-gem
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.2
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Klaus Meyer
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # LinuxProcMem
2
+
3
+ Get memory usage of a process including all child-processes (linux only).
4
+
5
+ Uses `get_process_mem` gem for reading the memory from proc filesystem and adds a small layer to handle the child processes.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'linux_proc_mem'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install linux_proc_mem
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ require 'linux_proc_mem'
27
+ pid = 1234
28
+ bytes = LinuxProcMem.get_bytes(pid)
29
+ puts "process #{pid} including childs is using #{bytes} bytes of system memory"
30
+ ```
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it ( https://github.com/klausmeyer/linux_proc_mem/fork )
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,3 @@
1
+ module LinuxProcMem
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,29 @@
1
+ require "linux_proc_mem/version"
2
+ require "get_process_mem"
3
+
4
+ module LinuxProcMem
5
+ extend self
6
+
7
+ def get_bytes(pid)
8
+ file = Pathname.new("/proc/#{pid}/smaps")
9
+ raise "File #{file} not found!" unless File.exists?(file.to_s)
10
+ GetProcessMem.new.linux_memory(file) + child_pids(pid).inject(0) { |sum, cpid| sum += get_child_bytes(cpid) }
11
+ end
12
+
13
+ def child_pids(pid)
14
+ Dir["/proc/#{pid}/task/*/children"].map { |file| pids_from_file(file) }.flatten
15
+ end
16
+
17
+ private
18
+
19
+ # NOTE: This method is used as mock target to prevent
20
+ # too deep stack level during tests :)
21
+ def get_child_bytes(pid)
22
+ get_bytes(pid)
23
+ end
24
+
25
+ def pids_from_file(file)
26
+ File.read(file).split(/\s/).map(&:to_i)
27
+ end
28
+ end
29
+
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'linux_proc_mem/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "linux_proc_mem"
8
+ spec.version = LinuxProcMem::VERSION
9
+ spec.authors = ["Klaus Meyer"]
10
+ spec.email = ["spam@klaus-meyer.net"]
11
+ spec.summary = "Get full memory usage of processes on linux"
12
+ spec.description = "Get memory usage of processes including their child processes.\n" +
13
+ "Works on Linux only."
14
+ spec.homepage = "https://github.com/klausmeyer/linux_proc_mem"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "get_process_mem", "~> 0.1"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.7"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.1"
27
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe LinuxProcMem do
4
+ let(:pid) { 1234 }
5
+ let(:child_pids) { [1235, 1236, 1237, 1238] }
6
+
7
+ describe '.get_bytes(pid)' do
8
+ let(:process_mem) { double('GetProcessMem', linux_memory: 500.0) }
9
+
10
+ before do
11
+ allow(File).to receive(:exists?) { true }
12
+ allow(GetProcessMem).to receive(:new) { process_mem }
13
+ allow(LinuxProcMem).to receive(:child_pids) { child_pids }
14
+ allow(LinuxProcMem).to receive(:get_child_bytes) { 500.0 }
15
+ end
16
+
17
+ it 'returns the memory usage of pid and all childs in bytes' do
18
+ expect(LinuxProcMem.get_bytes(pid).to_f).to eq 2500.0
19
+ end
20
+ end
21
+
22
+ describe '.child_pids(pid)' do
23
+ context 'when the proc file exists' do
24
+ before do
25
+ allow(Dir).to receive(:[]) { ["/proc/#{pid}/task/#{pid}/children"] }
26
+ allow(File).to receive(:read) { child_pids.join(' ') }
27
+ end
28
+
29
+ it 'returns an array of child pids' do
30
+ expect(LinuxProcMem.child_pids(pid)).to eq child_pids
31
+ end
32
+ end
33
+
34
+ context 'when the proc file does not exists' do
35
+ before do
36
+ allow(Dir).to receive(:[]) { [] }
37
+ end
38
+
39
+ it 'returns an empty array' do
40
+ expect(LinuxProcMem.child_pids(pid)).to eq []
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,11 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+ Bundler.require(:default, :development, :test)
4
+
5
+ RSpec.configure do |config|
6
+ # Run specs in random order to surface order dependencies. If you find an
7
+ # order dependency and want to debug it, you can fix the order by providing
8
+ # the seed, which is printed after each run.
9
+ # --seed 1234
10
+ config.order = 'random'
11
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: linux_proc_mem
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Klaus Meyer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: get_process_mem
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.1'
69
+ description: |-
70
+ Get memory usage of processes including their child processes.
71
+ Works on Linux only.
72
+ email:
73
+ - spam@klaus-meyer.net
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".ruby-gemset"
81
+ - ".ruby-version"
82
+ - Gemfile
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - lib/linux_proc_mem.rb
87
+ - lib/linux_proc_mem/version.rb
88
+ - linux_proc_mem.gemspec
89
+ - spec/linux_proc_mem_spec.rb
90
+ - spec/spec_helper.rb
91
+ homepage: https://github.com/klausmeyer/linux_proc_mem
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.2.2
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Get full memory usage of processes on linux
115
+ test_files:
116
+ - spec/linux_proc_mem_spec.rb
117
+ - spec/spec_helper.rb