contrib/fixpax.py
author Gregory Szorc <gregory.szorc@gmail.com>
Sat, 20 Feb 2016 15:56:44 -0800
changeset 28181 f8efc8a3a991
parent 27495 58eb1c5bba58
child 28354 00f317788d33
permissions -rwxr-xr-x
worker: change partition strategy to every Nth element The only consumer of the worker pool code today is `hg update`. Previously, the algorithm to partition work to each worker process preserved input list ordering. We'd take the first N elements, then the next N elements, etc. Measurements on mozilla-central demonstrate this isn't an optimal partitioning strategy. I added debug code to print when workers were exiting. When performing a working copy update on a previously empty working copy of mozilla-central, I noticed that process lifetimes were all over the map. One worker would complete after 7s. Many would complete after 12s. And another worker would often take >16s. This behavior occurred for many worker process counts and was more pronounced on some than others. What I suspect is happening is some workers end up with lots of small files and others with large files. This is because the update code passes in actions according to sorted filenames. And, directories under tend to accumulate similar files. For example, test directories often consist of many small test files and media directories contain binary (often larger) media files. This patch changes the partitioning algorithm to select every Nth element from the input list. Each worker thus has a similar composition of files to operate on. The result of this change is that worker processes now all tend to exit around the same time. The possibility of a long pole due to being unlucky and receiving all the large files has been mitigated. Overall execution time seems to drop, but not by a statistically significant amount on mozilla-central. However, repositories with directories containing many large files will likely show a drop. There shouldn't be any regressions due to partial manifest decoding because the update code already iterates the manifest to determine what files to operate on, so the manifest should already be decoded.

#!/usr/bin/env python
# fixpax - fix ownership in bdist_mpkg output
#
# Copyright 2015 Matt Mackall <mpm@selenic.com>
#
# This software may be used and distributed according to the terms of the
# MIT license (http://opensource.org/licenses/MIT)

"""Set file ownership to 0 in an Archive.pax.gz.
Suitable for fixing files bdist_mpkg output:
*.mpkg/Contents/Packages/*.pkg/Contents/Archive.pax.gz
"""

import sys, os, gzip

def fixpax(iname, oname):
    i = gzip.GzipFile(iname)
    o = gzip.GzipFile(oname, "w")

    while True:
        magic = i.read(6)
        dev = i.read(6)
        ino = i.read(6)
        mode = i.read(6)
        i.read(6) # uid
        i.read(6) # gid
        nlink = i.read(6)
        rdev = i.read(6)
        mtime = i.read(11)
        namesize = i.read(6)
        filesize = i.read(11)
        name = i.read(int(namesize, 8))
        data = i.read(int(filesize, 8))

        o.write(magic)
        o.write(dev)
        o.write(ino)
        o.write(mode)
        o.write("000000")
        o.write("000000")
        o.write(nlink)
        o.write(rdev)
        o.write(mtime)
        o.write(namesize)
        o.write(filesize)
        o.write(name)
        o.write(data)

        if name.startswith("TRAILER!!!"):
            o.write(i.read())
            break

    o.close()
    i.close()

if __name__ == '__main__':
    for iname in sys.argv[1:]:
        print 'fixing file ownership in %s' % iname
        oname = sys.argv[1] + '.tmp'
        fixpax(iname, oname)
        os.rename(oname, iname)