linux_stat 2.1.0 → 2.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +54 -1052
- data/exe/linuxstat.rb +2 -2
- data/ext/procfs/procfs.c +1 -0
- data/ext/procfs/stat.h +21 -0
- data/lib/linux_stat/battery.rb +32 -6
- data/lib/linux_stat/os.rb +11 -5
- data/lib/linux_stat/process_info.rb +58 -69
- data/lib/linux_stat/swap.rb +1 -0
- data/lib/linux_stat/version.rb +1 -1
- metadata +3 -3
data/exe/linuxstat.rb
CHANGED
@@ -150,9 +150,9 @@ execute.sort.each do |c|
|
|
150
150
|
disp_meth = "#{meth}"
|
151
151
|
disp_meth.concat(arg ? "(#{param})" : "(#{param})")
|
152
152
|
|
153
|
-
time =
|
153
|
+
time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
154
154
|
ret = arg ? e.send(meth, arg) : e.send(meth)
|
155
|
-
time2 =
|
155
|
+
time2 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
156
156
|
time = time2.-(time).*(1_000_000).round(3)
|
157
157
|
|
158
158
|
v = ret.inspect
|
data/ext/procfs/procfs.c
CHANGED
data/ext/procfs/stat.h
CHANGED
@@ -17,6 +17,27 @@ VALUE ps_state(VALUE obj, VALUE pid) {
|
|
17
17
|
return rb_str_new_cstr(_s) ;
|
18
18
|
}
|
19
19
|
|
20
|
+
VALUE ps_times(VALUE obj, VALUE pid) {
|
21
|
+
int _pid = FIX2INT(pid) ;
|
22
|
+
if (_pid < 0) return Qnil ;
|
23
|
+
|
24
|
+
char _path[22] ;
|
25
|
+
sprintf(_path, "/proc/%d/stat", _pid) ;
|
26
|
+
|
27
|
+
FILE *f = fopen(_path, "r") ;
|
28
|
+
if (!f) return Qnil ;
|
29
|
+
|
30
|
+
unsigned long utime, stime ;
|
31
|
+
|
32
|
+
char status = fscanf(f, "%*llu (%*[^)]%*[)] %*c %*d %*d %*d %*d %*d %*u %*lu %*lu %*lu %*lu %lu %lu", &utime, &stime) ;
|
33
|
+
fclose(f) ;
|
34
|
+
|
35
|
+
if (status != 2) return Qnil ;
|
36
|
+
double total_time = (utime + stime) / (float)sysconf(_SC_CLK_TCK);
|
37
|
+
|
38
|
+
return DBL2NUM(total_time) ;
|
39
|
+
}
|
40
|
+
|
20
41
|
VALUE ps_stat(VALUE obj, VALUE pid) {
|
21
42
|
int _pid = FIX2INT(pid) ;
|
22
43
|
if (_pid < 0) return rb_str_new_cstr("") ;
|
data/lib/linux_stat/battery.rb
CHANGED
@@ -105,11 +105,19 @@ module LinuxStat
|
|
105
105
|
#
|
106
106
|
# If the battery is not present or the information is not available, it will return nil.
|
107
107
|
def charge
|
108
|
-
@@charge_now_file ||= File.join(PATH, 'charge_now')
|
109
|
-
|
108
|
+
@@charge_now_file ||= if File.readable?(File.join(PATH, 'charge_now'))
|
109
|
+
File.join(PATH, 'charge_now').freeze
|
110
|
+
elsif File.readable?(File.join(PATH, 'energy_now'))
|
111
|
+
File.join(PATH, 'energy_now').freeze
|
112
|
+
end
|
113
|
+
|
114
|
+
@@charge_full_file ||= if File.readable?(File.join(PATH, 'charge_full'))
|
115
|
+
File.join(PATH, 'charge_full').freeze
|
116
|
+
elsif File.readable?(File.join(PATH, 'energy_full'))
|
117
|
+
File.join(PATH, 'energy_full').freeze
|
118
|
+
end
|
110
119
|
|
111
|
-
|
112
|
-
return nil unless @@charge_now_readable
|
120
|
+
return nil unless @@charge_now_file && @@charge_full_file
|
113
121
|
|
114
122
|
charge_now = IO.read(@@charge_now_file).to_i
|
115
123
|
charge_full = IO.read(@@charge_full_file).to_i
|
@@ -215,7 +223,16 @@ module LinuxStat
|
|
215
223
|
|
216
224
|
# charge now
|
217
225
|
cn_f = File.join(x, 'charge_now'.freeze).freeze
|
218
|
-
charge_now = File.readable?(cn_f)
|
226
|
+
charge_now = if File.readable?(cn_f)
|
227
|
+
IO.read(cn_f).to_i.fdiv(1_000_000)
|
228
|
+
else
|
229
|
+
en_f = File.join(x, 'energy_now'.freeze)
|
230
|
+
if File.readable?(en_f)
|
231
|
+
IO.read(en_f) .to_i.fdiv(1_000_000)
|
232
|
+
else
|
233
|
+
nil
|
234
|
+
end
|
235
|
+
end
|
219
236
|
|
220
237
|
# voltage min design
|
221
238
|
vmd_f = File.join(x, 'voltage_min_design'.freeze).freeze
|
@@ -227,7 +244,16 @@ module LinuxStat
|
|
227
244
|
|
228
245
|
# charge full
|
229
246
|
cf_f = File.join(x, 'charge_full'.freeze).freeze
|
230
|
-
charge_full = File.readable?(cf_f)
|
247
|
+
charge_full = if File.readable?(cf_f)
|
248
|
+
IO.read(cf_f).to_i.fdiv(1_000_000)
|
249
|
+
else
|
250
|
+
ef_f = File.join(x, 'energy_full'.freeze)
|
251
|
+
if File.readable?(ef_f)
|
252
|
+
IO.read(ef_f).to_i.fdiv(1_000_000)
|
253
|
+
else
|
254
|
+
nil
|
255
|
+
end
|
256
|
+
end
|
231
257
|
|
232
258
|
# status
|
233
259
|
s_f = File.join(x, 'status'.freeze).freeze
|
data/lib/linux_stat/os.rb
CHANGED
@@ -7,7 +7,11 @@ module LinuxStat
|
|
7
7
|
# Reads /etc/os-release and returns a Hash. For example:
|
8
8
|
# LinuxStat::OS.os_release
|
9
9
|
#
|
10
|
-
# => {:NAME=>"Arch Linux", :PRETTY_NAME=>"Arch Linux", :ID=>"arch", :BUILD_ID=>"rolling",
|
10
|
+
# => {:NAME=>"Arch Linux", :PRETTY_NAME=>"Arch Linux", :ID=>"arch", :BUILD_ID=>"rolling",
|
11
|
+
# :ANSI_COLOR=>"38;2;23;147;209", :HOME_URL=>"https://www.archlinux.org/",
|
12
|
+
# :DOCUMENTATION_URL=>"https://wiki.archlinux.org/",
|
13
|
+
# :SUPPORT_URL=>"https://bbs.archlinux.org/", :BUG_REPORT_URL=>"https://bugs.archlinux.org/",
|
14
|
+
# :LOGO=>"archlinux"}
|
11
15
|
#
|
12
16
|
# If the info isn't available, it will return an empty Hash.
|
13
17
|
#
|
@@ -127,7 +131,7 @@ module LinuxStat
|
|
127
131
|
#
|
128
132
|
# The return type is strictly Integer and doesn't fail.
|
129
133
|
def bits
|
130
|
-
@@bits ||= if
|
134
|
+
@@bits ||= if machine.end_with?('64') || RbConfig::CONFIG['host_cpu'].end_with?('64') || RUBY_PLATFORM.end_with?('64')
|
131
135
|
64
|
132
136
|
else
|
133
137
|
32
|
@@ -138,7 +142,7 @@ module LinuxStat
|
|
138
142
|
# Reads /proc/uptime and returns the system uptime:
|
139
143
|
# LinuxStat::OS.uptime
|
140
144
|
#
|
141
|
-
# => {:hour=>
|
145
|
+
# => {:hour=>16, :minute=>10, :second=>11, :jiffy=>20}
|
142
146
|
#
|
143
147
|
# Using uptime is 10x slower than using uptime_i
|
144
148
|
#
|
@@ -151,12 +155,14 @@ module LinuxStat
|
|
151
155
|
|
152
156
|
h = uptime_i / 3600
|
153
157
|
m = uptime_i % 3600 / 60
|
154
|
-
s =
|
158
|
+
s = uptime_i.%(60)
|
159
|
+
j = _uptime.-(uptime_i) * 100
|
155
160
|
|
156
161
|
{
|
157
162
|
hour: h,
|
158
163
|
minute: m,
|
159
|
-
second: s
|
164
|
+
second: s,
|
165
|
+
jiffy: j.to_i
|
160
166
|
}
|
161
167
|
end
|
162
168
|
|
@@ -283,7 +283,7 @@ module LinuxStat
|
|
283
283
|
uptime = LS::ProcFS.uptime_f
|
284
284
|
return {} unless uptime && !stat.empty?
|
285
285
|
|
286
|
-
utime, stime, starttime = *stat.values_at(
|
286
|
+
utime, stime, starttime = *stat.values_at(10, 11, 18).map(&:to_f)
|
287
287
|
uptime *= ticks
|
288
288
|
|
289
289
|
total_time = utime + stime
|
@@ -295,14 +295,14 @@ module LinuxStat
|
|
295
295
|
uptime = LS::ProcFS.uptime_f
|
296
296
|
return {} unless uptime && !stat.empty?
|
297
297
|
|
298
|
-
utime2, stime2, starttime2 = *stat.values_at(
|
298
|
+
utime2, stime2, starttime2 = *stat.values_at(10, 11, 18).map(&:to_f)
|
299
299
|
uptime *= ticks
|
300
300
|
|
301
301
|
total_time2 = utime2 + stime2
|
302
302
|
idle2 = uptime - starttime2 - total_time2
|
303
303
|
|
304
304
|
totald = idle2.+(total_time2).-(idle1 + total_time)
|
305
|
-
cpu_u = totald.-(idle2 - idle1).fdiv(totald).abs.*(100)./(
|
305
|
+
cpu_u = totald.-(idle2 - idle1).fdiv(totald).abs.*(100)./(LinuxStat::CPU.count)
|
306
306
|
|
307
307
|
{
|
308
308
|
cpu_usage: cpu_u > 100 ? 100.0 : cpu_u.round(2),
|
@@ -338,32 +338,10 @@ module LinuxStat
|
|
338
338
|
#
|
339
339
|
# This method is more efficient than running LinuxStat::ProcessInfo.cpu_stat()
|
340
340
|
def cpu_usage(pid: $$, sleep: ticks_to_ms_t5)
|
341
|
-
|
342
|
-
|
343
|
-
uptime = LS::ProcFS.uptime_f
|
344
|
-
return nil unless uptime && !stat.empty?
|
345
|
-
|
346
|
-
utime, stime, starttime = *stat.values_at(10, 11, 18).map(&:to_f)
|
347
|
-
uptime *= ticks
|
348
|
-
|
349
|
-
total_time = utime + stime
|
350
|
-
idle1 = uptime - starttime - total_time
|
351
|
-
|
352
|
-
sleep(sleep)
|
353
|
-
|
354
|
-
stat = LinuxStat::ProcFS.ps_stat(pid)
|
355
|
-
uptime = LS::ProcFS.uptime_f
|
356
|
-
return nil unless uptime && !stat.empty?
|
357
|
-
|
358
|
-
utime2, stime2, starttime2 = *stat.values_at(10, 11, 18).map(&:to_f)
|
359
|
-
uptime *= ticks
|
360
|
-
|
361
|
-
total_time2 = utime2 + stime2
|
362
|
-
idle2 = uptime - starttime2 - total_time2
|
363
|
-
|
364
|
-
totald = idle2.+(total_time2).-(idle1 + total_time)
|
341
|
+
u = cpu_usage_thread(pid, sleep)
|
342
|
+
return nil unless u
|
365
343
|
|
366
|
-
u
|
344
|
+
u /= LinuxStat::CPU.count
|
367
345
|
u > 100 ? 100.0 : u.round(2)
|
368
346
|
end
|
369
347
|
|
@@ -390,34 +368,10 @@ module LinuxStat
|
|
390
368
|
#
|
391
369
|
# But if the info isn't available, it will return nil.
|
392
370
|
def thread_usage(pid: $$, sleep: ticks_to_ms_t5)
|
393
|
-
|
394
|
-
|
395
|
-
uptime = LS::ProcFS.uptime_f
|
396
|
-
return nil unless uptime && !stat.empty?
|
397
|
-
|
398
|
-
utime, stime, starttime = *stat.values_at(10, 11, 18).map(&:to_f)
|
399
|
-
uptime *= ticks
|
400
|
-
|
401
|
-
total_time = utime + stime
|
402
|
-
idle1 = uptime - starttime - total_time
|
403
|
-
|
404
|
-
sleep(sleep)
|
405
|
-
|
406
|
-
stat = LinuxStat::ProcFS.ps_stat(pid)
|
407
|
-
uptime = LS::ProcFS.uptime_f
|
408
|
-
return nil unless uptime && !stat.empty?
|
409
|
-
|
410
|
-
utime2, stime2, starttime2 = *stat.values_at(10, 11, 18).map(&:to_f)
|
411
|
-
uptime *= ticks
|
412
|
-
|
413
|
-
total_time2 = utime2 + stime2
|
414
|
-
idle2 = uptime - starttime2 - total_time2
|
415
|
-
|
416
|
-
totald = idle2.+(total_time2).-(idle1 + total_time)
|
371
|
+
u = cpu_usage_thread(pid, sleep)
|
372
|
+
return nil unless u
|
417
373
|
|
418
|
-
|
419
|
-
|
420
|
-
cpu_count_t100 = cpu_count * 100
|
374
|
+
cpu_count_t100 = LinuxStat::CPU.count * 100
|
421
375
|
u > cpu_count_t100 ? cpu_count_t100 : u.round(2)
|
422
376
|
end
|
423
377
|
|
@@ -567,7 +521,9 @@ module LinuxStat
|
|
567
521
|
def start_time(pid = $$)
|
568
522
|
# Getting two Time objects and dealing with floating point numbers
|
569
523
|
# Just to make sure the time goes monotonically
|
570
|
-
|
524
|
+
_ste = start_time_epoch(pid)
|
525
|
+
return nil unless _ste
|
526
|
+
Time.at(_ste)
|
571
527
|
end
|
572
528
|
|
573
529
|
##
|
@@ -660,12 +616,10 @@ module LinuxStat
|
|
660
616
|
#
|
661
617
|
# Shows the CPU time used by the process.
|
662
618
|
#
|
663
|
-
# The return value is
|
619
|
+
# The return value is a Float.
|
620
|
+
# But if the info isn't available, it will return nil.
|
664
621
|
def cpu_time(pid = $$)
|
665
|
-
|
666
|
-
utime, stime, cutime, cstime = times[10], times[11], times[12], times[13]
|
667
|
-
|
668
|
-
utime.+(stime).+(cutime).+(cstime) / get_ticks
|
622
|
+
LinuxStat::ProcFS.ps_times(pid)
|
669
623
|
end
|
670
624
|
|
671
625
|
##
|
@@ -673,18 +627,28 @@ module LinuxStat
|
|
673
627
|
#
|
674
628
|
# Shows the CPU time used by the process.
|
675
629
|
#
|
676
|
-
# The return value is a Hash
|
630
|
+
# The return value is a Hash formatted like this:
|
631
|
+
# LS::ProcessInfo.cpu_times($$)
|
632
|
+
#
|
633
|
+
# => {:hour=>0, :minute=>39, :second=>12, :jiffy=>0.42}
|
634
|
+
#
|
635
|
+
# But if the info isn't available, it will return an empty Hash..
|
677
636
|
def cpu_times(pid = $$)
|
678
|
-
v =
|
637
|
+
v = LinuxStat::ProcFS.ps_times(pid)
|
638
|
+
return {} unless v
|
679
639
|
|
680
|
-
|
681
|
-
|
682
|
-
|
640
|
+
v_i = v.to_i
|
641
|
+
|
642
|
+
hour = v_i / 3600
|
643
|
+
min = v_i % 3600 / 60
|
644
|
+
sec = v_i % 60
|
645
|
+
jiffy = v.-(v_i) * 100
|
683
646
|
|
684
647
|
{
|
685
648
|
hour: hour,
|
686
649
|
minute: min,
|
687
|
-
second: sec
|
650
|
+
second: sec,
|
651
|
+
jiffy: jiffy.to_i
|
688
652
|
}
|
689
653
|
end
|
690
654
|
|
@@ -707,8 +671,33 @@ module LinuxStat
|
|
707
671
|
@@pagesize ||= LinuxStat::Sysconf.pagesize.to_i
|
708
672
|
end
|
709
673
|
|
710
|
-
def
|
711
|
-
|
674
|
+
def cpu_usage_thread(pid, delay)
|
675
|
+
ticks = get_ticks
|
676
|
+
stat = LinuxStat::ProcFS.ps_stat(pid)
|
677
|
+
uptime = LS::ProcFS.uptime_f
|
678
|
+
return nil unless uptime && !stat.empty?
|
679
|
+
|
680
|
+
utime, stime, starttime = *stat.values_at(10, 11, 18).map(&:to_f)
|
681
|
+
uptime *= ticks
|
682
|
+
|
683
|
+
total_time = utime + stime
|
684
|
+
idle1 = uptime - starttime - total_time
|
685
|
+
|
686
|
+
sleep(delay)
|
687
|
+
|
688
|
+
stat = LinuxStat::ProcFS.ps_stat(pid)
|
689
|
+
uptime = LS::ProcFS.uptime_f
|
690
|
+
return nil unless uptime && !stat.empty?
|
691
|
+
|
692
|
+
utime2, stime2, starttime2 = *stat.values_at(10, 11, 18).map(&:to_f)
|
693
|
+
uptime *= ticks
|
694
|
+
|
695
|
+
total_time2 = utime2 + stime2
|
696
|
+
idle2 = uptime - starttime2 - total_time2
|
697
|
+
|
698
|
+
totald = idle2.+(total_time2).-(idle1 + total_time)
|
699
|
+
|
700
|
+
totald.-(idle2 - idle1).fdiv(totald).abs.*(100)
|
712
701
|
end
|
713
702
|
end
|
714
703
|
end
|
data/lib/linux_stat/swap.rb
CHANGED
data/lib/linux_stat/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: linux_stat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sourav Goswami
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-03-09 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Linux only, efficient linux system utilization reporting and system monitoring
|
14
14
|
gem
|
@@ -85,7 +85,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
85
85
|
- !ruby/object:Gem::Version
|
86
86
|
version: '0'
|
87
87
|
requirements: []
|
88
|
-
rubygems_version: 3.
|
88
|
+
rubygems_version: 3.2.13
|
89
89
|
signing_key:
|
90
90
|
specification_version: 4
|
91
91
|
summary: Efficient linux system reporting gem
|