#!/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)


