author Trevor Saunders <>
Fri, 07 Mar 2014 16:35:19 -0500
changeset 207782 a11adf1705ec702e75b493060fa7a5e18b15145a
parent 199754 ff64d9bbc03cd14dcd05d94f54a0e9c14f57b787
child 414174 bc4dc3fbb4896386cbd49545edfe3c3f9aa6055b
child 415761 20702baacfae544276279219a07e24087829789d
permissions -rw-r--r--
bug 982842 - initial a11y ipc impl r=davidb, bent

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at

import expandlibs_exec
import sys
import threading
import time

from win32 import procmem

def measure_vsize_threadfunc(proc, output_file):
    Measure the virtual memory usage of |proc| at regular intervals
    until it exits, then print the maximum value and write it to
    |output_file|.  Also, print something to the console every
    half an hour to prevent the build job from getting killed when
    linking a large PGOed binary.
    maxvsize = 0
    idleTime = 0
    while proc.returncode is None:
        maxvsize, vsize = procmem.get_vmsize(proc._handle)
        idleTime += 0.5
        if idleTime > 30 * 60:
          print "Still linking, 30 minutes passed..."
          idleTime = 0
    print "TinderboxPrint: linker max vsize: %d" % maxvsize
    with open(output_file, "w") as f:
        f.write("%d\n" % maxvsize)

def measure_link_vsize(output_file, args):
    Execute |args|, and measure the maximum virtual memory usage of the process,
    printing it to stdout when finished.

    # This needs to be a list in order for the callback to set the
    # variable properly with python-2's scoping rules.
    t = [None]
    def callback(proc):
        t[0] = threading.Thread(target=measure_vsize_threadfunc,
                             args=(proc, output_file))
    exitcode = expandlibs_exec.main(args, proc_callback=callback)
    # Wait for the background thread to finish.
    return exitcode

if __name__ == "__main__":
    if sys.platform != "win32":
        print >>sys.stderr, " is only for use on Windows!"
    if len(sys.argv) < 3:
        print >>sys.stderr, "Usage: <output filename> <commandline>"
    output_file = sys.argv.pop(1)
    sys.exit(measure_link_vsize(output_file, sys.argv[1:]))