rprocfs 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
6
+ *.o
7
+ *.bundle
8
+ Makefile
9
+ .rake*
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Valentin Mihov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,25 @@
1
+ = rprocfs
2
+
3
+ A simple ruby lib for reading process details from the proc filesystem.
4
+
5
+ == Install
6
+ The gem is hosted on gemcutter, so you will have to add the gemcutter to your sources first:
7
+
8
+ gem sources --add http://gemcutter.org
9
+ gem install rprocfs
10
+
11
+ == Usage
12
+
13
+ require 'rubygems'
14
+ require 'rprocfs'
15
+
16
+ puts "User time of the current process: #{RProcFS.utime($$)}"
17
+ puts "Current data memory size of the current process: #{RProcFS.data($$)}"
18
+
19
+ == Portability
20
+
21
+ Currently this is tested only on ubuntu 9.04 system. It should work on most linux systems. Please file an issue if you find any problem.
22
+
23
+ == Copyright
24
+
25
+ Copyright (c) 2009 Valentin Mihov. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "rprocfs"
8
+ gem.summary = %Q{Ruby proc filesystem interface}
9
+ gem.description = %Q{A library, which allows easily reading various process stats from the proc filesystem}
10
+ gem.email = "valentin.mihov@gmail.com"
11
+ gem.homepage = "http://github.com/valo/rprocfs"
12
+ gem.authors = ["Valentin Mihov"]
13
+ gem.extensions << 'ext/rprocfs/extconf.rb'
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/*_test.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ if File.exist?('VERSION')
47
+ version = File.read('VERSION')
48
+ else
49
+ version = ""
50
+ end
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "rprocfs #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.2
@@ -0,0 +1,5 @@
1
+ require 'mkmf'
2
+
3
+ extension_name = "native"
4
+ dir_config(extension_name)
5
+ create_makefile(extension_name)
@@ -0,0 +1,12 @@
1
+ #include <ruby.h>
2
+ #include <unistd.h>
3
+
4
+ VALUE RProcFS = Qnil;
5
+
6
+ void Init_native();
7
+
8
+ void Init_native() {
9
+ RProcFS = rb_define_class("RProcFS", rb_cObject);
10
+ rb_define_const(RProcFS, "CLOCKS_PER_SEC", INT2FIX(sysconf(_SC_CLK_TCK)));
11
+ rb_define_const(RProcFS, "MEMORY_PAGESIZE", INT2FIX(getpagesize()));
12
+ }
@@ -0,0 +1,82 @@
1
+ module RProcFS::Stat
2
+ def self.included(base)
3
+ base.extend ClassMethods
4
+ end
5
+
6
+ module ClassMethods
7
+ STAT_FIELDS = [:pid, :comm, :state, :ppid, :pgrp, :session, :tty_nr, :tpgid, :flags,
8
+ :minflt, :cminflt, :majflt, :cmajflt, :utime, :stime, :cutime, :cstime, :priority,
9
+ :nice, :num_threads, :itrealvalue, :starttime, :vsize, :rss, :rsslim, :startcode,
10
+ :endcode, :startstack, :kstkesp, :kstkeip, :signal, :blocked, :sigignore,
11
+ :sigcatch, :wchan, :nswap, :cnswap, :exit_signal, :processor, :rt_priority,
12
+ :policy, :delayacct_blkio_ticks, :guest_time, :cguest_time]
13
+
14
+ STAT_FIELDS_FORMAT = {:pid => :to_i,
15
+ :comm => lambda { |value| value[1..-2] },
16
+ :state => :to_s,
17
+ :ppid => :to_i,
18
+ :pgrp => :to_i,
19
+ :session => :to_i,
20
+ :tty_nr => :to_i,
21
+ :tpgid => :to_i,
22
+ :flags => :to_i,
23
+ :minflt => :to_i,
24
+ :cminflt => :to_i,
25
+ :majflt => :to_i,
26
+ :cmajflt => :to_i,
27
+ :utime => lambda { |value| value.to_f / RProcFS::CLOCKS_PER_SEC },
28
+ :stime => lambda { |value| value.to_f / RProcFS::CLOCKS_PER_SEC },
29
+ :cutime => :to_i,
30
+ :cstime => :to_i,
31
+ :priority => :to_i,
32
+ :nice => :to_i,
33
+ :num_threads => :to_i,
34
+ :itrealvalue => :to_i,
35
+ :starttime => :to_i,
36
+ :vsize => :to_i,
37
+ :rss => :to_i,
38
+ :rsslim => :to_i,
39
+ :startcode => :to_i,
40
+ :endcode => :to_i,
41
+ :startstack => :to_i,
42
+ :kstkesp => :to_i,
43
+ :kstkeip => :to_i,
44
+ :signal => :to_i,
45
+ :blocked => :to_i,
46
+ :sigignore => :to_i,
47
+ :sigcatch => :to_i,
48
+ :wchan => :to_i,
49
+ :nswap => :to_i,
50
+ :cnswap => :to_i,
51
+ :exit_signal => :to_i,
52
+ :processor => :to_i,
53
+ :rt_priority => :to_i,
54
+ :policy => :to_i,
55
+ :delayacct_blkio_ticks => :to_i,
56
+ :guest_time => :to_i,
57
+ :cguest_time => :to_i}
58
+
59
+ def stat_parse(pid)
60
+ stat = File.read("/proc/#{pid}/stat").chomp.split(" ")
61
+
62
+ result = {}
63
+
64
+ STAT_FIELDS.zip(stat) do |field, value|
65
+ case STAT_FIELDS_FORMAT[field]
66
+ when Symbol
67
+ result[field] = value.send(STAT_FIELDS_FORMAT[field])
68
+ when Proc
69
+ result[field] = STAT_FIELDS_FORMAT[field].call(value)
70
+ end
71
+ end
72
+
73
+ result
74
+ end
75
+
76
+ STAT_FIELDS.each do |field|
77
+ define_method field do |pid|
78
+ stat_parse(pid)[field]
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,34 @@
1
+ module RProcFS::StatM
2
+ def self.included(base)
3
+ base.extend ClassMethods
4
+ end
5
+
6
+ module ClassMethods
7
+ STATM_FIELDS = [:size, :resident, :share, :text, :lib, :data, :dt]
8
+ PAGES_TO_BYTES = lambda { |value| value.to_i * RProcFS::MEMORY_PAGESIZE }
9
+ STATM_FIELDS_FORMAT = STATM_FIELDS.inject({}) { |hash, field| hash[field] = PAGES_TO_BYTES; hash }
10
+
11
+ def statm_parse(pid)
12
+ stat = File.read("/proc/#{pid}/statm").chomp.split(" ")
13
+
14
+ result = {}
15
+
16
+ STATM_FIELDS.zip(stat) do |field, value|
17
+ case STATM_FIELDS_FORMAT[field]
18
+ when Symbol
19
+ result[field] = value.send(STATM_FIELDS_FORMAT[field])
20
+ when Proc
21
+ result[field] = STATM_FIELDS_FORMAT[field].call(value)
22
+ end
23
+ end
24
+
25
+ result
26
+ end
27
+
28
+ STATM_FIELDS.each do |field|
29
+ define_method field do |pid|
30
+ statm_parse(pid)[field]
31
+ end
32
+ end
33
+ end
34
+ end
data/lib/rprocfs.rb ADDED
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'ext', 'rprocfs'))
2
+ require 'native'
3
+
4
+ require 'rprocfs/stat'
5
+ require 'rprocfs/stat_m'
6
+
7
+ class RProcFS
8
+ include RProcFS::Stat
9
+ include RProcFS::StatM
10
+ end
data/rprocfs.gemspec ADDED
@@ -0,0 +1,59 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rprocfs}
8
+ s.version = "0.0.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Valentin Mihov"]
12
+ s.date = %q{2009-10-12}
13
+ s.description = %q{A library, which allows easily reading various process stats from the proc filesystem}
14
+ s.email = %q{valentin.mihov@gmail.com}
15
+ s.extensions = ["ext/rprocfs/extconf.rb"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE",
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ ".gitignore",
23
+ "LICENSE",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "ext/rprocfs/extconf.rb",
28
+ "ext/rprocfs/native.c",
29
+ "lib/rprocfs.rb",
30
+ "lib/rprocfs/stat.rb",
31
+ "lib/rprocfs/stat_m.rb",
32
+ "rprocfs.gemspec",
33
+ "spec/rprocfs/stat.rb",
34
+ "spec/rprocfs/statm.rb",
35
+ "spec/rprocfs_spec.rb",
36
+ "spec/spec_helper.rb"
37
+ ]
38
+ s.homepage = %q{http://github.com/valo/rprocfs}
39
+ s.rdoc_options = ["--charset=UTF-8"]
40
+ s.require_paths = ["lib"]
41
+ s.rubygems_version = %q{1.3.5}
42
+ s.summary = %q{Ruby proc filesystem interface}
43
+ s.test_files = [
44
+ "spec/rprocfs/stat.rb",
45
+ "spec/rprocfs/statm.rb",
46
+ "spec/rprocfs_spec.rb",
47
+ "spec/spec_helper.rb"
48
+ ]
49
+
50
+ if s.respond_to? :specification_version then
51
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
52
+ s.specification_version = 3
53
+
54
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
55
+ else
56
+ end
57
+ else
58
+ end
59
+ end
@@ -0,0 +1,14 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "Stat" do
4
+ it "should parse the info from the stat files" do
5
+ File.stub!(:read).and_return("2452 (imap-login) S 2411 2411 2411 0 -1 4202752 501 0 0 0 20 3 0 0 20 0 1 0 4462 3760128 396 4294967295 3086491648 3086638316 3215609600 3215609124 3086365732 0 0 4096 16386 3223266791 0 0 17 0 0 0 0 0 0")
6
+
7
+ RProcFS.pid(123).should == 2452
8
+ RProcFS.utime(123).should == 20.0 / RProcFS::CLOCKS_PER_SEC
9
+ RProcFS.stime(123).should == 3.0 / RProcFS::CLOCKS_PER_SEC
10
+ RProcFS.comm(123).should == "imap-login"
11
+ RProcFS.state(123).should == "S"
12
+ RProcFS.vsize(123).should == 3760128
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "StatM" do
4
+ it "should parse the info from the stat files" do
5
+ File.stub!(:read).and_return("918 396 329 36 0 74 0")
6
+
7
+ RProcFS.size(123).should == 918 * RProcFS::MEMORY_PAGESIZE
8
+ RProcFS.resident(123).should == 396 * RProcFS::MEMORY_PAGESIZE
9
+ RProcFS.share(123).should == 329 * RProcFS::MEMORY_PAGESIZE
10
+ RProcFS.text(123).should == 36 * RProcFS::MEMORY_PAGESIZE
11
+ RProcFS.lib(123).should == 0
12
+ RProcFS.data(123).should == 74 * RProcFS::MEMORY_PAGESIZE
13
+ RProcFS.dt(123).should == 0
14
+ end
15
+ end
@@ -0,0 +1 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rprocfs'
4
+ require 'rubygems'
5
+ require 'spec'
6
+ require 'spec/autorun'
7
+
8
+ Spec::Runner.configure do |config|
9
+
10
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rprocfs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Valentin Mihov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-12 00:00:00 +03:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A library, which allows easily reading various process stats from the proc filesystem
17
+ email: valentin.mihov@gmail.com
18
+ executables: []
19
+
20
+ extensions:
21
+ - ext/rprocfs/extconf.rb
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - ext/rprocfs/extconf.rb
33
+ - ext/rprocfs/native.c
34
+ - lib/rprocfs.rb
35
+ - lib/rprocfs/stat.rb
36
+ - lib/rprocfs/stat_m.rb
37
+ - rprocfs.gemspec
38
+ - spec/rprocfs/stat.rb
39
+ - spec/rprocfs/statm.rb
40
+ - spec/rprocfs_spec.rb
41
+ - spec/spec_helper.rb
42
+ has_rdoc: true
43
+ homepage: http://github.com/valo/rprocfs
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --charset=UTF-8
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.3.5
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Ruby proc filesystem interface
70
+ test_files:
71
+ - spec/rprocfs/stat.rb
72
+ - spec/rprocfs/statm.rb
73
+ - spec/rprocfs_spec.rb
74
+ - spec/spec_helper.rb