linux_stat 0.6.2 → 0.7.2

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,11 +2,56 @@
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
9
9
 
10
+ module LinuxStat::CPU
11
+ define_singleton_method(:total_usage) do
12
+ end
13
+ end
14
+
15
+ # Gradient colour to strings
16
+ class String
17
+ def colourize(colour = 1, flip: false)
18
+ colours, line_length = [], -1
19
+ temp = ''
20
+
21
+ each_line do |c|
22
+ n, i = c.length, -1
23
+
24
+ if line_length != n
25
+ step, line_length = 255.0./(n), n
26
+ colours.clear
27
+
28
+ while (i += 1) < n
29
+ l = i.*(step)
30
+ colours.<<(
31
+ case colour
32
+ when 0 then [ l.*(2).to_i.clamp(0, 255), l.to_i.clamp(0, 255), 255.-(l).to_i.clamp(0, 255) ]
33
+ when 1 then [ 255, 255.-(l).to_i.clamp(0, 255), l.to_i.clamp(0, 255) ]
34
+ when 2 then [ l.to_i.clamp(0, 255), 255.-(l).to_i.clamp(0, 255), l.to_i.clamp(0, 255) ]
35
+ when 3 then [ l.*(2).to_i.clamp(0, 255), 255.-(l).to_i.clamp(0, 255), 100.+(l / 2).to_i.clamp(0, 255) ]
36
+ when 4 then [ 30, 255.-(l / 2).to_i.clamp(0, 255), 110.+(l / 2).to_i.clamp(0, 255) ]
37
+ when 5 then [ 255.-(l * 2).to_i.clamp(0, 255), l.to_i.clamp(0, 255), 200 ]
38
+ when 6 then [ 50.+(255 - l).to_i.clamp(0, 255), 255.-(l / 2).to_i.clamp(0, 255), (l * 2).to_i.clamp(0, 255) ]
39
+ else [ l.*(2).to_i.clamp(0, 255), 255.-(l).to_i.clamp(0, 255), 100.+(l / 2).to_i.clamp(0, 255) ]
40
+ end
41
+ )
42
+ end
43
+
44
+ colours.reverse! if flip
45
+ end
46
+
47
+ i = -1
48
+ temp.concat "\e[38;2;#{colours[i][0]};#{colours[i][1]};#{colours[i][2]}m#{c[i]}" while (i += 1) < n
49
+ end
50
+
51
+ temp << "\e[0m".freeze
52
+ end
53
+ end
54
+
10
55
  # Check which conflicting argument (e.g., `-md -html` together) is passed last
11
56
  # Always use the last argument
12
57
  conflicting, hash = [
@@ -54,7 +99,6 @@ execute.sort.each do |c|
54
99
  next if e.class != Module && e.class != Class
55
100
 
56
101
  meths = e.methods(false).sort
57
- next if meths.any? { |a| e.method(a).arity > 0 }
58
102
 
59
103
  if meths.length > 0
60
104
  if MARKDOWN
@@ -66,21 +110,66 @@ execute.sort.each do |c|
66
110
  end
67
111
  end
68
112
 
113
+
69
114
  meths.each do |meth|
115
+ arg = nil
116
+ params = e.method(meth).parameters
117
+
118
+ param = ''
119
+
120
+ params.each do |p|
121
+ case p[0]
122
+ when :opt
123
+ param << "#{p[1]}, "
124
+ when :key
125
+ param << "#{p[1]}:, "
126
+ end
127
+ end
128
+
129
+ param.delete_suffix!(", ")
130
+
131
+ if e.method(meth).arity > 0
132
+ if c == :PrettifyBytes
133
+ arg = rand(10 ** 15)
134
+ else
135
+ next
136
+ end
137
+ end
138
+
139
+ disp_meth = "#{meth}"
140
+ disp_meth.concat(arg ? "(#{param} = #{arg})" : "(#{param})")
141
+
70
142
  time = Time.now
71
- v = e.send(meth)
143
+ v = arg ? e.send(meth, arg) : e.send(meth)
72
144
  time2 = Time.now
73
145
  time = time2.-(time).*(1_000_000).round(3)
74
146
 
75
147
  v = v.inspect
76
148
  dis = v.length > 253 ? v[0..250].strip + '...'.freeze : v
77
149
 
150
+ source = e.singleton_method(meth).source_location.to_a
151
+ src, src_meth = '', ''
152
+
153
+ unless source.empty?
154
+ src << " File: #{File.split(source[0])[-1]} | Line: #{source[1]}\n"
155
+ src_meth << " Definition: #{IO.foreach(source[0]).first(source[1])[-1].strip}\n"
156
+
157
+ if MARKDOWN || HTML
158
+ src.prepend('#'.freeze)
159
+ src_meth.prepend('#'.freeze)
160
+ else
161
+ src.prepend(?\u2B23.freeze)
162
+ src_meth.prepend(?\u2B23.freeze)
163
+ end
164
+ end
165
+
166
+
78
167
  if MARKDOWN
79
- puts "#{e}.#{meth}\n=> #{dis}"
168
+ puts "#{src}#{src_meth}#{e}.#{disp_meth}\n=> #{dis}"
80
169
  elsif HTML
81
- puts "#{e}.#{meth}\n=> #{dis}"
170
+ puts "#{src}#{src_meth}#{e}.#{disp_meth}\n=> #{dis}"
82
171
  else
83
- puts "\e[1;38;2;80;80;255m#{e}.#{meth}\e[0m\n=> #{dis}"
172
+ puts "\e[1m#{src.colourize}\e[1m#{src_meth.colourize(4)}\e[0m\e[1;38;2;80;80;255m#{e}.#{disp_meth}\e[0m\n=> #{dis}"
84
173
  end
85
174
 
86
175
  puts( "(" +
@@ -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)) ;
@@ -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):