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