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) ⇒ Object



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

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

  apply_container_params_and_retry(usable_params, keep_going:, &)
end

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/osctld/cgroup/container_params.rb', line 92

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) ⇒ Object (protected)



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/osctld/cgroup/container_params.rb', line 113

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

  return unless failed.any?

  apply_container_params(failed, keep_going:, &)
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: 50) ⇒ Object

Temporarily expand container memory by given percentage



20
21
22
23
24
25
26
27
28
# File 'lib/osctld/cgroup/container_params.rb', line 20

def temporarily_expand_memory(percent: 50)
  return unless owner.running?

  if CGroup.v1?
    temporarily_expand_memory_v1(percent:)
  else
    temporarily_expand_memory_v2(percent:)
  end
end

#temporarily_expand_memory_v1(percent:) ⇒ Object (protected)



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/osctld/cgroup/container_params.rb', line 32

def temporarily_expand_memory_v1(percent:)
  # 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

#temporarily_expand_memory_v2(percent:) ⇒ Object (protected)



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/osctld/cgroup/container_params.rb', line 66

def temporarily_expand_memory_v2(percent:)
  # Determine new memory limit
  memory_max = each_usable.detect { |p| p.name == 'memory.max' }
  return if memory_max.nil?

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

  new_param = CGroup::Param.new(2, 'memory', 'memory.max', [new_limit], false)

  # Apply new memory limits
  return unless owner.running?

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

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

  nil
end