thin-provisioning-tools/thin-provisioning/thin_metadata_size

113 lines
3.6 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env ruby
#
# Copyright (C) 2013 Red Hat, GmbH
#
# This file is released under the GPL
#
#
# Script to calculate device-mapper thin privisioning
# metadata device size based on pool, block size and
# maximum expected thin provisioned devices and snapshots.
#
require 'optparse'
require 'pathname'
#----------------------------------------------------------------
$prg = Pathname.new($0).basename
def init_units
2013-06-24 16:00:43 +05:30
units = {}
units[:chars] = "bsKkMmGgTtPpEeZzYy"
units[:strings] = [ 'bytes', 'sectors',
'kilobytes', 'kibibytes', 'megabytes', 'mebibytes',
'gigabytes', 'gibibytes', 'terabytes', 'tebibytes',
'petabytes', 'pebibytes', 'exabytes', 'ebibytes',
'zetabytes', 'zebibytes', 'yottabytes', 'yobibytes' ]
2013-06-24 16:00:43 +05:30
units[:factors] = [ 1, 512 ]
1.step(8) { |e| units[:factors] += [ 1024**e, 1000**e ] }
units
end
def check_opts(opts)
abort "#{$prg} - 3 arguments required!" if opts.length < 3
abort "#{$prg} - poolsize must be much larger than blocksize" if opts[:poolsize] < opts[:blocksize]
abort "#{$prg} - maximum number of thin provisioned devices must be > 0" if opts[:maxthins].nil? || opts[:maxthins] == 0
end
def to_sectors(size, units)
a = size.split(/[#{units[:chars]}]/)
s = size.to_i.to_s
abort "#{$prg} - only one unit character allowed!" if a.length > 1 || size.length - s.length > 1
abort "#{$prg} - invalid unit specifier!" if s != a[0]
idx = units[:chars].index(size[a[0].length])
2013-06-24 16:00:43 +05:30
r = size.to_i * units[:factors][idx] / units[:factors][1]
r
end
def parse_command_line(argv, units)
opts = {}
os = OptionParser.new do |o|
o.banner = "Thin Provisioning Metadata Size Calculator.\nUsage: #{$prg} [opts]"
o.on("-b", "--block-size BLOCKSIZE[#{units[:chars]}]", String,
"Block size of thin provisioned devices.") do |bs|
opts[:blocksize] = to_sectors(bs, units)
end
o.on("-s", "--pool-size SIZE[#{units[:chars]}]", String, "Size of the pool device.") do |ps|
opts[:poolsize] = to_sectors(ps, units)
end
o.on("-m", "--max-thins #MAXTHINS", Integer, "Maximum sum of all thin devices and snapshots.") do |mt|
opts[:maxthins] = mt
end
o.on("-u", "--unit [#{units[:chars]}]", String, "Output unit specifier.") do |u|
abort "#{$prg} - output unit specifier invalid." if u.length > 1 || !(u =~ /[#{units[:chars]}]/)
opts[:units] = u
end
o.on("-n", "--numeric-only", "Output numeric value only.") do
opts[:numeric] = true
end
o.on("-h", "--help", "Output this help.") do
puts o
exit
end
end
begin
os.parse!(argv)
rescue OptionParser::ParseError => e
abort "#{$prg} #{e}\n#{$prg} #{os}"
end
check_opts(opts)
opts
end
def mappings_per_block
btree_nodesize, btree_node_headersize, btree_entrysize = 4096, 64, 16
(btree_nodesize - btree_node_headersize) / btree_entrysize
end
def estimated_result(opts, units)
2013-06-24 16:00:43 +05:30
idx = opts[:units] ? units[:chars].index(opts[:units]) : 1
# double-fold # of nodes, because they aren't fully populated in average
2013-06-24 16:00:43 +05:30
r = (1.0 + ((opts[:poolsize] / opts[:blocksize] / mappings_per_block) * 2 + opts[:maxthins])) * 8 * units[:factors][1] # in bytes!
r /= units[:factors][idx]
tmp = "%.2f" % r
if tmp.to_f > 0.0
r = tmp.to_i.to_f == tmp.to_f ? tmp.to_i : tmp
else
r = "%.2e" % r
end
r = "#{$prg} - estimated metadata area size is #{r} #{units[:strings][idx]}." if !opts[:numeric]
r
end
#----------------------------------------------------------------
# Main
units = init_units
puts estimated_result(parse_command_line(ARGV, units), units)