Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 1 | # Copyright (C) 2008 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Mike Frysinger | 8a11f6f | 2019-08-27 00:26:15 -0400 | [diff] [blame] | 15 | """Logic for tracing repo interactions. |
| 16 | |
| 17 | Activated via `repo --trace ...` or `REPO_TRACE=1 repo ...`. |
Joanna Wang | a6c52f5 | 2022-11-03 16:51:19 -0400 | [diff] [blame] | 18 | |
| 19 | Temporary: Tracing is always on. Set `REPO_TRACE=0` to turn off. |
| 20 | To also include trace outputs in stderr do `repo --trace_to_stderr ...` |
Mike Frysinger | 8a11f6f | 2019-08-27 00:26:15 -0400 | [diff] [blame] | 21 | """ |
| 22 | |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 23 | import sys |
| 24 | import os |
Joanna Wang | a6c52f5 | 2022-11-03 16:51:19 -0400 | [diff] [blame] | 25 | import time |
| 26 | from contextlib import ContextDecorator |
Mike Frysinger | 8a11f6f | 2019-08-27 00:26:15 -0400 | [diff] [blame] | 27 | |
Joanna Wang | 24c6314 | 2022-11-08 18:56:52 -0500 | [diff] [blame] | 28 | import platform_utils |
| 29 | |
Mike Frysinger | 8a11f6f | 2019-08-27 00:26:15 -0400 | [diff] [blame] | 30 | # Env var to implicitly turn on tracing. |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 31 | REPO_TRACE = 'REPO_TRACE' |
| 32 | |
Joanna Wang | a6c52f5 | 2022-11-03 16:51:19 -0400 | [diff] [blame] | 33 | # Temporarily set tracing to always on unless user expicitly sets to 0. |
| 34 | _TRACE = os.environ.get(REPO_TRACE) != '0' |
Joanna Wang | a6c52f5 | 2022-11-03 16:51:19 -0400 | [diff] [blame] | 35 | _TRACE_TO_STDERR = False |
Joanna Wang | a6c52f5 | 2022-11-03 16:51:19 -0400 | [diff] [blame] | 36 | _TRACE_FILE = None |
Joanna Wang | a6c52f5 | 2022-11-03 16:51:19 -0400 | [diff] [blame] | 37 | _TRACE_FILE_NAME = 'TRACE_FILE' |
Joanna Wang | 24c6314 | 2022-11-08 18:56:52 -0500 | [diff] [blame] | 38 | _MAX_SIZE = 70 # in mb |
Joanna Wang | a6c52f5 | 2022-11-03 16:51:19 -0400 | [diff] [blame] | 39 | _NEW_COMMAND_SEP = '+++++++++++++++NEW COMMAND+++++++++++++++++++' |
| 40 | |
| 41 | |
LaMont Jones | 47020ba | 2022-11-10 00:11:51 +0000 | [diff] [blame^] | 42 | def IsTraceToStderr(): |
Joanna Wang | a6c52f5 | 2022-11-03 16:51:19 -0400 | [diff] [blame] | 43 | return _TRACE_TO_STDERR |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 44 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 45 | |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 46 | def IsTrace(): |
| 47 | return _TRACE |
| 48 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 49 | |
Joanna Wang | a6c52f5 | 2022-11-03 16:51:19 -0400 | [diff] [blame] | 50 | def SetTraceToStderr(): |
| 51 | global _TRACE_TO_STDERR |
| 52 | _TRACE_TO_STDERR = True |
| 53 | |
| 54 | |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 55 | def SetTrace(): |
| 56 | global _TRACE |
| 57 | _TRACE = True |
| 58 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 59 | |
Joanna Wang | a6c52f5 | 2022-11-03 16:51:19 -0400 | [diff] [blame] | 60 | def _SetTraceFile(): |
| 61 | global _TRACE_FILE |
| 62 | _TRACE_FILE = _GetTraceFile() |
| 63 | |
| 64 | |
| 65 | class Trace(ContextDecorator): |
| 66 | |
| 67 | def _time(self): |
| 68 | """Generate nanoseconds of time in a py3.6 safe way""" |
| 69 | return int(time.time()*1e+9) |
| 70 | |
| 71 | def __init__(self, fmt, *args, first_trace=False): |
| 72 | if not IsTrace(): |
| 73 | return |
| 74 | self._trace_msg = fmt % args |
| 75 | |
| 76 | if not _TRACE_FILE: |
| 77 | _SetTraceFile() |
| 78 | |
| 79 | if first_trace: |
| 80 | _ClearOldTraces() |
| 81 | self._trace_msg = '%s %s' % (_NEW_COMMAND_SEP, self._trace_msg) |
| 82 | |
| 83 | |
| 84 | def __enter__(self): |
| 85 | if not IsTrace(): |
| 86 | return self |
| 87 | |
LaMont Jones | 47020ba | 2022-11-10 00:11:51 +0000 | [diff] [blame^] | 88 | print_msg = f'PID: {os.getpid()} START: {self._time()} :' + self._trace_msg + '\n' |
Joanna Wang | a6c52f5 | 2022-11-03 16:51:19 -0400 | [diff] [blame] | 89 | |
| 90 | with open(_TRACE_FILE, 'a') as f: |
| 91 | print(print_msg, file=f) |
| 92 | |
| 93 | if _TRACE_TO_STDERR: |
| 94 | print(print_msg, file=sys.stderr) |
| 95 | |
| 96 | return self |
| 97 | |
| 98 | def __exit__(self, *exc): |
| 99 | if not IsTrace(): |
| 100 | return False |
| 101 | |
LaMont Jones | 47020ba | 2022-11-10 00:11:51 +0000 | [diff] [blame^] | 102 | print_msg = f'PID: {os.getpid()} END: {self._time()} :' + self._trace_msg + '\n' |
Joanna Wang | a6c52f5 | 2022-11-03 16:51:19 -0400 | [diff] [blame] | 103 | |
| 104 | with open(_TRACE_FILE, 'a') as f: |
| 105 | print(print_msg, file=f) |
| 106 | |
| 107 | if _TRACE_TO_STDERR: |
| 108 | print(print_msg, file=sys.stderr) |
| 109 | |
| 110 | return False |
| 111 | |
| 112 | |
| 113 | def _GetTraceFile(): |
| 114 | """Get the trace file or create one.""" |
| 115 | # TODO: refactor to pass repodir to Trace. |
| 116 | repo_dir = os.path.dirname(os.path.dirname(__file__)) |
| 117 | trace_file = os.path.join(repo_dir, _TRACE_FILE_NAME) |
LaMont Jones | a3ff64c | 2022-11-07 23:19:14 +0000 | [diff] [blame] | 118 | print('Trace outputs in %s' % trace_file, file=sys.stderr) |
Joanna Wang | a6c52f5 | 2022-11-03 16:51:19 -0400 | [diff] [blame] | 119 | return trace_file |
| 120 | |
| 121 | def _ClearOldTraces(): |
Joanna Wang | 24c6314 | 2022-11-08 18:56:52 -0500 | [diff] [blame] | 122 | """Clear the oldest commands if trace file is too big. |
Joanna Wang | a6c52f5 | 2022-11-03 16:51:19 -0400 | [diff] [blame] | 123 | |
| 124 | Note: If the trace file contains output from two `repo` |
| 125 | commands that were running at the same time, this |
| 126 | will not work precisely. |
| 127 | """ |
| 128 | if os.path.isfile(_TRACE_FILE): |
| 129 | while os.path.getsize(_TRACE_FILE)/(1024*1024) > _MAX_SIZE: |
Joanna Wang | 24c6314 | 2022-11-08 18:56:52 -0500 | [diff] [blame] | 130 | temp_file = _TRACE_FILE + '.tmp' |
Joanna Wang | a6c52f5 | 2022-11-03 16:51:19 -0400 | [diff] [blame] | 131 | with open(_TRACE_FILE, 'r', errors='ignore') as fin: |
Joanna Wang | 24c6314 | 2022-11-08 18:56:52 -0500 | [diff] [blame] | 132 | with open(temp_file, 'w') as tf: |
| 133 | trace_lines = fin.readlines() |
| 134 | for i , l in enumerate(trace_lines): |
| 135 | if 'END:' in l and _NEW_COMMAND_SEP in l: |
| 136 | tf.writelines(trace_lines[i+1:]) |
| 137 | break |
| 138 | platform_utils.rename(temp_file, _TRACE_FILE) |