linux_stat 1.4.0 → 2.1.1

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.
@@ -149,20 +149,7 @@ module LinuxStat
149
149
  #
150
150
  # If the info isn't available it will return an empty Hash.
151
151
  def mem_stat(pid = $$)
152
- statm = "/proc/#{pid}/statm".freeze
153
- return {} unless File.readable?(statm)
154
-
155
- data = IO.read(statm).split
156
-
157
- _rss_anon = (data[1] && data[2]) ? data[1].to_i.-(data[2].to_i).*(pagesize).fdiv(1000) : nil
158
- _virtual_memory = data[0] ? data[0].to_i*(pagesize).fdiv(1000) : nil
159
- _resident_memory = data[1] ? data[1].to_i.*(pagesize).fdiv(1000) : nil
160
-
161
- {
162
- memory: _rss_anon,
163
- virtual_memory: _virtual_memory,
164
- resident_memory: _resident_memory
165
- }
152
+ LinuxStat::ProcFS.statm(pid)
166
153
  end
167
154
 
168
155
  ##
@@ -182,11 +169,7 @@ module LinuxStat
182
169
  #
183
170
  # If the info isn't available it will return nil.
184
171
  def memory(pid = $$)
185
- file = "/proc/#{pid}/statm".freeze
186
- return nil unless File.readable?(file)
187
-
188
- data = IO.read(file).split
189
- (data[1] && data[2]) ? data[1].to_i.-(data[2].to_i).*(pagesize).fdiv(1000) : nil
172
+ LinuxStat::ProcFS.statm_memory(pid) &.fdiv(1000)
190
173
  end
191
174
 
192
175
  ##
@@ -207,11 +190,7 @@ module LinuxStat
207
190
  #
208
191
  # If the info isn't available it will return nil.
209
192
  def virtual_memory(pid = $$)
210
- file = "/proc/#{pid}/statm".freeze
211
- return nil unless File.readable?(file)
212
-
213
- _virtual_memory = IO.read(file).split[0]
214
- _virtual_memory ? _virtual_memory.to_i.*(pagesize).fdiv(1000) : nil
193
+ LinuxStat::ProcFS.statm_virtual(pid) &.fdiv(1000)
215
194
  end
216
195
 
217
196
  ##
@@ -226,17 +205,34 @@ module LinuxStat
226
205
  # The value is in kilobytes.
227
206
  #
228
207
  # The output is an Integer. For example:
229
- # LinuxStat::ProcessInfo.cpu_stat
208
+ # LinuxStat::ProcessInfo.resident_memory
230
209
  #
231
210
  # => 13996.032
232
211
  #
233
212
  # If the info isn't available it will return nil.
234
213
  def resident_memory(pid = $$)
235
- file = "/proc/#{pid}/statm".freeze
236
- return nil unless File.readable?(file)
214
+ LinuxStat::ProcFS.statm_resident(pid) &.fdiv(1000)
215
+ end
237
216
 
238
- _vm_rss = IO.read(file).split[1]
239
- _vm_rss ? _vm_rss.to_i.*(pagesize).fdiv(1000) : nil
217
+ ##
218
+ # = shared_memory(pid = $$)
219
+ #
220
+ # Where pid is the process ID.
221
+ #
222
+ # By default it is the id of the current process ($$)
223
+ #
224
+ # It retuns the shared memory for the process.
225
+ #
226
+ # The value is in kilobytes.
227
+ #
228
+ # The output is an Integer. For example:
229
+ # LinuxStat::ProcessInfo.shared_memory
230
+ #
231
+ # => 13996.032
232
+ #
233
+ # If the info isn't available it will return nil.
234
+ def shared_memory(pid = $$)
235
+ LinuxStat::ProcFS.statm_shared(pid) &.fdiv(1000)
240
236
  end
241
237
 
242
238
  ##
@@ -282,31 +278,25 @@ module LinuxStat
282
278
  #
283
279
  # The :last_executed_cpu also returns an Integer indicating the last executed cpu of the process.
284
280
  def cpu_stat(pid: $$, sleep: ticks_to_ms_t5)
285
- file = "/proc/#{pid}/stat"
286
281
  ticks = get_ticks
287
-
288
- return {} unless File.readable?(file)
289
-
290
- stat = IO.read(file).split(/(\(.*\))/)[-1] &.split
291
- return {} unless stat
282
+ stat = LinuxStat::ProcFS.ps_stat(pid)
283
+ uptime = LS::ProcFS.uptime_f
284
+ return {} unless uptime && !stat.empty?
292
285
 
293
286
  utime, stime, starttime = *stat.values_at(11, 12, 19).map(&:to_f)
294
-
295
- uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
287
+ uptime *= ticks
296
288
 
297
289
  total_time = utime + stime
298
290
  idle1 = uptime - starttime - total_time
299
291
 
300
292
  sleep(sleep)
301
293
 
302
- return {} unless File.readable?(file)
303
-
304
- stat = IO.read(file).split(/(\(.*\))/)[-1] &.split
305
- return {} unless stat
294
+ stat = LinuxStat::ProcFS.ps_stat(pid)
295
+ uptime = LS::ProcFS.uptime_f
296
+ return {} unless uptime && !stat.empty?
306
297
 
307
298
  utime2, stime2, starttime2 = *stat.values_at(11, 12, 19).map(&:to_f)
308
-
309
- uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
299
+ uptime *= ticks
310
300
 
311
301
  total_time2 = utime2 + stime2
312
302
  idle2 = uptime - starttime2 - total_time2
@@ -348,30 +338,25 @@ module LinuxStat
348
338
  #
349
339
  # This method is more efficient than running LinuxStat::ProcessInfo.cpu_stat()
350
340
  def cpu_usage(pid: $$, sleep: ticks_to_ms_t5)
351
- file = "/proc/#{pid}/stat"
352
341
  ticks = get_ticks
342
+ stat = LinuxStat::ProcFS.ps_stat(pid)
343
+ uptime = LS::ProcFS.uptime_f
344
+ return nil unless uptime && !stat.empty?
353
345
 
354
- return nil unless File.readable?(file)
355
-
356
- stat = IO.read(file).split(/(\(.*\))/)[-1] &.split
357
- return nil unless stat
358
-
359
- utime, stime, starttime = *stat.values_at(11, 12, 19).map(&:to_f)
360
-
361
- uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
346
+ utime, stime, starttime = *stat.values_at(10, 11, 18).map(&:to_f)
347
+ uptime *= ticks
362
348
 
363
349
  total_time = utime + stime
364
350
  idle1 = uptime - starttime - total_time
365
351
 
366
352
  sleep(sleep)
367
353
 
368
- return nil unless File.readable?(file)
369
-
370
- stat = IO.read(file).split(/(\(.*\))/)[-1] &.split
371
- return nil unless stat
354
+ stat = LinuxStat::ProcFS.ps_stat(pid)
355
+ uptime = LS::ProcFS.uptime_f
356
+ return nil unless uptime && !stat.empty?
372
357
 
373
- utime2, stime2, starttime2 = *stat.values_at(11, 12, 19).map(&:to_f)
374
- uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
358
+ utime2, stime2, starttime2 = *stat.values_at(10, 11, 18).map(&:to_f)
359
+ uptime *= ticks
375
360
 
376
361
  total_time2 = utime2 + stime2
377
362
  idle2 = uptime - starttime2 - total_time2
@@ -405,31 +390,25 @@ module LinuxStat
405
390
  #
406
391
  # But if the info isn't available, it will return nil.
407
392
  def thread_usage(pid: $$, sleep: ticks_to_ms_t5)
408
- file = "/proc/#{pid}/stat"
409
393
  ticks = get_ticks
394
+ stat = LinuxStat::ProcFS.ps_stat(pid)
395
+ uptime = LS::ProcFS.uptime_f
396
+ return nil unless uptime && !stat.empty?
410
397
 
411
- return nil unless File.readable?(file)
412
-
413
- stat = IO.read(file).split(/(\(.*\))/)[-1] &.split
414
- return nil unless stat
415
-
416
- utime, stime, starttime = *stat.values_at(11, 12, 19).map(&:to_f)
417
-
418
- uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
398
+ utime, stime, starttime = *stat.values_at(10, 11, 18).map(&:to_f)
399
+ uptime *= ticks
419
400
 
420
401
  total_time = utime + stime
421
402
  idle1 = uptime - starttime - total_time
422
403
 
423
404
  sleep(sleep)
424
405
 
425
- return nil unless File.readable?(file)
426
-
427
- stat = IO.read(file).split(/(\(.*\))/)[-1] &.split
428
- return nil unless stat
406
+ stat = LinuxStat::ProcFS.ps_stat(pid)
407
+ uptime = LS::ProcFS.uptime_f
408
+ return nil unless uptime && !stat.empty?
429
409
 
430
- utime2, stime2, starttime2 = *stat.values_at(11, 12, 19).map(&:to_f)
431
-
432
- uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
410
+ utime2, stime2, starttime2 = *stat.values_at(10, 11, 18).map(&:to_f)
411
+ uptime *= ticks
433
412
 
434
413
  total_time2 = utime2 + stime2
435
414
  idle2 = uptime - starttime2 - total_time2
@@ -460,11 +439,7 @@ module LinuxStat
460
439
  #
461
440
  # This method is way more efficient than running LinuxStat::ProcessInfo.cpu_stat()
462
441
  def threads(pid = $$)
463
- file = "/proc/#{pid}/stat".freeze
464
- return nil unless File.readable?(file)
465
-
466
- data = IO.read(file).split(/(\(.*\))/)[-1] &.split &.at(17)
467
- data ? data.to_i : nil
442
+ LinuxStat::ProcFS.ps_stat(pid)[16]
468
443
  end
469
444
 
470
445
  ##
@@ -485,11 +460,7 @@ module LinuxStat
485
460
  #
486
461
  # This method is way more efficient than running LinuxStat::ProcessInfo.cpu_stat()
487
462
  def last_executed_cpu(pid = $$)
488
- file = "/proc/#{pid}/stat".freeze
489
- return nil unless File.readable?(file)
490
-
491
- data = IO.read(file).split(/(\(.*\))/)[-1] &.split &.at(36)
492
- data ? data.to_i : nil
463
+ LinuxStat::ProcFS.ps_stat(pid)[35]
493
464
  end
494
465
 
495
466
  ##
@@ -566,21 +537,15 @@ module LinuxStat
566
537
  #
567
538
  # If the info isn't available or the argument passed doesn't exist as a process ID, it will return nil.
568
539
  def start_time_epoch(pid = $$)
569
- stat_file = "/proc/#{pid}/stat".freeze
570
- uptime = "/proc/uptime".freeze
571
-
572
- @@u_readable ||= File.readable?(uptime)
573
- return nil unless @@u_readable && File.readable?(stat_file)
540
+ uptime = LS::ProcFS.uptime_f
541
+ stat = LinuxStat::ProcFS.ps_stat(pid)[18]
574
542
 
575
- stat = IO.read(stat_file).split(/(\(.*\))/)[-1] &.split
576
- return nil unless stat
577
-
578
- u = IO.foreach(uptime, ' '.freeze).next.to_f
579
- st = stat[19].to_f / get_ticks
543
+ return nil unless uptime && stat
544
+ st = stat.to_f / get_ticks
580
545
 
581
546
  # Getting two Time objects and dealing with floating point numbers
582
- # Just to make sure the time goes monotonically
583
- Time.now.-(u - st).to_i
547
+ # Just to make sure the time goes monotonically unless the clock changes
548
+ Time.now.-(uptime - st).to_i
584
549
  end
585
550
 
586
551
  ##
@@ -619,17 +584,10 @@ module LinuxStat
619
584
  #
620
585
  # If the info isn't available or the argument passed doesn't exist as a process ID, it will return nil.
621
586
  def running_time(pid = $$)
622
- stat_file = "/proc/#{pid}/stat".freeze
623
- uptime = "/proc/uptime".freeze
624
-
625
- @@u_readable ||= File.readable?(uptime)
626
- return nil unless @@u_readable && File.readable?(stat_file)
627
-
628
- stat = IO.read(stat_file).split(/(\(.*\))/)[-1] &.split
629
- return nil unless stat
630
-
631
- IO.foreach(uptime, ' '.freeze).next.to_f
632
- .-(stat[19].to_f / get_ticks).round(2)
587
+ uptime = LS::ProcFS.uptime_f
588
+ stat = LinuxStat::ProcFS.ps_stat(pid)[18]
589
+ return nil unless uptime && stat
590
+ uptime.-(stat.to_f / get_ticks).round(2)
633
591
  end
634
592
 
635
593
  ##
@@ -651,13 +609,7 @@ module LinuxStat
651
609
  # If the info isn't available or the argument passed doesn't exist as a process ID,
652
610
  # it will return an empty String.
653
611
  def state(pid = $$)
654
- file = "/proc/#{pid}/stat".freeze
655
- return ''.freeze unless File.readable?(file)
656
-
657
- stat = IO.read(file).split(/(\(.*\))/)[-1]
658
- return '' unless stat
659
-
660
- stat[/\s.+?/].strip
612
+ LinuxStat::ProcFS.ps_state(pid)
661
613
  end
662
614
 
663
615
  ##
@@ -670,13 +622,7 @@ module LinuxStat
670
622
  #
671
623
  # If the info isn't available or the argument passed doesn't exist as a process ID, it will return nil.
672
624
  def nice(pid = $$)
673
- file = "/proc/#{pid}/stat"
674
- return nil unless File.readable?(file)
675
-
676
- stat = IO.read(file).split(/(\(.*\))/)[-1] &.split
677
- return nil unless stat
678
-
679
- stat[16].to_i
625
+ LinuxStat::ProcFS.ps_stat(pid)[15]
680
626
  end
681
627
 
682
628
  ##
@@ -709,6 +655,41 @@ module LinuxStat
709
655
  LinuxStat::Nproc.count_cpu_for_pid(pid)
710
656
  end
711
657
 
658
+ ##
659
+ # = def cpu_times_i(pid = $$)
660
+ #
661
+ # Shows the CPU time used by the process.
662
+ #
663
+ # The return value is an Integer.
664
+ def cpu_time(pid = $$)
665
+ times = LinuxStat::ProcFS.ps_stat(pid)
666
+ utime, stime, cutime, cstime = times[10], times[11], times[12], times[13]
667
+ return nil unless utime && stime && cutime && cstime
668
+
669
+ utime.+(stime).+(cutime).+(cstime) / get_ticks
670
+ end
671
+
672
+ ##
673
+ # = def cpu_times(pid = $$)
674
+ #
675
+ # Shows the CPU time used by the process.
676
+ #
677
+ # The return value is a Hash.
678
+ def cpu_times(pid = $$)
679
+ v = cpu_time(pid)
680
+ return {} unless v
681
+
682
+ hour = v / 3600
683
+ min = v % 3600 / 60
684
+ sec = v % 60
685
+
686
+ {
687
+ hour: hour,
688
+ minute: min,
689
+ second: sec
690
+ }
691
+ end
692
+
712
693
  alias count_cpu nproc
713
694
 
714
695
  private
@@ -35,7 +35,10 @@ module LinuxStat
35
35
  return {} unless swaps_readable?
36
36
  values_t = read_usage
37
37
 
38
- total, used = values_t[0].sum, values_t[-1].sum
38
+ _total, _used = values_t[0], values_t[-1]
39
+ return {} if _total.empty? || _used.empty?
40
+
41
+ total, used = _total.reduce(:+), _used.reduce(:+)
39
42
  available = total - used
40
43
  percent_used = total == 0 ? 0.0 : used.*(100).fdiv(total).round(2)
41
44
  percent_available = total == 0.0 ? 0 : available.*(100).fdiv(total).round(2)
@@ -57,8 +60,19 @@ module LinuxStat
57
60
  #
58
61
  # The return type is a Integer but if the info isn't available, it will return nil.
59
62
  def total
60
- return nil unless swaps_readable?
61
- read_usage[0].sum
63
+ v = LinuxStat::Sysinfo.totalswap
64
+ v ? v.fdiv(1024).to_i : nil
65
+ end
66
+
67
+ ##
68
+ # Shows free swap.
69
+ #
70
+ # The value is in kilobytes.
71
+ #
72
+ # The return type is a Integer but if the info isn't available, it will return nil.
73
+ def free
74
+ v = LinuxStat::Sysinfo.freeswap
75
+ v ? v.fdiv(1024).to_i : nil
62
76
  end
63
77
 
64
78
  ##
@@ -70,7 +84,9 @@ module LinuxStat
70
84
  def available
71
85
  return nil unless swaps_readable?
72
86
  values_t = read_usage
73
- values_t[0].sum - values_t[1].sum
87
+ t = values_t[0].reduce(:+)
88
+ u = values_t[1].reduce(:+)
89
+ (t && u) ? t - u : nil
74
90
  end
75
91
 
76
92
  ##
@@ -81,7 +97,7 @@ module LinuxStat
81
97
  # The return type is a Integer but if the info isn't available, it will return nil.
82
98
  def used
83
99
  return nil unless swaps_readable?
84
- read_usage[-1].sum
100
+ read_usage[-1].reduce(:+)
85
101
  end
86
102
 
87
103
  ##
@@ -92,10 +108,16 @@ module LinuxStat
92
108
  return nil unless swaps_readable?
93
109
  values_t = read_usage
94
110
 
95
- total = values_t[0].sum
111
+ _total = values_t[0]
112
+ _used = values_t[-1]
113
+ return nil if _total.empty? || _used.empty?
114
+
115
+ total = _total.reduce(:+)
116
+ used = _used.reduce(:+)
117
+
96
118
  return 0.0 if total == 0
97
119
 
98
- values_t[-1].sum.*(100).fdiv(total).round(2)
120
+ used.*(100).fdiv(total).round(2)
99
121
  end
100
122
 
101
123
  ##
@@ -106,10 +128,15 @@ module LinuxStat
106
128
  return nil unless swaps_readable?
107
129
  values_t = read_usage
108
130
 
109
- total = values_t[0].sum
131
+ _total = values_t[0]
132
+ _used = values_t[-1]
133
+ return nil if _total.empty? || _used.empty?
134
+
135
+ total, used = _total.reduce(:+), _used.reduce(:+)
136
+
110
137
  return 0.0 if total == 0
111
138
 
112
- total.-(values_t[-1].sum).*(100).fdiv(total).round(2)
139
+ total.-(used).*(100).fdiv(total).round(2)
113
140
  end
114
141
 
115
142
  private
@@ -1,3 +1,3 @@
1
1
  module LinuxStat
2
- VERSION ||= "1.4.0"
2
+ VERSION ||= "2.1.1"
3
3
  end
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: 1.4.0
4
+ version: 2.1.1
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-01-19 00:00:00.000000000 Z
11
+ date: 2021-02-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Linux only, efficient linux system utilization reporting and system monitoring
14
14
  gem
@@ -19,7 +19,9 @@ executables:
19
19
  extensions:
20
20
  - ext/fs_stat/extconf.rb
21
21
  - ext/nproc/extconf.rb
22
+ - ext/procfs/extconf.rb
22
23
  - ext/sysconf/extconf.rb
24
+ - ext/sysinfo/extconf.rb
23
25
  - ext/utsname/extconf.rb
24
26
  extra_rdoc_files:
25
27
  - README.md
@@ -33,8 +35,16 @@ files:
33
35
  - ext/fs_stat/fs_stat.c
34
36
  - ext/nproc/extconf.rb
35
37
  - ext/nproc/nproc.c
38
+ - ext/procfs/extconf.rb
39
+ - ext/procfs/loadavg_pid.h
40
+ - ext/procfs/procfs.c
41
+ - ext/procfs/stat.h
42
+ - ext/procfs/statm.h
43
+ - ext/procfs/uptime.h
36
44
  - ext/sysconf/extconf.rb
37
45
  - ext/sysconf/sysconf.c
46
+ - ext/sysinfo/extconf.rb
47
+ - ext/sysinfo/sysinfo.c
38
48
  - ext/utsname/extconf.rb
39
49
  - ext/utsname/utsname.c
40
50
  - lib/linux_stat.rb
@@ -68,7 +78,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
68
78
  requirements:
69
79
  - - ">="
70
80
  - !ruby/object:Gem::Version
71
- version: 2.5.0
81
+ version: 2.3.0
72
82
  required_rubygems_version: !ruby/object:Gem::Requirement
73
83
  requirements:
74
84
  - - ">="