]> arthur.barton.de Git - bup.git/blob - lib/cmd/margin-cmd.py
9dc854aaee57447607a93068bfb760029dcb37e4
[bup.git] / lib / cmd / margin-cmd.py
1 #!/bin/sh
2 """": # -*-python-*-
3 # https://sourceware.org/bugzilla/show_bug.cgi?id=26034
4 export "BUP_ARGV_0"="$0"
5 arg_i=1
6 for arg in "$@"; do
7     export "BUP_ARGV_${arg_i}"="$arg"
8     shift
9     arg_i=$((arg_i + 1))
10 done
11 # Here to end of preamble replaced during install
12 bup_python="$(dirname "$0")/bup-python" || exit $?
13 exec "$bup_python" "$0"
14 """
15 # end of bup preamble
16
17 from __future__ import absolute_import
18 import sys, struct, math
19
20 from bup import compat, options, git, _helpers
21 from bup.helpers import log
22 from bup.io import byte_stream
23
24 POPULATION_OF_EARTH=6.7e9  # as of September, 2010
25
26 optspec = """
27 bup margin
28 --
29 predict    Guess object offsets and report the maximum deviation
30 ignore-midx  Don't use midx files; use only plain pack idx files.
31 """
32 o = options.Options(optspec)
33 opt, flags, extra = o.parse(compat.argv[1:])
34
35 if extra:
36     o.fatal("no arguments expected")
37
38 git.check_repo_or_die()
39
40 mi = git.PackIdxList(git.repo(b'objects/pack'), ignore_midx=opt.ignore_midx)
41
42 def do_predict(ix, out):
43     total = len(ix)
44     maxdiff = 0
45     for count,i in enumerate(ix):
46         prefix = struct.unpack('!Q', i[:8])[0]
47         expected = prefix * total // (1 << 64)
48         diff = count - expected
49         maxdiff = max(maxdiff, abs(diff))
50     out.write(b'%d of %d (%.3f%%) '
51               % (maxdiff, len(ix), maxdiff * 100.0 / len(ix)))
52     out.flush()
53     assert(count+1 == len(ix))
54
55 sys.stdout.flush()
56 out = byte_stream(sys.stdout)
57
58 if opt.predict:
59     if opt.ignore_midx:
60         for pack in mi.packs:
61             do_predict(pack, out)
62     else:
63         do_predict(mi, out)
64 else:
65     # default mode: find longest matching prefix
66     last = b'\0'*20
67     longmatch = 0
68     for i in mi:
69         if i == last:
70             continue
71         #assert(str(i) >= last)
72         pm = _helpers.bitmatch(last, i)
73         longmatch = max(longmatch, pm)
74         last = i
75     out.write(b'%d\n' % longmatch)
76     log('%d matching prefix bits\n' % longmatch)
77     doublings = math.log(len(mi), 2)
78     bpd = longmatch / doublings
79     log('%.2f bits per doubling\n' % bpd)
80     remain = 160 - longmatch
81     rdoublings = remain / bpd
82     log('%d bits (%.2f doublings) remaining\n' % (remain, rdoublings))
83     larger = 2**rdoublings
84     log('%g times larger is possible\n' % larger)
85     perperson = larger/POPULATION_OF_EARTH
86     log('\nEveryone on earth could have %d data sets like yours, all in one\n'
87         'repository, and we would expect 1 object collision.\n'
88         % int(perperson))