viff

changeset 1064:753b0258a5be

New option for tracking memory usage over time. Adding --track-memory will make the runtime print out a line with the current and peak memory usage.
author Martin Geisler <mg@daimi.au.dk>
date Fri, 09 Jan 2009 17:17:05 +0100
parents 8bca4980563f
children 9f176022087d
files viff/runtime.py viff/util.py
diffstat 2 files changed, 47 insertions(+), 4 deletions(-) [+]
line diff
     1.1 --- a/viff/runtime.py	Thu Jan 08 11:31:57 2009 +0100
     1.2 +++ b/viff/runtime.py	Fri Jan 09 17:17:05 2009 +0100
     1.3 @@ -1,6 +1,6 @@
     1.4  # -*- coding: utf-8 -*-
     1.5  #
     1.6 -# Copyright 2007, 2008 VIFF Development Team.
     1.7 +# Copyright 2007, 2008, 2009 VIFF Development Team.
     1.8  #
     1.9  # This file is part of VIFF, the Virtual Ideal Functionality Framework.
    1.10  #
    1.11 @@ -39,9 +39,10 @@
    1.12  from collections import deque
    1.13  
    1.14  from viff.field import GF256, FieldElement
    1.15 -from viff.util import wrapper, rand, deep_wait
    1.16 +from viff.util import wrapper, rand, deep_wait, track_memory_usage
    1.17  
    1.18  from twisted.internet import reactor
    1.19 +from twisted.internet.task import LoopingCall
    1.20  from twisted.internet.error import ConnectionDone, CannotListenError
    1.21  from twisted.internet.defer import Deferred, DeferredList, gatherResults, succeed
    1.22  from twisted.internet.defer import maybeDeferred
    1.23 @@ -428,6 +429,8 @@
    1.24                           help="Enable extra debug output for deferreds.")
    1.25          group.add_option("--profile", action="store_true",
    1.26                           help="Collect and print profiling information.")
    1.27 +        group.add_option("--track-memory", action="store_true",
    1.28 +                         help="Track memory usage over time.")
    1.29  
    1.30          try:
    1.31              # Using __import__ since we do not use the module, we are
    1.32 @@ -441,7 +444,8 @@
    1.33                              security_parameter=30,
    1.34                              ssl=have_openssl,
    1.35                              deferred_debug=False,
    1.36 -                            profile=False)
    1.37 +                            profile=False,
    1.38 +                            track_memory=False)
    1.39  
    1.40      def __init__(self, player, threshold, options=None):
    1.41          """Initialize runtime.
    1.42 @@ -766,6 +770,13 @@
    1.43      Please see the example applications for more examples.
    1.44  
    1.45      """
    1.46 +    if options and options.track_memory:
    1.47 +        lc = LoopingCall(track_memory_usage)
    1.48 +        # Five times per second seems like a fair value. Besides, the
    1.49 +        # kernel will track the peak memory usage for us anyway.
    1.50 +        lc.start(0.2)
    1.51 +        reactor.addSystemEventTrigger("after", "shutdown", track_memory_usage)
    1.52 +
    1.53      if runtime_class is None:
    1.54          # The import is put here because of circular depencencies
    1.55          # between viff.runtime and viff.passive.
     2.1 --- a/viff/util.py	Thu Jan 08 11:31:57 2009 +0100
     2.2 +++ b/viff/util.py	Fri Jan 09 17:17:05 2009 +0100
     2.3 @@ -1,4 +1,4 @@
     2.4 -# Copyright 2007, 2008 VIFF Development Team.
     2.5 +# Copyright 2007, 2008, 2009 VIFF Development Team.
     2.6  #
     2.7  # This file is part of VIFF, the Virtual Ideal Functionality Framework.
     2.8  #
     2.9 @@ -356,6 +356,38 @@
    2.10  
    2.11      return profile_wrapper
    2.12  
    2.13 +
    2.14 +def memory_usage():
    2.15 +    """Read memory usage of the current process."""
    2.16 +    status = None
    2.17 +    result = {'peak': 0, 'rss': 0}
    2.18 +    try:
    2.19 +        # This will only work on systems with a /proc file system
    2.20 +        # (like Linux).
    2.21 +        status = open('/proc/self/status', 'r')
    2.22 +        for line in status:
    2.23 +            parts = line.split()
    2.24 +            key = parts[0][2:-1].lower()
    2.25 +            if key in result:
    2.26 +                result[key] = int(parts[1])
    2.27 +    finally:
    2.28 +        if status is not None:
    2.29 +            status.close()
    2.30 +    return result
    2.31 +
    2.32 +
    2.33 +_last_memory_usage = None
    2.34 +
    2.35 +
    2.36 +def track_memory_usage():
    2.37 +    """Print memory usage if changed since last time."""
    2.38 +    global _last_memory_usage
    2.39 +    usage = memory_usage()
    2.40 +    if usage != _last_memory_usage:
    2.41 +        print ", ".join(["%s: %.1f MiB" % (key, value/1024.0)
    2.42 +                         for key, value in usage.iteritems()])
    2.43 +        _last_memory_usage = usage
    2.44 +
    2.45  if __name__ == "__main__":
    2.46      import doctest    #pragma NO COVER
    2.47      doctest.testmod() #pragma NO COVER