linux_stat 0.6.5 → 0.7.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 73b3180065620ed0fc96662eaa96fa9e7971176e7a5d19054ae1f10b3204987c
4
- data.tar.gz: 4fb253f925728426bfad5764b97cc59b71771365e984e78478ff996d7e63378c
3
+ metadata.gz: a54eca551d77c1284989f5f8edeea8e363d32228e0ea5a2d28b2df9eefeedf53
4
+ data.tar.gz: 407820d169fe900f639cd43730bd03eb66c43a2ec0bcd5c3d1399af741418c4a
5
5
  SHA512:
6
- metadata.gz: f4b2b9296a34fbb4415587a5f6752602a3eb35f2e7da6af38e9c76a9946e18a5a120b54901d61cd43d2e624612564cfddc50c330dee7b910a5eeb12ed0b34a73
7
- data.tar.gz: 2753d9fe91b324f93ce59ea4abd3d84178f515b6e31053b1b382866089dc242f680533f3fded18c9840cbdd1679d7a81920cb87b563454c8423aa2dc3fe35307
6
+ metadata.gz: ff513a54d80eb9e9c101dd2fb92ff874c3e644da5752e8f7b2c9bdf535d56a1d942e64ed3b083eb051dfcb62be02cb521b51f0b1e08baef7f3ec0a261bacd1f1
7
+ data.tar.gz: ef5b77eb019ab19976a5799a276f22a6967603c6d03b0219a6314e74e00a275a288102f0ab6d753e38867b96d6621e8673a9b0fa733d772c2d97118749ada933
data/README.md CHANGED
@@ -10,7 +10,7 @@ It only works for Linux, and detecting the OS is upto the user of this gem.
10
10
  ---
11
11
 
12
12
  ## Dependencies:
13
- + You need to have the C compile to be able to compile the C extension.
13
+ + You need to have the C compiler installed to be able to compile the C extensions.
14
14
  On Arch Linux:
15
15
  ```
16
16
  # pacman -S gcc make
@@ -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 ;
@@ -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)) ;
@@ -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 ;
@@ -135,8 +135,8 @@ module LinuxStat
135
135
  key = splitted[0].to_s.strip
136
136
  value = splitted[1..-1].join(?=).to_s.strip
137
137
 
138
- value[0] = '' if value[0] == ?"
139
- value[-1] = '' if value[-1] == ?"
138
+ value[0] = ''.freeze if value[0] == ?".freeze
139
+ value[-1] = ''.freeze if value[-1] == ?".freeze
140
140
 
141
141
  h.merge!( key.to_sym => value )
142
142
  }
@@ -345,7 +345,7 @@ module LinuxStat
345
345
  file = "/proc/#{pid}/stat".freeze
346
346
  return nil unless File.readable?(file)
347
347
 
348
- data = IO.read(file).split[19]
348
+ data = IO.foreach(file, ' '.freeze).first(20)[-1]
349
349
  data ? data.to_i : nil
350
350
  end
351
351
 
@@ -383,7 +383,7 @@ module LinuxStat
383
383
  file = "/proc/#{pid}/status".freeze
384
384
  return nil unless File.readable?(file)
385
385
 
386
- data = IO.readlines(file.freeze).find { |x|
386
+ data = IO.foreach(file.freeze).find { |x|
387
387
  x[/Uid.*\d*/]
388
388
  }.to_s.split.drop(1)
389
389
 
@@ -402,12 +402,12 @@ module LinuxStat
402
402
  #
403
403
  # :real, :effective, :saved_set, :filesystem_uid
404
404
  #
405
- # If the info isn't available it returns an empty Hash.
405
+ # If the info isn't available or the argument passed doesn't exist as a process ID, it will return an empty Hash.
406
406
  def gid(pid = $$)
407
407
  file = "/proc/#{pid}/status".freeze
408
408
  return nil unless File.readable?(file)
409
409
 
410
- data = IO.readlines(file.freeze).find { |x|
410
+ data = IO.foreach(file.freeze).find { |x|
411
411
  x[/Gid.*\d*/]
412
412
  }.split.drop(1)
413
413
 
@@ -428,13 +428,100 @@ module LinuxStat
428
428
  file = "/proc/#{pid}/status".freeze
429
429
  return ''.freeze unless File.readable?(file)
430
430
 
431
- gid = IO.readlines(file.freeze).find { |x|
431
+ gid = IO.foreach(file.freeze).find { |x|
432
432
  x[/Gid.*\d*/]
433
433
  }.split.drop(1)[2].to_i
434
434
 
435
435
  LinuxStat::User.username_by_gid(gid)
436
436
  end
437
437
 
438
+ ##
439
+ # = start_time(pid = $$)
440
+ #
441
+ # Returns the time (as Time object) the process was started.
442
+ #
443
+ # For example:
444
+ # LinuxStat::ProcessInfo.start_time 14183
445
+ #
446
+ # => 2020-12-16 13:31:43.559061275 +0000
447
+ #
448
+ # If the info isn't available or the argument passed doesn't exist as a process ID, it will return nil.
449
+ #
450
+ # The timezone returned based on current TZ.
451
+ # Thus the timezone could be affected by changing the ENV['TZ'] variable.
452
+ #
453
+ # Don't trust the timezone returned by the time.
454
+ def start_time(pid = $$)
455
+ stat_file = "/proc/#{pid}/stat".freeze
456
+ uptime = "/proc/uptime".freeze
457
+
458
+ @@u_readable ||= File.readable?(uptime)
459
+ return nil unless @@u_readable && File.readable?(stat_file)
460
+
461
+ Time.now.-(IO.foreach(uptime, ' '.freeze).next.to_i - (IO.read(stat_file).split[21].to_i / get_ticks))
462
+ end
463
+
464
+ ##
465
+ # = running_time(pid = $$)
466
+ #
467
+ # Returns the time (in seconds, as Float) the process is running for.
468
+ #
469
+ # For example:
470
+ # LinuxStat::ProcessInfo.running_time 14183
471
+ #
472
+ # => 1947.619999999999
473
+ #
474
+ # If the info isn't available or the argument passed doesn't exist as a process ID, it will return nil.
475
+ def running_time(pid = $$)
476
+ stat_file = "/proc/#{pid}/stat".freeze
477
+ uptime = "/proc/uptime".freeze
478
+
479
+ @@u_readable ||= File.readable?(uptime)
480
+ return nil unless @@u_readable && File.readable?(stat_file)
481
+
482
+ IO.foreach(uptime, ' '.freeze).next.to_f - (IO.read(stat_file).split[21].to_i / get_ticks)
483
+ end
484
+
485
+ ##
486
+ # = state(pid = $$)
487
+ # Returns the state of the process as a frozen String
488
+ #
489
+ # * A process could have multiple states:
490
+ #
491
+ # 1. S => Sleeping
492
+ #
493
+ # 2. R => Running
494
+ #
495
+ # 3. I => Idle
496
+ #
497
+ # 4. Z => Zombie
498
+ #
499
+ # It returns any one of them.
500
+ #
501
+ # If the info isn't available or the argument passed doesn't exist as a process ID,
502
+ # it will return an empty String.
503
+ def state(pid = $$)
504
+ file = "/proc/#{pid}/stat".freeze
505
+ return ''.freeze unless File.readable?(file)
506
+ IO.foreach(file, ' '.freeze).first(3)[-1].tap(&:rstrip!).freeze
507
+ end
508
+
509
+ ##
510
+ # = nice(pid = $$)
511
+ # Returns the nice of the process
512
+ #
513
+ # The output value is an Integer ranging from -20 to 19
514
+ #
515
+ # -20 means the process has high priority, and 19 means the process has low priority
516
+ #
517
+ # If the info isn't available or the argument passed doesn't exist as a process ID, it will return nil.
518
+ def nice(pid = $$)
519
+ file = "/proc/#{pid}/stat"
520
+ return nil unless File.readable?(file)
521
+
522
+ IO.foreach(file, ' ').first(19)[-1].to_i
523
+ end
524
+
438
525
  private
439
526
  def get_ticks
440
527
  @@ticks ||= Sysconf.sc_clk_tck
@@ -207,7 +207,7 @@ module LinuxStat
207
207
  # For example:
208
208
  # LinuxStat::User.gid_by_username('root')
209
209
  #
210
- # => "0"
210
+ # => 0
211
211
  #
212
212
  # The return type is Integer.
213
213
  #
@@ -1,3 +1,3 @@
1
1
  module LinuxStat
2
- VERSION = "0.6.5"
2
+ VERSION ||= "0.7.0"
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.6.5
4
+ version: 0.7.0
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-13 00:00:00.000000000 Z
11
+ date: 2020-12-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Linux only, efficient linux system utilization reporting and system monitoring
14
14
  gem