linux_stat 0.7.4 → 0.7.5
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 +8 -8
- data/lib/linux_stat/cpu.rb +8 -5
- data/lib/linux_stat/prettify_bytes.rb +1 -4
- data/lib/linux_stat/process_info.rb +59 -9
- data/lib/linux_stat/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 11d9c04524805452a0662ea750b21a137a69912f4f699f7582076c5fa46ad639
|
4
|
+
data.tar.gz: 6428b4caee5b475349b34c667c5d7157b6fb5d64b9a0a90dbdb160a8b3182042
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9cb66d951d9f1b7beb0115e3c0e040c377930efeeb323aa38c2cfd4af29988c6aa99041efdfbcb00c8e29383256254997c4d5297777c5cac7503834f279fa922
|
7
|
+
data.tar.gz: bc42532fabf55a172b9688c56e66e695e3caa45f4b94d2006601021cbec6569b8aea5a15dfcb50b82646350707e915fb5b76198c6ff37bf4cf79207d91542509
|
data/README.md
CHANGED
@@ -435,23 +435,23 @@ LinuxStat::OS.uptime()
|
|
435
435
|
```
|
436
436
|
# File: prettify_bytes.rb | Line: 42
|
437
437
|
# Definition: def convert_binary(n)
|
438
|
-
LinuxStat::PrettifyBytes.convert_binary( =
|
439
|
-
=> "
|
438
|
+
LinuxStat::PrettifyBytes.convert_binary(n = 948100170920596)
|
439
|
+
=> "862.29 tebibytes"
|
440
440
|
|
441
441
|
# File: prettify_bytes.rb | Line: 19
|
442
442
|
# Definition: def convert_decimal(n)
|
443
|
-
LinuxStat::PrettifyBytes.convert_decimal( =
|
444
|
-
=> "
|
443
|
+
LinuxStat::PrettifyBytes.convert_decimal(n = 778254889812055)
|
444
|
+
=> "778.25 terabytes"
|
445
445
|
|
446
446
|
# File: prettify_bytes.rb | Line: 90
|
447
447
|
# Definition: def convert_short_binary(n)
|
448
|
-
LinuxStat::PrettifyBytes.convert_short_binary( =
|
449
|
-
=> "
|
448
|
+
LinuxStat::PrettifyBytes.convert_short_binary(n = 924133737100164)
|
449
|
+
=> "840.49 TiB"
|
450
450
|
|
451
451
|
# File: prettify_bytes.rb | Line: 65
|
452
452
|
# Definition: def convert_short_decimal(n)
|
453
|
-
LinuxStat::PrettifyBytes.convert_short_decimal( =
|
454
|
-
=> "
|
453
|
+
LinuxStat::PrettifyBytes.convert_short_decimal(n = 723831850276707)
|
454
|
+
=> "723.83 TB"
|
455
455
|
|
456
456
|
```
|
457
457
|
|
data/lib/linux_stat/cpu.rb
CHANGED
@@ -2,7 +2,7 @@ module LinuxStat
|
|
2
2
|
module CPU
|
3
3
|
class << self
|
4
4
|
##
|
5
|
-
# = stat(sleep = 1.0 / LinuxStat::Sysconf.sc_clk_tck)
|
5
|
+
# = stat(sleep = 1.0 / LinuxStat::Sysconf.sc_clk_tck * 5)
|
6
6
|
#
|
7
7
|
# Where sleep is the delay to gather the data.
|
8
8
|
#
|
@@ -18,7 +18,7 @@ module LinuxStat
|
|
18
18
|
# {0=>84.38, 1=>100.0, 2=>50.0, 3=>87.5, 4=>87.5}
|
19
19
|
#
|
20
20
|
# If the information is not available, it will return an empty Hash
|
21
|
-
def stat(sleep =
|
21
|
+
def stat(sleep = ticks_to_ms_t5)
|
22
22
|
return {} unless stat?
|
23
23
|
|
24
24
|
data = IO.readlines('/proc/stat').select! { |x| x[/^cpu\d*/] }.map! { |x| x.split.map!(&:to_f) }
|
@@ -60,7 +60,7 @@ module LinuxStat
|
|
60
60
|
# It's like running LinuxStat::CPU.stat[0] but it's much more efficient and calculates just the aggregated usage which is available at the top of the /proc/stat file.
|
61
61
|
#
|
62
62
|
# If the information is not available, it will return nil.
|
63
|
-
def total_usage(sleep =
|
63
|
+
def total_usage(sleep = ticks_to_ms_t5)
|
64
64
|
return nil unless stat?
|
65
65
|
|
66
66
|
data = IO.foreach('/proc/stat').first.split.tap(&:shift).map!(&:to_f)
|
@@ -136,8 +136,11 @@ module LinuxStat
|
|
136
136
|
@@stat_readable ||= File.readable?('/proc/stat')
|
137
137
|
end
|
138
138
|
|
139
|
-
|
140
|
-
|
139
|
+
# Just to avoid duplicate calculations
|
140
|
+
# ticks to ms times 5
|
141
|
+
# If the ticks is 100, it will return 0.05
|
142
|
+
def ticks_to_ms_t5
|
143
|
+
@@ms_t5 ||= 1.0 / LinuxStat::Sysconf.sc_clk_tck * 5
|
141
144
|
end
|
142
145
|
end
|
143
146
|
end
|
@@ -100,10 +100,7 @@ module LinuxStat
|
|
100
100
|
|
101
101
|
private
|
102
102
|
def pad_left(n, mantissa_length = 2)
|
103
|
-
|
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
|
103
|
+
sprintf("%.#{mantissa_length}f".freeze, n)
|
107
104
|
end
|
108
105
|
end
|
109
106
|
end
|
@@ -203,13 +203,13 @@ module LinuxStat
|
|
203
203
|
end
|
204
204
|
|
205
205
|
##
|
206
|
-
# = cpu_stat(pid: $$, sleep: 1.0 / LinuxStat::Sysconf.sc_clk_tck)
|
206
|
+
# = cpu_stat(pid: $$, sleep: 1.0 / LinuxStat::Sysconf.sc_clk_tck * 5)
|
207
207
|
#
|
208
208
|
# Where pid is the process ID and sleep time is the interval between measurements.
|
209
209
|
#
|
210
210
|
# By default it is the id of the current process ($$), and sleep is LinuxStat::Sysconf.sc_clk_tck
|
211
211
|
#
|
212
|
-
# The smallest amount of available sleep time is 1.0 / LinuxStat::Sysconf.sc_clk_tck.
|
212
|
+
# The smallest amount of available sleep time is 1.0 / LinuxStat::Sysconf.sc_clk_tck * 5.
|
213
213
|
#
|
214
214
|
# * Note 1:
|
215
215
|
# 1. Do note that the sleep time can slow down your application.
|
@@ -244,7 +244,7 @@ module LinuxStat
|
|
244
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.
|
245
245
|
#
|
246
246
|
# The :last_executed_cpu also returns an Integer indicating the last executed cpu of the process.
|
247
|
-
def cpu_stat(pid: $$, sleep:
|
247
|
+
def cpu_stat(pid: $$, sleep: ticks_to_ms_t5)
|
248
248
|
file = "/proc/#{pid}/stat"
|
249
249
|
return {} unless File.readable?(file)
|
250
250
|
|
@@ -277,15 +277,15 @@ module LinuxStat
|
|
277
277
|
end
|
278
278
|
|
279
279
|
##
|
280
|
-
# = cpu_usage(pid: $$, sleep: 1.0 / LinuxStat::Sysconf.sc_clk_tck)
|
280
|
+
# = cpu_usage(pid: $$, sleep: 1.0 / LinuxStat::Sysconf.sc_clk_tck * 5)
|
281
281
|
#
|
282
282
|
# Where pid is the process ID and sleep time is the interval between measurements.
|
283
283
|
#
|
284
|
-
# By default it is the id of the current process ($$), and sleep is 1.0 / LinuxStat::Sysconf.sc_clk_tck
|
284
|
+
# By default it is the id of the current process ($$), and sleep is 1.0 / LinuxStat::Sysconf.sc_clk_tck * 5
|
285
285
|
#
|
286
286
|
# The smallest amount of available sleep time is LinuxStat::Sysconf.sc_clk_tck.
|
287
287
|
#
|
288
|
-
# It retuns the CPU usage
|
288
|
+
# It retuns the CPU usage as Float.
|
289
289
|
#
|
290
290
|
# For example:
|
291
291
|
#
|
@@ -298,7 +298,7 @@ module LinuxStat
|
|
298
298
|
# But if the info isn't available, it will return nil.
|
299
299
|
#
|
300
300
|
# This method is more efficient than running LinuxStat::ProcessInfo.cpu_stat()
|
301
|
-
def cpu_usage(pid: $$, sleep:
|
301
|
+
def cpu_usage(pid: $$, sleep: ticks_to_ms_t5)
|
302
302
|
file = "/proc/#{pid}/stat"
|
303
303
|
return nil unless File.readable?(file)
|
304
304
|
|
@@ -324,6 +324,54 @@ module LinuxStat
|
|
324
324
|
totald.-(idle2 - idle1).fdiv(totald).*(100).round(2).abs./(LinuxStat::CPU.count)
|
325
325
|
end
|
326
326
|
|
327
|
+
##
|
328
|
+
# = thread_usage(pid: $$, sleep: 1.0 / LinuxStat::Sysconf.sc_clk_tck * 5)
|
329
|
+
#
|
330
|
+
# Where pid is the process ID and sleep time is the interval between measurements.
|
331
|
+
#
|
332
|
+
# By default it is the id of the current process ($$), and sleep is 1.0 / LinuxStat::Sysconf.sc_clk_tck * 5
|
333
|
+
#
|
334
|
+
# The smallest amount of available sleep time is LinuxStat::Sysconf.sc_clk_tck.
|
335
|
+
#
|
336
|
+
# It retuns the per core CPU usage as Float.
|
337
|
+
#
|
338
|
+
# For example:
|
339
|
+
#
|
340
|
+
# LinuxStat::ProcessInfo.core_usage
|
341
|
+
#
|
342
|
+
# => 200.0
|
343
|
+
#
|
344
|
+
# A value of 100.0 indicates it is using 100% processing power of a core.
|
345
|
+
#
|
346
|
+
# The value could be 0 to (100 * the number of CPU threads (including hyperthreading) in the system)
|
347
|
+
#
|
348
|
+
# But if the info isn't available, it will return nil.
|
349
|
+
def thread_usage(pid: $$, sleep: ticks_to_ms_t5)
|
350
|
+
file = "/proc/#{pid}/stat"
|
351
|
+
return nil unless File.readable?(file)
|
352
|
+
|
353
|
+
ticks = get_ticks
|
354
|
+
|
355
|
+
utime, stime, starttime = IO.read(file)
|
356
|
+
.split.values_at(13, 14, 21).map(&:to_f)
|
357
|
+
uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
|
358
|
+
|
359
|
+
total_time = utime + stime
|
360
|
+
idle1 = uptime - starttime - total_time
|
361
|
+
|
362
|
+
sleep(sleep)
|
363
|
+
|
364
|
+
utime2, stime2, starttime2 = IO.read(file)
|
365
|
+
.split.values_at(13, 14, 21).map(&:to_f)
|
366
|
+
uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
|
367
|
+
|
368
|
+
total_time2 = utime2 + stime2
|
369
|
+
idle2 = uptime - starttime2 - total_time2
|
370
|
+
|
371
|
+
totald = idle2.+(total_time2).-(idle1 + total_time)
|
372
|
+
totald.-(idle2 - idle1).fdiv(totald).*(100).round(2).abs
|
373
|
+
end
|
374
|
+
|
327
375
|
##
|
328
376
|
# = threads(pid = $$)
|
329
377
|
#
|
@@ -550,8 +598,10 @@ module LinuxStat
|
|
550
598
|
end
|
551
599
|
|
552
600
|
# Just to avoid multiple calculations!...
|
553
|
-
|
554
|
-
|
601
|
+
# ticks to ms * 5
|
602
|
+
# If ticks is 100, it will return 0.05
|
603
|
+
def ticks_to_ms_t5
|
604
|
+
@@ms_t5 ||= 1.0 / get_ticks * 5
|
555
605
|
end
|
556
606
|
|
557
607
|
def pagesize
|
data/lib/linux_stat/version.rb
CHANGED