Class: CPU::Usage
- Inherits:
-
Struct::Usage
- Object
- Struct::Usage
- CPU::Usage
- Includes:
- Shared
- Defined in:
- lib/cpu/usage.rb
Instance Attribute Summary collapse
-
#idle ⇒ Object
Returns the value of attribute idle.
-
#nice ⇒ Object
Returns the value of attribute nice.
-
#processor_id ⇒ Object
Returns the value of attribute processor_id.
-
#start_at ⇒ Object
Returns the value of attribute start_at.
-
#stop_at ⇒ Object
Returns the value of attribute stop_at.
-
#system ⇒ Object
Returns the value of attribute system.
-
#usage ⇒ Object
Returns the value of attribute usage.
-
#user ⇒ Object
Returns the value of attribute user.
Attributes included from Shared
Instance Method Summary collapse
-
#*(scalar) ⇒ Object
Multiply the cpu times in this CPU::Usage instance with
scalar
. -
#+(other) ⇒ Object
Add this CPU::Usage instance to the other and return a resulting sum object.
-
#-(other) ⇒ Object
Subtract the other from this CPU::Usage instance the other and return a resulting difference object.
-
#/(scalar) ⇒ Object
Divide the cpu times in this CPU::Usage instance by
scalar
. - #inspect ⇒ Object (also: #to_s)
-
#percentage(time = total_time) ⇒ Object
Return the CPU usage as a percentage number between 0.0..100.0.
-
#process_time ⇒ Object
Return the cpu time that where used to process instructions since booting up the system.
-
#real_time ⇒ Object
Return the real time passed in the range of all CPU::Usage instances, that were used to create this summed up CPU::Usage instance.
-
#total_time ⇒ Object
Return the total cpu time that has passed since booting the system.
Instance Attribute Details
#idle ⇒ Object
Returns the value of attribute idle
2 3 4 |
# File 'lib/cpu/usage.rb', line 2 def idle @idle end |
#nice ⇒ Object
Returns the value of attribute nice
2 3 4 |
# File 'lib/cpu/usage.rb', line 2 def nice @nice end |
#processor_id ⇒ Object
Returns the value of attribute processor_id
2 3 4 |
# File 'lib/cpu/usage.rb', line 2 def processor_id @processor_id end |
#start_at ⇒ Object
Returns the value of attribute start_at
2 3 4 |
# File 'lib/cpu/usage.rb', line 2 def start_at @start_at end |
#stop_at ⇒ Object
Returns the value of attribute stop_at
2 3 4 |
# File 'lib/cpu/usage.rb', line 2 def stop_at @stop_at end |
#system ⇒ Object
Returns the value of attribute system
2 3 4 |
# File 'lib/cpu/usage.rb', line 2 def system @system end |
#usage ⇒ Object
Returns the value of attribute usage
2 3 4 |
# File 'lib/cpu/usage.rb', line 2 def usage @usage end |
#user ⇒ Object
Returns the value of attribute 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 |
#inspect ⇒ Object 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_time ⇒ Object
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_time ⇒ Object
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_time ⇒ Object
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 |