From 75ca8b945240718a8165b1620852ae42ca13b177 Mon Sep 17 00:00:00 2001 From: slava Date: Wed, 16 Mar 2016 20:23:02 +0300 Subject: [PATCH] Convert cgroups memory limits to bytes Cgroup tool accepts memory limits in bytes, but, user specifies it in megabytes in configuration file. So, it's necessary to convert memory limits in appropriate transform function. Change-Id: Id771da82e52d87c88a9185a4987dfe9331519c07 Closes-bug: #1558118 --- .../parser/functions/prepare_cgroups_hash.rb | 23 ++++++++++++++----- .../functions/prepare_cgroups_hash__spec.rb | 4 ++-- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/deployment/puppet/cgroups/lib/puppet/parser/functions/prepare_cgroups_hash.rb b/deployment/puppet/cgroups/lib/puppet/parser/functions/prepare_cgroups_hash.rb index 11f6539085..1bc98aa62f 100644 --- a/deployment/puppet/cgroups/lib/puppet/parser/functions/prepare_cgroups_hash.rb +++ b/deployment/puppet/cgroups/lib/puppet/parser/functions/prepare_cgroups_hash.rb @@ -5,14 +5,25 @@ module CgroupsSettings # value is valid if value has integer type or # matches with pattern: %percent, min_value, max_value def self.handle_value(group, value) - return value if value.is_a?(Numeric) - if group == 'memory' and value.match(/%(\d+), (\d+), (\d+)/) - percent, min, max = value.scan(/%(\d+), (\d+), (\d+)/).flatten.map { |i| i.to_i } - total_memory = Facter.value(:memorysize_mb) - res = (total_memory.to_f / 100.0) * percent.to_f - return [min, max, res].sort[1] + # transform value in megabytes to bytes for memory limits + return handle_memory(value) if group == 'memory' + # keep it as it is for others + return value if value.is_a?(Integer) + end + + def self.handle_memory(value) + return mb_to_bytes(value) if value.is_a?(Integer) + if value.is_a?(String) and value.match(/%(\d+), (\d+), (\d+)/) + percent, min, max = value.scan(/%(\d+), (\d+), (\d+)/).flatten.map { |i| i.to_i } + total_memory = Facter.value(:memorysize_mb) + res = (total_memory.to_f / 100.0) * percent.to_f + return mb_to_bytes([min, max, res].sort[1]) end end + + def self.mb_to_bytes(value) + return value * 1024 * 1024 + end end Puppet::Parser::Functions::newfunction(:prepare_cgroups_hash, :type => :rvalue, :arity => 1, :doc => <<-EOS diff --git a/deployment/puppet/cgroups/spec/functions/prepare_cgroups_hash__spec.rb b/deployment/puppet/cgroups/spec/functions/prepare_cgroups_hash__spec.rb index 5a2dfb23b7..914f5dc3c6 100644 --- a/deployment/puppet/cgroups/spec/functions/prepare_cgroups_hash__spec.rb +++ b/deployment/puppet/cgroups/spec/functions/prepare_cgroups_hash__spec.rb @@ -36,7 +36,7 @@ describe Puppet::Parser::Functions.function(:prepare_cgroups_hash) do 'blkio.test' => 800 }, 'memory' => { - 'memory.soft_limit_in_bytes' => 700 + 'memory.soft_limit_in_bytes' => 700 * 1024 * 1024 }, }, 'keystone' => { @@ -72,7 +72,7 @@ describe Puppet::Parser::Functions.function(:prepare_cgroups_hash) do { 'neutron' => { 'memory' => { - 'memory.soft_limit_in_bytes' => 512 + 'memory.soft_limit_in_bytes' => 512 * 1024 * 1024 } } }