2013-06-21 16:11:53 +05:30
|
|
|
#!/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
|
2013-06-21 18:03:14 +05:30
|
|
|
# maximum expected thin provisioned devices and snapshots.
|
2013-06-21 16:11:53 +05:30
|
|
|
#
|
|
|
|
|
|
|
|
require 'optparse'
|
|
|
|
require 'pathname'
|
|
|
|
|
|
|
|
#----------------------------------------------------------------
|
|
|
|
|
|
|
|
$prg = Pathname.new($0).basename
|
|
|
|
|
|
|
|
def init_units
|
2013-06-24 16:00:43 +05:30
|
|
|
units = {}
|
2013-06-24 16:48:33 +05:30
|
|
|
units[:bytes_per_sector] = 512
|
2013-06-24 16:00:43 +05:30
|
|
|
units[:chars] = "bsKkMmGgTtPpEeZzYy"
|
|
|
|
units[:strings] = [ 'bytes', 'sectors',
|
2013-06-21 16:11:53 +05:30
|
|
|
'kilobytes', 'kibibytes', 'megabytes', 'mebibytes',
|
|
|
|
'gigabytes', 'gibibytes', 'terabytes', 'tebibytes',
|
|
|
|
'petabytes', 'pebibytes', 'exabytes', 'ebibytes',
|
|
|
|
'zetabytes', 'zebibytes', 'yottabytes', 'yobibytes' ]
|
2013-06-24 16:48:33 +05:30
|
|
|
units[:factors] = [ 1, units[:bytes_per_sector] ]
|
2013-06-26 16:31:51 +05:30
|
|
|
1.step(8) { |e| units[:factors] += [ 1000**e, 1024**e ] }
|
2013-06-21 16:11:53 +05:30
|
|
|
units
|
|
|
|
end
|
|
|
|
|
2013-06-24 16:48:33 +05:30
|
|
|
def get_index(unit_char, units)
|
|
|
|
unit_char ? units[:chars].index(unit_char) : 1
|
|
|
|
end
|
|
|
|
|
2013-06-21 16:11:53 +05:30
|
|
|
def check_opts(opts)
|
|
|
|
abort "#{$prg} - 3 arguments required!" if opts.length < 3
|
2013-06-21 18:03:14 +05:30
|
|
|
abort "#{$prg} - poolsize must be much larger than blocksize" if opts[:poolsize] < opts[:blocksize]
|
2013-06-21 16:11:53 +05:30
|
|
|
abort "#{$prg} - maximum number of thin provisioned devices must be > 0" if opts[:maxthins].nil? || opts[:maxthins] == 0
|
|
|
|
end
|
|
|
|
|
2013-06-24 17:22:46 +05:30
|
|
|
def to_bytes(size, units)
|
2013-06-21 18:28:53 +05:30
|
|
|
a = size.split(/[#{units[:chars]}]/)
|
2013-06-21 16:11:53 +05:30
|
|
|
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]
|
2013-06-24 17:22:46 +05:30
|
|
|
size.to_i * units[:factors][get_index(size[a[0].length], units)]
|
2013-06-21 16:11:53 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def parse_command_line(argv, units)
|
|
|
|
opts = {}
|
|
|
|
|
|
|
|
os = OptionParser.new do |o|
|
2013-06-24 17:22:46 +05:30
|
|
|
o.banner = "Thin Provisioning Metadata Device Size Calculator.\nUsage: #{$prg} [opts]"
|
2013-06-21 18:03:14 +05:30
|
|
|
o.on("-b", "--block-size BLOCKSIZE[#{units[:chars]}]", String,
|
|
|
|
"Block size of thin provisioned devices.") do |bs|
|
2013-06-24 17:22:46 +05:30
|
|
|
opts[:blocksize] = to_bytes(bs, units)
|
2013-06-21 16:11:53 +05:30
|
|
|
end
|
2013-06-24 16:48:33 +05:30
|
|
|
o.on("-s", "--pool-size SIZE[#{units[:chars]}]", String, "Size of pool device.") do |ps|
|
2013-06-24 17:22:46 +05:30
|
|
|
opts[:poolsize] = to_bytes(ps, units)
|
2013-06-21 16:11:53 +05:30
|
|
|
end
|
2013-06-21 18:03:14 +05:30
|
|
|
o.on("-m", "--max-thins #MAXTHINS", Integer, "Maximum sum of all thin devices and snapshots.") do |mt|
|
2013-06-21 16:11:53 +05:30
|
|
|
opts[:maxthins] = mt
|
|
|
|
end
|
2013-06-21 18:03:14 +05:30
|
|
|
o.on("-u", "--unit [#{units[:chars]}]", String, "Output unit specifier.") do |u|
|
2013-06-21 18:28:53 +05:30
|
|
|
abort "#{$prg} - output unit specifier invalid." if u.length > 1 || !(u =~ /[#{units[:chars]}]/)
|
2013-06-21 18:03:14 +05:30
|
|
|
opts[:units] = u
|
2013-06-21 16:11:53 +05:30
|
|
|
end
|
2013-06-21 18:03:14 +05:30
|
|
|
o.on("-n", "--numeric-only", "Output numeric value only.") do
|
|
|
|
opts[:numeric] = true
|
2013-06-21 16:11:53 +05:30
|
|
|
end
|
2013-06-21 19:16:39 +05:30
|
|
|
o.on("-h", "--help", "Output this help.") do
|
2013-06-21 16:11:53 +05:30
|
|
|
puts o
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
2013-06-21 18:03:14 +05:30
|
|
|
os.parse!(argv)
|
2013-06-21 16:11:53 +05:30
|
|
|
rescue OptionParser::ParseError => e
|
|
|
|
abort "#{$prg} #{e}\n#{$prg} #{os}"
|
|
|
|
end
|
|
|
|
|
|
|
|
check_opts(opts)
|
|
|
|
opts
|
|
|
|
end
|
|
|
|
|
2013-06-21 18:03:14 +05:30
|
|
|
def mappings_per_block
|
|
|
|
btree_nodesize, btree_node_headersize, btree_entrysize = 4096, 64, 16
|
|
|
|
(btree_nodesize - btree_node_headersize) / btree_entrysize
|
2013-06-21 16:11:53 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def estimated_result(opts, units)
|
2013-06-24 16:48:33 +05:30
|
|
|
idx = get_index(opts[:units], units)
|
2013-06-21 18:03:14 +05:30
|
|
|
# double-fold # of nodes, because they aren't fully populated in average
|
2013-06-26 16:15:19 +05:30
|
|
|
r = (1.0 + (2 * opts[:poolsize] / opts[:blocksize] / mappings_per_block + opts[:maxthins])) * 8 * units[:bytes_per_sector] # in bytes!
|
2013-06-24 17:22:46 +05:30
|
|
|
r /= units[:factors][idx] # in requested unit
|
2013-06-21 16:11:53 +05:30
|
|
|
tmp = "%.2f" % r
|
2013-06-21 18:03:14 +05:30
|
|
|
if tmp.to_f > 0.0
|
|
|
|
r = tmp.to_i.to_f == tmp.to_f ? tmp.to_i : tmp
|
|
|
|
else
|
|
|
|
r = "%.2e" % r
|
|
|
|
end
|
2013-06-21 18:28:53 +05:30
|
|
|
r = "#{$prg} - estimated metadata area size is #{r} #{units[:strings][idx]}." if !opts[:numeric]
|
2013-06-24 16:51:14 +05:30
|
|
|
r
|
2013-06-21 16:11:53 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------
|
|
|
|
# Main
|
2013-06-24 16:48:33 +05:30
|
|
|
#
|
|
|
|
puts estimated_result(parse_command_line(ARGV, (units = init_units)), units)
|