linux_stat 2.1.1 → 2.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/exe/linuxstat.rb CHANGED
@@ -150,9 +150,9 @@ execute.sort.each do |c|
150
150
  disp_meth = "#{meth}"
151
151
  disp_meth.concat(arg ? "(#{param})" : "(#{param})")
152
152
 
153
- time = Time.now
153
+ time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
154
154
  ret = arg ? e.send(meth, arg) : e.send(meth)
155
- time2 = Time.now
155
+ time2 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
156
156
  time = time2.-(time).*(1_000_000).round(3)
157
157
 
158
158
  v = ret.inspect
data/ext/procfs/procfs.c CHANGED
@@ -38,5 +38,6 @@ int Init_procfs() {
38
38
 
39
39
  // stat
40
40
  rb_define_module_function(_procfs, "ps_state", ps_state, 1) ;
41
+ rb_define_module_function(_procfs, "ps_times", ps_times, 1) ;
41
42
  rb_define_module_function(_procfs, "ps_stat", ps_stat, 1) ;
42
43
  }
data/ext/procfs/stat.h CHANGED
@@ -17,6 +17,27 @@ VALUE ps_state(VALUE obj, VALUE pid) {
17
17
  return rb_str_new_cstr(_s) ;
18
18
  }
19
19
 
20
+ VALUE ps_times(VALUE obj, VALUE pid) {
21
+ int _pid = FIX2INT(pid) ;
22
+ if (_pid < 0) return Qnil ;
23
+
24
+ char _path[22] ;
25
+ sprintf(_path, "/proc/%d/stat", _pid) ;
26
+
27
+ FILE *f = fopen(_path, "r") ;
28
+ if (!f) return Qnil ;
29
+
30
+ unsigned long utime, stime ;
31
+
32
+ char status = fscanf(f, "%*llu (%*[^)]%*[)] %*c %*d %*d %*d %*d %*d %*u %*lu %*lu %*lu %*lu %lu %lu", &utime, &stime) ;
33
+ fclose(f) ;
34
+
35
+ if (status != 2) return Qnil ;
36
+ double total_time = (utime + stime) / (float)sysconf(_SC_CLK_TCK);
37
+
38
+ return DBL2NUM(total_time) ;
39
+ }
40
+
20
41
  VALUE ps_stat(VALUE obj, VALUE pid) {
21
42
  int _pid = FIX2INT(pid) ;
22
43
  if (_pid < 0) return rb_str_new_cstr("") ;
@@ -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
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
@@ -283,7 +283,7 @@ module LinuxStat
283
283
  uptime = LS::ProcFS.uptime_f
284
284
  return {} unless uptime && !stat.empty?
285
285
 
286
- utime, stime, starttime = *stat.values_at(11, 12, 19).map(&:to_f)
286
+ utime, stime, starttime = *stat.values_at(10, 11, 18).map(&:to_f)
287
287
  uptime *= ticks
288
288
 
289
289
  total_time = utime + stime
@@ -295,14 +295,14 @@ module LinuxStat
295
295
  uptime = LS::ProcFS.uptime_f
296
296
  return {} unless uptime && !stat.empty?
297
297
 
298
- utime2, stime2, starttime2 = *stat.values_at(11, 12, 19).map(&:to_f)
298
+ utime2, stime2, starttime2 = *stat.values_at(10, 11, 18).map(&:to_f)
299
299
  uptime *= ticks
300
300
 
301
301
  total_time2 = utime2 + stime2
302
302
  idle2 = uptime - starttime2 - total_time2
303
303
 
304
304
  totald = idle2.+(total_time2).-(idle1 + total_time)
305
- cpu_u = totald.-(idle2 - idle1).fdiv(totald).abs.*(100)./(cpu_count)
305
+ cpu_u = totald.-(idle2 - idle1).fdiv(totald).abs.*(100)./(LinuxStat::CPU.count)
306
306
 
307
307
  {
308
308
  cpu_usage: cpu_u > 100 ? 100.0 : cpu_u.round(2),
@@ -338,32 +338,10 @@ module LinuxStat
338
338
  #
339
339
  # This method is more efficient than running LinuxStat::ProcessInfo.cpu_stat()
340
340
  def cpu_usage(pid: $$, sleep: ticks_to_ms_t5)
341
- ticks = get_ticks
342
- stat = LinuxStat::ProcFS.ps_stat(pid)
343
- uptime = LS::ProcFS.uptime_f
344
- return nil unless uptime && !stat.empty?
345
-
346
- utime, stime, starttime = *stat.values_at(10, 11, 18).map(&:to_f)
347
- uptime *= ticks
348
-
349
- total_time = utime + stime
350
- idle1 = uptime - starttime - total_time
351
-
352
- sleep(sleep)
353
-
354
- stat = LinuxStat::ProcFS.ps_stat(pid)
355
- uptime = LS::ProcFS.uptime_f
356
- return nil unless uptime && !stat.empty?
357
-
358
- utime2, stime2, starttime2 = *stat.values_at(10, 11, 18).map(&:to_f)
359
- uptime *= ticks
360
-
361
- total_time2 = utime2 + stime2
362
- idle2 = uptime - starttime2 - total_time2
363
-
364
- totald = idle2.+(total_time2).-(idle1 + total_time)
341
+ u = cpu_usage_thread(pid, sleep)
342
+ return nil unless u
365
343
 
366
- u = totald.-(idle2 - idle1).fdiv(totald).abs.*(100)./(cpu_count)
344
+ u /= LinuxStat::CPU.count
367
345
  u > 100 ? 100.0 : u.round(2)
368
346
  end
369
347
 
@@ -390,34 +368,10 @@ module LinuxStat
390
368
  #
391
369
  # But if the info isn't available, it will return nil.
392
370
  def thread_usage(pid: $$, sleep: ticks_to_ms_t5)
393
- ticks = get_ticks
394
- stat = LinuxStat::ProcFS.ps_stat(pid)
395
- uptime = LS::ProcFS.uptime_f
396
- return nil unless uptime && !stat.empty?
397
-
398
- utime, stime, starttime = *stat.values_at(10, 11, 18).map(&:to_f)
399
- uptime *= ticks
400
-
401
- total_time = utime + stime
402
- idle1 = uptime - starttime - total_time
403
-
404
- sleep(sleep)
405
-
406
- stat = LinuxStat::ProcFS.ps_stat(pid)
407
- uptime = LS::ProcFS.uptime_f
408
- return nil unless uptime && !stat.empty?
409
-
410
- utime2, stime2, starttime2 = *stat.values_at(10, 11, 18).map(&:to_f)
411
- uptime *= ticks
412
-
413
- total_time2 = utime2 + stime2
414
- idle2 = uptime - starttime2 - total_time2
415
-
416
- totald = idle2.+(total_time2).-(idle1 + total_time)
371
+ u = cpu_usage_thread(pid, sleep)
372
+ return nil unless u
417
373
 
418
- u = totald.-(idle2 - idle1).fdiv(totald).abs.*(100)
419
-
420
- cpu_count_t100 = cpu_count * 100
374
+ cpu_count_t100 = LinuxStat::CPU.count * 100
421
375
  u > cpu_count_t100 ? cpu_count_t100 : u.round(2)
422
376
  end
423
377
 
@@ -567,7 +521,9 @@ module LinuxStat
567
521
  def start_time(pid = $$)
568
522
  # Getting two Time objects and dealing with floating point numbers
569
523
  # Just to make sure the time goes monotonically
570
- Time.at(start_time_epoch(pid))
524
+ _ste = start_time_epoch(pid)
525
+ return nil unless _ste
526
+ Time.at(_ste)
571
527
  end
572
528
 
573
529
  ##
@@ -660,13 +616,10 @@ module LinuxStat
660
616
  #
661
617
  # Shows the CPU time used by the process.
662
618
  #
663
- # The return value is an Integer.
619
+ # The return value is a Float.
620
+ # But if the info isn't available, it will return nil.
664
621
  def cpu_time(pid = $$)
665
- times = LinuxStat::ProcFS.ps_stat(pid)
666
- utime, stime, cutime, cstime = times[10], times[11], times[12], times[13]
667
- return nil unless utime && stime && cutime && cstime
668
-
669
- utime.+(stime).+(cutime).+(cstime) / get_ticks
622
+ LinuxStat::ProcFS.ps_times(pid)
670
623
  end
671
624
 
672
625
  ##
@@ -674,19 +627,28 @@ module LinuxStat
674
627
  #
675
628
  # Shows the CPU time used by the process.
676
629
  #
677
- # The return value is a Hash.
630
+ # The return value is a Hash formatted like this:
631
+ # LS::ProcessInfo.cpu_times($$)
632
+ #
633
+ # => {:hour=>0, :minute=>39, :second=>12, :jiffy=>0.42}
634
+ #
635
+ # But if the info isn't available, it will return an empty Hash..
678
636
  def cpu_times(pid = $$)
679
- v = cpu_time(pid)
637
+ v = LinuxStat::ProcFS.ps_times(pid)
680
638
  return {} unless v
681
639
 
682
- hour = v / 3600
683
- min = v % 3600 / 60
684
- sec = v % 60
640
+ v_i = v.to_i
641
+
642
+ hour = v_i / 3600
643
+ min = v_i % 3600 / 60
644
+ sec = v_i % 60
645
+ jiffy = v.-(v_i) * 100
685
646
 
686
647
  {
687
648
  hour: hour,
688
649
  minute: min,
689
- second: sec
650
+ second: sec,
651
+ jiffy: jiffy.to_i
690
652
  }
691
653
  end
692
654
 
@@ -709,8 +671,33 @@ module LinuxStat
709
671
  @@pagesize ||= LinuxStat::Sysconf.pagesize.to_i
710
672
  end
711
673
 
712
- def cpu_count
713
- @@nprocessors_conf ||= LinuxStat::CPU.count
674
+ def cpu_usage_thread(pid, delay)
675
+ ticks = get_ticks
676
+ stat = LinuxStat::ProcFS.ps_stat(pid)
677
+ uptime = LS::ProcFS.uptime_f
678
+ return nil unless uptime && !stat.empty?
679
+
680
+ utime, stime, starttime = *stat.values_at(10, 11, 18).map(&:to_f)
681
+ uptime *= ticks
682
+
683
+ total_time = utime + stime
684
+ idle1 = uptime - starttime - total_time
685
+
686
+ sleep(delay)
687
+
688
+ stat = LinuxStat::ProcFS.ps_stat(pid)
689
+ uptime = LS::ProcFS.uptime_f
690
+ return nil unless uptime && !stat.empty?
691
+
692
+ utime2, stime2, starttime2 = *stat.values_at(10, 11, 18).map(&:to_f)
693
+ uptime *= ticks
694
+
695
+ total_time2 = utime2 + stime2
696
+ idle2 = uptime - starttime2 - total_time2
697
+
698
+ totald = idle2.+(total_time2).-(idle1 + total_time)
699
+
700
+ totald.-(idle2 - idle1).fdiv(totald).abs.*(100)
714
701
  end
715
702
  end
716
703
  end