sys-proctable 0.7.3 → 0.7.4

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,177 @@
1
+ /*****************************************************************************
2
+ * solaris.h
3
+ *
4
+ * Solaris specific header file. Some things copy/pasted straight out
5
+ * of Dan Urist's Proc::ProcessTable Perl module.
6
+ *****************************************************************************/
7
+ #include <stdio.h>
8
+ #include <stdlib.h>
9
+ #include <dirent.h>
10
+ #include <sys/param.h>
11
+ #include <sys/types.h>
12
+ #include <sys/proc.h>
13
+ #include <fcntl.h>
14
+ #include <string.h>
15
+ #include <unistd.h>
16
+ #include <utmpx.h>
17
+
18
+ /* This bit of magic was taken from a post by Stefan Teleman on the KDE
19
+ * mailing list. It allows us to build without having to disable largefile
20
+ * support or explicitly build a 64 bit Ruby.
21
+ */
22
+ #if (!defined(_LP64)) && (_FILE_OFFSET_BITS - 0 == 64)
23
+ #define PROCFS_FILE_OFFSET_BITS_HACK 1
24
+ #undef _FILE_OFFSET_BITS
25
+ #else
26
+ #define PROCFS_FILE_OFFSET_BITS_HACK 0
27
+ #endif
28
+
29
+ #if defined(HAVE_PROCFS_H)
30
+ #include <procfs.h>
31
+ #else
32
+ #include <sys/procfs.h>
33
+ #endif
34
+
35
+ #if (PROCFS_FILE_OFFSET_BITS_HACK - 0 == 1)
36
+ #define _FILE_OFFSET_BITS 64
37
+ #endif
38
+
39
+ #ifndef CLK_TCK
40
+ #define CLK_TCK sysconf(_SC_CLK_TCK)
41
+ #endif
42
+
43
+ #ifndef NAME_MAX
44
+ #define NAME_MAX 255
45
+ #endif
46
+
47
+ /* Ruby 1.8.3 or later */
48
+ #ifdef HAVE_TYPE_RB_PID_T
49
+ #define pid_t rb_pid_t
50
+ #endif
51
+
52
+ #define COMM_MAX 64
53
+ #define STATE_MAX 20
54
+ #define WCHAN_MAX 64
55
+
56
+ /* Function prototypes */
57
+ static void getprusage(pid_t pid, prusage_t* pr_usage);
58
+
59
+ /* Process state strings that we return */
60
+ #define SLEEP "sleep"
61
+ #define WAIT "wait"
62
+ #define RUN "run"
63
+ #define IDLE "idle"
64
+ #define ZOMBIE "zombie"
65
+ #define STOP "stop"
66
+ #define ONPROC "onprocessor"
67
+
68
+ /*
69
+ * A simple static structure, mainly for use with the 'fields()' class method.
70
+ * The fields here *should* be identical to the fields listed in the ps_struct.
71
+ * The exception is the 'comm' field, which I added to give it a bit of
72
+ * (foolish?) consistency with the other platforms. I will try to make sure
73
+ * that field is common to all platforms.
74
+ */
75
+ static char* fields[] = {
76
+ "flag",
77
+ #ifdef HAVE_PROCFS_H
78
+ "nlwp",
79
+ #endif
80
+ "pid",
81
+ "ppid",
82
+ "pgid",
83
+ "sid",
84
+ "uid",
85
+ "euid",
86
+ "gid",
87
+ "egid",
88
+ "priority",
89
+ "nice",
90
+ "ttydev",
91
+ "time",
92
+ "ctime",
93
+ "size",
94
+ "rss",
95
+ "wchan",
96
+ "pctcpu",
97
+ "pctmem",
98
+ "state",
99
+ "fname",
100
+ "cmdline",
101
+ "start",
102
+ #ifdef HAVE_PROCFS_H
103
+ "processor",
104
+ #endif
105
+ "comm",
106
+ "num_args",
107
+ "cmd_args",
108
+ "lwpid",
109
+ "count",
110
+ "tstamp",
111
+ "create",
112
+ "term",
113
+ "rtime",
114
+ "utime",
115
+ "stime",
116
+ "ttime",
117
+ "tftime",
118
+ "dftime",
119
+ "kftime",
120
+ "ltime",
121
+ "slptime",
122
+ "wtime",
123
+ "stoptime",
124
+ "minf",
125
+ "majf",
126
+ "nswap",
127
+ "inblk",
128
+ "oublk",
129
+ "msnd",
130
+ "mrcv",
131
+ "sigs",
132
+ "vctx",
133
+ "ictx",
134
+ "sysc",
135
+ "ioch"
136
+ };
137
+
138
+ /*
139
+ * Private function for getting data out of the /proc/self/usage directory.
140
+ * Based on Rich Teer's function in "Solaris Systems Programming", p.329
141
+ * and p.332.
142
+ */
143
+ #if defined(HAVE_PROCFS_H)
144
+ static void getprusage(pid_t pid, prusage_t* pr_usage){
145
+ int fd;
146
+ char name[PATH_MAX];
147
+
148
+ snprintf(name, PATH_MAX, "/proc/%ld/usage", (long)pid);
149
+
150
+ if((fd = open(name, O_RDONLY)) == -1)
151
+ rb_sys_fail(0);
152
+
153
+ if(read(fd,pr_usage,sizeof(prusage_t)) == -1){
154
+ close(fd);
155
+ rb_sys_fail(0);
156
+ }
157
+
158
+ close(fd);
159
+ }
160
+ #else
161
+ static void getprusage(pid_t pid, prusage_t* pr_usage){
162
+ int fd;
163
+ char name[PATH_MAX];
164
+
165
+ snprintf(name, PATH_MAX, "/proc/%ld", (long)pid);
166
+
167
+ if((fd = open(name, O_RDONLY)) == -1)
168
+ rb_sys_fail(0);
169
+
170
+ if(ioctl(fd,PIOCUSAGE,pr_usage) == -1){
171
+ close(fd);
172
+ rb_sys_fail(0);
173
+ }
174
+
175
+ close(fd);
176
+ }
177
+ #endif
@@ -1,2 +1,2 @@
1
1
  /* version.h - stores the version number for all platforms (that use C) */
2
- #define SYS_PROCTABLE_VERSION "0.8.0"
2
+ #define SYS_PROCTABLE_VERSION "0.7.4"
@@ -0,0 +1,23 @@
1
+ require "sys/proctable"
2
+ module Sys
3
+ class Top
4
+ VERSION = "1.0.1"
5
+ WINDOWS = PLATFORM.match("mswin")
6
+
7
+ def self.top(num=10, field="pctcpu")
8
+ # Sort by pid on Windows by default
9
+ if WINDOWS && field == "pctcpu"
10
+ field = "pid"
11
+ end
12
+
13
+ # Linux does not have a pctcpu field yet
14
+ if RUBY_PLATFORM =~ /linux/ && field == "pctcpu"
15
+ field = "pid"
16
+ end
17
+
18
+ Sys::ProcTable.ps.sort{ |a,b|
19
+ b.send(field) <=> a.send(field)
20
+ }[0..num-1]
21
+ end
22
+ end
23
+ end
@@ -5,35 +5,31 @@
5
5
  ##################################################
6
6
  base = File.basename(Dir.pwd)
7
7
 
8
- if base == "test" || base =~ /sys-proctable.*/
9
- require "ftools"
10
- Dir.chdir ".." if base == "test"
11
- Dir.mkdir("sys") unless File.exists?("sys")
12
- $LOAD_PATH.unshift Dir.pwd
13
- end
14
-
15
- if File::ALT_SEPARATOR
16
- File.copy("lib/os/windows.rb","sys/proctable.rb")
17
- else
18
- extension = ".so"
19
- extension = ".bundle" if RUBY_PLATFORM =~ /powerpc|darwin/i
20
- extension = ".sl" if RUBY_PLATFORM =~ /hpux/i
8
+ if base == 'test' || base =~ /sys-proctable.*/
9
+ require 'fileutils'
10
+ Dir.chdir '..' if base == 'test'
21
11
 
22
- pt_file = "proctable" + extension
23
-
24
- if File.exist?(pt_file)
25
- File.copy(pt_file, "sys")
12
+ if RUBY_PLATFORM.match('mswin')
13
+ File.rename('lib/sys/windows.rb', 'lib/sys/proctable.rb')
14
+ else
15
+ require 'rbconfig'
16
+ file = 'ext/proctable.' + Config::CONFIG['DLEXT']
17
+ Dir.mkdir('sys') unless File.exists?('sys')
18
+ FileUtils.cp(file, 'sys')
26
19
  end
20
+
21
+ $LOAD_PATH.unshift(Dir.pwd)
22
+ $LOAD_PATH.unshift(Dir.pwd + '/lib')
27
23
  end
28
24
 
29
- require "sys/proctable"
30
- require "test/unit"
25
+ require 'sys/proctable'
26
+ require 'test/unit'
31
27
  include Sys
32
28
 
33
29
  class TC_ProcTable_All < Test::Unit::TestCase
34
30
 
35
31
  def test_version
36
- assert_equal('0.8.0', ProcTable::VERSION, "Bad VERSION constant")
32
+ assert_equal('0.7.4', ProcTable::VERSION)
37
33
  end
38
34
 
39
35
  def test_fields
@@ -52,7 +48,7 @@ class TC_ProcTable_All < Test::Unit::TestCase
52
48
  def test_ps_with_args
53
49
  assert_nothing_raised{ ProcTable.ps(0) }
54
50
  assert_nothing_raised{ ProcTable.ps(nil) }
55
- assert_raises(TypeError){ ProcTable.ps("vim") }
51
+ assert_raises(TypeError){ ProcTable.ps('vim') }
56
52
  assert_raises(ArgumentError){ ProcTable.ps(0,1) }
57
53
  end
58
54
 
@@ -5,11 +5,15 @@
5
5
  ############################################
6
6
  base = File.basename(Dir.pwd)
7
7
 
8
- if base == "test" || base =~ /sys-proctable.*/
9
- require "ftools"
10
- Dir.chdir("..") if base == "test"
11
- Dir.mkdir("sys") unless File.exists?("sys")
12
- File.copy("proctable.so","sys") if File.exists?("proctable.so")
8
+ if base == 'test' || base =~ /sys-proctable.*/
9
+ require 'fileutils'
10
+ require 'rbconfig'
11
+
12
+ Dir.chdir '..' if base == 'test'
13
+ file = 'ext/proctable.' + Config::CONFIG['DLEXT']
14
+ Dir.mkdir('sys') unless File.exists?('sys')
15
+ FileUtils.cp(file, 'sys')
16
+
13
17
  $LOAD_PATH.unshift(Dir.pwd)
14
18
  end
15
19
 
@@ -1,16 +1,19 @@
1
1
  ##################################################
2
2
  # tc_hpux.rb
3
3
  #
4
- # Test suite for sys-proctable for HP-UX
5
- # Requires testunit 0.1.6 or later.
4
+ # Test case for sys-proctable for HP-UX.
6
5
  ##################################################
7
6
  base = File.basename(Dir.pwd)
8
7
 
9
- if base == "test" || base =~ /sys-proctable.*/
10
- require "ftools"
11
- Dir.chdir("..") if base == "test"
12
- Dir.mkdir("sys") unless File.exists?("sys")
13
- File.copy("proctable.sl","sys") if File.exists?("proctable.sl")
8
+ if base == 'test' || base =~ /sys-proctable.*/
9
+ require 'fileutils'
10
+ require 'rbconfig'
11
+
12
+ Dir.chdir('..') if base == 'test'
13
+ file = 'ext/proctable.' + Config::CONFIG['DLEXT']
14
+ Dir.mkdir('sys') unless File.exists?('sys')
15
+ FileUtils.cp(file, 'sys')
16
+
14
17
  $LOAD_PATH.unshift(Dir.pwd)
15
18
  end
16
19
 
@@ -4,11 +4,16 @@
4
4
  # Test suite for the BSD specific version of the kvm interface.
5
5
  ################################################################
6
6
  base = File.basename(Dir.pwd)
7
- if base == "test" || base =~ /sys-proctable.*/
8
- require "ftools"
9
- Dir.chdir("..") if base == "test"
10
- Dir.mkdir("sys") unless File.exists?("sys")
11
- File.copy("proctable.so","sys") if File.exists?("proctable.so")
7
+
8
+ if base == 'test' || base =~ /sys-proctable.*/
9
+ require 'fileutils'
10
+ require 'rbconfig'
11
+
12
+ Dir.chdir '..' if base == 'test'
13
+ file = 'ext/proctable.' + Config::CONFIG['DLEXT']
14
+ Dir.mkdir('sys') unless File.exists?('sys')
15
+ FileUtils.cp(file, 'sys')
16
+
12
17
  $LOAD_PATH.unshift(Dir.pwd)
13
18
  end
14
19
 
@@ -5,12 +5,15 @@
5
5
  ##########################################
6
6
  base = File.basename(Dir.pwd)
7
7
 
8
- if base == "test" || base =~ /sys-proctable.*/
9
- require "ftools"
10
- Dir.chdir("..") if base == "test"
11
- Dir.mkdir("sys") unless File.exists?("sys")
12
- File.delete("sys/proctable.so") rescue nil
13
- File.copy("proctable.so","sys") if File.exists?("proctable.so")
8
+ if base == 'test' || base =~ /sys-proctable.*/
9
+ require 'fileutils'
10
+ require 'rbconfig'
11
+
12
+ Dir.chdir '..' if base == 'test'
13
+ file = 'ext/proctable.' + Config::CONFIG['DLEXT']
14
+ Dir.mkdir('sys') unless File.exists?('sys')
15
+ FileUtils.cp(file, 'sys')
16
+
14
17
  $LOAD_PATH.unshift(Dir.pwd)
15
18
  end
16
19
 
@@ -5,11 +5,15 @@
5
5
  ##################################################
6
6
  base = File.basename(Dir.pwd)
7
7
 
8
- if base == "test" || base =~ /sys-proctable.*/
9
- require "ftools"
10
- Dir.chdir("..") if base == "test"
11
- Dir.mkdir("sys") unless File.exists?("sys")
12
- File.copy("proctable.so","sys") if File.exists?("proctable.so")
8
+ if base == 'test' || base =~ /sys-proctable.*/
9
+ require 'fileutils'
10
+ require 'rbconfig'
11
+
12
+ Dir.chdir '..' if base == 'test'
13
+ file = 'ext/proctable.' + Config::CONFIG['DLEXT']
14
+ Dir.mkdir('sys') unless File.exists?('sys')
15
+ FileUtils.cp(file, 'sys')
16
+
13
17
  $LOAD_PATH.unshift(Dir.pwd)
14
18
  end
15
19
 
@@ -5,33 +5,32 @@
5
5
  ##############################################################################
6
6
  base = File.basename(Dir.pwd)
7
7
 
8
- if base == "test" || base =~ /sys-proctable.*/
9
- require "ftools"
10
- Dir.chdir("..") if base == "test"
11
- Dir.mkdir("sys") unless File.exists?("sys")
12
- File.copy("lib/top.rb","sys")
8
+ if base == 'test' || base =~ /sys-proctable.*/
9
+ require 'fileutils'
10
+ Dir.chdir('..') if base == 'test'
11
+ Dir.mkdir('sys') unless File.exists?('sys')
12
+ FileUtils.cp('lib/top.rb', 'sys')
13
13
  $LOAD_PATH.unshift Dir.pwd
14
- $LOAD_PATH.unshift Dir.pwd + "/lib"
15
14
  end
16
15
 
17
- require "sys/top"
18
- require "test/unit"
16
+ require 'sys/top'
17
+ require 'test/unit'
19
18
  include Sys
20
19
 
21
20
  class TC_Top < Test::Unit::TestCase
22
21
  def test_version
23
- assert_equal("1.0.1", Top::VERSION)
22
+ assert_equal('1.0.1', Top::VERSION)
24
23
  end
25
24
 
26
25
  def test_top
27
26
  assert_respond_to(Top,:top)
28
27
  assert_nothing_raised{ Top.top }
29
28
  assert_nothing_raised{ Top.top(5) }
30
- assert_nothing_raised{ Top.top(5,"cmdline") }
31
- assert_raises(ArgumentError){ Top.top(1,"foo",2) }
32
- assert_kind_of(Array,Top.top)
33
- assert_equal(10,Top.top.length)
34
- assert_equal(5,Top.top(5).length)
35
- assert_kind_of(Struct::ProcTableStruct,Top.top.first)
29
+ assert_nothing_raised{ Top.top(5, 'cmdline') }
30
+ assert_raises(ArgumentError){ Top.top(1,'foo',2) }
31
+ assert_kind_of(Array, Top.top)
32
+ assert_equal(10, Top.top.length)
33
+ assert_equal(5, Top.top(5).length)
34
+ assert_kind_of(Struct::ProcTableStruct, Top.top.first)
36
35
  end
37
36
  end
@@ -5,17 +5,15 @@
5
5
  ##################################################
6
6
  base = File.basename(Dir.pwd)
7
7
 
8
- if base == "test" || base =~ /sys-proctable.*/
9
- require "ftools"
10
- Dir.chdir ".." if base == "test"
11
- Dir.mkdir("sys") unless File.exists?("sys")
12
- File.copy("lib/os/windows.rb","sys/proctable.rb")
13
- $LOAD_PATH.unshift Dir.pwd
8
+ if base == 'test' || base =~ /sys-proctable.*/
9
+ Dir.chdir('..') if base == 'test'
10
+ File.rename("lib/sys/windows.rb", "lib/sys/proctable.rb")
11
+ $LOAD_PATH.unshift(Dir.pwd + '/lib')
14
12
  end
15
13
 
16
- require "sys/proctable"
17
- require "test/unit"
18
- require "test/tc_all"
14
+ require 'sys/proctable'
15
+ require 'test/unit'
16
+ require 'test/tc_all'
19
17
  include Sys
20
18
 
21
19
  class TC_ProcTable_MSWindows < Test::Unit::TestCase