linux_stat 2.2.3 → 2.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/ext/procfs/statm.h CHANGED
@@ -1,6 +1,6 @@
1
1
  #define PAGESIZE sysconf(_SC_PAGESIZE)
2
2
 
3
- VALUE statm(VALUE obj, VALUE pid) {
3
+ static VALUE statm(volatile VALUE obj, volatile VALUE pid) {
4
4
  VALUE hash = rb_hash_new() ;
5
5
 
6
6
  int _pid = FIX2INT(pid) ;
@@ -34,7 +34,7 @@ VALUE statm(VALUE obj, VALUE pid) {
34
34
  return hash ;
35
35
  }
36
36
 
37
- VALUE statm_virtual(VALUE obj, VALUE pid) {
37
+ static VALUE statm_virtual(volatile VALUE obj, volatile VALUE pid) {
38
38
  int _pid = FIX2INT(pid) ;
39
39
  if (_pid < 0) return Qnil ;
40
40
 
@@ -52,7 +52,7 @@ VALUE statm_virtual(VALUE obj, VALUE pid) {
52
52
  return UINT2NUM(_virtual * PAGESIZE) ;
53
53
  }
54
54
 
55
- VALUE statm_resident(VALUE obj, VALUE pid) {
55
+ static VALUE statm_resident(volatile VALUE obj, volatile VALUE pid) {
56
56
  int _pid = FIX2INT(pid) ;
57
57
  if (_pid < 0) return Qnil ;
58
58
 
@@ -70,7 +70,7 @@ VALUE statm_resident(VALUE obj, VALUE pid) {
70
70
  return UINT2NUM(resident * PAGESIZE) ;
71
71
  }
72
72
 
73
- VALUE statm_shared(VALUE obj, VALUE pid) {
73
+ static VALUE statm_shared(volatile VALUE obj, volatile VALUE pid) {
74
74
  int _pid = FIX2INT(pid) ;
75
75
  if (_pid < 0) return Qnil ;
76
76
 
@@ -88,7 +88,7 @@ VALUE statm_shared(VALUE obj, VALUE pid) {
88
88
  return UINT2NUM(shared * PAGESIZE) ;
89
89
  }
90
90
 
91
- VALUE statm_memory(VALUE obj, VALUE pid) {
91
+ static VALUE statm_memory(volatile VALUE obj, volatile VALUE pid) {
92
92
  int _pid = FIX2INT(pid) ;
93
93
  if (_pid < 0) return Qnil ;
94
94
 
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
 
@@ -308,7 +308,7 @@ module LinuxStat
308
308
  ret[:charge_full_wh] = c_f_wh if c_f_wh
309
309
  ret[:charge_percentage] = charge_percentage if charge_percentage
310
310
 
311
- h.merge!(File.split(x)[-1].to_sym => ret)
311
+ h.store(File.split(x)[-1].to_sym, ret)
312
312
  end
313
313
  h
314
314
  end
@@ -21,24 +21,53 @@ module LinuxStat
21
21
  #
22
22
  # => {0=>84.38, 1=>100.0, 2=>50.0, 3=>87.5, 4=>87.5}
23
23
  #
24
+ # It discards any offline CPU or disabled CPU.
25
+ # For example, if your system CPU has 4 cores, and you disabled core 3, the output will be:
26
+ # LinuxStat::CPU.stat
27
+ #
28
+ # => {0=>26.67, 1=>0.0, 2=>20.0, 4=>20.0}
29
+ #
24
30
  # If the information is not available, it will return an empty Hash
25
31
  def stat(sleep = ticks_to_ms_t5)
26
32
  return {} unless stat?
27
33
 
28
- data = IO.readlines('/proc/stat'.freeze).select { |x| x[/^cpu\d*/] }.map! { |x| x.split.map!(&:to_f) }
34
+ data = IO.readlines('/proc/stat'.freeze).select { |x| x[/^cpu\d*/] }
35
+ cpu_names1 = []
36
+ data.map! { |x|
37
+ splitted = x.split
38
+ name = splitted.shift[/\d*$/]
39
+ cpu_names1.push(name.empty? ? 0 : name.to_i + 1)
40
+ splitted.map!(&:to_f)
41
+ }
42
+
29
43
  sleep(sleep)
30
- data2 = IO.readlines('/proc/stat'.freeze).select { |x| x[/^cpu\d*/] }.map! { |x| x.split.map!(&:to_f) }
44
+ data2 = IO.readlines('/proc/stat'.freeze).select { |x| x[/^cpu\d*/] }
45
+
46
+ cpu_names2 = []
47
+ data2.map! { |x|
48
+ splitted = x.split
49
+ name = splitted.shift[/\d*$/]
50
+ cpu_names2.push(name.empty? ? 0 : name.to_i + 1)
51
+ splitted.map!(&:to_f)
52
+ }
31
53
 
32
54
  # On devices like android, the core count can change anytime (hotplugging).
33
55
  # I had crashes on Termux.
34
56
  # So better just count the min number of CPU and iterate over that
35
57
  # If data.length is smaller than data2.length, we don't have enough data to compare.
36
- dl, d2l = data.length, data2.length
37
- min = dl > d2l ? d2l : dl
58
+ dl, d2l = cpu_names1.length, cpu_names2.length
59
+ if dl > d2l
60
+ min = d2l
61
+ cpu_cores = cpu_names2
62
+ else
63
+ min = dl
64
+ cpu_cores = cpu_names1
65
+ end
38
66
 
39
67
  min.times.reduce({}) do |h, x|
40
- user, nice, sys, idle, iowait, irq, softirq, steal = *data[x].drop(1)
41
- user2, nice2, sys2, idle2, iowait2, irq2, softirq2, steal2 = *data2[x].drop(1)
68
+ cpu_core = cpu_cores[x]
69
+ user, nice, sys, idle, iowait, irq, softirq, steal = *data[x]
70
+ user2, nice2, sys2, idle2, iowait2, irq2, softirq2, steal2 = *data2[x]
42
71
 
43
72
  idle_then, idle_now = idle + iowait, idle2 + iowait2
44
73
  totald = idle_now.+(user2 + nice2 + sys2 + irq2 + softirq2 + steal2) - idle_then.+(user + nice + sys + irq + softirq + steal)
@@ -46,7 +75,8 @@ module LinuxStat
46
75
  res = totald.-(idle_now - idle_then).fdiv(totald).abs.*(100)
47
76
  res = res.nan? ? 0.0 : res > 100 ? 100.0 : res.round(2)
48
77
 
49
- h.merge!( x => res )
78
+ h.store(cpu_core, res)
79
+ h
50
80
  end
51
81
  end
52
82
 
@@ -203,12 +233,16 @@ module LinuxStat
203
233
 
204
234
  h = {}
205
235
  @@cur_f.each { |id, file|
206
- h.merge!(id => IO.read(file).to_i) if File.readable?(file)
236
+ h.store(id, IO.read(file).to_i) if File.readable?(file)
207
237
  }
208
238
 
209
239
  h
210
240
  end
211
241
 
242
+ def times
243
+ LinuxStat::ProcFS.cpu_times
244
+ end
245
+
212
246
  ##
213
247
  # Returns a Hash with max core frequencies corresponding to the CPUs.
214
248
  #
@@ -225,7 +259,7 @@ module LinuxStat
225
259
 
226
260
  h = {}
227
261
  @@min_f.each { |id, file|
228
- h.merge!(id => IO.read(file).to_i) if File.readable?(file)
262
+ h.store(id, IO.read(file).to_i) if File.readable?(file)
229
263
  }
230
264
 
231
265
  h
@@ -247,7 +281,7 @@ module LinuxStat
247
281
 
248
282
  h = {}
249
283
  @@min_f.each { |id, file|
250
- h.merge!(id => IO.read(file).to_i) if File.readable?(file)
284
+ h.store(id, IO.read(file).to_i) if File.readable?(file)
251
285
  }
252
286
 
253
287
  h
@@ -271,7 +305,7 @@ module LinuxStat
271
305
 
272
306
  h = {}
273
307
  @@scaling_g.each { |id, file|
274
- h.merge!(id => IO.read(file).tap(&:strip!)) if File.readable?(file)
308
+ h.store(id, IO.read(file).tap(&:strip!)) if File.readable?(file)
275
309
  }
276
310
 
277
311
  h
@@ -293,14 +327,89 @@ module LinuxStat
293
327
 
294
328
  h = {}
295
329
  @@scaling_av_g.each { |id, file|
296
- h.merge!(id => IO.read(file).split.each(&:strip!)) if File.readable?(file)
330
+ h.store(id, IO.read(file).split.each(&:strip!)) if File.readable?(file)
297
331
  }
298
332
 
299
333
  h
300
334
  end
301
335
 
336
+ ##
337
+ # Returns the number of physical cores on the system.
338
+ #
339
+ # The return value is an Array of Integers. Each number denoting the physical processor number.
340
+ # You can later use this to schedule tasks or something else (not provided by LinuxStat).
341
+ #
342
+ # However, if the information isn't available on /sys/devices/system/cpu[0-9]*/topology/thread_siblings_list, it will return an empty Array.
343
+ def physical_core_list
344
+ physical_cores = []
345
+ hyperthreaded = {}
346
+
347
+ entries = Dir.entries('/sys/devices/system/cpu/'.freeze)
348
+ entries.delete(?..freeze)
349
+ entries.delete('..'.freeze)
350
+
351
+ entries.each do |x|
352
+ if x[0..2] == 'cpu'.freeze && LinuxStat::Misc.integer?(x[3..-1])
353
+ file = "/sys/devices/system/cpu/#{x}/topology/thread_siblings_list"
354
+ next unless File.readable?(file)
355
+
356
+ data = IO.read(file)
357
+ data.strip!
358
+
359
+ val = data.split(?,.freeze).map(&:to_i)
360
+ val.shift
361
+
362
+ # Add items has for fast lookup.
363
+ # This hash includes all hyper threaded cores that doesn't map to anything.
364
+ # But this hash has the purpose to look up for items and not include in the list of physical_cores
365
+ # This is just an array, but can look for keys in O(1), so it's faster than ary.find() { ... }.
366
+ val.each { |x| hyperthreaded.store(x, nil) }
367
+
368
+ key = x[3..-1].to_i
369
+ physical_cores << key unless hyperthreaded.key?(key)
370
+ end
371
+ end
372
+
373
+ physical_cores
374
+ end
375
+
376
+ ##
377
+ # Returns the number of physical cores on the system.
378
+ #
379
+ # The return value is an Array of Integers. Each number denoting the hyperthreaded processor number.
380
+ # You can later use this to schedule tasks or something else (not provided by LinuxStat).
381
+ #
382
+ # However, if the information isn't available on /sys/devices/system/cpu[0-9]*/topology/thread_siblings_list, it will return an empty Array.
383
+ def hyperthreaded_core_list
384
+ hyperthreaded = {}
385
+
386
+ entries = Dir.entries('/sys/devices/system/cpu/'.freeze)
387
+ entries.delete(?..freeze)
388
+ entries.delete('..'.freeze)
389
+
390
+ entries.each do |x|
391
+ if x[0..2] == 'cpu'.freeze && LinuxStat::Misc.integer?(x[3..-1])
392
+ file = "/sys/devices/system/cpu/#{x}/topology/thread_siblings_list"
393
+ next unless File.readable?(file)
394
+
395
+ data = IO.read(file)
396
+ data.strip!
397
+
398
+ val = data.split(?,.freeze).map(&:to_i)
399
+ val.shift
400
+
401
+ # Add items has for fast lookup to get rid of duplicate items.
402
+ val.each { |x| hyperthreaded.store(x, nil) unless hyperthreaded.key?(x) }
403
+ end
404
+ end
405
+
406
+ hyperthreaded.keys
407
+ end
408
+
302
409
  alias usages stat
303
410
  alias usage total_usage
411
+ alias physical_cores physical_core_list
412
+ alias hyperthreaded_cores hyperthreaded_core_list
304
413
 
305
414
  private
306
415
  def cpuinfo
@@ -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
@@ -58,7 +58,7 @@ module LinuxStat
58
58
  def tmpfs
59
59
  ret = {}
60
60
  mounts.each { |x|
61
- ret.merge!({x.split[1] => x}) if x.start_with?('tmpfs '.freeze)
61
+ ret.store(x.split[1], x) if x.start_with?('tmpfs '.freeze)
62
62
  }
63
63
  ret
64
64
  end
@@ -115,7 +115,7 @@ module LinuxStat
115
115
 
116
116
  unless x.empty?
117
117
  _x = x.split
118
- m.merge!(_x[0] => _x[1])
118
+ m.store(_x[0], _x[1])
119
119
  end
120
120
  end
121
121
  m
@@ -146,7 +146,7 @@ module LinuxStat
146
146
  _x = x.split
147
147
  total, free, available, used = fs_info(_x[1])
148
148
 
149
- m.merge!(_x[0] => {
149
+ m.store(_x[0], {
150
150
  mountpoint: _x[1],
151
151
 
152
152
  total: total,
@@ -120,8 +120,8 @@ module LinuxStat
120
120
 
121
121
  r.each_with_index { |x, i|
122
122
  downcased = x.downcase
123
- h.merge!(:r => i) if downcased.start_with?('receive'.freeze)
124
- h.merge!(:t => i) if downcased.start_with?('transmit'.freeze)
123
+ h.store(:r, i) if downcased.start_with?('receive'.freeze)
124
+ h.store(:t, i) if downcased.start_with?('transmit'.freeze)
125
125
  }
126
126
 
127
127
  data_0 = data.next.gsub(?|.freeze, ' %'.freeze)
data/lib/linux_stat/os.rb CHANGED
@@ -234,7 +234,8 @@ module LinuxStat
234
234
  value[0] = ''.freeze if value[0] == ?".freeze
235
235
  value[-1] = ''.freeze if value[-1] == ?".freeze
236
236
 
237
- h.merge!( key.to_sym => value )
237
+ h.store(key.to_sym, value)
238
+ h
238
239
  }
239
240
  end
240
241
  end
@@ -76,7 +76,7 @@ module LinuxStat
76
76
  kernel_driver: kernel_driver
77
77
  }
78
78
 
79
- ret.merge!(hwdata: query) unless query.empty?
79
+ ret.store(:hwdata, query) unless query.empty?
80
80
 
81
81
  ret
82
82
  }
@@ -186,14 +186,14 @@ module LinuxStat
186
186
  vendor: vendor, device: device
187
187
  }
188
188
 
189
- ret.merge!(sub_vendor: sub_vendor) if sub_vendor
190
- ret.merge!(sub_device: sub_device) if sub_device
189
+ ret.store(:sub_vendor, sub_vendor) if sub_vendor
190
+ ret.store(:sub_device, sub_device) if sub_device
191
191
 
192
- ret.merge!(kernel_driver: kernel_driver) if kernel_driver
193
- ret.merge!(revision: revision) unless revision.empty?
194
- ret.merge!(irq: irq) if irq
195
- ret.merge!(enable: enable) unless enable.nil?
196
- ret.merge!(hwdata: query) unless query.empty?
192
+ ret.store(:kernel_driver, kernel_driver) if kernel_driver
193
+ ret.store(:revision, revision) unless revision.empty?
194
+ ret.store(:irq, irq) if irq
195
+ ret.store(:enable, enable) unless enable.nil?
196
+ ret.store(:hwdata, query) unless query.empty?
197
197
 
198
198
  ret
199
199
  rescue StandardError
@@ -355,7 +355,7 @@ module LinuxStat
355
355
 
356
356
  if sub_vendor_id && sub_device_id
357
357
  sub_product = vendor.dig(1, product_id, 1, sub_vendor_id, sub_device_id)
358
- ret.merge!(sub_system: sub_product) if sub_product
358
+ ret.store(:sub_system, sub_product) if sub_product
359
359
  end
360
360
 
361
361
  ret