linux_stat 0.5.1 → 0.6.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.
- checksums.yaml +4 -4
- data/README.md +292 -55
- data/exe/linuxstat.rb +1 -1
- data/ext/fs_stat/extconf.rb +5 -1
- data/ext/fs_stat/fs_stat.c +2 -0
- data/ext/sysconf/extconf.rb +5 -1
- data/ext/sysconf/sysconf.c +6 -2
- data/ext/utsname/extconf.rb +5 -1
- data/ext/utsname/utsname.c +2 -0
- data/lib/linux_stat.rb +32 -5
- data/lib/linux_stat/battery.rb +20 -1
- data/lib/linux_stat/bios.rb +8 -0
- data/lib/linux_stat/cpu.rb +32 -6
- data/lib/linux_stat/filesystem.rb +31 -10
- data/lib/linux_stat/kernel.rb +36 -2
- data/lib/linux_stat/memory.rb +7 -0
- data/lib/linux_stat/mounts.rb +176 -1
- data/lib/linux_stat/net.rb +119 -1
- data/lib/linux_stat/os.rb +15 -3
- data/lib/linux_stat/prettify_bytes.rb +110 -0
- data/lib/linux_stat/process.rb +10 -0
- data/lib/linux_stat/process_info.rb +223 -91
- data/lib/linux_stat/swap.rb +15 -1
- data/lib/linux_stat/user.rb +119 -26
- data/lib/linux_stat/version.rb +1 -1
- metadata +3 -2
data/lib/linux_stat/os.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
module LinuxStat
|
2
2
|
module OS
|
3
3
|
class << self
|
4
|
+
##
|
4
5
|
# Reads /etc/os-release and returns a Hash. For example:
|
5
6
|
# {:NAME=>"Arch Linux", :PRETTY_NAME=>"Arch Linux", :ID=>"arch", :BUILD_ID=>"rolling", :ANSI_COLOR=>"38;2;23;147;209", :HOME_URL=>"https://www.archlinux.org/", :DOCUMENTATION_URL=>"https://wiki.archlinux.org/", :SUPPORT_URL=>"https://bbs.archlinux.org/", :BUG_REPORT_URL=>"https://bugs.archlinux.org/", :LOGO=>"archlinux"}
|
6
7
|
#
|
@@ -8,13 +9,15 @@ module LinuxStat
|
|
8
9
|
#
|
9
10
|
# The amount of data read is 4096 bytes. Any more than that will result in truncated output.
|
10
11
|
#
|
11
|
-
# The information is also cached, and once loaded, won't change in runtime.
|
12
|
+
# The information is also cached, and once loaded, won't change in runtime.
|
13
|
+
# Because changing the /etc/lsb-release
|
12
14
|
# isn't expected in runtime.
|
13
15
|
def os_release
|
14
16
|
# cached (memoized) ; as changing the value in runtime is unexpected
|
15
17
|
@@os_release ||= File.readable?('/etc/os-release') ? release('/etc/os-release') : {}
|
16
18
|
end
|
17
19
|
|
20
|
+
##
|
18
21
|
# Reads /etc/lsb-release and returns a Hash. For example:
|
19
22
|
# {:LSB_VERSION=>"1.4", :DISTRIB_ID=>"Arch", :DISTRIB_RELEASE=>"rolling", :DISTRIB_DESCRIPTION=>"Arch Linux"}
|
20
23
|
#
|
@@ -22,14 +25,16 @@ module LinuxStat
|
|
22
25
|
#
|
23
26
|
# The amount of data read is 4096 bytes. Any more than that will result in truncated output.
|
24
27
|
#
|
25
|
-
# The information is also cached, and once loaded, won't change in runtime.
|
26
|
-
# isn't expected in runtime.
|
28
|
+
# The information is also cached, and once loaded, won't change in runtime.
|
29
|
+
# Because changing the /etc/lsb-release isn't expected in runtime.
|
27
30
|
def lsb_release
|
28
31
|
# cached (memoized) ; as changing the value in runtime is unexpected
|
29
32
|
@@lsb_release ||= File.readable?('/etc/lsb-release') ? release('/etc/lsb-release') : {}
|
30
33
|
end
|
31
34
|
|
35
|
+
##
|
32
36
|
# Reads /etc/lsb-release or /etc/os-release tries to get information about the distribution.
|
37
|
+
#
|
33
38
|
# If the information isn't available, it will read and return /etc/issue.
|
34
39
|
#
|
35
40
|
# The return type is String.
|
@@ -52,6 +57,7 @@ module LinuxStat
|
|
52
57
|
end
|
53
58
|
end
|
54
59
|
|
60
|
+
##
|
55
61
|
# Uses utsname.h to determine the machine
|
56
62
|
#
|
57
63
|
# It returns a String but if the info isn't available, it will return an empty String
|
@@ -59,6 +65,7 @@ module LinuxStat
|
|
59
65
|
@@machine ||= LinuxStat::Uname.machine
|
60
66
|
end
|
61
67
|
|
68
|
+
##
|
62
69
|
# Uses utsname.h to determine the system nodename
|
63
70
|
#
|
64
71
|
# It returns String but if the info isn't available, it will return an empty String
|
@@ -66,6 +73,7 @@ module LinuxStat
|
|
66
73
|
@@nodename ||= LinuxStat::Uname.nodename
|
67
74
|
end
|
68
75
|
|
76
|
+
##
|
69
77
|
# Reads /etc/hostname and returns the hostname.
|
70
78
|
#
|
71
79
|
# The return type is String.
|
@@ -78,8 +86,11 @@ module LinuxStat
|
|
78
86
|
end
|
79
87
|
end
|
80
88
|
|
89
|
+
##
|
81
90
|
# Reads ruby configuration and tries to guess if the system is 64 bit.
|
91
|
+
#
|
82
92
|
# If it fails then it runs utsname.h to guess the machine.
|
93
|
+
#
|
83
94
|
# It the machine is 64 bits, it will return 64, else it returns 32.
|
84
95
|
#
|
85
96
|
# The return type is strictly Integer and doesn't fail.
|
@@ -91,6 +102,7 @@ module LinuxStat
|
|
91
102
|
end
|
92
103
|
end
|
93
104
|
|
105
|
+
##
|
94
106
|
# Reads /proc/uptime and returns the system uptime:
|
95
107
|
# {:hour=>10, :minute=>34, :second=>12.59}
|
96
108
|
#
|
@@ -0,0 +1,110 @@
|
|
1
|
+
module LinuxStat
|
2
|
+
module PrettifyBytes
|
3
|
+
class << self
|
4
|
+
##
|
5
|
+
# Converts a number to decimal byte units and outputs with the metric prefix
|
6
|
+
# For example,
|
7
|
+
#
|
8
|
+
# LinuxStat::PrettifyBytes.convert_decimal(1000)
|
9
|
+
#
|
10
|
+
# => "1.0 kilobyte"
|
11
|
+
#
|
12
|
+
# LinuxStat::PrettifyBytes.convert_decimal(1000 ** 3)
|
13
|
+
#
|
14
|
+
# => "1.0 gigabyte"
|
15
|
+
#
|
16
|
+
# LinuxStat::PrettifyBytes.convert_decimal(1024 ** 3)
|
17
|
+
#
|
18
|
+
# => "1.07 gigabytes"
|
19
|
+
def convert_decimal(n)
|
20
|
+
@@d_units ||= %W(#{''} kilo mega giga tera peta exa zetta)
|
21
|
+
.map.with_index { |x, i| [x, 1000.**(i + 1)] }
|
22
|
+
unit = @@d_units.find { |x| n < x[1] } || ['yotta'.freeze, 10 ** 27]
|
23
|
+
|
24
|
+
converted = n.fdiv(unit[1] / 1000).round(2)
|
25
|
+
"#{pad_left(converted)} #{unit[0]}byte#{?s.freeze if converted != 1}"
|
26
|
+
end
|
27
|
+
|
28
|
+
# Converts a number to binary byte units and outputs with the IEC prefix
|
29
|
+
# For example,
|
30
|
+
#
|
31
|
+
# LinuxStat::PrettifyBytes.convert_binary(1000)
|
32
|
+
#
|
33
|
+
# => "1000.0 bytes"
|
34
|
+
#
|
35
|
+
# LinuxStat::PrettifyBytes.convert_binary(1000 ** 3)
|
36
|
+
#
|
37
|
+
# => "953.67 mebibytes"
|
38
|
+
#
|
39
|
+
# LinuxStat::PrettifyBytes.convert_binary(1024 ** 3)
|
40
|
+
#
|
41
|
+
# => "1.0 gibibyte"
|
42
|
+
def convert_binary(n)
|
43
|
+
@@b_units ||= %W(#{''} kibi mebi gibi tebi pebi exbi zebi)
|
44
|
+
.map.with_index { |x, i| [x, 1024.**(i + 1)] }
|
45
|
+
unit = @@b_units.find { |x| n < x[1] } || ['yobi'.freeze, 10 ** 27]
|
46
|
+
|
47
|
+
converted = n.fdiv(unit[1] / 1024).round(2)
|
48
|
+
"#{pad_left(converted)} #{unit[0]}byte#{?s.freeze if converted != 1}"
|
49
|
+
end
|
50
|
+
|
51
|
+
# Converts a number to decimal byte units
|
52
|
+
# For example,
|
53
|
+
#
|
54
|
+
# LinuxStat::PrettifyBytes.convert_short_decimal(1000)
|
55
|
+
#
|
56
|
+
# => "1.0 kB"
|
57
|
+
#
|
58
|
+
# LinuxStat::PrettifyBytes.convert_short_decimal(1000 ** 3)
|
59
|
+
#
|
60
|
+
# => "1.0 GB"
|
61
|
+
#
|
62
|
+
# LinuxStat::PrettifyBytes.convert_short_decimal(1024 ** 3)
|
63
|
+
#
|
64
|
+
# => "1.07 GB"
|
65
|
+
def convert_short_decimal(n)
|
66
|
+
@@sd_units ||= %W(#{''} k M G T P E Z)
|
67
|
+
.map.with_index { |x, i| [x, 1000.**(i + 1)] }
|
68
|
+
unit = @@sd_units.find { |x| n < x[1] } || [?Y.freeze, 10 ** 27]
|
69
|
+
|
70
|
+
converted = n.fdiv(unit[1] / 1000).round(2)
|
71
|
+
"#{pad_left(converted)} #{unit[0]}B"
|
72
|
+
end
|
73
|
+
|
74
|
+
##
|
75
|
+
# Converts a number to binary byte units
|
76
|
+
#
|
77
|
+
# For example,
|
78
|
+
#
|
79
|
+
# LinuxStat::PrettifyBytes.convert_short_binary(1000)
|
80
|
+
#
|
81
|
+
# => "1000 B"
|
82
|
+
#
|
83
|
+
# LinuxStat::PrettifyBytes.convert_short_binary(1000 ** 3)
|
84
|
+
#
|
85
|
+
# => "953.67 MiB"
|
86
|
+
#
|
87
|
+
# LinuxStat::PrettifyBytes.convert_short_binary(1024 ** 3)
|
88
|
+
#
|
89
|
+
# => "1.0 GiB"
|
90
|
+
def convert_short_binary(n)
|
91
|
+
return "#{n} B" if n < 1024
|
92
|
+
|
93
|
+
@@sb_units ||= %W(#{''} K M G T P E Z)
|
94
|
+
.map.with_index { |x, i| [x, 1024.**(i + 1)] }
|
95
|
+
unit = @@sb_units.find { |x| n < x[1] } || [?Y.freeze, 1024 ** 9]
|
96
|
+
|
97
|
+
converted = n.fdiv(unit[1] / 1024).round(2)
|
98
|
+
"#{pad_left(converted)} #{unit[0]}iB"
|
99
|
+
end
|
100
|
+
|
101
|
+
private
|
102
|
+
def pad_left(n, mantissa_length = 2)
|
103
|
+
n = n.round(mantissa_length)
|
104
|
+
exp, mant = n.to_s.split(?..freeze)
|
105
|
+
m = mant.length < mantissa_length ? mant + ?0.freeze * (mantissa_length - mant.length) : mant
|
106
|
+
exp + ?..freeze + m
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
data/lib/linux_stat/process.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
module LinuxStat
|
2
2
|
module Process
|
3
3
|
class << self
|
4
|
+
##
|
4
5
|
# Returns the list of processes from /proc/.
|
6
|
+
#
|
5
7
|
# The return type is an Array of Integers.
|
6
8
|
def list
|
7
9
|
Dir['/proc/*'].select! { |x|
|
@@ -10,12 +12,15 @@ module LinuxStat
|
|
10
12
|
}.map! { |x| File.split(x)[-1].to_i }
|
11
13
|
end
|
12
14
|
|
15
|
+
##
|
13
16
|
# Counts and returns the total number of process running on the system.
|
17
|
+
#
|
14
18
|
# The return type is Integer.
|
15
19
|
def count
|
16
20
|
list.count
|
17
21
|
end
|
18
22
|
|
23
|
+
##
|
19
24
|
# Returns all the id of processes mapped with their names as a Hash.
|
20
25
|
def names
|
21
26
|
list.reduce({}) { |h, x|
|
@@ -27,6 +32,7 @@ module LinuxStat
|
|
27
32
|
}
|
28
33
|
end
|
29
34
|
|
35
|
+
##
|
30
36
|
# Returns all the id of processes mapped with their status as a Hash.
|
31
37
|
def types
|
32
38
|
list.reduce({}) { |h, x|
|
@@ -46,6 +52,7 @@ module LinuxStat
|
|
46
52
|
}
|
47
53
|
end
|
48
54
|
|
55
|
+
##
|
49
56
|
# Returns all the id of processes that are sleeping.
|
50
57
|
# The return type is an Array of Integers.
|
51
58
|
def sleeping
|
@@ -58,6 +65,7 @@ module LinuxStat
|
|
58
65
|
}
|
59
66
|
end
|
60
67
|
|
68
|
+
##
|
61
69
|
# Returns all the id of processes that are idle.
|
62
70
|
# The return type is an Array of Integers.
|
63
71
|
def idle
|
@@ -70,6 +78,7 @@ module LinuxStat
|
|
70
78
|
}
|
71
79
|
end
|
72
80
|
|
81
|
+
##
|
73
82
|
# Returns all the id of processes that are zombies.
|
74
83
|
# The return type is an Array of Integers.
|
75
84
|
def zombie
|
@@ -82,6 +91,7 @@ module LinuxStat
|
|
82
91
|
}
|
83
92
|
end
|
84
93
|
|
94
|
+
##
|
85
95
|
# Returns all the id of processes that are running.
|
86
96
|
# The return type is an Array of Integers.
|
87
97
|
def running
|
@@ -1,12 +1,20 @@
|
|
1
1
|
module LinuxStat
|
2
2
|
module ProcessInfo
|
3
3
|
class << self
|
4
|
-
|
4
|
+
##
|
5
|
+
# = total_io(pid = $$)
|
6
|
+
#
|
5
7
|
# Where pid is the process ID.
|
8
|
+
#
|
6
9
|
# By default it is the id of the current process ($$)
|
7
10
|
#
|
8
11
|
# It retuns the total read/write caused by a process.
|
9
|
-
#
|
12
|
+
#
|
13
|
+
# The output is Hash.
|
14
|
+
#
|
15
|
+
# For example:
|
16
|
+
# LinuxStat::ProcessInfo.total_io
|
17
|
+
#
|
10
18
|
# {:read_bytes=>0, :write_bytes=>0}
|
11
19
|
#
|
12
20
|
# The output is only based on the total disk IO the process has done.
|
@@ -28,12 +36,18 @@ module LinuxStat
|
|
28
36
|
out
|
29
37
|
end
|
30
38
|
|
31
|
-
|
39
|
+
##
|
40
|
+
# = cmdline(pid = $$)
|
41
|
+
#
|
32
42
|
# Where pid is the process ID.
|
43
|
+
#
|
33
44
|
# By default it is the id of the current process ($$)
|
34
45
|
#
|
35
46
|
# It retuns the total command of the process.
|
36
|
-
#
|
47
|
+
#
|
48
|
+
# The output is String. For example:
|
49
|
+
# LinuxStat::ProcessInfo.cmdline
|
50
|
+
#
|
37
51
|
# "ruby -r linux_stat -e p LinuxStat::ProcessInfo.cmdline"
|
38
52
|
#
|
39
53
|
# If the info isn't available it will return an empty frozen String.
|
@@ -46,12 +60,18 @@ module LinuxStat
|
|
46
60
|
_cmdline.tap(&:strip!)
|
47
61
|
end
|
48
62
|
|
49
|
-
|
63
|
+
##
|
64
|
+
# = command_name(pid = $$)
|
65
|
+
#
|
50
66
|
# Where pid is the process ID.
|
67
|
+
#
|
51
68
|
# By default it is the id of the current process ($$)
|
52
69
|
#
|
53
70
|
# It retuns the total command name of the process.
|
54
|
-
#
|
71
|
+
#
|
72
|
+
# The output is String. For example:
|
73
|
+
# LinuxStat::ProcessInfo.command_name
|
74
|
+
#
|
55
75
|
# "ruby"
|
56
76
|
#
|
57
77
|
# If the info isn't available it will return an empty frozen String.
|
@@ -65,146 +85,166 @@ module LinuxStat
|
|
65
85
|
File.split(_cmdline.tap(&:strip!).split[0])[-1]
|
66
86
|
end
|
67
87
|
|
68
|
-
|
88
|
+
##
|
89
|
+
# = mem_stat(pid = $$)
|
90
|
+
#
|
69
91
|
# Where pid is the process ID.
|
92
|
+
#
|
70
93
|
# By default it is the id of the current process ($$)
|
71
94
|
#
|
72
95
|
# It retuns the memory, virtual memory, and resident memory of the process.
|
73
|
-
# All values are in Kilobytes.
|
74
96
|
#
|
75
|
-
#
|
76
|
-
#
|
97
|
+
# All values are in kilobytes.
|
98
|
+
#
|
99
|
+
# The output is a Hash. For example:
|
100
|
+
# LinuxStat::ProcessInfo.mem_stat
|
101
|
+
#
|
102
|
+
# {:memory=>8515.584, :virtual_memory=>79781.888, :resident_memory=>13955.072}
|
77
103
|
#
|
78
|
-
# Note:
|
79
|
-
# If you need only memory usage of a process, run LinuxStat::ProcessInfo.memory(pid)
|
80
|
-
# If you need only virtual memory for a process, run LinuxStat::ProcessInfo.virtual_memory(pid)
|
81
|
-
# If you need only resident memory of a process, run LinuxStat::ProcessInfo.resident_memory(pid)
|
104
|
+
# * Note:
|
105
|
+
# 1. If you need only memory usage of a process, run LinuxStat::ProcessInfo.memory(pid)
|
106
|
+
# 2. If you need only virtual memory for a process, run LinuxStat::ProcessInfo.virtual_memory(pid)
|
107
|
+
# 3. If you need only resident memory of a process, run LinuxStat::ProcessInfo.resident_memory(pid)
|
82
108
|
#
|
83
109
|
# This method opens opens multiple files.
|
110
|
+
#
|
84
111
|
# But if you need all of the info, then running this method once is efficient.
|
85
112
|
#
|
86
113
|
# If the info isn't available it will return an empty Hash.
|
87
114
|
def mem_stat(pid = $$)
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
stat = if File.readable?(stat_file)
|
92
|
-
IO.read(stat_file).split
|
93
|
-
else
|
94
|
-
[]
|
95
|
-
end
|
96
|
-
|
97
|
-
status = if File.readable?(status_file)
|
98
|
-
IO.readlines(status_file)
|
99
|
-
else
|
100
|
-
[]
|
101
|
-
end
|
115
|
+
statm = "/proc/#{pid}/statm".freeze
|
116
|
+
return {} unless File.readable?(statm)
|
102
117
|
|
103
|
-
|
104
|
-
rss_anon = _rss_anon ? _rss_anon.split[1].to_i : nil
|
118
|
+
data = IO.read(statm).split
|
105
119
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
_vm_rss = status.find { |x| x.start_with?('VmRSS') }
|
110
|
-
vm_rss = _vm_rss ? _vm_rss.split[1].to_i : nil
|
120
|
+
_rss_anon = (data[1] && data[2]) ? data[1].to_i.-(data[2].to_i).*(pagesize).fdiv(1000) : nil
|
121
|
+
_virtual_memory = data[0] ? data[0].to_i*(pagesize).fdiv(1000) : nil
|
122
|
+
_resident_memory = data[1] ? data[1].to_i.*(pagesize).fdiv(1000) : nil
|
111
123
|
|
112
124
|
{
|
113
|
-
memory:
|
114
|
-
virtual_memory:
|
115
|
-
resident_memory:
|
125
|
+
memory: _rss_anon,
|
126
|
+
virtual_memory: _virtual_memory,
|
127
|
+
resident_memory: _resident_memory
|
116
128
|
}
|
117
129
|
end
|
118
130
|
|
119
|
-
|
131
|
+
##
|
132
|
+
# = memory(pid = $$)
|
133
|
+
#
|
120
134
|
# Where pid is the process ID.
|
135
|
+
#
|
121
136
|
# By default it is the id of the current process ($$)
|
122
137
|
#
|
123
138
|
# It retuns the memory of the process.
|
124
|
-
# The value is in
|
125
|
-
#
|
126
|
-
#
|
139
|
+
# The value is in kilobytes.
|
140
|
+
#
|
141
|
+
# The output is an Integer. For example:
|
142
|
+
# LinuxStat::ProcessInfo.memory
|
143
|
+
#
|
144
|
+
# 8523.776
|
127
145
|
#
|
128
146
|
# If the info isn't available it will return nil.
|
129
147
|
def memory(pid = $$)
|
130
|
-
file = "/proc/#{pid}/
|
148
|
+
file = "/proc/#{pid}/statm".freeze
|
131
149
|
return nil unless File.readable?(file)
|
132
150
|
|
133
|
-
|
134
|
-
|
151
|
+
data = IO.read(file).split
|
152
|
+
(data[1] && data[2]) ? data[1].to_i.-(data[2].to_i).*(pagesize).fdiv(1000) : nil
|
135
153
|
end
|
136
154
|
|
137
|
-
|
155
|
+
##
|
156
|
+
# = virtual_memory(pid = $$)
|
157
|
+
#
|
138
158
|
# Where pid is the process ID.
|
159
|
+
#
|
139
160
|
# By default it is the id of the current process ($$)
|
140
161
|
#
|
141
162
|
# It retuns the virtual memory for the process.
|
142
|
-
#
|
143
|
-
# The
|
144
|
-
#
|
163
|
+
#
|
164
|
+
# The value is in kilobytes.
|
165
|
+
#
|
166
|
+
# The output is an Integer. For example:
|
167
|
+
# LinuxStat::ProcessInfo.virtual_memory
|
168
|
+
#
|
169
|
+
# 79781.888
|
145
170
|
#
|
146
171
|
# If the info isn't available it will return nil.
|
147
172
|
def virtual_memory(pid = $$)
|
148
|
-
file = "/proc/#{pid}/
|
173
|
+
file = "/proc/#{pid}/statm".freeze
|
149
174
|
return nil unless File.readable?(file)
|
150
175
|
|
151
|
-
_virtual_memory = IO.read(file).split[
|
152
|
-
_virtual_memory ? _virtual_memory.to_i.fdiv(
|
176
|
+
_virtual_memory = IO.read(file).split[0]
|
177
|
+
_virtual_memory ? _virtual_memory.to_i.*(pagesize).fdiv(1000) : nil
|
153
178
|
end
|
154
179
|
|
155
|
-
|
180
|
+
##
|
181
|
+
# = resident_memory(pid = $$)
|
182
|
+
#
|
156
183
|
# Where pid is the process ID.
|
184
|
+
#
|
157
185
|
# By default it is the id of the current process ($$)
|
158
186
|
#
|
159
187
|
# It retuns the resident memory for the process.
|
160
|
-
#
|
161
|
-
# The
|
162
|
-
#
|
188
|
+
#
|
189
|
+
# The value is in kilobytes.
|
190
|
+
#
|
191
|
+
# The output is an Integer. For example:
|
192
|
+
# LinuxStat::ProcessInfo.cpu_stat
|
193
|
+
#
|
194
|
+
# => 13996.032
|
163
195
|
#
|
164
196
|
# If the info isn't available it will return nil.
|
165
197
|
def resident_memory(pid = $$)
|
166
|
-
file = "/proc/#{pid}/
|
198
|
+
file = "/proc/#{pid}/statm".freeze
|
167
199
|
return nil unless File.readable?(file)
|
168
200
|
|
169
|
-
_vm_rss = IO.
|
170
|
-
|
171
|
-
|
172
|
-
_vm_rss ? _vm_rss.split[1].to_i : nil
|
201
|
+
_vm_rss = IO.read(file).split[1]
|
202
|
+
_vm_rss ? _vm_rss.to_i.*(pagesize).fdiv(1000) : nil
|
173
203
|
end
|
174
204
|
|
175
|
-
|
205
|
+
##
|
206
|
+
# = cpu_stat(pid: $$, sleep: 1.0 / LinuxStat::Sysconf.sc_clk_tck)
|
207
|
+
#
|
176
208
|
# Where pid is the process ID and sleep time is the interval between measurements.
|
177
|
-
# By default it is the id of the current process ($$), and sleep is 0.05
|
178
209
|
#
|
179
|
-
#
|
180
|
-
#
|
181
|
-
#
|
210
|
+
# By default it is the id of the current process ($$), and sleep is LinuxStat::Sysconf.sc_clk_tck
|
211
|
+
#
|
212
|
+
# The smallest amount of available sleep time is 1.0 / LinuxStat::Sysconf.sc_clk_tck.
|
213
|
+
#
|
214
|
+
# * Note 1:
|
215
|
+
# 1. Do note that the sleep time can slow down your application.
|
216
|
+
# 2. And it's only needed for the cpu usage calculation.
|
182
217
|
#
|
183
218
|
# It retuns the CPU usage, threads, and the last executed CPU in Hash.
|
219
|
+
#
|
184
220
|
# For example:
|
185
|
-
#
|
221
|
+
# LinuxStat::ProcessInfo.cpu_stat
|
222
|
+
#
|
223
|
+
# => {:cpu_usage=>0.0, :threads=>1, :last_executed_cpu=>1}
|
186
224
|
#
|
187
225
|
# But if the info isn't available, it will return an empty Hash.
|
188
226
|
#
|
189
|
-
# The :cpu_usage is in percentage.
|
190
|
-
#
|
227
|
+
# The :cpu_usage is in percentage.
|
228
|
+
# It's also divided with the number of CPU.
|
229
|
+
#
|
230
|
+
# :cpu_usage for example, will return 25.0 if the CPU count
|
191
231
|
# is 4, and the process is using 100% of a thread / core.
|
192
|
-
#
|
232
|
+
#
|
233
|
+
# A value of 100.0 indicates it is using 100% processing power available to the system.
|
193
234
|
#
|
194
235
|
# The :threads returns the number of threads for the process.
|
195
236
|
# The value is a Integer.
|
196
237
|
#
|
197
|
-
# Note 2:
|
198
|
-
# If you just need the CPU usage run LinuxStat::ProcessInfo.cpu_usage(pid = $$)
|
199
|
-
# If you just need the threads run LinuxStat::ProcessInfo.threads(pid = $$)
|
200
|
-
# If you just need the last executed CPU run LinuxStat::ProcessInfo.last_executed_cpu(pid = $$)
|
201
|
-
# Running this method is slower and it opens multiple files at once
|
238
|
+
# * Note 2:
|
239
|
+
# 1. If you just need the CPU usage run LinuxStat::ProcessInfo.cpu_usage(pid = $$)
|
240
|
+
# 2. If you just need the threads run LinuxStat::ProcessInfo.threads(pid = $$)
|
241
|
+
# 3. If you just need the last executed CPU run LinuxStat::ProcessInfo.last_executed_cpu(pid = $$)
|
242
|
+
# 4. Running this method is slower and it opens multiple files at once
|
202
243
|
#
|
203
244
|
# Only use this method if you need all of the data at once, in such case, it's more efficient to use this method.
|
204
245
|
#
|
205
|
-
# The :last_executed_cpu also returns an Integer indicating
|
206
|
-
|
207
|
-
def cpu_stat(pid: $$, sleep: 0.05)
|
246
|
+
# The :last_executed_cpu also returns an Integer indicating the last executed cpu of the process.
|
247
|
+
def cpu_stat(pid: $$, sleep: ticks_to_ms)
|
208
248
|
file = "/proc/#{pid}/stat"
|
209
249
|
return {} unless File.readable?(file)
|
210
250
|
|
@@ -236,19 +276,29 @@ module LinuxStat
|
|
236
276
|
}
|
237
277
|
end
|
238
278
|
|
239
|
-
|
279
|
+
##
|
280
|
+
# = cpu_usage(pid: $$, sleep: 1.0 / LinuxStat::Sysconf.sc_clk_tck)
|
281
|
+
#
|
240
282
|
# Where pid is the process ID and sleep time is the interval between measurements.
|
241
|
-
#
|
283
|
+
#
|
284
|
+
# By default it is the id of the current process ($$), and sleep is 1.0 / LinuxStat::Sysconf.sc_clk_tck
|
285
|
+
#
|
286
|
+
# The smallest amount of available sleep time is LinuxStat::Sysconf.sc_clk_tck.
|
242
287
|
#
|
243
288
|
# It retuns the CPU usage in Float.
|
289
|
+
#
|
244
290
|
# For example:
|
245
|
-
#
|
246
|
-
#
|
291
|
+
#
|
292
|
+
# LinuxStat::ProcessInfo.cpu_usage
|
293
|
+
#
|
294
|
+
# => 10.0
|
295
|
+
#
|
296
|
+
# A value of 100.0 indicates it is using 100% processing power available to the system.
|
247
297
|
#
|
248
298
|
# But if the info isn't available, it will return nil.
|
249
299
|
#
|
250
300
|
# This method is more efficient than running LinuxStat::ProcessInfo.cpu_stat()
|
251
|
-
def cpu_usage(pid: $$, sleep:
|
301
|
+
def cpu_usage(pid: $$, sleep: ticks_to_ms)
|
252
302
|
file = "/proc/#{pid}/stat"
|
253
303
|
return nil unless File.readable?(file)
|
254
304
|
|
@@ -274,13 +324,20 @@ module LinuxStat
|
|
274
324
|
totald.-(idle2 - idle1).fdiv(totald).*(100).round(2).abs./(LinuxStat::CPU.count)
|
275
325
|
end
|
276
326
|
|
277
|
-
|
327
|
+
##
|
328
|
+
# = threads(pid = $$)
|
329
|
+
#
|
278
330
|
# Where pid is the process ID.
|
331
|
+
#
|
279
332
|
# By default it is the id of the current process ($$)
|
280
333
|
#
|
281
334
|
# It retuns the threads for the current process in Integer.
|
335
|
+
#
|
282
336
|
# For example:
|
283
|
-
#
|
337
|
+
# LinuxStat::ProcessInfo.threads
|
338
|
+
#
|
339
|
+
# => 2
|
340
|
+
#
|
284
341
|
# But if the info isn't available, it will return nil.
|
285
342
|
#
|
286
343
|
# This method is way more efficient than running LinuxStat::ProcessInfo.cpu_stat()
|
@@ -288,16 +345,24 @@ module LinuxStat
|
|
288
345
|
file = "/proc/#{pid}/stat".freeze
|
289
346
|
return nil unless File.readable?(file)
|
290
347
|
|
291
|
-
IO.read(file).split[19]
|
348
|
+
data = IO.read(file).split[19]
|
349
|
+
data ? data.to_i : nil
|
292
350
|
end
|
293
351
|
|
294
|
-
|
352
|
+
##
|
353
|
+
# = last_executed_cpu(pid = $$)
|
354
|
+
#
|
295
355
|
# Where pid is the process ID.
|
356
|
+
#
|
296
357
|
# By default it is the id of the current process ($$)
|
297
358
|
#
|
298
359
|
# It retuns the last executed CPU in Integer.
|
360
|
+
#
|
299
361
|
# For example:
|
300
|
-
#
|
362
|
+
# LinuxStat::ProcessInfo.last_executed_cpu
|
363
|
+
#
|
364
|
+
# => 2
|
365
|
+
#
|
301
366
|
# But if the info isn't available, it will return nil.
|
302
367
|
#
|
303
368
|
# This method is way more efficient than running LinuxStat::ProcessInfo.cpu_stat()
|
@@ -305,17 +370,84 @@ module LinuxStat
|
|
305
370
|
file = "/proc/#{pid}/stat".freeze
|
306
371
|
return nil unless File.readable?(file)
|
307
372
|
|
308
|
-
IO.read(
|
373
|
+
IO.read(file).split[38].to_i
|
309
374
|
end
|
310
375
|
|
311
|
-
|
376
|
+
##
|
377
|
+
# = uid(pid = $$)
|
378
|
+
#
|
379
|
+
# returns the UIDs of the process as an Array of Integers.
|
380
|
+
#
|
381
|
+
# If the info isn't available it returns an empty Array.
|
382
|
+
def uid(pid = $$)
|
383
|
+
file = "/proc/#{pid}/status".freeze
|
384
|
+
return nil unless File.readable?(file)
|
312
385
|
|
313
|
-
|
386
|
+
data = IO.readlines(file.freeze).find { |x|
|
387
|
+
x[/Uid.*\d*/]
|
388
|
+
}.to_s.split.drop(1)
|
389
|
+
|
390
|
+
{
|
391
|
+
real: data[0].to_i,
|
392
|
+
effective: data[1].to_i,
|
393
|
+
saved_set: data[2].to_i,
|
394
|
+
filesystem_uid: data[3].to_i
|
395
|
+
}
|
396
|
+
end
|
397
|
+
|
398
|
+
##
|
399
|
+
# = gid(pid = $$)
|
400
|
+
#
|
401
|
+
# returns the GIDs of the process as an Hash containing the following data:
|
402
|
+
#
|
403
|
+
# :real, :effective, :saved_set, :filesystem_uid
|
404
|
+
#
|
405
|
+
# If the info isn't available it returns an empty Hash.
|
406
|
+
def gid(pid = $$)
|
407
|
+
file = "/proc/#{pid}/status".freeze
|
408
|
+
return nil unless File.readable?(file)
|
409
|
+
|
410
|
+
data = IO.readlines(file.freeze).find { |x|
|
411
|
+
x[/Gid.*\d*/]
|
412
|
+
}.split.drop(1)
|
413
|
+
|
414
|
+
{
|
415
|
+
real: data[0].to_i,
|
416
|
+
effective: data[1].to_i,
|
417
|
+
saved_set: data[2].to_i,
|
418
|
+
filesystem_uid: data[3].to_i
|
419
|
+
}
|
420
|
+
end
|
421
|
+
|
422
|
+
##
|
423
|
+
# = owner(pid = $$)
|
424
|
+
#
|
425
|
+
# Returns the owner of the process
|
426
|
+
# But if the status is not available, it will return an empty frozen String.
|
427
|
+
def owner(pid = $$)
|
428
|
+
file = "/proc/#{pid}/status".freeze
|
429
|
+
return ''.freeze unless File.readable?(file)
|
430
|
+
|
431
|
+
gid = IO.readlines(file.freeze).find { |x|
|
432
|
+
x[/Gid.*\d*/]
|
433
|
+
}.split.drop(1)[2].to_i
|
434
|
+
|
435
|
+
LinuxStat::User.username_by_gid(gid)
|
436
|
+
end
|
314
437
|
|
315
438
|
private
|
316
439
|
def get_ticks
|
317
440
|
@@ticks ||= Sysconf.sc_clk_tck
|
318
441
|
end
|
442
|
+
|
443
|
+
# Just to avoid multiple calculations!...
|
444
|
+
def ticks_to_ms
|
445
|
+
@@ms ||= 1.0 / get_ticks
|
446
|
+
end
|
447
|
+
|
448
|
+
def pagesize
|
449
|
+
@@pagesize ||= LinuxStat::Sysconf.pagesize
|
450
|
+
end
|
319
451
|
end
|
320
452
|
end
|
321
453
|
end
|