linux_stat 0.8.0 → 0.9.0

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.
@@ -2,7 +2,7 @@ module LinuxStat
2
2
  module Filesystem
3
3
  class << self
4
4
  ##
5
- # = stat(fs = '/')
5
+ # = stat(fs = '.')
6
6
  #
7
7
  # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
8
8
  #
@@ -16,7 +16,7 @@ module LinuxStat
16
16
  # {:total=>119981191168, :free=>43155574784, :used=>76825616384, :available=>43155574784}
17
17
  #
18
18
  # If the stat can't be acquired, this method will return an empty Hash.
19
- def stat(fs = ?/.freeze)
19
+ def stat(fs = ?..freeze)
20
20
  s = stat_raw(fs)
21
21
  return {} if s.empty?
22
22
  s.default = 0
@@ -29,14 +29,14 @@ module LinuxStat
29
29
  end
30
30
 
31
31
  ##
32
- # = total(fs = '/')
32
+ # = total(fs = '.')
33
33
  #
34
34
  # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
35
35
  #
36
36
  # It returns the total size of a given disk in bytes.
37
37
  #
38
38
  # If the stat can't be acquired, this method will return nil.
39
- def total(fs = ?/.freeze)
39
+ def total(fs = ?..freeze)
40
40
  s = stat_raw(fs)
41
41
  return nil if s.empty?
42
42
  s.default = 0
@@ -44,7 +44,7 @@ module LinuxStat
44
44
  end
45
45
 
46
46
  ##
47
- # = free(fs = '/')
47
+ # = free(fs = '.')
48
48
  #
49
49
  # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
50
50
  #
@@ -55,7 +55,7 @@ module LinuxStat
55
55
  # Free returns the size of free blocks.
56
56
  #
57
57
  # If the stat can't be acquired, this method will return an empty Hash.
58
- def free(fs = ?/.freeze)
58
+ def free(fs = ?..freeze)
59
59
  s = stat_raw(fs)
60
60
  return nil if s.empty?
61
61
  s.default = 0
@@ -63,14 +63,14 @@ module LinuxStat
63
63
  end
64
64
 
65
65
  ##
66
- # = used(fs = '/')
66
+ # = used(fs = '.')
67
67
  #
68
68
  # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
69
69
  #
70
70
  # It returns the used space of a given disk in bytes.
71
71
  #
72
72
  # If the stat can't be acquired, this method will return nil.
73
- def used(fs = ?/.freeze)
73
+ def used(fs = ?..freeze)
74
74
  s = stat_raw(fs)
75
75
  return nil if s.empty?
76
76
  s.default = 0
@@ -78,7 +78,7 @@ module LinuxStat
78
78
  end
79
79
 
80
80
  ##
81
- # = available(fs = '/')
81
+ # = available(fs = '.')
82
82
  #
83
83
  # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
84
84
  #
@@ -89,7 +89,7 @@ module LinuxStat
89
89
  # Available returns the size of free blocks for unpriviledged users.
90
90
  #
91
91
  # If the stat can't be acquired, this method will return an empty Hash.
92
- def available(fs = ?/.freeze)
92
+ def available(fs = ?..freeze)
93
93
  s = stat_raw(fs)
94
94
  return nil if s.empty?
95
95
  s.default = 0
@@ -97,7 +97,7 @@ module LinuxStat
97
97
  end
98
98
 
99
99
  ##
100
- # = stat_raw(fs = '/')
100
+ # = stat_raw(fs = '.')
101
101
  #
102
102
  # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
103
103
  #
@@ -105,7 +105,7 @@ module LinuxStat
105
105
  # {:block_size=>4096, :fragment_size=>4096, :blocks=>29292283, :block_free=>10535967, :block_avail_unpriv=>10535967, :inodes=>58612160, :free_inodes=>56718550, :filesystem_id=>2050, :mount_flags=>1024, :max_filename_length=>255}
106
106
  #
107
107
  # If the stat can't be acquired, this method will return an empty Hash.
108
- def stat_raw(fs = ?/.freeze)
108
+ def stat_raw(fs = ?..freeze)
109
109
  LinuxStat::FS.stat(fs)
110
110
  end
111
111
  end
@@ -185,7 +185,7 @@ module LinuxStat
185
185
 
186
186
  private
187
187
  def splitted
188
- @@string_splitted ||= string.split
188
+ @@string_splitted ||= string.split.freeze
189
189
  end
190
190
  end
191
191
  end
@@ -121,7 +121,7 @@ module LinuxStat
121
121
  hour: h,
122
122
  minute: m,
123
123
  second: s
124
- }
124
+ }.freeze
125
125
  end
126
126
 
127
127
  private
@@ -143,7 +143,7 @@ module LinuxStat
143
143
  end
144
144
 
145
145
  def uptime_readable?
146
- @@uptime_readable = File.readable?('/proc/uptime')
146
+ @@uptime_readable ||= File.readable?('/proc/uptime')
147
147
  end
148
148
  end
149
149
  end
@@ -6,7 +6,7 @@ module LinuxStat
6
6
  #
7
7
  # The return type is an Array of Integers.
8
8
  def list
9
- Dir['/proc/*'].select! { |x|
9
+ Dir['/proc/*'].select { |x|
10
10
  pid = File.split(x)[1]
11
11
  pid.to_i.to_s == pid
12
12
  }.map! { |x| File.split(x)[-1].to_i }
@@ -246,18 +246,20 @@ module LinuxStat
246
246
  # The :last_executed_cpu also returns an Integer indicating the last executed cpu of the process.
247
247
  def cpu_stat(pid: $$, sleep: ticks_to_ms_t5)
248
248
  file = "/proc/#{pid}/stat"
249
- return {} unless File.readable?(file)
250
-
251
249
  ticks = get_ticks
252
250
 
251
+ return {} unless File.readable?(file)
253
252
  utime, stime, starttime = IO.read(file)
254
253
  .split.values_at(13, 14, 21).map(&:to_f)
254
+
255
255
  uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
256
256
 
257
257
  total_time = utime + stime
258
258
  idle1 = uptime - starttime - total_time
259
259
 
260
260
  sleep(sleep)
261
+
262
+ return {} unless File.readable?(file)
261
263
  stat = IO.read(file).split
262
264
 
263
265
  utime2, stime2, starttime2 = stat.values_at(13, 14, 21).map(&:to_f)
@@ -267,10 +269,10 @@ module LinuxStat
267
269
  idle2 = uptime - starttime2 - total_time2
268
270
 
269
271
  totald = idle2.+(total_time2).-(idle1 + total_time)
270
- cpu = totald.-(idle2 - idle1).fdiv(totald).*(100).round(2).abs./(LinuxStat::CPU.count)
272
+ cpu_u = totald.-(idle2 - idle1).fdiv(totald).abs.*(100)./(cpu_count)
271
273
 
272
274
  {
273
- cpu_usage: cpu,
275
+ cpu_usage: cpu_u > 100 ? 100.0 : cpu_u.round(2),
274
276
  threads: stat[19].to_i,
275
277
  last_executed_cpu: stat[38].to_i
276
278
  }
@@ -293,6 +295,10 @@ module LinuxStat
293
295
  #
294
296
  # => 10.0
295
297
  #
298
+ # 10.0 means it's using 10% of the total processing power of the system.
299
+ #
300
+ # The value is divided with the configured number of CPU and not online CPU.
301
+ #
296
302
  # A value of 100.0 indicates it is using 100% processing power available to the system.
297
303
  #
298
304
  # But if the info isn't available, it will return nil.
@@ -300,12 +306,12 @@ module LinuxStat
300
306
  # This method is more efficient than running LinuxStat::ProcessInfo.cpu_stat()
301
307
  def cpu_usage(pid: $$, sleep: ticks_to_ms_t5)
302
308
  file = "/proc/#{pid}/stat"
303
- return nil unless File.readable?(file)
304
-
305
309
  ticks = get_ticks
306
310
 
311
+ return nil unless File.readable?(file)
307
312
  utime, stime, starttime = IO.read(file)
308
313
  .split.values_at(13, 14, 21).map(&:to_f)
314
+
309
315
  uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
310
316
 
311
317
  total_time = utime + stime
@@ -313,15 +319,19 @@ module LinuxStat
313
319
 
314
320
  sleep(sleep)
315
321
 
322
+ return nil unless File.readable?(file)
316
323
  utime2, stime2, starttime2 = IO.read(file)
317
324
  .split.values_at(13, 14, 21).map(&:to_f)
325
+
318
326
  uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
319
327
 
320
328
  total_time2 = utime2 + stime2
321
329
  idle2 = uptime - starttime2 - total_time2
322
330
 
323
331
  totald = idle2.+(total_time2).-(idle1 + total_time)
324
- totald.-(idle2 - idle1).fdiv(totald).*(100).round(2).abs./(LinuxStat::CPU.count)
332
+
333
+ u = totald.-(idle2 - idle1).fdiv(totald).abs.*(100)./(cpu_count)
334
+ u > 100 ? 100.0 : u.round(2)
325
335
  end
326
336
 
327
337
  ##
@@ -348,12 +358,12 @@ module LinuxStat
348
358
  # But if the info isn't available, it will return nil.
349
359
  def thread_usage(pid: $$, sleep: ticks_to_ms_t5)
350
360
  file = "/proc/#{pid}/stat"
351
- return nil unless File.readable?(file)
352
-
353
361
  ticks = get_ticks
354
362
 
363
+ return nil unless File.readable?(file)
355
364
  utime, stime, starttime = IO.read(file)
356
365
  .split.values_at(13, 14, 21).map(&:to_f)
366
+
357
367
  uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
358
368
 
359
369
  total_time = utime + stime
@@ -361,15 +371,21 @@ module LinuxStat
361
371
 
362
372
  sleep(sleep)
363
373
 
374
+ return nil unless File.readable?(file)
364
375
  utime2, stime2, starttime2 = IO.read(file)
365
376
  .split.values_at(13, 14, 21).map(&:to_f)
377
+
366
378
  uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
367
379
 
368
380
  total_time2 = utime2 + stime2
369
381
  idle2 = uptime - starttime2 - total_time2
370
382
 
371
383
  totald = idle2.+(total_time2).-(idle1 + total_time)
372
- totald.-(idle2 - idle1).fdiv(totald).*(100).round(2).abs
384
+
385
+ u = totald.-(idle2 - idle1).fdiv(totald).abs.*(100)
386
+
387
+ cpu_count_t100 = cpu_count * 100
388
+ u > cpu_count_t100 ? cpu_count_t100 : u.round(2)
373
389
  end
374
390
 
375
391
  ##
@@ -539,7 +555,9 @@ module LinuxStat
539
555
  # For example:
540
556
  # LinuxStat::ProcessInfo.running_time 14183
541
557
  #
542
- # => 1947.619999999999
558
+ # => 1947.61
559
+ #
560
+ # It always rounds the float number upto 2 decimal places
543
561
  #
544
562
  # If the info isn't available or the argument passed doesn't exist as a process ID, it will return nil.
545
563
  def running_time(pid = $$)
@@ -549,7 +567,8 @@ module LinuxStat
549
567
  @@u_readable ||= File.readable?(uptime)
550
568
  return nil unless @@u_readable && File.readable?(stat_file)
551
569
 
552
- IO.foreach(uptime, ' '.freeze).next.to_f - (IO.read(stat_file).split[21].to_i / get_ticks)
570
+ IO.foreach(uptime, ' '.freeze).next.to_f
571
+ .-(IO.read(stat_file).split[21].to_f / get_ticks).round(2)
553
572
  end
554
573
 
555
574
  ##
@@ -592,6 +611,38 @@ module LinuxStat
592
611
  IO.foreach(file, ' ').first(19)[-1].to_i
593
612
  end
594
613
 
614
+ ##
615
+ # = nproc(pid = $$)
616
+ # Returns the cpu allocated to the process.
617
+ #
618
+ # The output value is an Integer.
619
+ #
620
+ # For example:
621
+ # $ taskset -c 0 irb
622
+ #
623
+ # irb(main):001:0> require 'linux_stat'
624
+ #
625
+ # => true
626
+ #
627
+ # irb(main):002:0> LinuxStat::ProcessInfo.nproc
628
+ #
629
+ # => 1
630
+ #
631
+ # irb(main):003:0> LinuxStat::ProcessInfo.nproc 11562
632
+ #
633
+ # => 3
634
+ #
635
+ # irb(main):004:0> LinuxStat::ProcessInfo.nproc 12513
636
+ #
637
+ # => 4
638
+ #
639
+ # If the info isn't available or the argument passed doesn't exist as a process ID, it will return nil.
640
+ def nproc(pid = $$)
641
+ LinuxStat::Nproc.count_cpu_for_pid(pid)
642
+ end
643
+
644
+ alias count_cpu nproc
645
+
595
646
  private
596
647
  def get_ticks
597
648
  @@ticks ||= Sysconf.sc_clk_tck
@@ -607,6 +658,10 @@ module LinuxStat
607
658
  def pagesize
608
659
  @@pagesize ||= LinuxStat::Sysconf.pagesize
609
660
  end
661
+
662
+ def cpu_count
663
+ @@nprocessors_conf ||= LinuxStat::CPU.count
664
+ end
610
665
  end
611
666
  end
612
667
  end
@@ -117,7 +117,7 @@ module LinuxStat
117
117
  val = IO.readlines('/proc/swaps').drop(1)
118
118
  return [[], []] if val.empty?
119
119
 
120
- val.map { |x|
120
+ val.map! { |x|
121
121
  x.strip.split.values_at(2, 3).map!(&:to_i)
122
122
  }.transpose
123
123
  end
@@ -1,3 +1,3 @@
1
1
  module LinuxStat
2
- VERSION ||= "0.8.0"
2
+ VERSION ||= "0.9.0"
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: 0.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sourav Goswami
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-12-18 00:00:00.000000000 Z
11
+ date: 2020-12-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Linux only, efficient linux system utilization reporting and system monitoring
14
14
  gem
@@ -20,6 +20,7 @@ extensions:
20
20
  - ext/fs_stat/extconf.rb
21
21
  - ext/sysconf/extconf.rb
22
22
  - ext/utsname/extconf.rb
23
+ - ext/nproc/extconf.rb
23
24
  extra_rdoc_files:
24
25
  - README.md
25
26
  files:
@@ -30,6 +31,8 @@ files:
30
31
  - exe/linuxstat.rb
31
32
  - ext/fs_stat/extconf.rb
32
33
  - ext/fs_stat/fs_stat.c
34
+ - ext/nproc/extconf.rb
35
+ - ext/nproc/nproc.c
33
36
  - ext/sysconf/extconf.rb
34
37
  - ext/sysconf/sysconf.c
35
38
  - ext/utsname/extconf.rb