commit 9897ae9e22d68787d366773c14b0063996ffccde Author: Loic Nageleisen Date: Fri Jul 31 12:14:37 2015 +0200 initial commit diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..49e1b72 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,2 @@ +PerlBackrefs: + Enabled: false diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..c64aa4d --- /dev/null +++ b/.travis.yml @@ -0,0 +1,7 @@ +language: ruby +rvm: + - '2.1' + - '2.2' +script: + - bundle exec rubocop + - bundle exec ruby -e'load "lib/meminfo.rb"; Process.rss; ObjectSpace.memsize' diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..fa75df1 --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org' + +gemspec diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..225832b --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,40 @@ +PATH + remote: . + specs: + meminfo (0.1.0) + +GEM + remote: https://rubygems.org/ + specs: + ast (2.0.0) + astrolabe (1.3.1) + parser (~> 2.2) + coderay (1.1.0) + method_source (0.8.2) + parser (2.2.2.6) + ast (>= 1.1, < 3.0) + powerpack (0.1.1) + pry (0.10.1) + coderay (~> 1.1.0) + method_source (~> 0.8.1) + slop (~> 3.4) + rainbow (2.0.0) + rubocop (0.32.1) + astrolabe (~> 1.3) + parser (>= 2.2.2.5, < 3.0) + powerpack (~> 0.1) + rainbow (>= 1.99.1, < 3.0) + ruby-progressbar (~> 1.4) + ruby-progressbar (1.7.5) + slop (3.6.0) + +PLATFORMS + ruby + +DEPENDENCIES + meminfo! + pry + rubocop + +BUNDLED WITH + 1.10.6 diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..d8a9d9c --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,20 @@ +Copyright (c) 2014 Loic Nageleisen + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/lib/meminfo.rb b/lib/meminfo.rb new file mode 100644 index 0000000..d17d735 --- /dev/null +++ b/lib/meminfo.rb @@ -0,0 +1,23 @@ +require 'objspace' +require 'meminfo/ext' + +# MemInfo gives a handful of methods allowing inspection of process memory use. +module MemInfo + module_function + + class ProcessNotFound < StandardError; end + + def rss(pid = Process.pid) + Integer(ps_rss(pid)) * 1024 + rescue TypeError + raise ProcessNotFound, pid + end + + def memsize + ObjectSpace.each_object.reduce(0) { |a, e| a + ObjectSpace.memsize_of(e) } + end + + private def ps_rss(pid) + `ps ax -o rss,pid`.each_line.grep(/^\s*(\d+)\s+#{pid}$/) { $1 }.first + end +end diff --git a/lib/meminfo/ext.rb b/lib/meminfo/ext.rb new file mode 100644 index 0000000..8d2bea5 --- /dev/null +++ b/lib/meminfo/ext.rb @@ -0,0 +1,11 @@ +Process.instance_eval do + def rss(pid = Process.pid) + MemInfo.rss(pid) + end +end + +ObjectSpace.instance_eval do + def memsize + MemInfo.memsize + end +end diff --git a/meminfo.gemspec b/meminfo.gemspec new file mode 100644 index 0000000..5d07bec --- /dev/null +++ b/meminfo.gemspec @@ -0,0 +1,14 @@ +Gem::Specification.new do |s| + s.name = 'meminfo' + s.version = '0.1.0' + s.licenses = ['MIT'] + s.summary = 'Obtain process memory information' + s.description = 'Obtain process memory information' + s.authors = ['Loic Nageleisen'] + s.email = 'loic.nageleisen@gmail.com' + s.files = ['lib/meminfo.rb', 'lib/meminfo/ext.rb'] + s.homepage = 'https://github.com/lloeki/meminfo' + + s.add_development_dependency 'pry' + s.add_development_dependency 'rubocop' +end