2007-03-29 21:01:28 +00:00
|
|
|
#! /usr/bin/env python
|
2006-10-22 03:10:04 +00:00
|
|
|
|
2019-03-22 10:40:58 +00:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2007-09-15 22:07:21 +00:00
|
|
|
import random, math, re
|
2006-10-22 03:10:04 +00:00
|
|
|
from twisted.python import usage
|
|
|
|
|
|
|
|
class Args(usage.Options):
|
|
|
|
optParameters = [
|
|
|
|
["mode", "m", "alpha", "validation scheme"],
|
|
|
|
["arity", "k", 2, "k (airty) for hash tree"],
|
|
|
|
]
|
|
|
|
def opt_arity(self, option):
|
|
|
|
self['arity'] = int(option)
|
|
|
|
def parseArgs(self, *args):
|
|
|
|
if len(args) > 0:
|
|
|
|
self['mode'] = args[0]
|
|
|
|
|
|
|
|
|
|
|
|
def charttest():
|
|
|
|
import gdchart
|
|
|
|
sizes = [random.randrange(10, 20) for i in range(10)]
|
|
|
|
x = gdchart.Line()
|
|
|
|
x.width = 250
|
|
|
|
x.height = 250
|
|
|
|
x.xtitle = "sample"
|
|
|
|
x.ytitle = "size"
|
|
|
|
x.title = "Example Graph"
|
|
|
|
#x.ext_color = [ "white", "yellow", "red", "blue", "green"]
|
|
|
|
x.setData(sizes)
|
|
|
|
#x.setLabels(["Mon", "Tue", "Wed", "Thu", "Fri"])
|
|
|
|
x.draw("simple.png")
|
|
|
|
|
|
|
|
KiB=1024
|
|
|
|
MiB=1024*KiB
|
|
|
|
GiB=1024*MiB
|
|
|
|
TiB=1024*GiB
|
2006-10-22 03:39:12 +00:00
|
|
|
PiB=1024*TiB
|
2006-10-22 03:10:04 +00:00
|
|
|
|
2019-05-15 06:17:44 +00:00
|
|
|
class Sizes(object):
|
2006-10-22 03:10:04 +00:00
|
|
|
def __init__(self, mode, file_size, arity=2):
|
2008-04-16 23:29:53 +00:00
|
|
|
MAX_SEGSIZE = 128*KiB
|
2006-10-22 03:10:04 +00:00
|
|
|
self.mode = mode
|
|
|
|
self.file_size = file_size
|
|
|
|
self.seg_size = seg_size = 1.0 * min(MAX_SEGSIZE, file_size)
|
|
|
|
self.num_segs = num_segs = math.ceil(file_size / seg_size)
|
2007-08-09 05:31:12 +00:00
|
|
|
self.num_blocks = num_blocks = num_segs
|
2006-10-22 03:10:04 +00:00
|
|
|
|
2008-04-16 23:29:53 +00:00
|
|
|
self.num_shares = num_shares = 10
|
|
|
|
self.shares_needed = shares_needed = 3
|
2006-10-22 03:10:04 +00:00
|
|
|
|
2007-08-09 05:31:12 +00:00
|
|
|
self.block_size = block_size = seg_size / shares_needed
|
|
|
|
self.share_size = share_size = block_size * num_blocks
|
2006-10-22 03:10:04 +00:00
|
|
|
|
2007-08-09 05:31:12 +00:00
|
|
|
# none of this includes the share-level hash chain yet, since that is
|
|
|
|
# only a function of the number of shares. All overhead numbers
|
|
|
|
# assume that the share-level hash chain has already been sent,
|
|
|
|
# including the root of the block-level hash tree.
|
2006-10-22 03:10:04 +00:00
|
|
|
|
|
|
|
if mode == "alpha":
|
|
|
|
# no hash tree at all
|
2007-08-09 05:31:12 +00:00
|
|
|
self.block_arity = 0
|
|
|
|
self.block_tree_depth = 0
|
|
|
|
self.block_overhead = 0
|
2010-08-04 07:09:42 +00:00
|
|
|
self.bytes_until_some_data = 32 + share_size
|
2007-08-09 05:31:12 +00:00
|
|
|
self.share_storage_overhead = 0
|
|
|
|
self.share_transmission_overhead = 0
|
2006-10-22 03:10:04 +00:00
|
|
|
|
|
|
|
elif mode == "beta":
|
2007-08-09 05:31:12 +00:00
|
|
|
# k=num_blocks, d=1
|
2010-08-04 07:09:42 +00:00
|
|
|
# each block has a 32-byte hash
|
2007-08-09 05:31:12 +00:00
|
|
|
self.block_arity = num_blocks
|
|
|
|
self.block_tree_depth = 1
|
2010-08-04 07:09:42 +00:00
|
|
|
self.block_overhead = 32
|
2007-08-09 05:31:12 +00:00
|
|
|
# the share has a list of hashes, one for each block
|
|
|
|
self.share_storage_overhead = (self.block_overhead *
|
|
|
|
num_blocks)
|
|
|
|
# we can get away with not sending the hash of the share that
|
2006-10-22 03:10:04 +00:00
|
|
|
# we're sending in full, once
|
2010-08-04 07:09:42 +00:00
|
|
|
self.share_transmission_overhead = self.share_storage_overhead - 32
|
2006-10-22 03:10:04 +00:00
|
|
|
# we must get the whole list (so it can be validated) before
|
|
|
|
# any data can be validated
|
2007-08-09 05:31:12 +00:00
|
|
|
self.bytes_until_some_data = (self.share_transmission_overhead +
|
|
|
|
block_size)
|
2006-10-22 03:10:04 +00:00
|
|
|
|
|
|
|
elif mode == "gamma":
|
2007-08-09 05:31:12 +00:00
|
|
|
self.block_arity = k = arity
|
|
|
|
d = math.ceil(math.log(num_blocks, k))
|
|
|
|
self.block_tree_depth = d
|
2006-10-22 03:10:04 +00:00
|
|
|
num_leaves = k ** d
|
|
|
|
# to make things easier, we make the pessimistic assumption that
|
|
|
|
# we have to store hashes for all the empty places in the tree
|
2007-08-09 05:31:12 +00:00
|
|
|
# (when the number of shares is not an exact exponent of k)
|
2010-08-04 07:09:42 +00:00
|
|
|
self.block_overhead = 32
|
2007-08-09 05:31:12 +00:00
|
|
|
# the block hashes are organized into a k-ary tree, which
|
2006-10-22 03:10:04 +00:00
|
|
|
# means storing (and eventually transmitting) more hashes. This
|
2007-08-09 05:31:12 +00:00
|
|
|
# count includes all the low-level share hashes and the root.
|
2006-10-22 03:10:04 +00:00
|
|
|
hash_nodes = (num_leaves*k - 1) / (k - 1)
|
2020-09-11 14:28:22 +00:00
|
|
|
#print("hash_depth", d)
|
|
|
|
#print("num_leaves", num_leaves)
|
|
|
|
#print("hash_nodes", hash_nodes)
|
2006-10-22 03:10:04 +00:00
|
|
|
# the storage overhead is this
|
2010-08-04 07:09:42 +00:00
|
|
|
self.share_storage_overhead = 32 * (hash_nodes - 1)
|
2006-10-22 03:10:04 +00:00
|
|
|
# the transmission overhead is smaller: if we actually transmit
|
2007-08-09 05:31:12 +00:00
|
|
|
# every block, we don't have to transmit 1/k of the
|
|
|
|
# lowest-level block hashes, and we don't have to transmit the
|
|
|
|
# root because it was already sent with the share-level hash tree
|
2010-08-04 07:09:42 +00:00
|
|
|
self.share_transmission_overhead = 32 * (hash_nodes
|
2006-10-22 03:10:04 +00:00
|
|
|
- 1 # the root
|
|
|
|
- num_leaves / k)
|
|
|
|
# we must get a full sibling hash chain before we can validate
|
|
|
|
# any data
|
|
|
|
sibling_length = d * (k-1)
|
2010-08-04 07:09:42 +00:00
|
|
|
self.bytes_until_some_data = 32 * sibling_length + block_size
|
2006-10-22 03:10:04 +00:00
|
|
|
|
|
|
|
else:
|
2009-02-23 00:34:01 +00:00
|
|
|
raise ValueError("unknown mode '%s" % mode)
|
2006-10-22 03:10:04 +00:00
|
|
|
|
2007-08-09 05:31:12 +00:00
|
|
|
self.storage_overhead = self.share_storage_overhead * num_shares
|
2006-10-22 03:10:04 +00:00
|
|
|
self.storage_overhead_percentage = 100.0 * self.storage_overhead / file_size
|
|
|
|
|
|
|
|
def dump(self):
|
|
|
|
for k in ("mode", "file_size", "seg_size",
|
2007-08-09 05:31:12 +00:00
|
|
|
"num_segs", "num_blocks", "num_shares", "shares_needed",
|
|
|
|
"block_size", "share_size",
|
|
|
|
"block_arity", "block_tree_depth",
|
|
|
|
"block_overhead",
|
|
|
|
"share_storage_overhead", "share_transmission_overhead",
|
2006-10-22 03:10:04 +00:00
|
|
|
"storage_overhead", "storage_overhead_percentage",
|
|
|
|
"bytes_until_some_data"):
|
2019-03-22 10:40:58 +00:00
|
|
|
print(k, getattr(self, k))
|
2006-10-22 03:10:04 +00:00
|
|
|
|
|
|
|
def fmt(num, trim=False):
|
|
|
|
if num < KiB:
|
2006-11-30 22:14:39 +00:00
|
|
|
#s = str(num) + "#"
|
|
|
|
s = "%.2f#" % num
|
2006-10-22 03:10:04 +00:00
|
|
|
elif num < MiB:
|
|
|
|
s = "%.2fk" % (num / KiB)
|
|
|
|
elif num < GiB:
|
|
|
|
s = "%.2fM" % (num / MiB)
|
|
|
|
elif num < TiB:
|
|
|
|
s = "%.2fG" % (num / GiB)
|
2006-10-22 03:39:12 +00:00
|
|
|
elif num < PiB:
|
|
|
|
s = "%.2fT" % (num / TiB)
|
2006-10-22 03:10:04 +00:00
|
|
|
else:
|
|
|
|
s = "big"
|
|
|
|
if trim:
|
2006-10-22 03:39:12 +00:00
|
|
|
s = re.sub(r'(\.0+)([kMGT#])',
|
2006-10-22 03:10:04 +00:00
|
|
|
lambda m: m.group(2),
|
|
|
|
s)
|
|
|
|
else:
|
2006-10-22 03:39:12 +00:00
|
|
|
s = re.sub(r'(\.0+)([kMGT#])',
|
2006-10-22 03:10:04 +00:00
|
|
|
lambda m: (" "*len(m.group(1))+m.group(2)),
|
|
|
|
s)
|
|
|
|
if s.endswith("#"):
|
|
|
|
s = s[:-1] + " "
|
|
|
|
return s
|
|
|
|
|
|
|
|
def text():
|
|
|
|
opts = Args()
|
|
|
|
opts.parseOptions()
|
|
|
|
mode = opts["mode"]
|
|
|
|
arity = opts["arity"]
|
|
|
|
# 0123456789012345678901234567890123456789012345678901234567890123456
|
2019-03-22 10:40:58 +00:00
|
|
|
print("mode=%s" % mode, " arity=%d" % arity)
|
|
|
|
print(" storage storage")
|
|
|
|
print("Size sharesize overhead overhead k d alacrity")
|
|
|
|
print(" (bytes) (%)")
|
|
|
|
print("------- ------- -------- -------- ---- -- --------")
|
2006-11-30 22:14:39 +00:00
|
|
|
#sizes = [2 ** i for i in range(7, 41)]
|
2017-12-30 23:19:41 +00:00
|
|
|
#radix = math.sqrt(10); expstep = 2
|
2006-11-30 22:14:39 +00:00
|
|
|
radix = 2; expstep = 2
|
|
|
|
#radix = 10; expstep = 1
|
|
|
|
maxexp = int(math.ceil(math.log(1e12, radix)))+2
|
|
|
|
sizes = [radix ** i for i in range(2,maxexp,expstep)]
|
2006-10-22 03:10:04 +00:00
|
|
|
for file_size in sizes:
|
|
|
|
s = Sizes(mode, file_size, arity)
|
|
|
|
out = ""
|
|
|
|
out += "%7s " % fmt(file_size, trim=True)
|
2007-08-09 05:31:12 +00:00
|
|
|
out += "%7s " % fmt(s.share_size)
|
2006-10-22 03:10:04 +00:00
|
|
|
out += "%8s" % fmt(s.storage_overhead)
|
|
|
|
out += "%10.2f " % s.storage_overhead_percentage
|
2007-08-09 05:31:12 +00:00
|
|
|
out += " %4d" % int(s.block_arity)
|
|
|
|
out += " %2d" % int(s.block_tree_depth)
|
2006-10-22 03:10:04 +00:00
|
|
|
out += " %8s" % fmt(s.bytes_until_some_data)
|
2019-03-22 10:40:58 +00:00
|
|
|
print(out)
|
2006-10-22 03:10:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
def graph():
|
|
|
|
# doesn't work yet
|
|
|
|
import Gnuplot
|
|
|
|
opts = Args()
|
|
|
|
opts.parseOptions()
|
|
|
|
mode = opts["mode"]
|
|
|
|
arity = opts["arity"]
|
|
|
|
g = Gnuplot.Gnuplot(debug=1)
|
|
|
|
g.title("overhead / alacrity tradeoffs")
|
|
|
|
g.xlabel("file size")
|
|
|
|
g.ylabel("stuff")
|
|
|
|
sizes = [2 ** i for i in range(7, 32)]
|
|
|
|
series = {"overhead": {}, "alacrity": {}}
|
|
|
|
for file_size in sizes:
|
|
|
|
s = Sizes(mode, file_size, arity)
|
|
|
|
series["overhead"][file_size] = s.storage_overhead_percentage
|
|
|
|
series["alacrity"][file_size] = s.bytes_until_some_data
|
|
|
|
g.plot([ (fs, series["overhead"][fs])
|
|
|
|
for fs in sizes ])
|
|
|
|
raw_input("press return")
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
text()
|
|
|
|
#graph()
|