sys-proctable 0.9.1-universal-mingw32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,175 @@
1
+ require 'rake'
2
+ require 'rake/clean'
3
+ require 'rake/testtask'
4
+ require 'rbconfig'
5
+ include Config
6
+
7
+ CLEAN.include(
8
+ '**/*.core', # Core dump files
9
+ '**/*.gem', # Gem files
10
+ '**/*.rbc', # Rubinius
11
+ '**/*.o', # C object file
12
+ '**/*.log', # Ruby extension build log
13
+ '**/Makefile', # C Makefile
14
+ '**/conftest.dSYM', # OS X build directory
15
+ "**/*.#{CONFIG['DLEXT']}" # C shared object
16
+ )
17
+
18
+ desc 'Build the sys-proctable library for C versions of sys-proctable'
19
+ task :build => [:clean] do
20
+ case Config::CONFIG['host_os']
21
+ when /bsd/i
22
+ dir = 'ext/bsd'
23
+ when /darwin/i
24
+ dir = 'ext/darwin'
25
+ when /hpux/i
26
+ dir = 'ext/hpux'
27
+ end
28
+
29
+ unless Config::CONFIG['host_os'] =~ /win32|mswin|dos|cygwin|mingw|windows|linux|sunos|solaris/i
30
+ Dir.chdir(dir) do
31
+ ruby 'extconf.rb'
32
+ sh 'make'
33
+ cp 'proctable.' + Config::CONFIG['DLEXT'], 'sys'
34
+ end
35
+ end
36
+ end
37
+
38
+ desc 'Install the sys-proctable library'
39
+ task :install => [:build] do
40
+ file = nil
41
+ dir = File.join(Config::CONFIG['sitelibdir'], 'sys')
42
+
43
+ Dir.mkdir(dir) unless File.exists?(dir)
44
+
45
+ case Config::CONFIG['host_os']
46
+ when /mswin|win32|msdos|cygwin|mingw|windows/i
47
+ file = 'lib/windows/sys/proctable.rb'
48
+ when /linux/i
49
+ file = 'lib/linux/sys/proctable.rb'
50
+ when /sunos|solaris/i
51
+ file = 'lib/sunos/sys/proctable.rb'
52
+ when /bsd/i
53
+ Dir.chdir('ext/bsd'){ sh 'make install' }
54
+ when /darwin/i
55
+ Dir.chdir('ext/darwin'){ sh 'make install' }
56
+ when /hpux/i
57
+ Dir.chdir('ext/hpux'){ sh 'make install' }
58
+ end
59
+
60
+ cp(file, dir, :verbose => true) if file
61
+ end
62
+
63
+ desc 'Uninstall the sys-proctable library'
64
+ task :uninstall do
65
+ case Config::CONFIG['host_os']
66
+ when /win32|mswin|dos|cygwin|mingw|windows|linux|sunos|solaris/i
67
+ dir = File.join(Config::CONFIG['sitelibdir'], 'sys')
68
+ file = File.join(dir, 'proctable.rb')
69
+ else
70
+ dir = File.join(Config::CONFIG['sitearchdir'], 'sys')
71
+ file = File.join(dir, 'proctable.' + Config::CONFIG['DLEXT'])
72
+ end
73
+
74
+ rm(file)
75
+ end
76
+
77
+ desc 'Run the benchmark suite'
78
+ task :bench => [:build] do
79
+ sh "ruby -Ilib benchmarks/bench_ps.rb"
80
+ end
81
+
82
+ desc 'Run the example program'
83
+ task :example => [:build] do
84
+ sh 'ruby -Ilib -Iext examples/example_ps.rb'
85
+ end
86
+
87
+ desc 'Run the test suite'
88
+ Rake::TestTask.new do |t|
89
+ task :test => :build
90
+ t.libs << 'test' << '.'
91
+
92
+ case Config::CONFIG['host_os']
93
+ when /mswin|msdos|cygwin|mingw|windows/i
94
+ t.test_files = FileList['test/test_sys_proctable_windows.rb']
95
+ t.libs << 'lib/windows'
96
+ when /linux/i
97
+ t.test_files = FileList['test/test_sys_proctable_linux.rb']
98
+ t.libs << 'lib/linux'
99
+ when /sunos|solaris/i
100
+ t.test_files = FileList['test/test_sys_proctable_sunos.rb']
101
+ t.libs << 'lib/sunos'
102
+ when /darwin/i
103
+ t.libs << 'ext/darwin'
104
+ t.test_files = FileList['test/test_sys_proctable_darwin.rb']
105
+ when /bsd/i
106
+ t.libs << 'ext/bsd'
107
+ t.test_files = FileList['test/test_sys_proctable_bsd.rb']
108
+ when /hpux/i
109
+ t.libs << 'ext/hpux'
110
+ t.test_files = FileList['test/test_sys_proctable_hpux.rb']
111
+ end
112
+ end
113
+
114
+ namespace :gem do
115
+ desc 'Create a gem'
116
+ task :create => [:clean] do
117
+ spec = eval(IO.read('sys-proctable.gemspec'))
118
+
119
+ # I've had to manually futz with the spec here in some cases
120
+ # in order to get the universal platform settings I want because
121
+ # of some bugginess in Rubygems' platform.rb.
122
+ #
123
+ case Config::CONFIG['host_os']
124
+ when /bsd/i
125
+ spec.platform = Gem::Platform.new('universal-bsd')
126
+ spec.files << 'ext/bsd/sys/proctable.c'
127
+ spec.extra_rdoc_files << 'ext/bsd/sys/proctable.c'
128
+ spec.test_files << 'test/test_sys_proctable_bsd.rb'
129
+ spec.extensions = ['ext/bsd/extconf.rb']
130
+ when /darwin/i
131
+ spec.platform = Gem::Platform.new('universal-darwin')
132
+ spec.files << 'ext/darwin/sys/proctable.c'
133
+ spec.extra_rdoc_files << 'ext/darwin/sys/proctable.c'
134
+ spec.test_files << 'test/test_sys_proctable_darwin.rb'
135
+ spec.extensions = ['ext/darwin/extconf.rb']
136
+ when /hpux/i
137
+ spec.platform = Gem::Platform.new('universal-hpux')
138
+ spec.files << 'ext/hpux/sys/proctable.c'
139
+ spec.extra_rdoc_files << 'ext/hpux/sys/proctable.c'
140
+ spec.test_files << 'test/test_sys_proctable_hpux.rb'
141
+ spec.extensions = ['ext/hpux/extconf.rb']
142
+ when /linux/i
143
+ spec.platform = Gem::Platform.new('universal-linux')
144
+ spec.require_paths = ['lib', 'lib/linux']
145
+ spec.files += ['lib/linux/sys/proctable.rb']
146
+ spec.test_files << 'test/test_sys_proctable_linux.rb'
147
+ when /sunos|solaris/i
148
+ spec.platform = Gem::Platform.new('universal-solaris10.0')
149
+ spec.platform.version = nil
150
+ spec.require_paths = ['lib', 'lib/sunos']
151
+ spec.files += ['lib/sunos/sys/proctable.rb']
152
+ spec.test_files << 'test/test_sys_proctable_sunos.rb'
153
+ when /mswin|win32|dos|cygwin|mingw|windows/i
154
+ spec.platform = Gem::Platform::CURRENT
155
+ spec.platform.cpu = 'universal'
156
+ spec.platform.version = nil
157
+ spec.require_paths = ['lib', 'lib/windows']
158
+ spec.files += ['lib/windows/sys/proctable.rb']
159
+ spec.test_files << 'test/test_sys_proctable_windows.rb'
160
+ end
161
+
162
+ # https://github.com/rubygems/rubygems/issues/147
163
+ spec.original_platform = spec.platform
164
+
165
+ Gem::Builder.new(spec).build
166
+ end
167
+
168
+ desc 'Install the sys-proctable library as a gem'
169
+ task :install => [:create] do
170
+ gem_name = Dir['*.gem'].first
171
+ sh "gem install #{gem_name}"
172
+ end
173
+ end
174
+
175
+ task :default => :test
@@ -0,0 +1,21 @@
1
+ ########################################################################
2
+ # bench_ps.rb
3
+ #
4
+ # Benchmark program to show overall speed and compare the block form
5
+ # versus the non-block form. You should run this benchmark via the
6
+ # 'rake bench' Rake task.
7
+ ########################################################################
8
+ require 'benchmark'
9
+ require 'sys/proctable'
10
+
11
+ MAX = 10
12
+
13
+ Benchmark.bm do |bench|
14
+ bench.report("Block form"){
15
+ MAX.times{ Sys::ProcTable.ps{} }
16
+ }
17
+
18
+ bench.report("Non-block form"){
19
+ MAX.times{ Sys::ProcTable.ps }
20
+ }
21
+ end
@@ -0,0 +1,47 @@
1
+ = Description
2
+ A simple 'top' interface for Ruby
3
+
4
+ = Prerequisites
5
+ Requires the "sys/proctable" package (which should be installed along
6
+ with this package).
7
+
8
+ = Synopsis
9
+ require "sys/top"
10
+
11
+ Sys::Top.top(5).each{ |ps|
12
+ p ps
13
+ }
14
+
15
+ = Constants
16
+ VERSION
17
+ Returns the version number of this package as a String.
18
+
19
+ = Class Methods
20
+ Sys::Top.top(number=10, field="pctcpu")
21
+ Returns an array of ProcTableStruct's. The size of the array (i.e. the
22
+ number of processes) that it returns is based on +number+, and sorted by
23
+ +pctcpu+. By default, the size and field values are 10 and "pctcpu",
24
+ respectively.
25
+
26
+ = Notes
27
+ Not all fields are available on all platforms. Please check your
28
+ platform specific documentation for which fields are available.
29
+
30
+ = Bugs
31
+ None that I'm aware of. Please log bug reports on the project page at
32
+ http://www.rubyforge.org/projects/sysutils
33
+
34
+ = License
35
+ Artistic 2.0
36
+
37
+ = Copyright
38
+ (C) 2004-2009 Daniel J. Berger
39
+ All Rights Reserved.
40
+
41
+ = Warranty
42
+ This package is provided "as is" and without any express or
43
+ implied warranties, including, without limitation, the implied
44
+ warranties of merchantability and fitness for a particular purpose.
45
+
46
+ = Author
47
+ Daniel J. Berger
@@ -0,0 +1,20 @@
1
+ #######################################################################
2
+ # example_ps.rb
3
+ #
4
+ # Generic test program that demonstrates the use of ProcTable.ps. You
5
+ # can run this via the 'rake example' task.
6
+ #
7
+ # Modify as you see fit
8
+ #######################################################################
9
+ require 'sys/proctable'
10
+ include Sys
11
+
12
+ puts "VERSION: " + ProcTable::VERSION
13
+ sleep 2
14
+
15
+ ProcTable.ps{ |s|
16
+ ProcTable.fields.each{ |field|
17
+ puts "#{field}: " + s.send(field).to_s
18
+ }
19
+ puts '=' * 30
20
+ }
@@ -0,0 +1,32 @@
1
+ require 'sys/proctable'
2
+ require 'rbconfig'
3
+
4
+ # The Sys module serves as a namespace only
5
+ module Sys
6
+
7
+ # The Top class serves as a toplevel name for the 'top' method.
8
+ class Top
9
+
10
+ # The version of the sys-top library
11
+ VERSION = '1.0.3'
12
+
13
+ # Returns an array of Struct::ProcTableStruct elements containing up
14
+ # to +num+ elements, sorted by +field+. The default number of elements
15
+ # is 10, while the default field is 'pctcpu'.
16
+ #
17
+ # Exception: the default sort field is 'pid' on Linux and Windows.
18
+ #
19
+ def self.top(num=10, field='pctcpu')
20
+ field = field.to_s if field.is_a?(Symbol)
21
+
22
+ windows = /mswin|win32|windows|dos|cygwin|mingw/i
23
+
24
+ # Sort by pid on Windows by default
25
+ if Config::CONFIG['host_os'].match(windows) && field == 'pctcpu'
26
+ field = 'pid'
27
+ end
28
+
29
+ Sys::ProcTable.ps.sort_by{ |obj| obj.send(field) || '' }[0..num-1]
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,209 @@
1
+ require 'win32ole'
2
+ require 'socket'
3
+ require 'date'
4
+
5
+ # The Sys module serves as a namespace only
6
+ module Sys
7
+
8
+ # The ProcTable class encapsulates process table information
9
+ class ProcTable
10
+
11
+ # There is no constructor
12
+ private_class_method :new
13
+
14
+ # Error typically raised if one of the Sys::ProcTable methods fails
15
+ class Error < StandardError; end
16
+
17
+ # The version of the sys-proctable library
18
+ VERSION = '0.9.1'
19
+
20
+ # The comm field corresponds to the 'name' field. The 'cmdline' field
21
+ # is the CommandLine attribute on Windows XP or later, or the
22
+ # 'executable_path' field on Windows 2000 or earlier.
23
+ #
24
+ @fields = %w[
25
+ caption
26
+ cmdline
27
+ comm
28
+ creation_class_name
29
+ creation_date
30
+ cs_creation_class_name
31
+ cs_name
32
+ description
33
+ executable_path
34
+ execution_state
35
+ handle
36
+ handle_count
37
+ install_date
38
+ kernel_mode_time
39
+ maximum_working_set_size
40
+ minimum_working_set_size
41
+ name
42
+ os_creation_class_name
43
+ os_name
44
+ other_operation_count
45
+ other_transfer_count
46
+ page_faults
47
+ page_file_usage
48
+ ppid
49
+ peak_page_file_usage
50
+ peak_virtual_size
51
+ peak_working_set_size
52
+ priority
53
+ private_page_count
54
+ pid
55
+ quota_non_paged_pool_usage
56
+ quota_paged_pool_usage
57
+ quota_peak_non_paged_pool_usage
58
+ quota_peak_paged_pool_usage
59
+ read_operation_count
60
+ read_transfer_count
61
+ session_id
62
+ status
63
+ termination_date
64
+ thread_count
65
+ user_mode_time
66
+ virtual_size
67
+ windows_version
68
+ working_set_size
69
+ write_operation_count
70
+ write_transfer_count
71
+ ]
72
+
73
+ ProcTableStruct = Struct.new("ProcTableStruct", *@fields)
74
+
75
+ # call-seq:
76
+ # ProcTable.fields
77
+ #
78
+ # Returns an array of fields that each ProcTableStruct will contain. This
79
+ # may be useful if you want to know in advance what fields are available
80
+ # without having to perform at least one read of the /proc table.
81
+ #
82
+ def self.fields
83
+ @fields
84
+ end
85
+
86
+ # call-seq:
87
+ # ProcTable.ps(pid=nil)
88
+ # ProcTable.ps(pid=nil){ |ps| ... }
89
+ #
90
+ # In block form, yields a ProcTableStruct for each process entry that you
91
+ # have rights to. This method returns an array of ProcTableStruct's in
92
+ # non-block form.
93
+ #
94
+ # If a +pid+ is provided, then only a single ProcTableStruct is yielded or
95
+ # returned, or nil if no process information is found for that +pid+.
96
+ #
97
+ def self.ps(pid=nil, host=Socket.gethostname)
98
+ if pid
99
+ raise TypeError unless pid.kind_of?(Fixnum)
100
+ end
101
+
102
+ array = block_given? ? nil : []
103
+ struct = nil
104
+
105
+ begin
106
+ wmi = WIN32OLE.connect("winmgmts://#{host}/root/cimv2")
107
+ rescue WIN32OLERuntimeError => e
108
+ raise Error, e # Re-raise as ProcTable::Error
109
+ else
110
+ wmi.InstancesOf("Win32_Process").each{ |wproc|
111
+ if pid
112
+ next unless wproc.ProcessId == pid
113
+ end
114
+
115
+ # Some fields are added later, and so are nil initially
116
+ struct = ProcTableStruct.new(
117
+ wproc.Caption,
118
+ nil, # Added later, based on OS version
119
+ wproc.Name,
120
+ wproc.CreationClassName,
121
+ self.parse_ms_date(wproc.CreationDate),
122
+ wproc.CSCreationClassName,
123
+ wproc.CSName,
124
+ wproc.Description,
125
+ wproc.ExecutablePath,
126
+ wproc.ExecutionState,
127
+ wproc.Handle,
128
+ wproc.HandleCount,
129
+ self.parse_ms_date(wproc.InstallDate),
130
+ self.convert(wproc.KernelModeTime),
131
+ wproc.MaximumWorkingSetSize,
132
+ wproc.MinimumWorkingSetSize,
133
+ wproc.Name,
134
+ wproc.OSCreationClassName,
135
+ wproc.OSName,
136
+ self.convert(wproc.OtherOperationCount),
137
+ self.convert(wproc.OtherTransferCount),
138
+ wproc.PageFaults,
139
+ wproc.PageFileUsage,
140
+ wproc.ParentProcessId,
141
+ self.convert(wproc.PeakPageFileUsage),
142
+ self.convert(wproc.PeakVirtualSize),
143
+ self.convert(wproc.PeakWorkingSetSize),
144
+ wproc.Priority,
145
+ self.convert(wproc.PrivatePageCount),
146
+ wproc.ProcessId,
147
+ wproc.QuotaNonPagedPoolUsage,
148
+ wproc.QuotaPagedPoolUsage,
149
+ wproc.QuotaPeakNonPagedPoolUsage,
150
+ wproc.QuotaPeakPagedPoolUsage,
151
+ self.convert(wproc.ReadOperationCount),
152
+ self.convert(wproc.ReadTransferCount),
153
+ wproc.SessionId,
154
+ wproc.Status,
155
+ self.parse_ms_date(wproc.TerminationDate),
156
+ wproc.ThreadCount,
157
+ self.convert(wproc.UserModeTime),
158
+ self.convert(wproc.VirtualSize),
159
+ wproc.WindowsVersion,
160
+ self.convert(wproc.WorkingSetSize),
161
+ self.convert(wproc.WriteOperationCount),
162
+ self.convert(wproc.WriteTransferCount)
163
+ )
164
+
165
+ ###############################################################
166
+ # On Windows XP or later, set the cmdline to the CommandLine
167
+ # attribute. Otherwise, set it to the ExecutablePath
168
+ # attribute.
169
+ ###############################################################
170
+ if wproc.WindowsVersion.to_f < 5.1
171
+ struct.cmdline = wproc.ExecutablePath
172
+ else
173
+ struct.cmdline = wproc.CommandLine
174
+ end
175
+
176
+ struct.freeze # This is read-only data
177
+
178
+ if block_given?
179
+ yield struct
180
+ else
181
+ array << struct
182
+ end
183
+ }
184
+ end
185
+
186
+ pid ? struct : array
187
+ end
188
+
189
+ private
190
+
191
+ #######################################################################
192
+ # Converts a string in the format '20040703074625.015625-360' into a
193
+ # Ruby Time object.
194
+ #######################################################################
195
+ def self.parse_ms_date(str)
196
+ return if str.nil?
197
+ return Date.parse(str.split('.').first)
198
+ end
199
+
200
+ #####################################################################
201
+ # There is a bug in win32ole where uint64 types are returned as a
202
+ # String instead of a Fixnum. This method deals with that for now.
203
+ #####################################################################
204
+ def self.convert(str)
205
+ return nil if str.nil? # Return nil, not 0
206
+ return str.to_i
207
+ end
208
+ end
209
+ end