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_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)



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/osctld/cgroup/container_params.rb', line 99

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)



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/osctld/cgroup/container_params.rb', line 120

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

#reset(param, keep_going) ⇒ Object



19
20
21
22
23
24
# File 'lib/osctld/cgroup/container_params.rb', line 19

def reset(param, keep_going, &)
  super
  return unless owner.running?

  reset_container_param(param, keep_going, &)
end

#reset_container_param(param, keep_going) ⇒ Object (protected)



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/osctld/cgroup/container_params.rb', line 132

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

  path = File.join(
    yield(param.subsystem),
    'user-owned',
    "lxc.payload.#{owner.id}",
    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

#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



27
28
29
30
31
32
33
34
35
# File 'lib/osctld/cgroup/container_params.rb', line 27

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)



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
65
66
67
68
69
70
71
# File 'lib/osctld/cgroup/container_params.rb', line 39

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)



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/osctld/cgroup/container_params.rb', line 73

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