]> arthur.barton.de Git - bup.git/blob - lib/bup/cmd/memtest.py
memtest-cmd: copy to bup.cmd.memtest
[bup.git] / lib / bup / cmd / memtest.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")/../../../config/bin/python" || exit $?
13 exec "$bup_python" "$0"
14 """
15 # end of bup preamble
16
17 from __future__ import absolute_import, print_function
18
19 # Intentionally replace the dirname "$0" that python prepends
20 import os, sys
21 sys.path[0] = os.path.dirname(os.path.realpath(__file__)) + '/../..'
22
23 import re, resource, struct, time
24
25 from bup import compat, git, bloom, midx, options, _helpers
26 from bup.compat import range
27 from bup.helpers import handle_ctrl_c
28 from bup.io import byte_stream
29
30
31 handle_ctrl_c()
32
33
34 _linux_warned = 0
35 def linux_memstat():
36     global _linux_warned
37     #fields = ['VmSize', 'VmRSS', 'VmData', 'VmStk', 'ms']
38     d = {}
39     try:
40         f = open(b'/proc/self/status', 'rb')
41     except IOError as e:
42         if not _linux_warned:
43             log('Warning: %s\n' % e)
44             _linux_warned = 1
45         return {}
46     for line in f:
47         # Note that on Solaris, this file exists but is binary.  If that
48         # happens, this split() might not return two elements.  We don't
49         # really need to care about the binary format since this output
50         # isn't used for much and report() can deal with missing entries.
51         t = re.split(br':\s*', line.strip(), 1)
52         if len(t) == 2:
53             k,v = t
54             d[k] = v
55     return d
56
57
58 last = last_u = last_s = start = 0
59 def report(count, out):
60     global last, last_u, last_s, start
61     headers = ['RSS', 'MajFlt', 'user', 'sys', 'ms']
62     ru = resource.getrusage(resource.RUSAGE_SELF)
63     now = time.time()
64     rss = int(ru.ru_maxrss // 1024)
65     if not rss:
66         rss = linux_memstat().get(b'VmRSS', b'??')
67     fields = [rss,
68               ru.ru_majflt,
69               int((ru.ru_utime - last_u) * 1000),
70               int((ru.ru_stime - last_s) * 1000),
71               int((now - last) * 1000)]
72     fmt = '%9s  ' + ('%10s ' * len(fields))
73     if count >= 0:
74         line = fmt % tuple([count] + fields)
75         out.write(line.encode('ascii') + b'\n')
76     else:
77         start = now
78         out.write((fmt % tuple([''] + headers)).encode('ascii') + b'\n')
79     out.flush()
80
81     # don't include time to run report() in usage counts
82     ru = resource.getrusage(resource.RUSAGE_SELF)
83     last_u = ru.ru_utime
84     last_s = ru.ru_stime
85     last = time.time()
86
87
88 optspec = """
89 bup memtest [-n elements] [-c cycles]
90 --
91 n,number=  number of objects per cycle [10000]
92 c,cycles=  number of cycles to run [100]
93 ignore-midx  ignore .midx files, use only .idx files
94 existing   test with existing objects instead of fake ones
95 """
96 o = options.Options(optspec)
97 opt, flags, extra = o.parse(compat.argv[1:])
98
99 if extra:
100     o.fatal('no arguments expected')
101
102 git.check_repo_or_die()
103 m = git.PackIdxList(git.repo(b'objects/pack'), ignore_midx=opt.ignore_midx)
104
105 sys.stdout.flush()
106 out = byte_stream(sys.stdout)
107
108 report(-1, out)
109 _helpers.random_sha()
110 report(0, out)
111
112 if opt.existing:
113     def foreverit(mi):
114         while 1:
115             for e in mi:
116                 yield e
117     objit = iter(foreverit(m))
118
119 for c in range(opt.cycles):
120     for n in range(opt.number):
121         if opt.existing:
122             bin = next(objit)
123             assert(m.exists(bin))
124         else:
125             bin = _helpers.random_sha()
126
127             # technically, a randomly generated object id might exist.
128             # but the likelihood of that is the likelihood of finding
129             # a collision in sha-1 by accident, which is so unlikely that
130             # we don't care.
131             assert(not m.exists(bin))
132     report((c+1)*opt.number, out)
133
134 if bloom._total_searches:
135     out.write(b'bloom: %d objects searched in %d steps: avg %.3f steps/object\n'
136               % (bloom._total_searches, bloom._total_steps,
137                  bloom._total_steps*1.0/bloom._total_searches))
138 if midx._total_searches:
139     out.write(b'midx: %d objects searched in %d steps: avg %.3f steps/object\n'
140               % (midx._total_searches, midx._total_steps,
141                  midx._total_steps*1.0/midx._total_searches))
142 if git._total_searches:
143     out.write(b'idx: %d objects searched in %d steps: avg %.3f steps/object\n'
144               % (git._total_searches, git._total_steps,
145                  git._total_steps*1.0/git._total_searches))
146 out.write(b'Total time: %.3fs\n' % (time.time() - start))