Skip to content

Commit

Permalink
Merge pull request #68 from haconiwa/fix-double-dot-cgroup
Browse files Browse the repository at this point in the history
Fix cannot set cgroup parameters including 2 dots, such as `"memory.memsw.limit_in_bytes"`
  • Loading branch information
udzura committed Jan 16, 2017
2 parents a35925b + 3e3d4fe commit f64c6d2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 53 deletions.
6 changes: 3 additions & 3 deletions mrblib/haconiwa/base.rb
Expand Up @@ -262,10 +262,10 @@ def [](key)

def []=(key, value)
@groups[key] = value
c, attr = key.split('.')
raise("Invalid cgroup name #{key}") unless attr
c, *attr = key.split('.')
raise("Invalid cgroup name #{key}") if attr.empty?
@groups_by_controller[c] ||= Array.new
@groups_by_controller[c] << [key, attr]
@groups_by_controller[c] << [key, attr.join('_')]
return value
end

Expand Down
58 changes: 58 additions & 0 deletions sample/cgroup.haco
@@ -0,0 +1,58 @@
# -*- mode: ruby -*-
ENV['CGROUP'] ||= 'cpu'

haconiwa = Haconiwa.define do |config|
config.name = "#{ENV['CGROUP']}-quota-test" # to be hostname
config.init_command = ["/usr/sbin/sshd", '-D'] # to be first process
config.daemonize!

root = Pathname.new("/var/lib/haconiwa/2a1042d9")
config.mount_independent "procfs"
config.mount_independent "sysfs"
config.mount_independent "devtmpfs"
config.mount_independent "devpts"
config.chroot_to root

config.namespace.unshare "mount"
config.namespace.unshare "ipc"
config.namespace.unshare "uts"
config.namespace.unshare "pid"

if ENV['CGROUP'] == 'cpu'
config.cgroup["cpu.cfs_period_us"] = 100000
config.cgroup["cpu.cfs_quota_us"] = 30000

config.add_handler :USR1 do |b, sig|
File.open("/tmp/log_#{b.name}.txt", "a+") {|f| f.puts "Hello signal: #{Time.now.to_s}" }
end

config.add_handler :USR2 do |b, sig|
cpu = ::Cgroup::CPU.new(b.name)
File.open("/tmp/log_#{b.name}.txt", "a+") {|f| f.puts "Current cpu.cfs_quota_us = #{cpu.cfs_quota_us}" }
end

config.add_handler :TTIN do |b, sig|
cpu = ::Cgroup::CPU.new(b.name)
cpu.cfs_quota_us = cpu.cfs_quota_us - 10000
if cpu.cfs_quota_us > 0
cpu.modify
File.open("/tmp/log_#{b.name}.txt", "a+") {|f| f.puts "Changed cpu.cfs_quota_us = #{cpu.cfs_quota_us}" }
end
end

config.add_handler :TTOU do |b, sig|
cpu = ::Cgroup::CPU.new(b.name)
cpu.cfs_quota_us = cpu.cfs_quota_us + 10000
if cpu.cfs_quota_us <= cpu.cfs_period_us
cpu.modify
File.open("/tmp/log_#{b.name}.txt", "a+") {|f| f.puts "Changed cpu.cfs_quota_us = #{cpu.cfs_quota_us}" }
end
end
elsif ENV['CGROUP'] = 'memory'
config.cgroup["memory.limit_in_bytes"] = 128 * 1024 * 1024 # 128MB
config.cgroup["memory.memsw.limit_in_bytes"] = 128 * 1024 * 1024 # 128MB
end

# sshd needs chroot!
config.capabilities.allow "cap_sys_chroot"
end
50 changes: 0 additions & 50 deletions sample/cpu.haco

This file was deleted.

0 comments on commit f64c6d2

Please sign in to comment.