sys-proctable 0.9.2-universal-mingw

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile ADDED
@@ -0,0 +1,174 @@
1
+ require 'rake'
2
+ require 'rake/clean'
3
+ require 'rake/testtask'
4
+ require 'rbconfig'
5
+ include RbConfig
6
+
7
+ CLEAN.include(
8
+ '**/*.core', # Core dump files
9
+ '**/*.gem', # Gem files
10
+ '**/*.rbc', # Rubinius
11
+ '.rbx', '**/*/.rbx', # Rubinius
12
+ '**/*.o', # C object file
13
+ '**/*.log', # Ruby extension build log
14
+ '**/Makefile', # C Makefile
15
+ '**/conftest.dSYM', # OS X build directory
16
+ "**/*.#{CONFIG['DLEXT']}" # C shared object
17
+ )
18
+
19
+ desc 'Build the sys-proctable library for C versions of sys-proctable'
20
+ task :build => [:clean] do
21
+ case CONFIG['host_os']
22
+ when /bsd/i
23
+ dir = 'ext/bsd'
24
+ when /darwin/i
25
+ dir = 'ext/darwin'
26
+ when /hpux/i
27
+ dir = 'ext/hpux'
28
+ end
29
+
30
+ unless CONFIG['host_os'] =~ /win32|mswin|dos|cygwin|mingw|windows|linux|sunos|solaris/i
31
+ Dir.chdir(dir) do
32
+ ruby 'extconf.rb'
33
+ sh 'make'
34
+ cp 'proctable.' + CONFIG['DLEXT'], 'sys'
35
+ end
36
+ end
37
+ end
38
+
39
+ desc 'Install the sys-proctable library'
40
+ task :install => [:build] do
41
+ file = nil
42
+ dir = File.join(CONFIG['sitelibdir'], 'sys')
43
+
44
+ Dir.mkdir(dir) unless File.exists?(dir)
45
+
46
+ case CONFIG['host_os']
47
+ when /mswin|win32|msdos|cygwin|mingw|windows/i
48
+ file = 'lib/windows/sys/proctable.rb'
49
+ when /linux/i
50
+ file = 'lib/linux/sys/proctable.rb'
51
+ when /sunos|solaris/i
52
+ file = 'lib/sunos/sys/proctable.rb'
53
+ when /bsd/i
54
+ Dir.chdir('ext/bsd'){ sh 'make install' }
55
+ when /darwin/i
56
+ Dir.chdir('ext/darwin'){ sh 'make install' }
57
+ when /hpux/i
58
+ Dir.chdir('ext/hpux'){ sh 'make install' }
59
+ end
60
+
61
+ cp(file, dir, :verbose => true) if file
62
+ end
63
+
64
+ desc 'Uninstall the sys-proctable library'
65
+ task :uninstall do
66
+ case CONFIG['host_os']
67
+ when /win32|mswin|dos|cygwin|mingw|windows|linux|sunos|solaris/i
68
+ dir = File.join(CONFIG['sitelibdir'], 'sys')
69
+ file = File.join(dir, 'proctable.rb')
70
+ else
71
+ dir = File.join(CONFIG['sitearchdir'], 'sys')
72
+ file = File.join(dir, 'proctable.' + CONFIG['DLEXT'])
73
+ end
74
+
75
+ rm(file)
76
+ end
77
+
78
+ desc 'Run the benchmark suite'
79
+ task :bench => [:build] do
80
+ sh "ruby -Ilib benchmarks/bench_ps.rb"
81
+ end
82
+
83
+ desc 'Run the example program'
84
+ task :example => [:build] do
85
+ sh 'ruby -Ilib -Iext examples/example_ps.rb'
86
+ end
87
+
88
+ desc 'Run the test suite'
89
+ Rake::TestTask.new do |t|
90
+ task :test => :build
91
+ t.libs << 'test' << '.'
92
+
93
+ case CONFIG['host_os']
94
+ when /mswin|msdos|cygwin|mingw|windows/i
95
+ t.test_files = FileList['test/test_sys_proctable_windows.rb']
96
+ t.libs << 'lib/windows'
97
+ when /linux/i
98
+ t.test_files = FileList['test/test_sys_proctable_linux.rb']
99
+ t.libs << 'lib/linux'
100
+ when /sunos|solaris/i
101
+ t.test_files = FileList['test/test_sys_proctable_sunos.rb']
102
+ t.libs << 'lib/sunos'
103
+ when /darwin/i
104
+ t.libs << 'ext/darwin'
105
+ t.test_files = FileList['test/test_sys_proctable_darwin.rb']
106
+ when /bsd/i
107
+ t.libs << 'ext/bsd'
108
+ t.test_files = FileList['test/test_sys_proctable_bsd.rb']
109
+ when /hpux/i
110
+ t.libs << 'ext/hpux'
111
+ t.test_files = FileList['test/test_sys_proctable_hpux.rb']
112
+ end
113
+ end
114
+
115
+ namespace :gem do
116
+ desc 'Create a gem'
117
+ task :create => [:clean] do
118
+ spec = eval(IO.read('sys-proctable.gemspec'))
119
+
120
+ # I've had to manually futz with the spec here in some cases
121
+ # in order to get the universal platform settings I want because
122
+ # of some bugginess in Rubygems' platform.rb.
123
+ #
124
+ case CONFIG['host_os']
125
+ when /bsd/i
126
+ spec.platform = Gem::Platform.new(['universal', 'freebsd'])
127
+ spec.platform.version = nil
128
+ spec.files << 'ext/bsd/sys/proctable.c'
129
+ spec.extra_rdoc_files << 'ext/bsd/sys/proctable.c'
130
+ spec.test_files << 'test/test_sys_proctable_bsd.rb'
131
+ spec.extensions = ['ext/bsd/extconf.rb']
132
+ when /darwin/i
133
+ spec.platform = Gem::Platform.new(['universal', 'darwin'])
134
+ spec.files << 'ext/darwin/sys/proctable.c'
135
+ spec.extra_rdoc_files << 'ext/darwin/sys/proctable.c'
136
+ spec.test_files << 'test/test_sys_proctable_darwin.rb'
137
+ spec.extensions = ['ext/darwin/extconf.rb']
138
+ when /hpux/i
139
+ spec.platform = Gem::Platform.new(['universal', 'hpux'])
140
+ spec.files << 'ext/hpux/sys/proctable.c'
141
+ spec.extra_rdoc_files << 'ext/hpux/sys/proctable.c'
142
+ spec.test_files << 'test/test_sys_proctable_hpux.rb'
143
+ spec.extensions = ['ext/hpux/extconf.rb']
144
+ when /linux/i
145
+ spec.platform = Gem::Platform.new(['universal', 'linux'])
146
+ spec.require_paths = ['lib', 'lib/linux']
147
+ spec.files += ['lib/linux/sys/proctable.rb']
148
+ spec.test_files << 'test/test_sys_proctable_linux.rb'
149
+ when /sunos|solaris/i
150
+ spec.platform = Gem::Platform.new(['universal', 'solaris'])
151
+ spec.require_paths = ['lib', 'lib/sunos']
152
+ spec.files += ['lib/sunos/sys/proctable.rb']
153
+ spec.test_files << 'test/test_sys_proctable_sunos.rb'
154
+ when /mswin|win32|dos|cygwin|mingw|windows/i
155
+ spec.platform = Gem::Platform.new(['universal', 'mingw'])
156
+ spec.require_paths = ['lib', 'lib/windows']
157
+ spec.files += ['lib/windows/sys/proctable.rb']
158
+ spec.test_files << 'test/test_sys_proctable_windows.rb'
159
+ end
160
+
161
+ # https://github.com/rubygems/rubygems/issues/147
162
+ spec.original_platform = spec.platform
163
+
164
+ Gem::Builder.new(spec).build
165
+ end
166
+
167
+ desc 'Install the sys-proctable library as a gem'
168
+ task :install => [:create] do
169
+ gem_name = Dir['*.gem'].first
170
+ sh "gem install #{gem_name}"
171
+ end
172
+ end
173
+
174
+ 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
data/doc/top.txt ADDED
@@ -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
+ }
data/lib/sys/top.rb ADDED
@@ -0,0 +1,29 @@
1
+ require 'sys/proctable'
2
+
3
+ # The Sys module serves as a namespace only
4
+ module Sys
5
+
6
+ # The Top class serves as a toplevel name for the 'top' method.
7
+ class Top
8
+
9
+ # The version of the sys-top library
10
+ VERSION = '1.0.3'
11
+
12
+ # Returns an array of Struct::ProcTableStruct elements containing up
13
+ # to +num+ elements, sorted by +field+. The default number of elements
14
+ # is 10, while the default field is 'pctcpu'.
15
+ #
16
+ # Exception: the default sort field is 'pid' on Linux and Windows.
17
+ #
18
+ def self.top(num=10, field='pctcpu')
19
+ field = field.to_s if field.is_a?(Symbol)
20
+
21
+ # Sort by pid on Windows by default
22
+ if File::ALT_SEPARATOR && field == 'pctcpu'
23
+ field = 'pid'
24
+ end
25
+
26
+ Sys::ProcTable.ps.sort_by{ |obj| obj.send(field) || '' }[0..num-1]
27
+ end
28
+ end
29
+ 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.2'
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 DateTime.parse(str)
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