Class: OsCtld::CGroup::Params

Inherits:
Object
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log, Lockable
Defined in:
lib/osctld/cgroup/params.rb

Direct Known Subclasses

ContainerParams

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Lockable

#exclusively, included, #inclusively, #init_lock, #lock, #unlock

Constructor Details

#initialize(owner, params: []) ⇒ Params

Returns a new instance of Params

Parameters:



16
17
18
19
20
# File 'lib/osctld/cgroup/params.rb', line 16

def initialize(owner, params: [])
  init_lock
  @owner = owner
  @params = params
end

Instance Attribute Details

#ownerObject (readonly, protected)

Returns the value of attribute owner



182
183
184
# File 'lib/osctld/cgroup/params.rb', line 182

def owner
  @owner
end

#paramsObject (readonly, protected)

Returns the value of attribute params



182
183
184
# File 'lib/osctld/cgroup/params.rb', line 182

def params
  @params
end

Class Method Details

.load(owner, cfg) ⇒ Object

Load CGroup parameters from config



10
11
12
# File 'lib/osctld/cgroup/params.rb', line 10

def self.load(owner, cfg)
  new(owner, params: (cfg || []).map { |v| CGroup::Param.load(v) })
end

Instance Method Details

#apply(keep_going: false) {|subsystem| ... } ⇒ Object

Apply configured cgroup parameters into the system

Parameters:

  • keep_going (Boolean)

    skip parameters that do not exist

Yield Parameters:

  • subsystem (String)

    cgroup subsystem

Yield Returns:

  • (String)

    absolute path to the cgroup directory



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/osctld/cgroup/params.rb', line 108

def apply(keep_going: false)
  params.each do |p|
    path = File.join(yield(p.subsystem), p.name)

    begin
      CGroup.set_param(path, p.value)

    rescue CGroupFileNotFound
      raise unless keep_going

      log(
        :info,
        :cgroup,
        "Skip #{path}, group or parameter does not exist"
      )
      next
    end
  end
end

#detect(&block) ⇒ Object



100
101
102
# File 'lib/osctld/cgroup/params.rb', line 100

def detect(&block)
  params.detect(&block)
end

#dumpObject

Dump params to config



169
170
171
# File 'lib/osctld/cgroup/params.rb', line 169

def dump
  params.select(&:persistent).map(&:dump)
end

#dup(new_owner) ⇒ Object



173
174
175
176
177
178
179
# File 'lib/osctld/cgroup/params.rb', line 173

def dup(new_owner)
  ret = super()
  ret.init_lock
  ret.instance_variable_set('@owner', new_owner)
  ret.instance_variable_set('@params', params.map(&:clone))
  ret
end

#each(&block) ⇒ Object



96
97
98
# File 'lib/osctld/cgroup/params.rb', line 96

def each(&block)
  params.each(&block)
end

#import(new_params) ⇒ Object

Process params from the client and return internal representation. Invalid parameters raise an exception.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/osctld/cgroup/params.rb', line 24

def import(new_params)
  new_params.map do |hash|
    p = CGroup::Param.import(hash)

    # Check if the subsystem is valid
    subsys = CGroup.real_subsystem(p.subsystem)
    path = File.join(CGroup::FS, subsys)

    unless Dir.exist?(path)
      raise CGroupSubsystemNotFound,
        "CGroup subsystem '#{p.subsystem}' not found at '#{path}'"
    end

    # Check parameter
    param = File.join(path, 'osctl', p.name)

    unless File.exist?(param)
      raise CGroupParameterNotFound, "CGroup parameter '#{param}' not found"
    end

    p
  end
end

#replace(new_params, save: true, &block) ⇒ Object

Replace all parameters by a new list of parameters

Parameters:

  • new_params (Array<CGroup::Param>)
  • save (Boolean)

    update the owner's config file



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/osctld/cgroup/params.rb', line 131

def replace(new_params, save: true, &block)
  @params.each do |p|
    found = new_params.detect do |n|
      n.subsystem == p.subsystem && n.name == p.name
    end

    reset(p, true, &block) unless found
  end

  @params = new_params
  owner.save_config if save
end

#reset(param, keep_going) {|subsystem| ... } ⇒ Object

Reset cgroup parameter to its initial/unlimited value.

Only a limited subset of cgroup parameters is supported.

Parameters:

Yield Parameters:

  • subsystem (String)

    cgroup subsystem

Yield Returns:

  • (String)

    absolute path to the cgroup directory



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/osctld/cgroup/params.rb', line 152

def reset(param, keep_going)
  v = reset_value(param)
  return unless v

  path = File.join(yield(param.subsystem), param.name)
  CGroup.set_param(path, v)

rescue CGroupFileNotFound
  raise unless keep_going
  log(
    :info,
    :cgroup,
    "Skip #{path}, group or parameter does not exist"
  )
end

#reset_value(param) ⇒ Object (protected)



184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/osctld/cgroup/params.rb', line 184

def reset_value(param)
  case param.name
  when 'cpu.cfs_quota_us'
    [-1]

  when 'memory.limit_in_bytes', 'memory.memsw.limit_in_bytes'
    [-1]

  else
    nil
  end
end

#set(new_params, append: false, save: true) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/osctld/cgroup/params.rb', line 48

def set(new_params, append: false, save: true)
  exclusively do
    new_params.each do |new_p|
      replaced = false

      params.map! do |p|
        if p.subsystem == new_p.subsystem && p.name == new_p.name
          replaced = true

          new_p.value = p.value + new_p.value if append
          new_p

        else
          p
        end
      end

      next if replaced

      params << new_p
    end
  end

  owner.save_config if save
end

#unset(del_params, save: true, reset: true, keep_going: false) {|subsystem| ... } ⇒ Object

Parameters:

  • save (Boolean)

    save config file

  • reset (Boolean)

    reset cgroup parameter value

  • keep_going (Boolean)

    skip parameters that do not exist

Yield Parameters:

  • subsystem (String)

    cgroup subsystem

Yield Returns:

  • (String)

    absolute path to the cgroup directory



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/osctld/cgroup/params.rb', line 79

def unset(del_params, save: true, reset: true, keep_going: false, &block)
  exclusively do
    del_params.each do |del_h|
      del_p = CGroup::Param.import(del_h)

      params.delete_if do |p|
        del = p.subsystem == del_p.subsystem && p.name == del_p.name
        next(del) if !del || !reset
        reset(p, keep_going, &block)
        true
      end
    end
  end

  owner.save_config if save
end