File: cvs2svn_memlog

package info (click to toggle)
cvs2svn 2.5.0-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 3,708 kB
  • sloc: python: 23,789; sh: 512; perl: 121; makefile: 84
file content (111 lines) | stat: -rwxr-xr-x 3,072 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python
# (Be in -*- python -*- mode.)
#
# ====================================================================
# Copyright (c) 2000-2008 CollabNet.  All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.  The terms
# are also available at http://subversion.tigris.org/license-1.html.
# If newer versions of this license are posted there, you may use a
# newer version instead, at your option.
#
# This software consists of voluntary contributions made by many
# individuals.  For exact contribution history, see the revision
# history and logs, available at http://cvs2svn.tigris.org/.
# ====================================================================

"""Run cvs2svn, but logging memory usage.

Memory use is logged every MemoryLogger.interval seconds.  This script
takes the same parameters as cvs2svn.

Memory use is determined by reading from the /proc filesystem.  This
method is not very portable, but should hopefully work on a typical
modern Linux."""


import sys
import os

# Make sure that a supported version of Python is being used.  Do this
# as early as possible, using only code compatible with Python 1.5.2
# and Python 3.x before the check.
if not (0x02040000 <= sys.hexversion < 0x03000000):
  sys.stderr.write("ERROR: Python 2, version 2.4 or higher required.\n")
  sys.exit(1)

sys.path.insert(0, os.path.dirname(os.path.dirname(sys.argv[0])))

import re
import time
import optparse
import threading

from cvs2svn_lib.common import FatalException
from cvs2svn_lib.log import logger
from cvs2svn_lib.main import main


usage = '%prog [--interval=VALUE] [--help|-h] -- CVS2SVN-ARGS'

description = """\
Run cvs2svn while logging its memory usage.  ('--' is required to
separate %(progname)s options from the options and arguments that will
be passed through to cvs2svn.)
"""


rss_re = re.compile(r'^VmRSS\:\s+(?P<mem>.*)$')

def get_memory_used():
  filename = '/proc/%d/status' % (os.getpid(),)
  f = open(filename)
  try:
    for l in f.readlines():
      l = l.strip()
      m = rss_re.match(l)
      if m:
        return m.group('mem')
  finally:
    f.close()

  return 'Unknown'


class MemoryLogger(threading.Thread):
  def __init__(self, interval):
    threading.Thread.__init__(self)
    self.setDaemon(True)
    self.start_time = time.time()
    self.interval = interval

  def run(self):
    i = 0
    while True:
      delay = self.start_time + self.interval * i - time.time()
      if delay > 0:
        time.sleep(delay)
      logger.write('Memory used: %s' % (get_memory_used(),))
      i += 1


parser = optparse.OptionParser(usage=usage, description=description)
parser.set_defaults(interval=1.0)
parser.add_option(
    '--interval',
    action='store', type='float',
    help='the time in seconds between memory logs',
    )

(options, args) = parser.parse_args()

MemoryLogger(interval=options.interval).start()

try:
  main(sys.argv[0], args)
except FatalException, e:
  sys.stderr.write(str(e) + '\n')
  sys.exit(1)