##// END OF EJS Templates
tracing: add support for emitting counters...
Augie Fackler -
r42677:e658ac39 default
parent child Browse files
Show More
@@ -1,48 +1,58 b''
1 # Support code for event tracing in Mercurial. Lives in demandimport
1 # Support code for event tracing in Mercurial. Lives in demandimport
2 # so it can also be used in demandimport.
2 # so it can also be used in demandimport.
3 #
3 #
4 # Copyright 2018 Google LLC.
4 # Copyright 2018 Google LLC.
5 #
5 #
6 # This software may be used and distributed according to the terms of the
6 # This software may be used and distributed according to the terms of the
7 # GNU General Public License version 2 or any later version.
7 # GNU General Public License version 2 or any later version.
8 from __future__ import absolute_import
8 from __future__ import absolute_import
9
9
10 import contextlib
10 import contextlib
11 import os
11 import os
12
12
13 _pipe = None
13 _pipe = None
14 _checked = False
14 _checked = False
15
15
16 def _isactive():
16 def _isactive():
17 global _pipe, _session, _checked
17 global _pipe, _session, _checked
18 if _pipe is None:
18 if _pipe is None:
19 if _checked:
19 if _checked:
20 return False
20 return False
21 _checked = True
21 _checked = True
22 if 'HGCATAPULTSERVERPIPE' not in os.environ:
22 if 'HGCATAPULTSERVERPIPE' not in os.environ:
23 return False
23 return False
24 _pipe = open(os.environ['HGCATAPULTSERVERPIPE'], 'w', 1)
24 _pipe = open(os.environ['HGCATAPULTSERVERPIPE'], 'w', 1)
25 _session = os.environ.get('HGCATAPULTSESSION', 'none')
25 _session = os.environ.get('HGCATAPULTSESSION', 'none')
26 return True
26 return True
27
27
28 @contextlib.contextmanager
28 @contextlib.contextmanager
29 def log(whencefmt, *whenceargs):
29 def log(whencefmt, *whenceargs):
30 if not _isactive():
30 if not _isactive():
31 yield
31 yield
32 return
32 return
33 whence = whencefmt % whenceargs
33 whence = whencefmt % whenceargs
34 try:
34 try:
35 # Both writes to the pipe are wrapped in try/except to ignore
35 # Both writes to the pipe are wrapped in try/except to ignore
36 # errors, as we can see mysterious errors in here if the pager
36 # errors, as we can see mysterious errors in here if the pager
37 # is active. Presumably other conditions could trigger
37 # is active. Presumably other conditions could trigger
38 # problems too.
38 # problems too.
39 try:
39 try:
40 _pipe.write('START %s %s\n' % (_session, whence))
40 _pipe.write('START %s %s\n' % (_session, whence))
41 except IOError:
41 except IOError:
42 pass
42 pass
43 yield
43 yield
44 finally:
44 finally:
45 try:
45 try:
46 _pipe.write('END %s %s\n' % (_session, whence))
46 _pipe.write('END %s %s\n' % (_session, whence))
47 except IOError:
47 except IOError:
48 pass
48 pass
49
50 def counter(label, amount, *labelargs):
51 if not _isactive():
52 return
53 l = label % labelargs
54 # See above in log() for why this is in a try/except.
55 try:
56 _pipe.write('COUNTER %s %d %s\n' % (_session, amount, l))
57 except IOError:
58 pass
General Comments 0
You need to be logged in to leave comments. Login now