Class: CPU::Usage

Inherits:
Struct::Usage
  • Object
show all
Includes:
Shared
Defined in:
lib/cpu/usage.rb

Instance Attribute Summary collapse

Attributes included from Shared

#num_cores, #num_processors

Instance Method Summary collapse

Instance Attribute Details

#idleObject

Returns the value of attribute idle

Returns:

  • (Object)

    the current value of idle



2
3
4
# File 'lib/cpu/usage.rb', line 2

def idle
  @idle
end

#niceObject

Returns the value of attribute nice

Returns:

  • (Object)

    the current value of nice



2
3
4
# File 'lib/cpu/usage.rb', line 2

def nice
  @nice
end

#processor_idObject

Returns the value of attribute processor_id

Returns:

  • (Object)

    the current value of processor_id



2
3
4
# File 'lib/cpu/usage.rb', line 2

def processor_id
  @processor_id
end

#start_atObject

Returns the value of attribute start_at

Returns:

  • (Object)

    the current value of start_at



2
3
4
# File 'lib/cpu/usage.rb', line 2

def start_at
  @start_at
end

#stop_atObject

Returns the value of attribute stop_at

Returns:

  • (Object)

    the current value of stop_at



2
3
4
# File 'lib/cpu/usage.rb', line 2

def stop_at
  @stop_at
end

#systemObject

Returns the value of attribute system

Returns:

  • (Object)

    the current value of system



2
3
4
# File 'lib/cpu/usage.rb', line 2

def system
  @system
end

#usageObject

Returns the value of attribute usage

Returns:

  • (Object)

    the current value of usage



2
3
4
# File 'lib/cpu/usage.rb', line 2

def usage
  @usage
end

#userObject

Returns the value of attribute user

Returns:

  • (Object)

    the current value of user



2
3
4
# File 'lib/cpu/usage.rb', line 2

def user
  @user
end

Instance Method Details

#*(scalar) ⇒ Object

Multiply the cpu times in this CPU::Usage instance with scalar.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cpu/usage.rb', line 28

def *(scalar)
  scalar = scalar.to_f
  self.class.new(*(
    [
      usage,
      processor_id,
    ] +
    values_at(2..-3).map { |x| x * scalar } +
    [
      start_at,
      stop_at,
    ])
  )
end

#+(other) ⇒ Object

Add this CPU::Usage instance to the other and return a resulting sum object.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/cpu/usage.rb', line 7

def +(other)
  self.class.new(*(
    [
      usage,
      processor_id == other.processor_id ? processor_id : 0,
    ] +
    values_at(2..-3).zip(other.values_at(2..-3)).map { |x, y| x + y } +
    [
      [ start_at, other.start_at ].min,
      [ stop_at, other.stop_at ].max
    ])
  )
end

#-(other) ⇒ Object

Subtract the other from this CPU::Usage instance the other and return a resulting difference object.



23
24
25
# File 'lib/cpu/usage.rb', line 23

def -(other)
  self + other * -1
end

#/(scalar) ⇒ Object

Divide the cpu times in this CPU::Usage instance by scalar.



44
45
46
# File 'lib/cpu/usage.rb', line 44

def /(scalar)
  self * (1.0 / scalar)
end

#inspectObject Also known as: to_s



71
72
73
# File 'lib/cpu/usage.rb', line 71

def inspect
  "#<#{self.class}: #{percentage}>"
end

#percentage(time = total_time) ⇒ Object

Return the CPU usage as a percentage number between 0.0..100.0.



67
68
69
# File 'lib/cpu/usage.rb', line 67

def percentage(time = total_time)
  100.0 * process_time / time
end

#process_timeObject

Return the cpu time that where used to process instructions since booting up the system.



50
51
52
# File 'lib/cpu/usage.rb', line 50

def process_time
  user + nice + system
end

#real_timeObject

Return the real time passed in the range of all CPU::Usage instances, that were used to create this summed up CPU::Usage instance. If this isn’t a sum object, this value will be 0.0.



57
58
59
# File 'lib/cpu/usage.rb', line 57

def real_time
  stop_at - start_at
end

#total_timeObject

Return the total cpu time that has passed since booting the system.



62
63
64
# File 'lib/cpu/usage.rb', line 62

def total_time
  values_at(2..-3).inject(0.0) { |s,  x| s + x }
end