linux_stat 0.6.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.
- checksums.yaml +4 -4
- data/README.md +553 -200
- data/exe/linuxstat.rb +92 -5
- data/ext/fs_stat/extconf.rb +5 -1
- data/ext/fs_stat/fs_stat.c +0 -1
- data/ext/sysconf/extconf.rb +5 -1
- data/ext/sysconf/sysconf.c +0 -1
- data/ext/utsname/extconf.rb +5 -1
- data/ext/utsname/utsname.c +0 -1
- data/lib/linux_stat/battery.rb +20 -1
- data/lib/linux_stat/bios.rb +8 -0
- data/lib/linux_stat/cpu.rb +20 -3
- 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 +24 -0
- data/lib/linux_stat/net.rb +12 -0
- data/lib/linux_stat/os.rb +17 -5
- data/lib/linux_stat/prettify_bytes.rb +47 -12
- data/lib/linux_stat/process.rb +10 -0
- data/lib/linux_stat/process_info.rb +243 -50
- data/lib/linux_stat/swap.rb +15 -1
- data/lib/linux_stat/user.rb +64 -11
- data/lib/linux_stat/version.rb +1 -1
- metadata +2 -2
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,22 +85,29 @@ 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.
|
96
|
+
#
|
73
97
|
# All values are in kilobytes.
|
74
98
|
#
|
75
|
-
# The output is a Hash. For example
|
99
|
+
# The output is a Hash. For example:
|
100
|
+
# LinuxStat::ProcessInfo.mem_stat
|
101
|
+
#
|
76
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.
|
@@ -101,13 +128,19 @@ module LinuxStat
|
|
101
128
|
}
|
102
129
|
end
|
103
130
|
|
104
|
-
|
131
|
+
##
|
132
|
+
# = memory(pid = $$)
|
133
|
+
#
|
105
134
|
# Where pid is the process ID.
|
135
|
+
#
|
106
136
|
# By default it is the id of the current process ($$)
|
107
137
|
#
|
108
138
|
# It retuns the memory of the process.
|
109
139
|
# The value is in kilobytes.
|
110
|
-
#
|
140
|
+
#
|
141
|
+
# The output is an Integer. For example:
|
142
|
+
# LinuxStat::ProcessInfo.memory
|
143
|
+
#
|
111
144
|
# 8523.776
|
112
145
|
#
|
113
146
|
# If the info isn't available it will return nil.
|
@@ -119,13 +152,20 @@ module LinuxStat
|
|
119
152
|
(data[1] && data[2]) ? data[1].to_i.-(data[2].to_i).*(pagesize).fdiv(1000) : nil
|
120
153
|
end
|
121
154
|
|
122
|
-
|
155
|
+
##
|
156
|
+
# = virtual_memory(pid = $$)
|
157
|
+
#
|
123
158
|
# Where pid is the process ID.
|
159
|
+
#
|
124
160
|
# By default it is the id of the current process ($$)
|
125
161
|
#
|
126
162
|
# It retuns the virtual memory for the process.
|
163
|
+
#
|
127
164
|
# The value is in kilobytes.
|
128
|
-
#
|
165
|
+
#
|
166
|
+
# The output is an Integer. For example:
|
167
|
+
# LinuxStat::ProcessInfo.virtual_memory
|
168
|
+
#
|
129
169
|
# 79781.888
|
130
170
|
#
|
131
171
|
# If the info isn't available it will return nil.
|
@@ -137,14 +177,21 @@ module LinuxStat
|
|
137
177
|
_virtual_memory ? _virtual_memory.to_i.*(pagesize).fdiv(1000) : nil
|
138
178
|
end
|
139
179
|
|
140
|
-
|
180
|
+
##
|
181
|
+
# = resident_memory(pid = $$)
|
182
|
+
#
|
141
183
|
# Where pid is the process ID.
|
184
|
+
#
|
142
185
|
# By default it is the id of the current process ($$)
|
143
186
|
#
|
144
187
|
# It retuns the resident memory for the process.
|
188
|
+
#
|
145
189
|
# The value is in kilobytes.
|
146
|
-
#
|
147
|
-
#
|
190
|
+
#
|
191
|
+
# The output is an Integer. For example:
|
192
|
+
# LinuxStat::ProcessInfo.cpu_stat
|
193
|
+
#
|
194
|
+
# => 13996.032
|
148
195
|
#
|
149
196
|
# If the info isn't available it will return nil.
|
150
197
|
def resident_memory(pid = $$)
|
@@ -155,40 +202,48 @@ module LinuxStat
|
|
155
202
|
_vm_rss ? _vm_rss.to_i.*(pagesize).fdiv(1000) : nil
|
156
203
|
end
|
157
204
|
|
158
|
-
|
205
|
+
##
|
206
|
+
# = cpu_stat(pid: $$, sleep: 1.0 / LinuxStat::Sysconf.sc_clk_tck)
|
207
|
+
#
|
159
208
|
# Where pid is the process ID and sleep time is the interval between measurements.
|
160
209
|
#
|
161
210
|
# By default it is the id of the current process ($$), and sleep is LinuxStat::Sysconf.sc_clk_tck
|
211
|
+
#
|
162
212
|
# The smallest amount of available sleep time is 1.0 / LinuxStat::Sysconf.sc_clk_tck.
|
163
213
|
#
|
164
|
-
# Note 1:
|
165
|
-
# Do note that the sleep time can slow down your application.
|
166
|
-
# And it's only needed for the cpu usage calculation.
|
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.
|
167
217
|
#
|
168
218
|
# It retuns the CPU usage, threads, and the last executed CPU in Hash.
|
219
|
+
#
|
169
220
|
# For example:
|
170
|
-
#
|
221
|
+
# LinuxStat::ProcessInfo.cpu_stat
|
222
|
+
#
|
223
|
+
# => {:cpu_usage=>0.0, :threads=>1, :last_executed_cpu=>1}
|
171
224
|
#
|
172
225
|
# But if the info isn't available, it will return an empty Hash.
|
173
226
|
#
|
174
|
-
# The :cpu_usage is in percentage.
|
175
|
-
#
|
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
|
176
231
|
# is 4, and the process is using 100% of a thread / core.
|
177
|
-
#
|
232
|
+
#
|
233
|
+
# A value of 100.0 indicates it is using 100% processing power available to the system.
|
178
234
|
#
|
179
235
|
# The :threads returns the number of threads for the process.
|
180
236
|
# The value is a Integer.
|
181
237
|
#
|
182
|
-
# Note 2:
|
183
|
-
# If you just need the CPU usage run LinuxStat::ProcessInfo.cpu_usage(pid = $$)
|
184
|
-
# If you just need the threads run LinuxStat::ProcessInfo.threads(pid = $$)
|
185
|
-
# If you just need the last executed CPU run LinuxStat::ProcessInfo.last_executed_cpu(pid = $$)
|
186
|
-
# 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
|
187
243
|
#
|
188
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.
|
189
245
|
#
|
190
|
-
# The :last_executed_cpu also returns an Integer indicating
|
191
|
-
# the last executed cpu of the process.
|
246
|
+
# The :last_executed_cpu also returns an Integer indicating the last executed cpu of the process.
|
192
247
|
def cpu_stat(pid: $$, sleep: ticks_to_ms)
|
193
248
|
file = "/proc/#{pid}/stat"
|
194
249
|
return {} unless File.readable?(file)
|
@@ -221,16 +276,24 @@ module LinuxStat
|
|
221
276
|
}
|
222
277
|
end
|
223
278
|
|
224
|
-
|
279
|
+
##
|
280
|
+
# = cpu_usage(pid: $$, sleep: 1.0 / LinuxStat::Sysconf.sc_clk_tck)
|
281
|
+
#
|
225
282
|
# Where pid is the process ID and sleep time is the interval between measurements.
|
226
283
|
#
|
227
284
|
# By default it is the id of the current process ($$), and sleep is 1.0 / LinuxStat::Sysconf.sc_clk_tck
|
285
|
+
#
|
228
286
|
# The smallest amount of available sleep time is LinuxStat::Sysconf.sc_clk_tck.
|
229
287
|
#
|
230
288
|
# It retuns the CPU usage in Float.
|
289
|
+
#
|
231
290
|
# For example:
|
232
|
-
#
|
233
|
-
#
|
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.
|
234
297
|
#
|
235
298
|
# But if the info isn't available, it will return nil.
|
236
299
|
#
|
@@ -261,13 +324,20 @@ module LinuxStat
|
|
261
324
|
totald.-(idle2 - idle1).fdiv(totald).*(100).round(2).abs./(LinuxStat::CPU.count)
|
262
325
|
end
|
263
326
|
|
264
|
-
|
327
|
+
##
|
328
|
+
# = threads(pid = $$)
|
329
|
+
#
|
265
330
|
# Where pid is the process ID.
|
331
|
+
#
|
266
332
|
# By default it is the id of the current process ($$)
|
267
333
|
#
|
268
334
|
# It retuns the threads for the current process in Integer.
|
335
|
+
#
|
269
336
|
# For example:
|
270
|
-
#
|
337
|
+
# LinuxStat::ProcessInfo.threads
|
338
|
+
#
|
339
|
+
# => 2
|
340
|
+
#
|
271
341
|
# But if the info isn't available, it will return nil.
|
272
342
|
#
|
273
343
|
# This method is way more efficient than running LinuxStat::ProcessInfo.cpu_stat()
|
@@ -275,17 +345,24 @@ module LinuxStat
|
|
275
345
|
file = "/proc/#{pid}/stat".freeze
|
276
346
|
return nil unless File.readable?(file)
|
277
347
|
|
278
|
-
data = IO.
|
348
|
+
data = IO.foreach(file, ' '.freeze).first(20)[-1]
|
279
349
|
data ? data.to_i : nil
|
280
350
|
end
|
281
351
|
|
282
|
-
|
352
|
+
##
|
353
|
+
# = last_executed_cpu(pid = $$)
|
354
|
+
#
|
283
355
|
# Where pid is the process ID.
|
356
|
+
#
|
284
357
|
# By default it is the id of the current process ($$)
|
285
358
|
#
|
286
359
|
# It retuns the last executed CPU in Integer.
|
360
|
+
#
|
287
361
|
# For example:
|
288
|
-
#
|
362
|
+
# LinuxStat::ProcessInfo.last_executed_cpu
|
363
|
+
#
|
364
|
+
# => 2
|
365
|
+
#
|
289
366
|
# But if the info isn't available, it will return nil.
|
290
367
|
#
|
291
368
|
# This method is way more efficient than running LinuxStat::ProcessInfo.cpu_stat()
|
@@ -296,7 +373,9 @@ module LinuxStat
|
|
296
373
|
IO.read(file).split[38].to_i
|
297
374
|
end
|
298
375
|
|
299
|
-
|
376
|
+
##
|
377
|
+
# = uid(pid = $$)
|
378
|
+
#
|
300
379
|
# returns the UIDs of the process as an Array of Integers.
|
301
380
|
#
|
302
381
|
# If the info isn't available it returns an empty Array.
|
@@ -304,7 +383,7 @@ module LinuxStat
|
|
304
383
|
file = "/proc/#{pid}/status".freeze
|
305
384
|
return nil unless File.readable?(file)
|
306
385
|
|
307
|
-
data = IO.
|
386
|
+
data = IO.foreach(file.freeze).find { |x|
|
308
387
|
x[/Uid.*\d*/]
|
309
388
|
}.to_s.split.drop(1)
|
310
389
|
|
@@ -316,16 +395,19 @@ module LinuxStat
|
|
316
395
|
}
|
317
396
|
end
|
318
397
|
|
319
|
-
|
398
|
+
##
|
399
|
+
# = gid(pid = $$)
|
400
|
+
#
|
320
401
|
# returns the GIDs of the process as an Hash containing the following data:
|
321
|
-
# :real, :effective, :saved_set, :filesystem_uid
|
322
402
|
#
|
323
|
-
#
|
403
|
+
# :real, :effective, :saved_set, :filesystem_uid
|
404
|
+
#
|
405
|
+
# If the info isn't available or the argument passed doesn't exist as a process ID, it will return an empty Hash.
|
324
406
|
def gid(pid = $$)
|
325
407
|
file = "/proc/#{pid}/status".freeze
|
326
408
|
return nil unless File.readable?(file)
|
327
409
|
|
328
|
-
data = IO.
|
410
|
+
data = IO.foreach(file.freeze).find { |x|
|
329
411
|
x[/Gid.*\d*/]
|
330
412
|
}.split.drop(1)
|
331
413
|
|
@@ -337,20 +419,131 @@ module LinuxStat
|
|
337
419
|
}
|
338
420
|
end
|
339
421
|
|
340
|
-
|
422
|
+
##
|
423
|
+
# = owner(pid = $$)
|
424
|
+
#
|
341
425
|
# Returns the owner of the process
|
342
426
|
# But if the status is not available, it will return an empty frozen String.
|
343
427
|
def owner(pid = $$)
|
344
428
|
file = "/proc/#{pid}/status".freeze
|
345
429
|
return ''.freeze unless File.readable?(file)
|
346
430
|
|
347
|
-
gid = IO.
|
431
|
+
gid = IO.foreach(file.freeze).find { |x|
|
348
432
|
x[/Gid.*\d*/]
|
349
433
|
}.split.drop(1)[2].to_i
|
350
434
|
|
351
435
|
LinuxStat::User.username_by_gid(gid)
|
352
436
|
end
|
353
437
|
|
438
|
+
##
|
439
|
+
# = start_time_epoch(pid = $$)
|
440
|
+
#
|
441
|
+
# Returns the epoch time (as Integer) the process was started.
|
442
|
+
#
|
443
|
+
# For example:
|
444
|
+
# LinuxStat::ProcessInfo.start_time_epoch 526
|
445
|
+
#
|
446
|
+
# => 1608097744
|
447
|
+
#
|
448
|
+
# If the info isn't available or the argument passed doesn't exist as a process ID, it will return nil.
|
449
|
+
def start_time_epoch(pid = $$)
|
450
|
+
stat_file = "/proc/#{pid}/stat".freeze
|
451
|
+
uptime = "/proc/uptime".freeze
|
452
|
+
|
453
|
+
@@u_readable ||= File.readable?(uptime)
|
454
|
+
return nil unless @@u_readable && File.readable?(stat_file)
|
455
|
+
|
456
|
+
u = IO.foreach(uptime, ' '.freeze).next.to_f
|
457
|
+
st = (IO.foreach(stat_file, ' '.freeze).first(22)[-1].to_f / get_ticks)
|
458
|
+
|
459
|
+
# Getting two Time objects and dealing with floating point numbers
|
460
|
+
# Just to make sure the time goes monotonically
|
461
|
+
Time.now.-(u - st).to_i
|
462
|
+
end
|
463
|
+
|
464
|
+
##
|
465
|
+
# = start_time(pid = $$)
|
466
|
+
#
|
467
|
+
# Returns the time (as Time object) the process was started.
|
468
|
+
#
|
469
|
+
# For example:
|
470
|
+
# LinuxStat::ProcessInfo.start_time 14183
|
471
|
+
#
|
472
|
+
# => 2020-12-16 13:31:43 +0000
|
473
|
+
#
|
474
|
+
# If the info isn't available or the argument passed doesn't exist as a process ID, it will return nil.
|
475
|
+
#
|
476
|
+
# The timezone returned based on current TZ.
|
477
|
+
# Thus the timezone could be affected by changing the ENV['TZ'] variable.
|
478
|
+
#
|
479
|
+
# Don't trust the timezone returned by the time.
|
480
|
+
def start_time(pid = $$)
|
481
|
+
# Getting two Time objects and dealing with floating point numbers
|
482
|
+
# Just to make sure the time goes monotonically
|
483
|
+
Time.at(start_time_epoch(pid))
|
484
|
+
end
|
485
|
+
|
486
|
+
##
|
487
|
+
# = running_time(pid = $$)
|
488
|
+
#
|
489
|
+
# Returns the time (in seconds, as Float) the process is running for.
|
490
|
+
#
|
491
|
+
# For example:
|
492
|
+
# LinuxStat::ProcessInfo.running_time 14183
|
493
|
+
#
|
494
|
+
# => 1947.619999999999
|
495
|
+
#
|
496
|
+
# If the info isn't available or the argument passed doesn't exist as a process ID, it will return nil.
|
497
|
+
def running_time(pid = $$)
|
498
|
+
stat_file = "/proc/#{pid}/stat".freeze
|
499
|
+
uptime = "/proc/uptime".freeze
|
500
|
+
|
501
|
+
@@u_readable ||= File.readable?(uptime)
|
502
|
+
return nil unless @@u_readable && File.readable?(stat_file)
|
503
|
+
|
504
|
+
IO.foreach(uptime, ' '.freeze).next.to_f - (IO.read(stat_file).split[21].to_i / get_ticks)
|
505
|
+
end
|
506
|
+
|
507
|
+
##
|
508
|
+
# = state(pid = $$)
|
509
|
+
# Returns the state of the process as a frozen String
|
510
|
+
#
|
511
|
+
# * A process could have multiple states:
|
512
|
+
#
|
513
|
+
# 1. S => Sleeping
|
514
|
+
#
|
515
|
+
# 2. R => Running
|
516
|
+
#
|
517
|
+
# 3. I => Idle
|
518
|
+
#
|
519
|
+
# 4. Z => Zombie
|
520
|
+
#
|
521
|
+
# It returns any one of them.
|
522
|
+
#
|
523
|
+
# If the info isn't available or the argument passed doesn't exist as a process ID,
|
524
|
+
# it will return an empty String.
|
525
|
+
def state(pid = $$)
|
526
|
+
file = "/proc/#{pid}/stat".freeze
|
527
|
+
return ''.freeze unless File.readable?(file)
|
528
|
+
IO.foreach(file, ' '.freeze).first(3)[-1].tap(&:rstrip!).freeze
|
529
|
+
end
|
530
|
+
|
531
|
+
##
|
532
|
+
# = nice(pid = $$)
|
533
|
+
# Returns the nice of the process
|
534
|
+
#
|
535
|
+
# The output value is an Integer ranging from -20 to 19
|
536
|
+
#
|
537
|
+
# -20 means the process has high priority, and 19 means the process has low priority
|
538
|
+
#
|
539
|
+
# If the info isn't available or the argument passed doesn't exist as a process ID, it will return nil.
|
540
|
+
def nice(pid = $$)
|
541
|
+
file = "/proc/#{pid}/stat"
|
542
|
+
return nil unless File.readable?(file)
|
543
|
+
|
544
|
+
IO.foreach(file, ' ').first(19)[-1].to_i
|
545
|
+
end
|
546
|
+
|
354
547
|
private
|
355
548
|
def get_ticks
|
356
549
|
@@ticks ||= Sysconf.sc_clk_tck
|