Bring CHANGES up to date.
[cvs2svn.git] / contrib / cvs2svn_memlog
blob7f35a39d334b80a98addf26d3ba8c74ac889273e
1 #!/usr/bin/env python
2 # (Be in -*- python -*- mode.)
4 # ====================================================================
5 # Copyright (c) 2000-2008 CollabNet. All rights reserved.
7 # This software is licensed as described in the file COPYING, which
8 # you should have received as part of this distribution. The terms
9 # are also available at http://subversion.tigris.org/license-1.html.
10 # If newer versions of this license are posted there, you may use a
11 # newer version instead, at your option.
13 # This software consists of voluntary contributions made by many
14 # individuals. For exact contribution history, see the revision
15 # history and logs, available at http://cvs2svn.tigris.org/.
16 # ====================================================================
18 """Run cvs2svn, but logging memory usage.
20 Memory use is logged every MemoryLogger.interval seconds. This script
21 takes the same parameters as cvs2svn.
23 Memory use is determined by reading from the /proc filesystem. This
24 method is not very portable, but should hopefully work on a typical
25 modern Linux."""
28 import sys
29 import os
31 # Make sure that a supported version of Python is being used. Do this
32 # as early as possible, using only code compatible with Python 1.5.2
33 # and Python 3.x before the check.
34 if not (0x02040000 <= sys.hexversion < 0x03000000):
35 sys.stderr.write("ERROR: Python 2, version 2.4 or higher required.\n")
36 sys.exit(1)
38 sys.path.insert(0, os.path.dirname(os.path.dirname(sys.argv[0])))
40 import re
41 import time
42 import optparse
43 import threading
45 from cvs2svn_lib.common import FatalException
46 from cvs2svn_lib.log import logger
47 from cvs2svn_lib.main import main
50 usage = '%prog [--interval=VALUE] [--help|-h] -- CVS2SVN-ARGS'
52 description = """\
53 Run cvs2svn while logging its memory usage. ('--' is required to
54 separate %(progname)s options from the options and arguments that will
55 be passed through to cvs2svn.)
56 """
59 rss_re = re.compile(r'^VmRSS\:\s+(?P<mem>.*)$')
61 def get_memory_used():
62 filename = '/proc/%d/status' % (os.getpid(),)
63 f = open(filename)
64 try:
65 for l in f.readlines():
66 l = l.strip()
67 m = rss_re.match(l)
68 if m:
69 return m.group('mem')
70 finally:
71 f.close()
73 return 'Unknown'
76 class MemoryLogger(threading.Thread):
77 def __init__(self, interval):
78 threading.Thread.__init__(self)
79 self.setDaemon(True)
80 self.start_time = time.time()
81 self.interval = interval
83 def run(self):
84 i = 0
85 while True:
86 delay = self.start_time + self.interval * i - time.time()
87 if delay > 0:
88 time.sleep(delay)
89 logger.write('Memory used: %s' % (get_memory_used(),))
90 i += 1
93 parser = optparse.OptionParser(usage=usage, description=description)
94 parser.set_defaults(interval=1.0)
95 parser.add_option(
96 '--interval',
97 action='store', type='float',
98 help='the time in seconds between memory logs',
101 (options, args) = parser.parse_args()
103 MemoryLogger(interval=options.interval).start()
105 try:
106 main(sys.argv[0], args)
107 except FatalException, e:
108 sys.stderr.write(str(e) + '\n')
109 sys.exit(1)