linux_stat 2.1.2 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/ext/procfs/uptime.h CHANGED
@@ -1,4 +1,4 @@
1
- VALUE uptime_f(VALUE obj) {
1
+ static VALUE uptime_f(volatile VALUE obj) {
2
2
  FILE *f = fopen("/proc/uptime", "r") ;
3
3
  if (!f) return Qnil ;
4
4
 
@@ -11,103 +11,103 @@
11
11
  #pragma intel optimization_level 3
12
12
  #endif
13
13
 
14
- static VALUE getTick(VALUE obj) {
14
+ static VALUE getTick(volatile VALUE obj) {
15
15
  int val = sysconf(_SC_CLK_TCK) ;
16
16
  if (val < 0) return Qnil ;
17
17
 
18
18
  return INT2FIX(val) ;
19
19
  }
20
20
 
21
- static VALUE getChildMax(VALUE obj) {
21
+ static VALUE getChildMax(volatile VALUE obj) {
22
22
  long long int val = sysconf(_SC_CHILD_MAX) ;
23
23
  if (val < 0) return Qnil ;
24
24
 
25
25
  return LL2NUM(val) ;
26
26
  }
27
27
 
28
- static VALUE getHostnameMax(VALUE obj) {
28
+ static VALUE getHostnameMax(volatile VALUE obj) {
29
29
  long long val = sysconf(_SC_HOST_NAME_MAX) ;
30
30
  if (val < 0) return Qnil ;
31
31
 
32
32
  return LL2NUM(val) ;
33
33
  }
34
34
 
35
- static VALUE getLoginNameMax(VALUE obj) {
35
+ static VALUE getLoginNameMax(volatile VALUE obj) {
36
36
  long long val = sysconf(_SC_LOGIN_NAME_MAX) ;
37
37
  if (val < 0) return Qnil ;
38
38
 
39
39
  return LL2NUM(val) ;
40
40
  }
41
41
 
42
- static VALUE getOpenMax(VALUE obj) {
42
+ static VALUE getOpenMax(volatile VALUE obj) {
43
43
  long long val = sysconf(_SC_OPEN_MAX) ;
44
44
  if (val < 0) return Qnil ;
45
45
 
46
46
  return LL2NUM(val) ;
47
47
  }
48
48
 
49
- static VALUE getPageSize(VALUE obj) {
49
+ static VALUE getPageSize(volatile VALUE obj) {
50
50
  int val = sysconf(_SC_PAGESIZE) ;
51
51
  if (val < 0) return Qnil ;
52
52
 
53
53
  return INT2FIX(val) ;
54
54
  }
55
55
 
56
- static VALUE getStreamMax(VALUE obj) {
56
+ static VALUE getStreamMax(volatile VALUE obj) {
57
57
  long long val = sysconf(_SC_STREAM_MAX) ;
58
58
  if (val < 0) return Qnil ;
59
59
 
60
60
  return LL2NUM(val) ;
61
61
  }
62
62
 
63
- static VALUE getTTYNameMax(VALUE obj) {
63
+ static VALUE getTTYNameMax(volatile VALUE obj) {
64
64
  long long val = sysconf(_SC_TTY_NAME_MAX) ;
65
65
  if (val < 0) return Qnil ;
66
66
 
67
67
  return LL2NUM(val) ;
68
68
  }
69
69
 
70
- static VALUE getPosixVersion(VALUE obj) {
70
+ static VALUE getPosixVersion(volatile VALUE obj) {
71
71
  long long val = sysconf(_SC_VERSION) ;
72
72
  if (val < 0) return Qnil ;
73
73
 
74
74
  return LL2NUM(val) ;
75
75
  }
76
76
 
77
- static VALUE getLineMax(VALUE obj) {
77
+ static VALUE getLineMax(volatile VALUE obj) {
78
78
  long long val = sysconf(_SC_LINE_MAX) ;
79
79
  if (val < 0) return Qnil ;
80
80
 
81
81
  return LL2NUM(val) ;
82
82
  }
83
83
 
84
- static VALUE getExprNestMax(VALUE obj) {
84
+ static VALUE getExprNestMax(volatile VALUE obj) {
85
85
  long long val = sysconf(_SC_EXPR_NEST_MAX) ;
86
86
  if (val < 0) return Qnil ;
87
87
 
88
88
  return LL2NUM(val) ;
89
89
  }
90
90
 
91
- static VALUE getProcessorConfigured(VALUE obj) {
91
+ static VALUE getProcessorConfigured(volatile VALUE obj) {
92
92
  long val = sysconf(_SC_NPROCESSORS_CONF) ;
93
93
  if (val < 0) return Qnil ;
94
94
 
95
95
  return LONG2NUM(val) ;
96
96
  }
97
97
 
98
- static VALUE getProcessorOnline(VALUE obj) {
98
+ static VALUE getProcessorOnline(volatile VALUE obj) {
99
99
  long val = sysconf(_SC_NPROCESSORS_ONLN) ;
100
100
  if (val < 0) return Qnil ;
101
101
 
102
102
  return LONG2NUM(val) ;
103
103
  }
104
104
 
105
- static VALUE getUser(VALUE obj) {
105
+ static VALUE getUser(volatile VALUE obj) {
106
106
  char *name = getlogin() ;
107
107
  return name ? rb_str_new_cstr(name) : rb_str_new_cstr("") ;
108
108
  }
109
109
 
110
- static VALUE getUID(VALUE obj) {
110
+ static VALUE getUID(volatile VALUE obj) {
111
111
  return UINT2NUM((unsigned int) getuid()) ;
112
112
  }
113
113
 
@@ -115,11 +115,11 @@ static VALUE getGID(VALUE obj) {
115
115
  return UINT2NUM((unsigned int) getgid()) ;
116
116
  }
117
117
 
118
- static VALUE getEUID(VALUE obj) {
118
+ static VALUE getEUID(volatile VALUE obj) {
119
119
  return UINT2NUM((unsigned int) geteuid()) ;
120
120
  }
121
121
 
122
- static VALUE getHostname(VALUE obj) {
122
+ static VALUE getHostname(volatile VALUE obj) {
123
123
  int h_max = sysconf(_SC_HOST_NAME_MAX) + 1 ;
124
124
  char hostname[h_max] ;
125
125
 
@@ -13,7 +13,7 @@
13
13
 
14
14
  static struct sysinfo info ;
15
15
 
16
- VALUE totalram(VALUE obj) {
16
+ static VALUE totalram(volatile VALUE obj) {
17
17
  char status = sysinfo(&info) ;
18
18
  if (status < 0) return Qnil ;
19
19
 
@@ -22,7 +22,7 @@ VALUE totalram(VALUE obj) {
22
22
  return rb_funcallv_public(_rb_v, rb_intern("*"), 1, &_rb_mem_unit) ;
23
23
  }
24
24
 
25
- VALUE freeram(VALUE obj) {
25
+ static VALUE freeram(volatile VALUE obj) {
26
26
  char status = sysinfo(&info) ;
27
27
  if (status < 0) return Qnil ;
28
28
 
@@ -31,7 +31,7 @@ VALUE freeram(VALUE obj) {
31
31
  return rb_funcallv_public(_rb_v, rb_intern("*"), 1, &_rb_mem_unit) ;
32
32
  }
33
33
 
34
- VALUE sharedram(VALUE obj) {
34
+ static VALUE sharedram(volatile VALUE obj) {
35
35
  char status = sysinfo(&info) ;
36
36
  if (status < 0) return Qnil ;
37
37
 
@@ -40,7 +40,7 @@ VALUE sharedram(VALUE obj) {
40
40
  return rb_funcallv_public(_rb_v, rb_intern("*"), 1, &_rb_mem_unit) ;
41
41
  }
42
42
 
43
- VALUE bufferram(VALUE obj) {
43
+ static VALUE bufferram(volatile VALUE obj) {
44
44
  char status = sysinfo(&info) ;
45
45
  if (status < 0) return Qnil ;
46
46
 
@@ -49,7 +49,7 @@ VALUE bufferram(VALUE obj) {
49
49
  return rb_funcallv_public(_rb_v, rb_intern("*"), 1, &_rb_mem_unit) ;
50
50
  }
51
51
 
52
- VALUE totalswap(VALUE obj) {
52
+ static VALUE totalswap(volatile VALUE obj) {
53
53
  static struct sysinfo info ;
54
54
  char status = sysinfo(&info) ;
55
55
  if (status < 0) return Qnil ;
@@ -59,7 +59,7 @@ VALUE totalswap(VALUE obj) {
59
59
  return rb_funcallv_public(_rb_v, rb_intern("*"), 1, &_rb_mem_unit) ;
60
60
  }
61
61
 
62
- VALUE freeswap(VALUE obj) {
62
+ static VALUE freeswap(volatile VALUE obj) {
63
63
  char status = sysinfo(&info) ;
64
64
  if (status < 0) return Qnil ;
65
65
 
@@ -68,7 +68,7 @@ VALUE freeswap(VALUE obj) {
68
68
  return rb_funcallv_public(_rb_v, rb_intern("*"), 1, &_rb_mem_unit) ;
69
69
  }
70
70
 
71
- VALUE totalhigh(VALUE obj) {
71
+ static VALUE totalhigh(volatile VALUE obj) {
72
72
  char status = sysinfo(&info) ;
73
73
  if (status < 0) return Qnil ;
74
74
 
@@ -77,7 +77,7 @@ VALUE totalhigh(VALUE obj) {
77
77
  return rb_funcallv_public(_rb_v, rb_intern("*"), 1, &_rb_mem_unit) ;
78
78
  }
79
79
 
80
- VALUE freehigh(VALUE obj) {
80
+ static VALUE freehigh(volatile VALUE obj) {
81
81
  char status = sysinfo(&info) ;
82
82
  if (status < 0) return Qnil ;
83
83
 
@@ -86,7 +86,7 @@ VALUE freehigh(VALUE obj) {
86
86
  return rb_funcallv_public(_rb_v, rb_intern("*"), 1, &_rb_mem_unit) ;
87
87
  }
88
88
 
89
- VALUE uptime(VALUE obj) {
89
+ static VALUE uptime(volatile VALUE obj) {
90
90
  char status = sysinfo(&info) ;
91
91
  if (status < 0) return Qnil ;
92
92
 
@@ -94,7 +94,7 @@ VALUE uptime(VALUE obj) {
94
94
  return ULL2NUM((unsigned long long) v) ;
95
95
  }
96
96
 
97
- VALUE loads(VALUE obj) {
97
+ static VALUE loads(volatile VALUE obj) {
98
98
  char status = sysinfo(&info) ;
99
99
  if(status < 0) return rb_ary_new() ;
100
100
 
@@ -112,7 +112,7 @@ VALUE loads(VALUE obj) {
112
112
  }
113
113
 
114
114
  // Some people may need this function, just keep it to not make unnecessary calls
115
- VALUE sysinfoStat(VALUE obj) {
115
+ static VALUE sysinfoStat(volatile VALUE obj) {
116
116
  char status = sysinfo(&info) ;
117
117
  VALUE hash = rb_hash_new() ;
118
118
  if (status < 0) return hash ;
@@ -28,23 +28,23 @@ void init_buf() {
28
28
  }
29
29
  }
30
30
 
31
- static VALUE getSysname(VALUE obj) {
31
+ static VALUE getSysname(volatile VALUE obj) {
32
32
  return rb_str_new_cstr(sysname) ;
33
33
  }
34
34
 
35
- static VALUE getNodename(VALUE obj) {
35
+ static VALUE getNodename(volatile VALUE obj) {
36
36
  return rb_str_new_cstr(nodename) ;
37
37
  }
38
38
 
39
- static VALUE getRelease(VALUE obj) {
39
+ static VALUE getRelease(volatile VALUE obj) {
40
40
  return rb_str_new_cstr(release) ;
41
41
  }
42
42
 
43
- static VALUE getVersion(VALUE obj) {
43
+ static VALUE getVersion(volatile VALUE obj) {
44
44
  return rb_str_new_cstr(version) ;
45
45
  }
46
46
 
47
- static VALUE getMachine(VALUE obj) {
47
+ static VALUE getMachine(volatile VALUE obj) {
48
48
  return rb_str_new_cstr(machine) ;
49
49
  }
50
50
 
@@ -105,11 +105,19 @@ module LinuxStat
105
105
  #
106
106
  # If the battery is not present or the information is not available, it will return nil.
107
107
  def charge
108
- @@charge_now_file ||= File.join(PATH, 'charge_now')
109
- @@charge_full_file ||= File.join(PATH, 'charge_full')
108
+ @@charge_now_file ||= if File.readable?(File.join(PATH, 'charge_now'))
109
+ File.join(PATH, 'charge_now').freeze
110
+ elsif File.readable?(File.join(PATH, 'energy_now'))
111
+ File.join(PATH, 'energy_now').freeze
112
+ end
113
+
114
+ @@charge_full_file ||= if File.readable?(File.join(PATH, 'charge_full'))
115
+ File.join(PATH, 'charge_full').freeze
116
+ elsif File.readable?(File.join(PATH, 'energy_full'))
117
+ File.join(PATH, 'energy_full').freeze
118
+ end
110
119
 
111
- @@charge_now_readable ||= File.readable?(@@charge_now_file) && File.readable?(@@charge_full_file)
112
- return nil unless @@charge_now_readable
120
+ return nil unless @@charge_now_file && @@charge_full_file
113
121
 
114
122
  charge_now = IO.read(@@charge_now_file).to_i
115
123
  charge_full = IO.read(@@charge_full_file).to_i
@@ -215,7 +223,16 @@ module LinuxStat
215
223
 
216
224
  # charge now
217
225
  cn_f = File.join(x, 'charge_now'.freeze).freeze
218
- charge_now = File.readable?(cn_f) ? IO.read(cn_f).to_i.fdiv(1_000_000) : nil
226
+ charge_now = if File.readable?(cn_f)
227
+ IO.read(cn_f).to_i.fdiv(1_000_000)
228
+ else
229
+ en_f = File.join(x, 'energy_now'.freeze)
230
+ if File.readable?(en_f)
231
+ IO.read(en_f) .to_i.fdiv(1_000_000)
232
+ else
233
+ nil
234
+ end
235
+ end
219
236
 
220
237
  # voltage min design
221
238
  vmd_f = File.join(x, 'voltage_min_design'.freeze).freeze
@@ -227,7 +244,16 @@ module LinuxStat
227
244
 
228
245
  # charge full
229
246
  cf_f = File.join(x, 'charge_full'.freeze).freeze
230
- charge_full = File.readable?(cf_f) ? IO.read(cf_f).to_i.fdiv(1_000_000) : nil
247
+ charge_full = if File.readable?(cf_f)
248
+ IO.read(cf_f).to_i.fdiv(1_000_000)
249
+ else
250
+ ef_f = File.join(x, 'energy_full'.freeze)
251
+ if File.readable?(ef_f)
252
+ IO.read(ef_f).to_i.fdiv(1_000_000)
253
+ else
254
+ nil
255
+ end
256
+ end
231
257
 
232
258
  # status
233
259
  s_f = File.join(x, 'status'.freeze).freeze
@@ -110,6 +110,51 @@ module LinuxStat
110
110
  def stat_raw(fs = ?..freeze)
111
111
  LinuxStat::FS.stat(fs)
112
112
  end
113
+
114
+ def sector_size(path = LinuxStat::Mounts.root)
115
+ LinuxStat::FS.sectors(path)
116
+ end
117
+
118
+ def io_total(path = LinuxStat::Mounts.root)
119
+ p = File.split(path)[-1]
120
+ _io_total = LinuxStat::FS.total_io(p)
121
+
122
+ return {} if _io_total.empty?
123
+
124
+ sector_size = LinuxStat::FS.sectors(path)
125
+ return {} unless sector_size
126
+
127
+ {
128
+ read: _io_total[0] &.*(sector_size),
129
+ write: _io_total[1] &.*(sector_size),
130
+ }
131
+ end
132
+
133
+ def io_usage(path = LinuxStat::Mounts.root, interval = 0.1)
134
+ p = File.split(path)[-1]
135
+
136
+ measure1 = LinuxStat::FS.total_io(p)
137
+ sleep(interval)
138
+ measure2 = LinuxStat::FS.total_io(p)
139
+
140
+ return {} if measure1.empty? || measure2.empty?
141
+
142
+ sector_size = LinuxStat::FS.sectors(path)
143
+ return {} unless sector_size
144
+
145
+ m1r = measure1[0]
146
+ m1w = measure1[1]
147
+
148
+ m2r = measure2[0]
149
+ m2w = measure2[1]
150
+
151
+ {
152
+ read: m2r.-(m1r).*(sector_size).fdiv(interval),
153
+ write: m2w.-(m1w).*(sector_size).fdiv(interval)
154
+ }
155
+ end
156
+
157
+ alias sectors sector_size
113
158
  end
114
159
  end
115
160
  end
data/lib/linux_stat/os.rb CHANGED
@@ -7,7 +7,11 @@ module LinuxStat
7
7
  # Reads /etc/os-release and returns a Hash. For example:
8
8
  # LinuxStat::OS.os_release
9
9
  #
10
- # => {:NAME=>"Arch Linux", :PRETTY_NAME=>"Arch Linux", :ID=>"arch", :BUILD_ID=>"rolling", :ANSI_COLOR=>"38;2;23;147;209", :HOME_URL=>"https://www.archlinux.org/", :DOCUMENTATION_URL=>"https://wiki.archlinux.org/", :SUPPORT_URL=>"https://bbs.archlinux.org/", :BUG_REPORT_URL=>"https://bugs.archlinux.org/", :LOGO=>"archlinux"}
10
+ # => {:NAME=>"Arch Linux", :PRETTY_NAME=>"Arch Linux", :ID=>"arch", :BUILD_ID=>"rolling",
11
+ # :ANSI_COLOR=>"38;2;23;147;209", :HOME_URL=>"https://www.archlinux.org/",
12
+ # :DOCUMENTATION_URL=>"https://wiki.archlinux.org/",
13
+ # :SUPPORT_URL=>"https://bbs.archlinux.org/", :BUG_REPORT_URL=>"https://bugs.archlinux.org/",
14
+ # :LOGO=>"archlinux"}
11
15
  #
12
16
  # If the info isn't available, it will return an empty Hash.
13
17
  #
@@ -127,7 +131,7 @@ module LinuxStat
127
131
  #
128
132
  # The return type is strictly Integer and doesn't fail.
129
133
  def bits
130
- @@bits ||= if RbConfig::CONFIG['host_cpu'].end_with?('64') || RUBY_PLATFORM.end_with?('64') || machine.end_with?('64')
134
+ @@bits ||= if machine.end_with?('64') || RbConfig::CONFIG['host_cpu'].end_with?('64') || RUBY_PLATFORM.end_with?('64')
131
135
  64
132
136
  else
133
137
  32
@@ -138,7 +142,7 @@ module LinuxStat
138
142
  # Reads /proc/uptime and returns the system uptime:
139
143
  # LinuxStat::OS.uptime
140
144
  #
141
- # => {:hour=>10, :minute=>34, :second=>12.59}
145
+ # => {:hour=>16, :minute=>10, :second=>11, :jiffy=>20}
142
146
  #
143
147
  # Using uptime is 10x slower than using uptime_i
144
148
  #
@@ -151,12 +155,14 @@ module LinuxStat
151
155
 
152
156
  h = uptime_i / 3600
153
157
  m = uptime_i % 3600 / 60
154
- s = _uptime.%(60).round(2)
158
+ s = uptime_i.%(60)
159
+ j = _uptime.-(uptime_i) * 100
155
160
 
156
161
  {
157
162
  hour: h,
158
163
  minute: m,
159
- second: s
164
+ second: s,
165
+ jiffy: j.to_i
160
166
  }
161
167
  end
162
168
 
@@ -7,6 +7,28 @@ module LinuxStat
7
7
  # 4. kiB, MiB, GiB, TiB, PiB, EiB, ZiB, YiB
8
8
 
9
9
  module PrettifyBytes
10
+ # Kilo = Kilobyte (1000 - 1), and so on...
11
+ # 8.times { |x| puts 1000.**(x.next).to_s << '.00' }
12
+ KILO = 1000.00
13
+ MEGA = 1000000.00
14
+ GIGA = 1000000000.00
15
+ TERA = 1000000000000.00
16
+ PETA = 1000000000000000.00
17
+ EXA = 1000000000000000000.00
18
+ ZETTA = 1000000000000000000000.00
19
+ YOTTA = 1000000000000000000000000.00
20
+
21
+ # Binary suffixes
22
+ # 8.times { |x| puts 1024.**(x.next).to_s << '.00' }
23
+ KIBI = 1024.00
24
+ MEBI = 1048576.00
25
+ GIBI = 1073741824.00
26
+ TEBI = 1099511627776.00
27
+ PEBI = 1125899906842624.00
28
+ EXBI = 1152921504606846976.00
29
+ ZEBI = 1180591620717411303424.00
30
+ YOBI = 1208925819614629174706176.00
31
+
10
32
  class << self
11
33
  ##
12
34
  # Converts a number to decimal byte units and outputs with the metric prefix
@@ -24,12 +46,33 @@ module LinuxStat
24
46
  #
25
47
  # => "1.07 gigabytes"
26
48
  def convert_decimal(n, precision: 2)
27
- @@d_units ||= %W(#{''} kilo mega giga tera peta exa zetta)
28
- .map.with_index { |x, i| [x, 1000.**(i + 1)] }
29
- unit = @@d_units.find { |x| n < x[1] } || ['yotta'.freeze, 10 ** 27]
30
-
31
- converted = n.fdiv(unit[1] / 1000).round(2)
32
- "#{pad_left(converted, precision)} #{unit[0]}byte#{?s.freeze if converted != 1}"
49
+ if n < KILO
50
+ "#{"%.#{precision}f" % n} byte#{?s.freeze if n != 1}"
51
+ elsif n < MEGA
52
+ n /= KILO
53
+ "#{"%.#{precision}f" % n} kilobyte#{?s.freeze if n != 1}"
54
+ elsif n < GIGA
55
+ n /= MEGA
56
+ "#{"%.#{precision}f" % n} megabyte#{?s.freeze if n != 1}"
57
+ elsif n < TERA
58
+ n /= GIGA
59
+ "#{"%.#{precision}f" % n} gigabyte#{?s.freeze if n != 1}"
60
+ elsif n < PETA
61
+ n /= TERA
62
+ "#{"%.#{precision}f" % n} terabyte#{?s.freeze if n != 1}"
63
+ elsif n < EXA
64
+ n /= PETA
65
+ "#{"%.#{precision}f" % n} petabyte#{?s.freeze if n != 1}"
66
+ elsif n < ZETTA
67
+ n /= EXA
68
+ "#{"%.#{precision}f" % n} exabyte#{?s.freeze if n != 1}"
69
+ elsif n < YOTTA
70
+ n /= ZETTA
71
+ "#{"%.#{precision}f" % n} zettabyte#{?s.freeze if n != 1}"
72
+ else
73
+ n /= YOTTA
74
+ "#{"%.#{precision}f" % n} yottabyte#{?s.freeze if n != 1}"
75
+ end
33
76
  end
34
77
 
35
78
  # Converts a number to binary byte units and outputs with the IEC prefix
@@ -47,12 +90,33 @@ module LinuxStat
47
90
  #
48
91
  # => "1.0 gibibyte"
49
92
  def convert_binary(n, precision: 2)
50
- @@b_units ||= %W(#{''} kibi mebi gibi tebi pebi exbi zebi)
51
- .map.with_index { |x, i| [x, 1024.**(i + 1)] }
52
- unit = @@b_units.find { |x| n < x[1] } || ['yobi'.freeze, 10 ** 27]
53
-
54
- converted = n.fdiv(unit[1] / 1024).round(2)
55
- "#{pad_left(converted, precision)} #{unit[0]}byte#{?s.freeze if converted != 1}"
93
+ if n < KIBI
94
+ "#{"%.#{precision}f" % n} byte#{?s.freeze if n != 1}"
95
+ elsif n < MEBI
96
+ n /= KIBI
97
+ "#{"%.#{precision}f" % n} kibibyte#{?s.freeze if n != 1}"
98
+ elsif n < GIBI
99
+ n /= MEBI
100
+ "#{"%.#{precision}f" % n} mebibyte#{?s.freeze if n != 1}"
101
+ elsif n < TEBI
102
+ n /= GIBI
103
+ "#{"%.#{precision}f" % n} gibibyte#{?s.freeze if n != 1}"
104
+ elsif n < PEBI
105
+ n /= TEBI
106
+ "#{"%.#{precision}f" % n} tebibyte#{?s.freeze if n != 1}"
107
+ elsif n < EXBI
108
+ n /= PEBI
109
+ "#{"%.#{precision}f" % n} pebibyte#{?s.freeze if n != 1}"
110
+ elsif n < ZEBI
111
+ n /= EXBI
112
+ "#{"%.#{precision}f" % n} exbiyte#{?s.freeze if n != 1}"
113
+ elsif n < YOBI
114
+ n /= ZEBI
115
+ "#{"%.#{precision}f" % n} zebibyte#{?s.freeze if n != 1}"
116
+ else
117
+ n /= YOBI
118
+ "#{"%.#{precision}f" % n} yobibyte#{?s.freeze if n != 1}"
119
+ end
56
120
  end
57
121
 
58
122
  # Converts a number to decimal byte units
@@ -70,12 +134,33 @@ module LinuxStat
70
134
  #
71
135
  # => "1.07 GB"
72
136
  def convert_short_decimal(n, precision: 2)
73
- @@sd_units ||= %W(#{''} k M G T P E Z)
74
- .map.with_index { |x, i| [x, 1000.**(i + 1)] }
75
- unit = @@sd_units.find { |x| n < x[1] } || [?Y.freeze, 10 ** 27]
76
-
77
- converted = n.fdiv(unit[1] / 1000).round(2)
78
- "#{pad_left(converted, precision)} #{unit[0]}B"
137
+ if n < KILO
138
+ "#{"%.#{precision}f" % n} B"
139
+ elsif n < MEGA
140
+ n /= KILO
141
+ "#{"%.#{precision}f" % n} kB"
142
+ elsif n < GIGA
143
+ n /= MEGA
144
+ "#{"%.#{precision}f" % n} MB"
145
+ elsif n < TERA
146
+ n /= GIGA
147
+ "#{"%.#{precision}f" % n} GB"
148
+ elsif n < PETA
149
+ n /= TERA
150
+ "#{"%.#{precision}f" % n} TB"
151
+ elsif n < EXA
152
+ n /= PETA
153
+ "#{"%.#{precision}f" % n} PB"
154
+ elsif n < ZETTA
155
+ n /= EXA
156
+ "#{"%.#{precision}f" % n} EB"
157
+ elsif n < YOTTA
158
+ n /= ZETTA
159
+ "#{"%.#{precision}f" % n} ZB"
160
+ else
161
+ n /= YOTTA
162
+ "#{"%.#{precision}f" % n} YB"
163
+ end
79
164
  end
80
165
 
81
166
  ##
@@ -95,19 +180,33 @@ module LinuxStat
95
180
  #
96
181
  # => "1.0 GiB"
97
182
  def convert_short_binary(n, precision: 2)
98
- return "#{pad_left(n, precision)} B" if n < 1024
99
-
100
- @@sb_units ||= %W(#{''} K M G T P E Z)
101
- .map.with_index { |x, i| [x, 1024.**(i + 1)] }
102
- unit = @@sb_units.find { |x| n < x[1] } || [?Y.freeze, 1024 ** 9]
103
-
104
- converted = n.fdiv(unit[1] / 1024).round(2)
105
- "#{pad_left(converted, precision)} #{unit[0]}iB"
106
- end
107
-
108
- private
109
- def pad_left(n, mantissa_length = 2)
110
- sprintf("%.#{mantissa_length}f".freeze, n)
183
+ if n < KIBI
184
+ "#{"%.#{precision}f" % n} B"
185
+ elsif n < MEBI
186
+ n /= KIBI
187
+ "#{"%.#{precision}f" % n} KiB"
188
+ elsif n < GIBI
189
+ n /= MEBI
190
+ "#{"%.#{precision}f" % n} MiB"
191
+ elsif n < TEBI
192
+ n /= GIBI
193
+ "#{"%.#{precision}f" % n} GiB"
194
+ elsif n < PEBI
195
+ n /= TEBI
196
+ "#{"%.#{precision}f" % n} TiB"
197
+ elsif n < EXBI
198
+ n /= PEBI
199
+ "#{"%.#{precision}f" % n} PiB"
200
+ elsif n < ZEBI
201
+ n /= EXBI
202
+ "#{"%.#{precision}f" % n} EiB"
203
+ elsif n < YOBI
204
+ n /= ZEBI
205
+ "#{"%.#{precision}f" % n} ZiB"
206
+ else
207
+ n /= YOBI
208
+ "#{"%.#{precision}f" % n} YiB"
209
+ end
111
210
  end
112
211
  end
113
212
  end