Class: OsCtld::CGroup::ContainerParams

Inherits:
Params
  • Object
show all
Defined in:
lib/osctld/cgroup/container_params.rb

Instance Attribute Summary

Attributes inherited from Params

#owner, #params

Instance Method Summary collapse

Methods inherited from Params

#apply_params, #apply_params_and_retry, #detect, #dump, #dup, #each, #each_usable, #each_version, #find_cpu_limit, #find_memory_limit, #find_swap_limit, #import, #initialize, load, #replace, #reset, #reset_value, #unset, #usable_params

Methods included from Lockable

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

Constructor Details

This class inherits a constructor from OsCtld::CGroup::Params

Instance Method Details

#apply(keep_going: false, &block) ⇒ Object



12
13
14
15
16
17
# File 'lib/osctld/cgroup/container_params.rb', line 12

def apply(keep_going: false, &block)
  super
  return unless owner.running?

  apply_container_params_and_retry(usable_params, keep_going: keep_going, &block)
end

#apply_container_params(param_list, keep_going: false) ⇒ Object (protected)



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/osctld/cgroup/container_params.rb', line 57

def apply_container_params(param_list, keep_going: false)
  failed = []

  param_list.each do |p|
    path = File.join(
      yield(p.subsystem),
      'user-owned',
      "lxc.payload.#{owner.id}",
      p.name,
    )

    begin
      failed << p unless CGroup.set_param(path, p.value)

    rescue CGroupFileNotFound
      next
    end
  end

  failed
end

#apply_container_params_and_retry(param_list, keep_going: false, &block) ⇒ Object (protected)



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/osctld/cgroup/container_params.rb', line 79

def apply_container_params_and_retry(param_list, keep_going: false, &block)
  failed = apply_container_params(
    param_list,
    keep_going: keep_going,
    &block
  ).select { |p| p.name.start_with?('memory.') }

  if failed.any?
    apply_container_params(failed, keep_going: keep_going, &block)
  end
end

#set(*args, **kwargs) ⇒ Object



5
6
7
8
9
10
# File 'lib/osctld/cgroup/container_params.rb', line 5

def set(*args, **kwargs)
  owner.exclusively do
    super
    owner.lxc_config.configure_cgparams
  end
end

#temporarily_expand_memory(percent: 30) ⇒ Object

Temporarily expand container memory by given percentage



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/osctld/cgroup/container_params.rb', line 20

def temporarily_expand_memory(percent: 30)
  return if !CGroup.v1? || !owner.running?

  # Determine new memory limits
  mem_limit = each_usable.detect { |p| p.name == 'memory.limit_in_bytes' }
  memsw_limit = each_usable.detect { |p| p.name == 'memory.memsw.limit_in_bytes' }

  tmp_params = [mem_limit, memsw_limit].map do |p|
    next if p.nil?

    cur_limit = p.value.last.to_i
    new_limit = (cur_limit + (cur_limit / 100.0 * percent)).round

    CGroup::Param.new(1, 'memory', p.name, [new_limit], false)
  end

  tmp_params.compact!

  return if tmp_params.empty?

  # Apply new memory limits
  return unless owner.running?

  # First apply them on ct.<id>
  apply_params_and_retry(tmp_params, keep_going: true) do |subsystem|
    owner.abs_apply_cgroup_path(subsystem)
  end

  # Then apply them on lxc.payload
  apply_container_params_and_retry(tmp_params, keep_going: true) do |subsystem|
    owner.abs_apply_cgroup_path(subsystem)
  end

  nil
end