]> arthur.barton.de Git - bup.git/blobdiff - cmd/memtest-cmd.py
README: add 0.30.x to CI status table
[bup.git] / cmd / memtest-cmd.py
index f1ab06b186786d24c45df916cb050a2d9af8d531..bf5f0d5a2b9365e7fd5ccef0ba206238d76cc5e9 100755 (executable)
@@ -1,33 +1,73 @@
-#!/usr/bin/env python
-import sys, re, struct, mmap, time
-from bup import git, options
-from bup.helpers import *
+#!/bin/sh
+"""": # -*-python-*-
+bup_python="$(dirname "$0")/bup-python" || exit $?
+exec "$bup_python" "$0" ${1+"$@"}
+"""
+# end of bup preamble
 
-handle_ctrl_c()
+from __future__ import absolute_import, print_function
+import sys, re, struct, time, resource
+
+from bup import git, bloom, midx, options, _helpers
+from bup.compat import range
+from bup.helpers import handle_ctrl_c
+from bup.io import byte_stream
 
-def s_from_bytes(bytes):
-    clist = [chr(b) for b in bytes]
-    return ''.join(clist)
+
+handle_ctrl_c()
 
 
-last = start = 0
-def report(count):
-    global last, start
-    fields = ['VmSize', 'VmRSS', 'VmData', 'VmStk', 'ms']
+_linux_warned = 0
+def linux_memstat():
+    global _linux_warned
+    #fields = ['VmSize', 'VmRSS', 'VmData', 'VmStk', 'ms']
     d = {}
-    for line in open('/proc/self/status').readlines():
-        l = re.split(r':\s*', line.strip(), 1)
-        d[l[0]] = l[1]
+    try:
+        f = open(b'/proc/self/status', 'rb')
+    except IOError as e:
+        if not _linux_warned:
+            log('Warning: %s\n' % e)
+            _linux_warned = 1
+        return {}
+    for line in f:
+        # Note that on Solaris, this file exists but is binary.  If that
+        # happens, this split() might not return two elements.  We don't
+        # really need to care about the binary format since this output
+        # isn't used for much and report() can deal with missing entries.
+        t = re.split(br':\s*', line.strip(), 1)
+        if len(t) == 2:
+            k,v = t
+            d[k] = v
+    return d
+
+
+last = last_u = last_s = start = 0
+def report(count, out):
+    global last, last_u, last_s, start
+    headers = ['RSS', 'MajFlt', 'user', 'sys', 'ms']
+    ru = resource.getrusage(resource.RUSAGE_SELF)
     now = time.time()
-    d['ms'] = int((now - last) * 1000)
+    rss = int(ru.ru_maxrss // 1024)
+    if not rss:
+        rss = linux_memstat().get(b'VmRSS', b'??')
+    fields = [rss,
+              ru.ru_majflt,
+              int((ru.ru_utime - last_u) * 1000),
+              int((ru.ru_stime - last_s) * 1000),
+              int((now - last) * 1000)]
+    fmt = '%9s  ' + ('%10s ' * len(fields))
     if count >= 0:
-        e1 = count
-        fields = [d[k] for k in fields]
+        line = fmt % tuple([count] + fields)
+        out.write(line.encode('ascii') + b'\n')
     else:
-        e1 = ''
         start = now
-    print ('%9s  ' + ('%10s ' * len(fields))) % tuple([e1] + fields)
-    sys.stdout.flush()
+        out.write((fmt % tuple([''] + headers)).encode('ascii') + b'\n')
+    out.flush()
+
+    # don't include time to run report() in usage counts
+    ru = resource.getrusage(resource.RUSAGE_SELF)
+    last_u = ru.ru_utime
+    last_s = ru.ru_stime
     last = time.time()
 
 
@@ -39,21 +79,21 @@ c,cycles=  number of cycles to run [100]
 ignore-midx  ignore .midx files, use only .idx files
 existing   test with existing objects instead of fake ones
 """
-o = options.Options('bup memtest', optspec)
+o = options.Options(optspec)
 (opt, flags, extra) = o.parse(sys.argv[1:])
 
 if extra:
     o.fatal('no arguments expected')
 
-git.ignore_midx = opt.ignore_midx
-
 git.check_repo_or_die()
-m = git.PackIdxList(git.repo('objects/pack'))
+m = git.PackIdxList(git.repo(b'objects/pack'), ignore_midx=opt.ignore_midx)
 
-report(-1)
-f = open('/dev/urandom')
-a = mmap.mmap(-1, 20)
-report(0)
+sys.stdout.flush()
+out = byte_stream(sys.stdout)
+
+report(-1, out)
+_helpers.random_sha()
+report(0, out)
 
 if opt.existing:
     def foreverit(mi):
@@ -61,26 +101,32 @@ if opt.existing:
             for e in mi:
                 yield e
     objit = iter(foreverit(m))
-    
-for c in xrange(opt.cycles):
-    for n in xrange(opt.number):
+
+for c in range(opt.cycles):
+    for n in range(opt.number):
         if opt.existing:
-            bin = objit.next()
+            bin = next(objit)
             assert(m.exists(bin))
         else:
-            b = f.read(3)
-            a[0:2] = b[0:2]
-            a[2] = chr(ord(b[2]) & 0xf0)
-            bin = str(a[0:20])
+            bin = _helpers.random_sha()
 
             # technically, a randomly generated object id might exist.
             # but the likelihood of that is the likelihood of finding
             # a collision in sha-1 by accident, which is so unlikely that
             # we don't care.
             assert(not m.exists(bin))
-    report((c+1)*opt.number)
+    report((c+1)*opt.number, out)
 
-print ('%d objects searched in %d steps: avg %.3f steps/object' 
-       % (git._total_searches, git._total_steps,
-          git._total_steps*1.0/git._total_searches))
-print 'Total time: %.3fs' % (time.time() - start)
+if bloom._total_searches:
+    out.write(b'bloom: %d objects searched in %d steps: avg %.3f steps/object\n'
+              % (bloom._total_searches, bloom._total_steps,
+                 bloom._total_steps*1.0/bloom._total_searches))
+if midx._total_searches:
+    out.write(b'midx: %d objects searched in %d steps: avg %.3f steps/object\n'
+              % (midx._total_searches, midx._total_steps,
+                 midx._total_steps*1.0/midx._total_searches))
+if git._total_searches:
+    out.write(b'idx: %d objects searched in %d steps: avg %.3f steps/object\n'
+              % (git._total_searches, git._total_steps,
+                 git._total_steps*1.0/git._total_searches))
+out.write(b'Total time: %.3fs\n' % (time.time() - start))