|
| 1 | +# Copyright 2016-2022 Swiss National Supercomputing Centre (CSCS/ETH Zurich) |
| 2 | +# ReFrame Project Developers. See the top-level LICENSE file for details. |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: BSD-3-Clause |
| 5 | + |
| 6 | +# A lightweight time profiler |
| 7 | + |
| 8 | +import time |
| 9 | +import sys |
| 10 | + |
| 11 | +from collections import OrderedDict |
| 12 | + |
| 13 | + |
| 14 | +class ProfilerError(Exception): |
| 15 | + pass |
| 16 | + |
| 17 | + |
| 18 | +class time_region: |
| 19 | + '''Context manager for timing a code region''' |
| 20 | + |
| 21 | + def __init__(self, region, profiler=None): |
| 22 | + self._profiler = profiler or TimeProfiler() |
| 23 | + self._region = region |
| 24 | + |
| 25 | + def __enter__(self): |
| 26 | + self._profiler.enter_region(self._region) |
| 27 | + return self._profiler |
| 28 | + |
| 29 | + def __exit__(self, exc_type, exc_val, exc_tb): |
| 30 | + self._profiler.exit_region() |
| 31 | + |
| 32 | + |
| 33 | +class TimeProfiler: |
| 34 | + def __init__(self): |
| 35 | + self._region_stack = ['root'] |
| 36 | + if sys.version_info[:2] < (3, 8): |
| 37 | + self._region_times = OrderedDict() |
| 38 | + else: |
| 39 | + self._region_times = {} |
| 40 | + |
| 41 | + @property |
| 42 | + def current_region(self): |
| 43 | + return self._region_stack[-1] |
| 44 | + |
| 45 | + def enter_region(self, region_name): |
| 46 | + timestamp = time.time() |
| 47 | + region_fullname = f'{self.current_region}:{region_name}' |
| 48 | + if region_fullname in self._region_times: |
| 49 | + elapsed = self._region_times[region_fullname][1] |
| 50 | + else: |
| 51 | + elapsed = 0.0 |
| 52 | + |
| 53 | + self._region_times[region_fullname] = (timestamp, elapsed) |
| 54 | + self._region_stack.append(region_fullname) |
| 55 | + |
| 56 | + def exit_region(self): |
| 57 | + timestamp = time.time() |
| 58 | + region = self.current_region |
| 59 | + t_start, elapsed = self._region_times[region] |
| 60 | + self._region_times[region] = (None, elapsed + timestamp - t_start) |
| 61 | + self._region_stack.pop() |
| 62 | + |
| 63 | + def total_time(self, region_name): |
| 64 | + for region in reversed(self._region_times.keys()): |
| 65 | + if (region == region_name or |
| 66 | + region.rsplit(':', maxsplit=1)[-1] == region_name): |
| 67 | + timestamp, elapsed = self._region_times[region] |
| 68 | + if timestamp: |
| 69 | + raise ProfilerError( |
| 70 | + f'region {region_name!r} has not exited' |
| 71 | + ) |
| 72 | + |
| 73 | + return elapsed |
| 74 | + |
| 75 | + raise ProfilerError(f'unknown region: {region_name!r}') |
| 76 | + |
| 77 | + def time_region(self, region): |
| 78 | + return globals()['time_region'](region, self) |
| 79 | + |
| 80 | + def print_report(self, print_fn=None): |
| 81 | + if print_fn is None: |
| 82 | + print_fn = print |
| 83 | + |
| 84 | + print_fn('>>> profiler report [start] <<<') |
| 85 | + for name, t_info in self._region_times.items(): |
| 86 | + # Remove the root prefix |
| 87 | + levels = name.count(':') |
| 88 | + indent = ' '*4*(levels - 1) |
| 89 | + region_name = name.rsplit(':', maxsplit=1)[-1] |
| 90 | + msg = f'{indent}{region_name}: {t_info[1]:.6f} s' |
| 91 | + if t_info[0]: |
| 92 | + msg += ' <incomplete>' |
| 93 | + |
| 94 | + print_fn(msg) |
| 95 | + |
| 96 | + print_fn('>>> profiler report [ end ] <<<') |
0 commit comments