]> arthur.barton.de Git - bup.git/blob - cmd/memtest-cmd.py
cmd/memtest: remove an unused function.
[bup.git] / cmd / memtest-cmd.py
1 #!/usr/bin/env python
2 import sys, re, struct, mmap, time, resource
3 from bup import git, options
4 from bup.helpers import *
5
6 handle_ctrl_c()
7
8 _linux_warned = 0
9 def linux_memstat():
10     global _linux_warned
11     #fields = ['VmSize', 'VmRSS', 'VmData', 'VmStk', 'ms']
12     d = {}
13     try:
14         f = open('/proc/self/status')
15     except IOError, e:
16         if not _linux_warned:
17             log('Warning: %s\n' % e)
18             _linux_warned = 1
19         return {}
20     for line in f:
21         k,v = re.split(r':\s*', line.strip(), 1)
22         d[k] = v
23     return d
24
25
26 last = last_u = last_s = start = 0
27 def report(count):
28     global last, last_u, last_s, start
29     headers = ['RSS', 'MajFlt', 'user', 'sys', 'ms']
30     ru = resource.getrusage(resource.RUSAGE_SELF)
31     now = time.time()
32     rss = int(ru.ru_maxrss/1024)
33     if not rss:
34         rss = linux_memstat().get('VmRSS', '??')
35     fields = [rss,
36               ru.ru_majflt,
37               int((ru.ru_utime - last_u) * 1000),
38               int((ru.ru_stime - last_s) * 1000),
39               int((now - last) * 1000)]
40     fmt = '%9s  ' + ('%10s ' * len(fields))
41     if count >= 0:
42         print fmt % tuple([count] + fields)
43     else:
44         start = now
45         print fmt % tuple([''] + headers)
46     sys.stdout.flush()
47     
48     # don't include time to run report() in usage counts
49     ru = resource.getrusage(resource.RUSAGE_SELF)
50     last_u = ru.ru_utime
51     last_s = ru.ru_stime
52     last = time.time()
53
54
55 optspec = """
56 bup memtest [-n elements] [-c cycles]
57 --
58 n,number=  number of objects per cycle [10000]
59 c,cycles=  number of cycles to run [100]
60 ignore-midx  ignore .midx files, use only .idx files
61 existing   test with existing objects instead of fake ones
62 """
63 o = options.Options('bup memtest', optspec)
64 (opt, flags, extra) = o.parse(sys.argv[1:])
65
66 if extra:
67     o.fatal('no arguments expected')
68
69 git.ignore_midx = opt.ignore_midx
70
71 git.check_repo_or_die()
72 m = git.PackIdxList(git.repo('objects/pack'))
73
74 report(-1)
75 f = open('/dev/urandom')
76 a = mmap.mmap(-1, 20)
77 report(0)
78
79 if opt.existing:
80     def foreverit(mi):
81         while 1:
82             for e in mi:
83                 yield e
84     objit = iter(foreverit(m))
85     
86 for c in xrange(opt.cycles):
87     for n in xrange(opt.number):
88         if opt.existing:
89             bin = objit.next()
90             assert(m.exists(bin))
91         else:
92             b = f.read(3)
93             a[0:2] = b[0:2]
94             a[2] = chr(ord(b[2]) & 0xf0)
95             bin = str(a[0:20])
96
97             # technically, a randomly generated object id might exist.
98             # but the likelihood of that is the likelihood of finding
99             # a collision in sha-1 by accident, which is so unlikely that
100             # we don't care.
101             assert(not m.exists(bin))
102     report((c+1)*opt.number)
103
104 print ('%d objects searched in %d steps: avg %.3f steps/object' 
105        % (git._total_searches, git._total_steps,
106           git._total_steps*1.0/git._total_searches))
107 print 'Total time: %.3fs' % (time.time() - start)