Class: PSI::Trigger

Inherits:
Object
  • Object
show all
Defined in:
lib/psi/trigger.rb

Overview

A kernel PSI trigger tied to an open pressure file descriptor.

Constant Summary collapse

KINDS =

Triggerable pressure categories.

%i[some full].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, kind: :some, stall:, window:, cgroup: nil) ⇒ Trigger

Returns a new instance of Trigger.



26
27
28
29
30
31
32
33
34
# File 'lib/psi/trigger.rb', line 26

def initialize(resource, kind: :some, stall:, window:, cgroup: nil)
  @resource = resource.respond_to?(:to_sym) ? resource.to_sym : resource
  @kind = kind.respond_to?(:to_sym) ? kind.to_sym : kind
  @stall = Float(stall)
  @window = Float(window)
  @cgroup = cgroup
  validate!
  register
end

Instance Attribute Details

#cgroupObject (readonly)

Returns the value of attribute cgroup.



11
12
13
# File 'lib/psi/trigger.rb', line 11

def cgroup
  @cgroup
end

#kindObject (readonly)

Returns the value of attribute kind.



11
12
13
# File 'lib/psi/trigger.rb', line 11

def kind
  @kind
end

#resourceObject (readonly)

Returns the value of attribute resource.



11
12
13
# File 'lib/psi/trigger.rb', line 11

def resource
  @resource
end

#stallObject (readonly)

Returns the value of attribute stall.



11
12
13
# File 'lib/psi/trigger.rb', line 11

def stall
  @stall
end

#windowObject (readonly)

Returns the value of attribute window.



11
12
13
# File 'lib/psi/trigger.rb', line 11

def window
  @window
end

Class Method Details

.openTrigger, Object

Opens a trigger and closes it after the optional block.

Returns:

  • (Trigger, Object)

    the trigger, or the block result



15
16
17
18
19
20
21
22
23
24
# File 'lib/psi/trigger.rb', line 15

def self.open(...)
  trigger = new(...)
  return trigger unless block_given?

  begin
    yield trigger
  ensure
    trigger.close
  end
end

Instance Method Details

#closenil

Closes the descriptor and unregisters the kernel trigger.

Returns:

  • (nil)


44
45
46
47
# File 'lib/psi/trigger.rb', line 44

def close
  @io&.close unless closed?
  nil
end

#closed?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/psi/trigger.rb', line 49

def closed?
  !@io || @io.closed?
end

#to_ioObject

Raises:



53
54
55
56
57
# File 'lib/psi/trigger.rb', line 53

def to_io
  raise Error, "trigger is closed" if closed?

  @io
end

#wait(timeout: nil) ⇒ Object

Raises:



36
37
38
39
40
# File 'lib/psi/trigger.rb', line 36

def wait(timeout: nil)
  raise Error, "trigger is closed" if closed?

  !!@io.wait(IO::PRIORITY, timeout)
end