linux_stat 0.6.1 → 0.7.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 @@
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
 
@@ -3,7 +3,6 @@
3
3
 
4
4
  #pragma GCC optimize ("O3")
5
5
  #pragma clang optimize on
6
- #pragma once
7
6
 
8
7
  static VALUE statfs(VALUE obj, VALUE dir) {
9
8
  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
 
@@ -3,7 +3,6 @@
3
3
 
4
4
  #pragma GCC optimize ("O3")
5
5
  #pragma clang optimize on
6
- #pragma once
7
6
 
8
7
  static VALUE getTick(VALUE obj) {
9
8
  return INT2FIX(sysconf(_SC_CLK_TCK)) ;
@@ -25,7 +24,7 @@ static VALUE getOpenMax(VALUE obj) {
25
24
  return INT2FIX(sysconf(_SC_OPEN_MAX)) ;
26
25
  }
27
26
 
28
- static VALUE getPageSizeMax(VALUE obj) {
27
+ static VALUE getPageSize(VALUE obj) {
29
28
  return INT2FIX(sysconf(_SC_PAGESIZE)) ;
30
29
  }
31
30
 
@@ -67,7 +66,7 @@ void Init_sysconf() {
67
66
  rb_define_module_function(_sysconf, "hostname_max", getHostnameMax, 0) ;
68
67
  rb_define_module_function(_sysconf, "login_name_max", getLoginNameMax, 0) ;
69
68
  rb_define_module_function(_sysconf, "open_max", getOpenMax, 0) ;
70
- rb_define_module_function(_sysconf, "page_size_max", getPageSizeMax, 0) ;
69
+ rb_define_module_function(_sysconf, "pagesize", getPageSize, 0) ;
71
70
  rb_define_module_function(_sysconf, "stream_max", getStreamMax, 0) ;
72
71
  rb_define_module_function(_sysconf, "tty_name_max", getTTYNameMax, 0) ;
73
72
  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
 
@@ -3,7 +3,6 @@
3
3
 
4
4
  #pragma GCC optimize ("O3")
5
5
  #pragma clang optimize on
6
- #pragma once
7
6
 
8
7
  static struct utsname buf ;
9
8
  static short status ;
@@ -1,8 +1,26 @@
1
- # Independed and LinuxStat specific unrelated modules
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
2
18
  require "linux_stat/version"
3
19
  require 'linux_stat/prettify_bytes'
4
20
 
5
- # Independed and LinuxStat related modules
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
6
24
  require "linux_stat/battery"
7
25
  require "linux_stat/bios"
8
26
  require "linux_stat/cpu"
@@ -11,6 +29,9 @@ require "linux_stat/net"
11
29
  require "linux_stat/process"
12
30
  require "linux_stat/swap"
13
31
 
32
+ # Dependent Modules
33
+ # Modules that can have reverse dependency
34
+
14
35
  # LinuxStat::Uname dependent modules
15
36
  require 'linux_stat/utsname'
16
37
  require "linux_stat/os"
@@ -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,16 +1,21 @@
1
1
  module LinuxStat
2
2
  module CPU
3
3
  class << self
4
- # stat(sleep = 1.0 / LinuxStat::Sysconf.sc_clk_tck)
4
+ ##
5
+ # = stat(sleep = 1.0 / LinuxStat::Sysconf.sc_clk_tck)
6
+ #
5
7
  # Where sleep is the delay to gather the data.
8
+ #
6
9
  # The minimum possible value at anytime is 1.0 / LinuxStat::Sysconf.sc_clk_tck
10
+ #
7
11
  # This method returns the cpu usage of all threads.
8
12
  #
9
13
  # The first one is aggregated CPU usage reported by the Linux kernel.
14
+ #
10
15
  # And the consecutive ones are the real core usages.
11
16
  #
12
17
  # On a system with 4 threads, the output will be like::
13
- # {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}
14
19
  #
15
20
  # If the information is not available, it will return an empty Hash
16
21
  def stat(sleep = ticks_to_ms)
@@ -43,9 +48,13 @@ module LinuxStat
43
48
  end
44
49
  end
45
50
 
46
- # total_usage(sleep = 1.0 / LinuxStat::Sysconf.sc_clk_tck)
51
+ ##
52
+ # = total_usage(sleep = 1.0 / LinuxStat::Sysconf.sc_clk_tck)
53
+ #
47
54
  # Where sleep is the delay to gather the data.
55
+ #
48
56
  # The minimum possible value at anytime is 1.0 / LinuxStat::Sysconf.sc_clk_tck
57
+ #
49
58
  # This method returns the cpu usage of all threads.
50
59
  #
51
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.
@@ -66,14 +75,18 @@ module LinuxStat
66
75
  totald.-(idle_now - idle_then).fdiv(totald).*(100).round(2).abs
67
76
  end
68
77
 
78
+ ##
69
79
  # Returns the total number of CPU threads.
80
+ #
70
81
  # If the information isn't available, it will return 0.
71
82
  def count
72
83
  # CPU count can change during the program runtime
73
84
  cpuinfo.count { |x| x.start_with?('processor') }
74
85
  end
75
86
 
87
+ ##
76
88
  # Returns the model of processor.
89
+ #
77
90
  # If the information isn't available, it will return en empty string.
78
91
  #
79
92
  # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
@@ -81,7 +94,9 @@ module LinuxStat
81
94
  @@name ||= cpuinfo.find { |x| x.start_with?('model name') }.to_s.split(?:)[-1].to_s.strip
82
95
  end
83
96
 
97
+ ##
84
98
  # Returns an array with current core frequencies corresponding to the usages.
99
+ #
85
100
  # If the information isn't available, it will return an empty array.
86
101
  def cur_freq
87
102
  @@cpu_freqs ||= Dir["/sys/devices/system/cpu/cpu[0-9]*/cpufreq/scaling_cur_freq"]
@@ -94,7 +109,9 @@ module LinuxStat
94
109
  end
95
110
  end
96
111
 
112
+ ##
97
113
  # Returns an array with max core frequencies corresponding to the usages.
114
+ #
98
115
  # If the information isn't available, it will return an empty array.
99
116
  def max_freq
100
117
  @@max_freqs ||= Dir["/sys/devices/system/cpu/cpu[0-9]*/cpufreq/scaling_max_freq"]
@@ -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):
@@ -1,16 +1,22 @@
1
1
  module LinuxStat
2
2
  module Kernel
3
3
  class << self
4
+ ##
4
5
  # Returns the Linux Kernel version.
6
+ #
5
7
  # If the information isn't available, it will return a frozen empty string.
8
+ #
6
9
  # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
7
10
  def version
8
11
  return ''.freeze if string.empty?
9
12
  @@version ||= splitted[2]
10
13
  end
11
14
 
15
+ ##
12
16
  # Returns the name of the user who built the kernel using KBUILD_FLAGS.
17
+ #
13
18
  # If the information isn't available, it will return a frozen empty string.
19
+ #
14
20
  # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
15
21
  def build_user
16
22
  @@build_user ||= string.split(/(\(.+\))/).each(&:strip!)
@@ -18,8 +24,11 @@ module LinuxStat
18
24
  .split[0].to_s[1..-2].to_s.freeze
19
25
  end
20
26
 
27
+ ##
21
28
  # Returns the compiler used to compile the Linux Kernel.
29
+ #
22
30
  # If the information isn't available, it will return a frozen empty string.
31
+ #
23
32
  # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
24
33
  def compiler
25
34
  return ''.freeze if string.empty?
@@ -37,8 +46,11 @@ module LinuxStat
37
46
  end << compiler_version
38
47
  end
39
48
 
49
+ ##
40
50
  # Returns the compiler version used to compile the Linux Kernel.
51
+ #
41
52
  # If the information isn't available, it will return a frozen empty string.
53
+ #
42
54
  # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
43
55
  def compiler_version
44
56
  @@compiler_version ||= string.split(/(\(.+?\))/).each(&:strip!)
@@ -46,20 +58,28 @@ module LinuxStat
46
58
  .find { |x| x[/[\d.]+/] }.to_s[/[\d.]+/].to_s.freeze
47
59
  end
48
60
 
61
+ ##
49
62
  # Returns the time when the kernel was compiled.
63
+ #
50
64
  # The return value is a Time object.
65
+ #
51
66
  # If the information isn't available, it will return nil
52
67
  #
53
68
  # The time will be searched in specific order.
54
- # It will match any date matching any of these formats:
69
+ #
70
+ # * It will match any date matching any of these formats:
55
71
  # 1. %b %d %H:%M:%S %z %Y
56
72
  # 2. %d %b %Y %H:%M:%S %z
57
73
  # 3. %Y-%m-%d
74
+ #
58
75
  # Most kernels have date in them in this format.
59
76
  #
60
77
  # Do note that Ruby sometimes fails to work with timezones like BST for example.
78
+ #
61
79
  # In such case, the timezone is unrealiable and often returns the local timezone.
80
+ #
62
81
  # You have to use regexp yourself to get the proper zone.
82
+ #
63
83
  # Use LinuxStat::Kernel.build_date_string to get the original string if you need that.
64
84
  #
65
85
  # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
@@ -91,20 +111,29 @@ module LinuxStat
91
111
  end
92
112
  end
93
113
 
114
+ ##
94
115
  # Returns the time when the kernel was compiled.
116
+ #
95
117
  # The return value is a String.
118
+ #
96
119
  # If the information isn't available, it will return nil
97
120
  #
98
121
  # The time will be searched in specific order.
99
- # It will match any date matching any of these formats:
122
+ #
123
+ # * It will match any date matching any of these formats:
124
+ #
100
125
  # 1. %b %d %H:%M:%S %z %Y
101
126
  # 2. %d %b %Y %H:%M:%S %z
102
127
  # 3. %Y-%m-%d
128
+ #
103
129
  # Most kernels have date in them in this format.
104
130
  #
105
131
  # Do note that Ruby sometimes fails to work with timezones like BST for example.
132
+ #
106
133
  # In such case, the timezone is unrealiable and often returns the local timezone.
134
+ #
107
135
  # You have to use regexp yourself to get the proper zone.
136
+ #
108
137
  # Use LinuxStat::Kernel.build_date_string to get the original string if you need that.
109
138
  #
110
139
  # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
@@ -133,14 +162,19 @@ module LinuxStat
133
162
  end
134
163
  end
135
164
 
165
+ ##
136
166
  # Reads maximum 1024 bytes from /proc/version and returns the string.
167
+ #
137
168
  # The output is also cached ; as changing the value in runtime is unexpected.
138
169
  def string
139
170
  @@string ||= File.readable?('/proc/version') ? IO.read('/proc/version', 1024).tap(&:strip!) : ''
140
171
  end
141
172
 
173
+ ##
142
174
  # Returns the sc_clk_tck or the same output from command `getconf CLK_TCK`.
175
+ #
143
176
  # Also, clk_tck is an alias of this method.
177
+ #
144
178
  # The output is also cached ; as changing the value in runtime is unexpected.
145
179
  def ticks
146
180
  @@tick ||= LinuxStat::Sysconf.sc_clk_tck