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