linux_stat 0.5.1 → 0.6.4

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 @@
2
2
  $-v = true
3
3
 
4
4
  begin
5
- require 'linux_stat'
5
+ require 'linux_stat' unless defined?(LinuxStat)
6
6
  rescue LoadError
7
7
  abort "The Gem needs to be installed before this test can be run!"
8
8
  end
@@ -1,6 +1,10 @@
1
1
  require 'mkmf'
2
2
 
3
- unless (have_header('sys/statvfs.h') && have_header('ruby.h'))
3
+ unless have_const('linux') || RbConfig::CONFIG['arch'].to_s[/linux/]
4
+ abort('Platform is not linux')
5
+ end
6
+
7
+ unless have_header('sys/statvfs.h') && have_header('ruby.h')
4
8
  abort('Missing header')
5
9
  end
6
10
 
@@ -1,7 +1,9 @@
1
1
  #include <sys/statvfs.h>
2
2
  #include "ruby.h"
3
+
3
4
  #pragma GCC optimize ("O3")
4
5
  #pragma clang optimize on
6
+ #pragma once
5
7
 
6
8
  static VALUE statfs(VALUE obj, VALUE dir) {
7
9
  struct statvfs buf ;
@@ -1,6 +1,10 @@
1
1
  require 'mkmf'
2
2
 
3
- unless (have_header('sys/unistd.h') && have_header('ruby.h'))
3
+ unless have_const('linux') || RbConfig::CONFIG['arch'].to_s[/linux/]
4
+ abort('Platform is not linux')
5
+ end
6
+
7
+ unless have_header('sys/unistd.h') && have_header('ruby.h')
4
8
  abort('Missing header')
5
9
  end
6
10
 
@@ -1,6 +1,10 @@
1
1
  #include <unistd.h>
2
2
  #include "ruby.h"
3
3
 
4
+ #pragma GCC optimize ("O3")
5
+ #pragma clang optimize on
6
+ #pragma once
7
+
4
8
  static VALUE getTick(VALUE obj) {
5
9
  return INT2FIX(sysconf(_SC_CLK_TCK)) ;
6
10
  }
@@ -21,7 +25,7 @@ static VALUE getOpenMax(VALUE obj) {
21
25
  return INT2FIX(sysconf(_SC_OPEN_MAX)) ;
22
26
  }
23
27
 
24
- static VALUE getPageSizeMax(VALUE obj) {
28
+ static VALUE getPageSize(VALUE obj) {
25
29
  return INT2FIX(sysconf(_SC_PAGESIZE)) ;
26
30
  }
27
31
 
@@ -63,7 +67,7 @@ void Init_sysconf() {
63
67
  rb_define_module_function(_sysconf, "hostname_max", getHostnameMax, 0) ;
64
68
  rb_define_module_function(_sysconf, "login_name_max", getLoginNameMax, 0) ;
65
69
  rb_define_module_function(_sysconf, "open_max", getOpenMax, 0) ;
66
- rb_define_module_function(_sysconf, "page_size_max", getPageSizeMax, 0) ;
70
+ rb_define_module_function(_sysconf, "pagesize", getPageSize, 0) ;
67
71
  rb_define_module_function(_sysconf, "stream_max", getStreamMax, 0) ;
68
72
  rb_define_module_function(_sysconf, "tty_name_max", getTTYNameMax, 0) ;
69
73
  rb_define_module_function(_sysconf, "posix_version", getPosixVersion, 0) ;
@@ -1,6 +1,10 @@
1
1
  require 'mkmf'
2
2
 
3
- unless (have_header('sys/utsname.h') && have_header('ruby.h'))
3
+ unless have_const('linux') || RbConfig::CONFIG['arch'].to_s[/linux/]
4
+ abort('Platform is not linux')
5
+ end
6
+
7
+ unless have_header('sys/utsname.h') && have_header('ruby.h')
4
8
  abort('Missing header')
5
9
  end
6
10
 
@@ -1,7 +1,9 @@
1
1
  #include <sys/utsname.h>
2
2
  #include "ruby.h"
3
+
3
4
  #pragma GCC optimize ("O3")
4
5
  #pragma clang optimize on
6
+ #pragma once
5
7
 
6
8
  static struct utsname buf ;
7
9
  static short status ;
@@ -1,21 +1,48 @@
1
+ # ----------------------------------------------------------------------------------------------------- #
2
+ # Don't edit this file unless you know what you are doing. #
3
+ # #
4
+ # A file can have reverse dependency. #
5
+ # For example, linux_stat/utsname is required before #
6
+ # linux_stat/os, which means utsname can be used by LinuxStat::OS and the files below. #
7
+ # #
8
+ # Once wrongly edited, you need to go through each method to know what #
9
+ # file is required by the module functions. Which can be very time consuming. #
10
+ # #
11
+ # If you are writng an independent module, just add them under "Independent" section #
12
+ # If you are writing a dependent module, just append that to the end of the file. #
13
+ # If you are writing something that is miscellaneous, just add it to miscellaneous section #
14
+ # ------------------------------------------------------------------------------------------------------ #
15
+
16
+ # Miscellaneous Modules
17
+ # Independed and LinuxStat's miscellaneous modules
1
18
  require "linux_stat/version"
19
+ require 'linux_stat/prettify_bytes'
20
+
21
+ # Independed Modules
22
+ # Modules that doesn't have any dependency on its own
23
+ # But might be required by other module functions in "Dependent Modules" section
2
24
  require "linux_stat/battery"
3
25
  require "linux_stat/bios"
4
26
  require "linux_stat/cpu"
5
27
  require "linux_stat/memory"
6
28
  require "linux_stat/net"
29
+ require "linux_stat/process"
30
+ require "linux_stat/swap"
31
+
32
+ # Dependent Modules
33
+ # Modules that can have reverse dependency
7
34
 
35
+ # LinuxStat::Uname dependent modules
8
36
  require 'linux_stat/utsname'
9
37
  require "linux_stat/os"
10
38
 
11
- require "linux_stat/process"
12
- require "linux_stat/swap"
13
- require "linux_stat/mounts"
14
-
39
+ # LinuxStat::FS dependent modules
15
40
  require "linux_stat/fs_stat"
16
41
  require "linux_stat/filesystem"
42
+ require "linux_stat/mounts"
17
43
 
44
+ # LinuxStat::Sysconf dependent modules
18
45
  require "linux_stat/sysconf"
19
46
  require "linux_stat/kernel"
20
- require "linux_stat/process_info"
21
47
  require 'linux_stat/user'
48
+ require "linux_stat/process_info"
@@ -3,12 +3,15 @@ module LinuxStat
3
3
  PATH = "/sys/class/power_supply/BAT0"
4
4
 
5
5
  class << self
6
+ ##
6
7
  # Returns true or false based on the presence of the battery.
7
8
  def present?
8
9
  @@present ||= Dir.exist?(PATH)
9
10
  end
10
11
 
12
+ ##
11
13
  # Returns the details of the battery.
14
+ #
12
15
  # If the battery is not present it will return an empty Hash.
13
16
  def stat
14
17
  st = status.downcase
@@ -26,57 +29,73 @@ module LinuxStat
26
29
  }
27
30
  end
28
31
 
32
+ ##
29
33
  # Returns the model of the battery.
34
+ #
30
35
  # If the battery is not present or the information isn't available it will return an empty String.
31
36
  def model
32
37
  return ''.freeze unless model_readable?
33
38
  IO.read(File.join(PATH, 'model_name')).tap(&:strip!)
34
39
  end
35
40
 
41
+ ##
36
42
  # Returns the manufacturer of the battery.
43
+ #
37
44
  # If the battery is not present or the information is not available, it will return an empty String.
38
45
  def manufacturer
39
46
  return ''.freeze unless manufacturer_readable?
40
47
  IO.read(File.join(PATH, 'manufacturer')).tap(&:strip!)
41
48
  end
42
49
 
50
+ ##
43
51
  # Returns the technology of the battery.
52
+ #
44
53
  # If the battery is not present or the information is not available, it will return an empty String.
45
54
  def technology
46
55
  return ''.freeze unless tech_readable?
47
56
  IO.read(File.join(PATH, 'technology')).tap(&:strip!)
48
57
  end
49
58
 
59
+ ##
50
60
  # Returns the status of the battery.
51
- # If the battery is not present or the information is not available, it will return an empty String.
52
61
  # The status generally includes either of the full, charging, discharging and unknown states in most cases.
62
+ #
63
+ # If the battery is not present or the information is not available, it will return an empty frozen String.
53
64
  def status
54
65
  return ''.freeze unless status_readable?
55
66
  IO.read(File.join(PATH, 'status')).tap(&:strip!)
56
67
  end
57
68
 
69
+ ##
58
70
  # Returns true if the battery is charging, false if the battery is not charging.
71
+ #
59
72
  # If the battery is not present or the information is not available, it will return nil.
60
73
  def charging?
61
74
  return nil if status.empty?
62
75
  %w(full charging unknown).each(&:freeze).include?(status.downcase)
63
76
  end
64
77
 
78
+ ##
65
79
  # Returns true if the battery is discharging, false if the battery is not discharging.
80
+ #
66
81
  # If the battery is not present or the information is not available, it will return nil.
67
82
  def discharging?
68
83
  return nil if status.empty?
69
84
  status.downcase == 'discharging'
70
85
  end
71
86
 
87
+ ##
72
88
  # Returns true if the battery status if full, false if the battery status is not full.
89
+ #
73
90
  # If the battery is not present or the information is not available, it will return nil.
74
91
  def full?
75
92
  return nil if status.empty?
76
93
  status.downcase == 'full'
77
94
  end
78
95
 
96
+ ##
79
97
  # Returns the charge of the battery.
98
+ #
80
99
  # If the battery is not present or the information is not available, it will return nil.
81
100
  def charge
82
101
  return nil unless charge_now_readable?
@@ -1,7 +1,9 @@
1
1
  module LinuxStat
2
2
  module BIOS
3
3
  class << self
4
+ ##
4
5
  # Returns the model of the BIOS.
6
+ #
5
7
  # If the information is not available it will return a frozen empty string.
6
8
  #
7
9
  # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
@@ -16,7 +18,9 @@ module LinuxStat
16
18
  end
17
19
  end
18
20
 
21
+ ##
19
22
  # Returns the vendor of the BIOS.
23
+ #
20
24
  # If the information is not available it will return a frozen empty string.
21
25
  #
22
26
  # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
@@ -29,7 +33,9 @@ module LinuxStat
29
33
  end
30
34
  end
31
35
 
36
+ ##
32
37
  # Returns the version of the BIOS.
38
+ #
33
39
  # If the information is not available it will return a frozen empty string.
34
40
  #
35
41
  # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
@@ -41,7 +47,9 @@ module LinuxStat
41
47
  end
42
48
  end
43
49
 
50
+ ##
44
51
  # Returns the date of the BIOS.
52
+ #
45
53
  # If the information is not available it will return a frozen empty string.
46
54
  #
47
55
  # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
@@ -1,18 +1,24 @@
1
1
  module LinuxStat
2
2
  module CPU
3
3
  class << self
4
- # stat(sleep = 0.075)
4
+ ##
5
+ # = stat(sleep = 1.0 / LinuxStat::Sysconf.sc_clk_tck)
6
+ #
5
7
  # Where sleep is the delay to gather the data.
8
+ #
9
+ # The minimum possible value at anytime is 1.0 / LinuxStat::Sysconf.sc_clk_tck
10
+ #
6
11
  # This method returns the cpu usage of all threads.
7
12
  #
8
13
  # The first one is aggregated CPU usage reported by the Linux kernel.
14
+ #
9
15
  # And the consecutive ones are the real core usages.
10
16
  #
11
17
  # On a system with 4 threads, the output will be like::
12
- # {0=>84.38, 1=>100.0, 2=>50.0, 3=>87.5, 4=>87.5}
18
+ # {0=>84.38, 1=>100.0, 2=>50.0, 3=>87.5, 4=>87.5}
13
19
  #
14
20
  # If the information is not available, it will return an empty Hash
15
- def stat(sleep = 0.075)
21
+ def stat(sleep = ticks_to_ms)
16
22
  return {} unless stat?
17
23
 
18
24
  data = IO.readlines('/proc/stat').select! { |x| x[/^cpu\d*/] }.map! { |x| x.split.map!(&:to_f) }
@@ -33,20 +39,28 @@ module LinuxStat
33
39
  idle_then, idle_now = idle + iowait, idle2 + iowait2
34
40
  totald = idle_now.+(user2 + nice2 + sys2 + irq2 + softirq2 + steal2) - idle_then.+(user + nice + sys + irq + softirq + steal)
35
41
 
42
+ res = totald.-(idle_now - idle_then).fdiv(totald).*(100).round(2).abs
43
+ res = 0.0 if res.nan?
44
+
36
45
  h.merge!(
37
- x => totald.-(idle_now - idle_then).fdiv(totald).*(100).round(2).abs
46
+ x => res
38
47
  )
39
48
  end
40
49
  end
41
50
 
42
- # total_usage(sleep = 0.075)
51
+ ##
52
+ # = total_usage(sleep = 1.0 / LinuxStat::Sysconf.sc_clk_tck)
53
+ #
43
54
  # Where sleep is the delay to gather the data.
55
+ #
56
+ # The minimum possible value at anytime is 1.0 / LinuxStat::Sysconf.sc_clk_tck
57
+ #
44
58
  # This method returns the cpu usage of all threads.
45
59
  #
46
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.
47
61
  #
48
62
  # If the information is not available, it will return nil.
49
- def total_usage(sleep = 0.075)
63
+ def total_usage(sleep = ticks_to_ms)
50
64
  return nil unless stat?
51
65
 
52
66
  data = IO.foreach('/proc/stat').first.split.tap(&:shift).map!(&:to_f)
@@ -61,14 +75,18 @@ module LinuxStat
61
75
  totald.-(idle_now - idle_then).fdiv(totald).*(100).round(2).abs
62
76
  end
63
77
 
78
+ ##
64
79
  # Returns the total number of CPU threads.
80
+ #
65
81
  # If the information isn't available, it will return 0.
66
82
  def count
67
83
  # CPU count can change during the program runtime
68
84
  cpuinfo.count { |x| x.start_with?('processor') }
69
85
  end
70
86
 
87
+ ##
71
88
  # Returns the model of processor.
89
+ #
72
90
  # If the information isn't available, it will return en empty string.
73
91
  #
74
92
  # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
@@ -76,7 +94,9 @@ module LinuxStat
76
94
  @@name ||= cpuinfo.find { |x| x.start_with?('model name') }.to_s.split(?:)[-1].to_s.strip
77
95
  end
78
96
 
97
+ ##
79
98
  # Returns an array with current core frequencies corresponding to the usages.
99
+ #
80
100
  # If the information isn't available, it will return an empty array.
81
101
  def cur_freq
82
102
  @@cpu_freqs ||= Dir["/sys/devices/system/cpu/cpu[0-9]*/cpufreq/scaling_cur_freq"]
@@ -89,7 +109,9 @@ module LinuxStat
89
109
  end
90
110
  end
91
111
 
112
+ ##
92
113
  # Returns an array with max core frequencies corresponding to the usages.
114
+ #
93
115
  # If the information isn't available, it will return an empty array.
94
116
  def max_freq
95
117
  @@max_freqs ||= Dir["/sys/devices/system/cpu/cpu[0-9]*/cpufreq/scaling_max_freq"]
@@ -113,6 +135,10 @@ module LinuxStat
113
135
  def stat?
114
136
  @@stat_readable ||= File.readable?('/proc/stat')
115
137
  end
138
+
139
+ def ticks_to_ms
140
+ @@ms ||= 1.0 / LinuxStat::Sysconf.sc_clk_tck
141
+ end
116
142
  end
117
143
  end
118
144
  end
@@ -1,13 +1,16 @@
1
1
  module LinuxStat
2
2
  module Filesystem
3
3
  class << self
4
- # stat(fs = '/')
4
+ ##
5
+ # = stat(fs = '/')
6
+ #
5
7
  # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
6
8
  #
7
- # It returns a Hash with the following info:
8
- # 1. total size of the device (in bytes)
9
- # 2. free space (in kilobytes)
10
- # 3. used space (in kilobytes)
9
+ # * It returns a Hash with the following info:
10
+ #
11
+ # 1. total size of the device (in bytes)
12
+ # 2. free space (in kilobytes)
13
+ # 3. used space (in kilobytes)
11
14
  #
12
15
  # In a hash format:
13
16
  # {:total=>119981191168, :free=>43155574784, :used=>76825616384, :available=>43155574784}
@@ -25,8 +28,11 @@ module LinuxStat
25
28
  }
26
29
  end
27
30
 
28
- # total(fs = '/')
31
+ ##
32
+ # = total(fs = '/')
33
+ #
29
34
  # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
35
+ #
30
36
  # It returns the total size of a given disk in bytes.
31
37
  #
32
38
  # If the stat can't be acquired, this method will return nil.
@@ -37,10 +43,15 @@ module LinuxStat
37
43
  s[:block_size] * s[:blocks]
38
44
  end
39
45
 
40
- # free(fs = '/')
46
+ ##
47
+ # = free(fs = '/')
48
+ #
41
49
  # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
50
+ #
42
51
  # It returns the total free space in a disk in bytes.
52
+ #
43
53
  # It is to be noted that free is not same as available.
54
+ #
44
55
  # Free returns the size of free blocks.
45
56
  #
46
57
  # If the stat can't be acquired, this method will return an empty Hash.
@@ -51,8 +62,11 @@ module LinuxStat
51
62
  s[:block_size] * s[:block_free]
52
63
  end
53
64
 
54
- # used(fs = '/')
65
+ ##
66
+ # = used(fs = '/')
67
+ #
55
68
  # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
69
+ #
56
70
  # It returns the used space of a given disk in bytes.
57
71
  #
58
72
  # If the stat can't be acquired, this method will return nil.
@@ -63,10 +77,15 @@ module LinuxStat
63
77
  s[:blocks].-(s[:block_free]) * s[:block_size]
64
78
  end
65
79
 
66
- # available(fs = '/')
80
+ ##
81
+ # = available(fs = '/')
82
+ #
67
83
  # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
84
+ #
68
85
  # It returns the total free space in a disk in bytes.
86
+ #
69
87
  # It is to be noted that free is not same as available.
88
+ #
70
89
  # Available returns the size of free blocks for unpriviledged users.
71
90
  #
72
91
  # If the stat can't be acquired, this method will return an empty Hash.
@@ -77,7 +96,9 @@ module LinuxStat
77
96
  s[:block_size] * s[:block_avail_unpriv]
78
97
  end
79
98
 
80
- # stat_raw(fs = '/')
99
+ ##
100
+ # = stat_raw(fs = '/')
101
+ #
81
102
  # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
82
103
  #
83
104
  # It returns a Hash with the following data (for example):