linux_stat 0.7.4 → 0.9.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.
@@ -100,10 +100,7 @@ module LinuxStat
100
100
 
101
101
  private
102
102
  def pad_left(n, mantissa_length = 2)
103
- n = n.round(mantissa_length)
104
- exp, mant = n.to_s.split(?..freeze)
105
- m = mant.length < mantissa_length ? mant + ?0.freeze * (mantissa_length - mant.length) : mant
106
- exp + ?..freeze + m
103
+ sprintf("%.#{mantissa_length}f".freeze, n)
107
104
  end
108
105
  end
109
106
  end
@@ -6,7 +6,7 @@ module LinuxStat
6
6
  #
7
7
  # The return type is an Array of Integers.
8
8
  def list
9
- Dir['/proc/*'].select! { |x|
9
+ Dir['/proc/*'].select { |x|
10
10
  pid = File.split(x)[1]
11
11
  pid.to_i.to_s == pid
12
12
  }.map! { |x| File.split(x)[-1].to_i }
@@ -203,13 +203,13 @@ module LinuxStat
203
203
  end
204
204
 
205
205
  ##
206
- # = cpu_stat(pid: $$, sleep: 1.0 / LinuxStat::Sysconf.sc_clk_tck)
206
+ # = cpu_stat(pid: $$, sleep: 1.0 / LinuxStat::Sysconf.sc_clk_tck * 5)
207
207
  #
208
208
  # Where pid is the process ID and sleep time is the interval between measurements.
209
209
  #
210
210
  # By default it is the id of the current process ($$), and sleep is LinuxStat::Sysconf.sc_clk_tck
211
211
  #
212
- # The smallest amount of available sleep time is 1.0 / LinuxStat::Sysconf.sc_clk_tck.
212
+ # The smallest amount of available sleep time is 1.0 / LinuxStat::Sysconf.sc_clk_tck * 5.
213
213
  #
214
214
  # * Note 1:
215
215
  # 1. Do note that the sleep time can slow down your application.
@@ -244,20 +244,22 @@ module LinuxStat
244
244
  # Only use this method if you need all of the data at once, in such case, it's more efficient to use this method.
245
245
  #
246
246
  # The :last_executed_cpu also returns an Integer indicating the last executed cpu of the process.
247
- def cpu_stat(pid: $$, sleep: ticks_to_ms)
247
+ def cpu_stat(pid: $$, sleep: ticks_to_ms_t5)
248
248
  file = "/proc/#{pid}/stat"
249
- return {} unless File.readable?(file)
250
-
251
249
  ticks = get_ticks
252
250
 
251
+ return {} unless File.readable?(file)
253
252
  utime, stime, starttime = IO.read(file)
254
253
  .split.values_at(13, 14, 21).map(&:to_f)
254
+
255
255
  uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
256
256
 
257
257
  total_time = utime + stime
258
258
  idle1 = uptime - starttime - total_time
259
259
 
260
260
  sleep(sleep)
261
+
262
+ return {} unless File.readable?(file)
261
263
  stat = IO.read(file).split
262
264
 
263
265
  utime2, stime2, starttime2 = stat.values_at(13, 14, 21).map(&:to_f)
@@ -267,25 +269,25 @@ module LinuxStat
267
269
  idle2 = uptime - starttime2 - total_time2
268
270
 
269
271
  totald = idle2.+(total_time2).-(idle1 + total_time)
270
- cpu = totald.-(idle2 - idle1).fdiv(totald).*(100).round(2).abs./(LinuxStat::CPU.count)
272
+ cpu_u = totald.-(idle2 - idle1).fdiv(totald).abs.*(100)./(cpu_count)
271
273
 
272
274
  {
273
- cpu_usage: cpu,
275
+ cpu_usage: cpu_u > 100 ? 100.0 : cpu_u.round(2),
274
276
  threads: stat[19].to_i,
275
277
  last_executed_cpu: stat[38].to_i
276
278
  }
277
279
  end
278
280
 
279
281
  ##
280
- # = cpu_usage(pid: $$, sleep: 1.0 / LinuxStat::Sysconf.sc_clk_tck)
282
+ # = cpu_usage(pid: $$, sleep: 1.0 / LinuxStat::Sysconf.sc_clk_tck * 5)
281
283
  #
282
284
  # Where pid is the process ID and sleep time is the interval between measurements.
283
285
  #
284
- # By default it is the id of the current process ($$), and sleep is 1.0 / LinuxStat::Sysconf.sc_clk_tck
286
+ # By default it is the id of the current process ($$), and sleep is 1.0 / LinuxStat::Sysconf.sc_clk_tck * 5
285
287
  #
286
288
  # The smallest amount of available sleep time is LinuxStat::Sysconf.sc_clk_tck.
287
289
  #
288
- # It retuns the CPU usage in Float.
290
+ # It retuns the CPU usage as Float.
289
291
  #
290
292
  # For example:
291
293
  #
@@ -293,19 +295,75 @@ module LinuxStat
293
295
  #
294
296
  # => 10.0
295
297
  #
298
+ # 10.0 means it's using 10% of the total processing power of the system.
299
+ #
300
+ # The value is divided with the configured number of CPU and not online CPU.
301
+ #
296
302
  # A value of 100.0 indicates it is using 100% processing power available to the system.
297
303
  #
298
304
  # But if the info isn't available, it will return nil.
299
305
  #
300
306
  # This method is more efficient than running LinuxStat::ProcessInfo.cpu_stat()
301
- def cpu_usage(pid: $$, sleep: ticks_to_ms)
307
+ def cpu_usage(pid: $$, sleep: ticks_to_ms_t5)
302
308
  file = "/proc/#{pid}/stat"
309
+ ticks = get_ticks
310
+
303
311
  return nil unless File.readable?(file)
312
+ utime, stime, starttime = IO.read(file)
313
+ .split.values_at(13, 14, 21).map(&:to_f)
314
+
315
+ uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
316
+
317
+ total_time = utime + stime
318
+ idle1 = uptime - starttime - total_time
319
+
320
+ sleep(sleep)
304
321
 
322
+ return nil unless File.readable?(file)
323
+ utime2, stime2, starttime2 = IO.read(file)
324
+ .split.values_at(13, 14, 21).map(&:to_f)
325
+
326
+ uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
327
+
328
+ total_time2 = utime2 + stime2
329
+ idle2 = uptime - starttime2 - total_time2
330
+
331
+ totald = idle2.+(total_time2).-(idle1 + total_time)
332
+
333
+ u = totald.-(idle2 - idle1).fdiv(totald).abs.*(100)./(cpu_count)
334
+ u > 100 ? 100.0 : u.round(2)
335
+ end
336
+
337
+ ##
338
+ # = thread_usage(pid: $$, sleep: 1.0 / LinuxStat::Sysconf.sc_clk_tck * 5)
339
+ #
340
+ # Where pid is the process ID and sleep time is the interval between measurements.
341
+ #
342
+ # By default it is the id of the current process ($$), and sleep is 1.0 / LinuxStat::Sysconf.sc_clk_tck * 5
343
+ #
344
+ # The smallest amount of available sleep time is LinuxStat::Sysconf.sc_clk_tck.
345
+ #
346
+ # It retuns the per core CPU usage as Float.
347
+ #
348
+ # For example:
349
+ #
350
+ # LinuxStat::ProcessInfo.core_usage
351
+ #
352
+ # => 200.0
353
+ #
354
+ # A value of 100.0 indicates it is using 100% processing power of a core.
355
+ #
356
+ # The value could be 0 to (100 * the number of CPU threads (including hyperthreading) in the system)
357
+ #
358
+ # But if the info isn't available, it will return nil.
359
+ def thread_usage(pid: $$, sleep: ticks_to_ms_t5)
360
+ file = "/proc/#{pid}/stat"
305
361
  ticks = get_ticks
306
362
 
363
+ return nil unless File.readable?(file)
307
364
  utime, stime, starttime = IO.read(file)
308
365
  .split.values_at(13, 14, 21).map(&:to_f)
366
+
309
367
  uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
310
368
 
311
369
  total_time = utime + stime
@@ -313,15 +371,21 @@ module LinuxStat
313
371
 
314
372
  sleep(sleep)
315
373
 
374
+ return nil unless File.readable?(file)
316
375
  utime2, stime2, starttime2 = IO.read(file)
317
376
  .split.values_at(13, 14, 21).map(&:to_f)
377
+
318
378
  uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
319
379
 
320
380
  total_time2 = utime2 + stime2
321
381
  idle2 = uptime - starttime2 - total_time2
322
382
 
323
383
  totald = idle2.+(total_time2).-(idle1 + total_time)
324
- totald.-(idle2 - idle1).fdiv(totald).*(100).round(2).abs./(LinuxStat::CPU.count)
384
+
385
+ u = totald.-(idle2 - idle1).fdiv(totald).abs.*(100)
386
+
387
+ cpu_count_t100 = cpu_count * 100
388
+ u > cpu_count_t100 ? cpu_count_t100 : u.round(2)
325
389
  end
326
390
 
327
391
  ##
@@ -491,7 +555,9 @@ module LinuxStat
491
555
  # For example:
492
556
  # LinuxStat::ProcessInfo.running_time 14183
493
557
  #
494
- # => 1947.619999999999
558
+ # => 1947.61
559
+ #
560
+ # It always rounds the float number upto 2 decimal places
495
561
  #
496
562
  # If the info isn't available or the argument passed doesn't exist as a process ID, it will return nil.
497
563
  def running_time(pid = $$)
@@ -501,7 +567,8 @@ module LinuxStat
501
567
  @@u_readable ||= File.readable?(uptime)
502
568
  return nil unless @@u_readable && File.readable?(stat_file)
503
569
 
504
- IO.foreach(uptime, ' '.freeze).next.to_f - (IO.read(stat_file).split[21].to_i / get_ticks)
570
+ IO.foreach(uptime, ' '.freeze).next.to_f
571
+ .-(IO.read(stat_file).split[21].to_f / get_ticks).round(2)
505
572
  end
506
573
 
507
574
  ##
@@ -544,18 +611,57 @@ module LinuxStat
544
611
  IO.foreach(file, ' ').first(19)[-1].to_i
545
612
  end
546
613
 
614
+ ##
615
+ # = nproc(pid = $$)
616
+ # Returns the cpu allocated to the process.
617
+ #
618
+ # The output value is an Integer.
619
+ #
620
+ # For example:
621
+ # $ taskset -c 0 irb
622
+ #
623
+ # irb(main):001:0> require 'linux_stat'
624
+ #
625
+ # => true
626
+ #
627
+ # irb(main):002:0> LinuxStat::ProcessInfo.nproc
628
+ #
629
+ # => 1
630
+ #
631
+ # irb(main):003:0> LinuxStat::ProcessInfo.nproc 11562
632
+ #
633
+ # => 3
634
+ #
635
+ # irb(main):004:0> LinuxStat::ProcessInfo.nproc 12513
636
+ #
637
+ # => 4
638
+ #
639
+ # If the info isn't available or the argument passed doesn't exist as a process ID, it will return nil.
640
+ def nproc(pid = $$)
641
+ LinuxStat::Nproc.count_cpu_for_pid(pid)
642
+ end
643
+
644
+ alias count_cpu nproc
645
+
547
646
  private
548
647
  def get_ticks
549
- @@ticks ||= Sysconf.sc_clk_tck
648
+ @@sc_clk_tck ||= LinuxStat::Sysconf.sc_clk_tck
649
+ @@ticks ||= @@sc_clk_tck < 1 ? 100 : @@sc_clk_tck
550
650
  end
551
651
 
552
652
  # Just to avoid multiple calculations!...
553
- def ticks_to_ms
554
- @@ms ||= 1.0 / get_ticks
653
+ # ticks to ms * 5
654
+ # If ticks is 100, it will return 0.05
655
+ def ticks_to_ms_t5
656
+ @@ms_t5 ||= 1.0 / get_ticks * 5
555
657
  end
556
658
 
557
659
  def pagesize
558
- @@pagesize ||= LinuxStat::Sysconf.pagesize
660
+ @@pagesize ||= LinuxStat::Sysconf.pagesize.to_i
661
+ end
662
+
663
+ def cpu_count
664
+ @@nprocessors_conf ||= LinuxStat::CPU.count
559
665
  end
560
666
  end
561
667
  end
@@ -117,7 +117,7 @@ module LinuxStat
117
117
  val = IO.readlines('/proc/swaps').drop(1)
118
118
  return [[], []] if val.empty?
119
119
 
120
- val.map { |x|
120
+ val.map! { |x|
121
121
  x.strip.split.values_at(2, 3).map!(&:to_i)
122
122
  }.transpose
123
123
  end
@@ -1,3 +1,3 @@
1
1
  module LinuxStat
2
- VERSION ||= "0.7.4"
2
+ VERSION ||= "0.9.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: linux_stat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.4
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sourav Goswami
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-12-17 00:00:00.000000000 Z
11
+ date: 2020-12-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Linux only, efficient linux system utilization reporting and system monitoring
14
14
  gem
@@ -20,6 +20,7 @@ extensions:
20
20
  - ext/fs_stat/extconf.rb
21
21
  - ext/sysconf/extconf.rb
22
22
  - ext/utsname/extconf.rb
23
+ - ext/nproc/extconf.rb
23
24
  extra_rdoc_files:
24
25
  - README.md
25
26
  files:
@@ -30,6 +31,8 @@ files:
30
31
  - exe/linuxstat.rb
31
32
  - ext/fs_stat/extconf.rb
32
33
  - ext/fs_stat/fs_stat.c
34
+ - ext/nproc/extconf.rb
35
+ - ext/nproc/nproc.c
33
36
  - ext/sysconf/extconf.rb
34
37
  - ext/sysconf/sysconf.c
35
38
  - ext/utsname/extconf.rb