annotate mercurial/loggingutil.py @ 42222:57203e0210f8

copies: calculate mergecopies() based on pathcopies() When copies are stored in changesets, we need a changeset-centric version of mergecopies() just like we have a changeset-centric version of pathcopies(). I think the natural way of thinking about mergecopies() is in terms of pathcopies() from the base to each of the commits. So if we can rewrite mergecopies() based on two such pathcopies() calls, we'll get the changeset-centric version for free. That's what this patch does. A nice bonus is that it ends up being a lot simpler. mergecopies() has accumulated a lot of technical debt over time. One good example is the code for dealing with grafts (the "partial/incomplete/dirty" stuff). Since pathcopies() already deals with backwards renames and ping-pong renames, we get that for free. I've run tests with hard-coded debug logging for "fullcopy" and while I haven't looked at every difference it produces, all the ones I have looked at seemed reasonable to me. I'm a little surprised that no more tests fail when run with '--extra-config-opt experimental.copies.read-from=compatibility' compared to before this patch. This patch also fixes the broken cases in test-annotate.t and test-fastannotate.t. It also enables the part of test-copies.t that was previously disabled exactly because mergecopies() needed to get a changeset-centric version. One drawback of the rewritten code is that we may now make remotefilelog prefetch more files. We used to prefetch files that were unique to either side of the merge compared to the other. We now prefetch files that are unique to either side of the merge compared to the base. This means that if you added the same file to each side, we would not prefetch it before, but we would now. Such cases are probably quite rare, but one likely scenario where they happen is when moving from a commit to its successor (or the other way around). The user will probably already have the files in the cache in such cases, so it's probably not a big deal. Some timings for calculating mergecopies between two revisions (revisions shown on each line, all using the common ancestor as base): In the hg repo: 4.8 4.9: 0.21s -> 0.21s 4.0 4.8: 0.35s -> 0.63s In and old copy of the mozilla-unified repo: FIREFOX_BETA_60_BASE^ FIREFOX_BETA_60_BASE: 0.82s -> 0.82s FIREFOX_NIGHTLY_59_END FIREFOX_BETA_60_BASE: 2.5s -> 2.6s FIREFOX_BETA_59_END FIREFOX_BETA_60_BASE: 3.9s -> 4.1s FIREFOX_AURORA_50_BASE FIREFOX_BETA_60_BASE: 31s -> 33s So it's measurably slower in most cases. The most significant difference is in the hg repo between revisions 4.0 and 4.8. In that case it seems to come from the fact that pathcopies() uses fctx.isintroducedafter() (in _tracefile), while the old mergecopies() used fctx.linkrev() (in _checkcopies()). That results in a single call to filectx._adjustlinkrev(), which is responsible for the entire difference in time (in my repo). So we pay a performance penalty but we get more correct code (see change in test-mv-cp-st-diff.t). Deleting the "== f.filenode()" in _tracefile() recovers the lost performance in the hg repo. There were are few other optimizations in _checkcopies() that I could not measure any impact from. One was from the "seen" set. Another was from a "continue" when the file was not in the destination manifest (corresponding to "am" in _tracefile). Also note that merge copies are not calculated when updating with a clean working copy, which is probably the most common case. I therefore think the much simpler code is worth the slowdown. Differential Revision: https://phab.mercurial-scm.org/D6255
author Martin von Zweigbergk <martinvonz@google.com>
date Thu, 11 Apr 2019 23:22:54 -0700
parents 96be0ecad648
children 2372284d9457
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
40799
03127e580980 loggingutil: extract openlogfile() and proxylogger to new module
Yuya Nishihara <yuya@tcha.org>
parents: 40798
diff changeset
1 # loggingutil.py - utility for logging events
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
2 #
18676
1506eb487ddd blackbox: fix copyright
Bryan O'Sullivan <bryano@fb.com>
parents: 18675
diff changeset
3 # Copyright 2010 Nicolas Dumazet
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
4 # Copyright 2013 Facebook, Inc.
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
5 #
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
6 # This software may be used and distributed according to the terms of the
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
7 # GNU General Public License version 2 or any later version.
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
8
28090
8113c88b8e6d blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28027
diff changeset
9 from __future__ import absolute_import
8113c88b8e6d blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28027
diff changeset
10
8113c88b8e6d blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28027
diff changeset
11 import errno
8113c88b8e6d blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28027
diff changeset
12
40799
03127e580980 loggingutil: extract openlogfile() and proxylogger to new module
Yuya Nishihara <yuya@tcha.org>
parents: 40798
diff changeset
13 from . import (
38783
e7aa113b14f7 global: use pycompat.xrange()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37120
diff changeset
14 pycompat,
37120
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36607
diff changeset
15 )
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
16
40821
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
17 from .utils import (
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
18 dateutil,
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
19 procutil,
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
20 stringutil,
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
21 )
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
22
40799
03127e580980 loggingutil: extract openlogfile() and proxylogger to new module
Yuya Nishihara <yuya@tcha.org>
parents: 40798
diff changeset
23 def openlogfile(ui, vfs, name, maxfiles=0, maxsize=0):
40800
698477777883 loggingutil: document openlogfile()
Yuya Nishihara <yuya@tcha.org>
parents: 40799
diff changeset
24 """Open log file in append mode, with optional rotation
698477777883 loggingutil: document openlogfile()
Yuya Nishihara <yuya@tcha.org>
parents: 40799
diff changeset
25
698477777883 loggingutil: document openlogfile()
Yuya Nishihara <yuya@tcha.org>
parents: 40799
diff changeset
26 If maxsize > 0, the log file will be rotated up to maxfiles.
698477777883 loggingutil: document openlogfile()
Yuya Nishihara <yuya@tcha.org>
parents: 40799
diff changeset
27 """
34300
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
28 def rotate(oldpath, newpath):
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
29 try:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
30 vfs.unlink(newpath)
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
31 except OSError as err:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
32 if err.errno != errno.ENOENT:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
33 ui.debug("warning: cannot remove '%s': %s\n" %
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
34 (newpath, err.strerror))
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
35 try:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
36 if newpath:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
37 vfs.rename(oldpath, newpath)
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
38 except OSError as err:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
39 if err.errno != errno.ENOENT:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
40 ui.debug("warning: cannot rename '%s' to '%s': %s\n" %
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
41 (newpath, oldpath, err.strerror))
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
42
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
43 if maxsize > 0:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
44 try:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
45 st = vfs.stat(name)
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
46 except OSError:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
47 pass
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
48 else:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
49 if st.st_size >= maxsize:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
50 path = vfs.join(name)
38783
e7aa113b14f7 global: use pycompat.xrange()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37120
diff changeset
51 for i in pycompat.xrange(maxfiles - 1, 1, -1):
34300
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
52 rotate(oldpath='%s.%d' % (path, i - 1),
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
53 newpath='%s.%d' % (path, i))
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
54 rotate(oldpath=path,
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
55 newpath=maxfiles > 0 and path + '.1')
40797
ea2688c84e4b blackbox: just try writing to repo.vfs and update lastlogger on success
Yuya Nishihara <yuya@tcha.org>
parents: 40764
diff changeset
56 return vfs(name, 'a', makeparentdirs=False)
34300
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
57
40821
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
58 def _formatlogline(msg):
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
59 date = dateutil.datestr(format=b'%Y/%m/%d %H:%M:%S')
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
60 pid = procutil.getpid()
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
61 return b'%s (%d)> %s' % (date, pid, msg)
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
62
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
63 def _matchevent(event, tracked):
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
64 return b'*' in tracked or event in tracked
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
65
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
66 class filelogger(object):
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
67 """Basic logger backed by physical file with optional rotation"""
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
68
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
69 def __init__(self, vfs, name, tracked, maxfiles=0, maxsize=0):
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
70 self._vfs = vfs
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
71 self._name = name
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
72 self._trackedevents = set(tracked)
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
73 self._maxfiles = maxfiles
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
74 self._maxsize = maxsize
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
75
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
76 def tracked(self, event):
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
77 return _matchevent(event, self._trackedevents)
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
78
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
79 def log(self, ui, event, msg, opts):
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
80 line = _formatlogline(msg)
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
81 try:
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
82 with openlogfile(ui, self._vfs, self._name,
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
83 maxfiles=self._maxfiles,
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
84 maxsize=self._maxsize) as fp:
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
85 fp.write(line)
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
86 except IOError as err:
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
87 ui.debug(b'cannot write to %s: %s\n'
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
88 % (self._name, stringutil.forcebytestr(err)))
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
89
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
90 class fileobjectlogger(object):
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
91 """Basic logger backed by file-like object"""
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
92
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
93 def __init__(self, fp, tracked):
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
94 self._fp = fp
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
95 self._trackedevents = set(tracked)
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
96
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
97 def tracked(self, event):
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
98 return _matchevent(event, self._trackedevents)
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
99
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
100 def log(self, ui, event, msg, opts):
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
101 line = _formatlogline(msg)
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
102 try:
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
103 self._fp.write(line)
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
104 self._fp.flush()
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
105 except IOError as err:
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
106 ui.debug(b'cannot write to %s: %s\n'
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
107 % (stringutil.forcebytestr(self._fp.name),
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
108 stringutil.forcebytestr(err)))
96be0ecad648 loggingutil: add basic logger backends
Yuya Nishihara <yuya@tcha.org>
parents: 40800
diff changeset
109
40762
37d6ee46a965 blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents: 40760
diff changeset
110 class proxylogger(object):
37d6ee46a965 blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents: 40760
diff changeset
111 """Forward log events to another logger to be set later"""
37d6ee46a965 blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents: 40760
diff changeset
112
37d6ee46a965 blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents: 40760
diff changeset
113 def __init__(self):
37d6ee46a965 blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents: 40760
diff changeset
114 self.logger = None
37d6ee46a965 blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents: 40760
diff changeset
115
37d6ee46a965 blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents: 40760
diff changeset
116 def tracked(self, event):
37d6ee46a965 blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents: 40760
diff changeset
117 return self.logger is not None and self.logger.tracked(event)
37d6ee46a965 blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents: 40760
diff changeset
118
37d6ee46a965 blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents: 40760
diff changeset
119 def log(self, ui, event, msg, opts):
37d6ee46a965 blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents: 40760
diff changeset
120 assert self.logger is not None
37d6ee46a965 blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents: 40760
diff changeset
121 self.logger.log(ui, event, msg, opts)