linux_stat 1.6.0 → 2.0.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)
354
+ stat = LinuxStat::ProcFS.ps_stat(pid)
355
+ uptime = LS::ProcFS.uptime_f
356
+ return nil unless uptime && !stat.empty?
369
357
 
370
- stat = IO.read(file).split(/(\(.*\))/)[-1] &.split
371
- return nil unless stat
372
-
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
429
-
430
- utime2, stime2, starttime2 = *stat.values_at(11, 12, 19).map(&:to_f)
406
+ stat = LinuxStat::ProcFS.ps_stat(pid)
407
+ uptime = LS::ProcFS.uptime_f
408
+ return nil unless uptime && !stat.empty?
431
409
 
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
  ##
@@ -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)
@@ -81,7 +84,9 @@ module LinuxStat
81
84
  def available
82
85
  return nil unless swaps_readable?
83
86
  values_t = read_usage
84
- 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
85
90
  end
86
91
 
87
92
  ##
@@ -92,7 +97,7 @@ module LinuxStat
92
97
  # The return type is a Integer but if the info isn't available, it will return nil.
93
98
  def used
94
99
  return nil unless swaps_readable?
95
- read_usage[-1].sum
100
+ read_usage[-1].reduce(:+)
96
101
  end
97
102
 
98
103
  ##
@@ -103,10 +108,16 @@ module LinuxStat
103
108
  return nil unless swaps_readable?
104
109
  values_t = read_usage
105
110
 
106
- 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
+
107
118
  return 0.0 if total == 0
108
119
 
109
- values_t[-1].sum.*(100).fdiv(total).round(2)
120
+ used.*(100).fdiv(total).round(2)
110
121
  end
111
122
 
112
123
  ##
@@ -117,10 +128,15 @@ module LinuxStat
117
128
  return nil unless swaps_readable?
118
129
  values_t = read_usage
119
130
 
120
- 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
+
121
137
  return 0.0 if total == 0
122
138
 
123
- total.-(values_t[-1].sum).*(100).fdiv(total).round(2)
139
+ total.-(used).*(100).fdiv(total).round(2)
124
140
  end
125
141
 
126
142
  private
@@ -1,3 +1,3 @@
1
1
  module LinuxStat
2
- VERSION ||= "1.6.0"
2
+ VERSION ||= "2.0.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.6.0
4
+ version: 2.0.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-28 00:00:00.000000000 Z
11
+ date: 2021-02-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Linux only, efficient linux system utilization reporting and system monitoring
14
14
  gem
@@ -22,6 +22,7 @@ extensions:
22
22
  - ext/sysconf/extconf.rb
23
23
  - ext/sysinfo/extconf.rb
24
24
  - ext/utsname/extconf.rb
25
+ - ext/procfs/extconf.rb
25
26
  extra_rdoc_files:
26
27
  - README.md
27
28
  files:
@@ -34,6 +35,12 @@ files:
34
35
  - ext/fs_stat/fs_stat.c
35
36
  - ext/nproc/extconf.rb
36
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
37
44
  - ext/sysconf/extconf.rb
38
45
  - ext/sysconf/sysconf.c
39
46
  - ext/sysinfo/extconf.rb
@@ -71,7 +78,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
71
78
  requirements:
72
79
  - - ">="
73
80
  - !ruby/object:Gem::Version
74
- version: 2.4.0
81
+ version: 2.3.0
75
82
  required_rubygems_version: !ruby/object:Gem::Requirement
76
83
  requirements:
77
84
  - - ">="