|
| 1 | +# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 2 | +# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 | +# |
| 4 | +# This program is free software: you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU General Public License as published by |
| 6 | +# the Free Software Foundation, either version 3 of the License, or |
| 7 | +# (at your option) any later version. |
| 8 | +# |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +"""Profile Cylc with tracemalloc. |
| 18 | +
|
| 19 | +This takes tracemalloc snapshots periodically. |
| 20 | +
|
| 21 | +Snapshots are written into "~/cylc-run/<workflow>/tracemalloc/", to load them |
| 22 | +for analysis, run: |
| 23 | +
|
| 24 | + tracemalloc.Snapshot.load('.../path/to/x.tracemalloc') |
| 25 | +
|
| 26 | +The memory diffs are written to stdout. |
| 27 | +""" |
| 28 | + |
| 29 | +from pathlib import Path |
| 30 | +import tracemalloc |
| 31 | + |
| 32 | +from cylc.flow import LOG |
| 33 | +from cylc.flow.main_loop import periodic, shutdown, startup |
| 34 | + |
| 35 | + |
| 36 | +@startup |
| 37 | +async def init(scheduler, state): |
| 38 | + """Create the state object on startup.""" |
| 39 | + tracemalloc.start() |
| 40 | + state['out_dir'] = Path(scheduler.workflow_run_dir, 'tracemalloc') |
| 41 | + state['out_dir'].mkdir() |
| 42 | + logfile = state['out_dir'] / 'log' |
| 43 | + state['log'] = logfile.open('w+') |
| 44 | + state['itt'] = 0 |
| 45 | + LOG.warning(f'Writing tracemalloc output to {logfile}') |
| 46 | + |
| 47 | + |
| 48 | +@periodic |
| 49 | +async def take_snapshot(scheduler, state, diff_filter='cylc/', max_length=20): |
| 50 | + """Take a memory snapshot and compare it to the previous one. |
| 51 | +
|
| 52 | + Args: |
| 53 | + scheduler: |
| 54 | + Unused in this plugin. |
| 55 | + state: |
| 56 | + The state object initialised in "init". |
| 57 | + diff_filter: |
| 58 | + If supplied, only changes containing this string will be displayed. |
| 59 | + Used to restrict reporting to items which contain Cylc file paths. |
| 60 | + max_length: |
| 61 | + The top "max_length" items will be displayed with each summary. |
| 62 | +
|
| 63 | + """ |
| 64 | + # take a snapshot |
| 65 | + new = tracemalloc.take_snapshot() |
| 66 | + |
| 67 | + # dump the snapshot to the filesystem |
| 68 | + new.dump(state['out_dir'] / f'{state["itt"]}.tracemalloc') |
| 69 | + |
| 70 | + # compare this snapshot to the previous one |
| 71 | + if state.get('prev'): |
| 72 | + # generate a list of the things which have changed |
| 73 | + cmp = [ |
| 74 | + item |
| 75 | + for item in new.compare_to(state['prev'], 'lineno') |
| 76 | + # filter for the libraries we are interested in |
| 77 | + if not diff_filter or diff_filter in str(item) |
| 78 | + ] |
| 79 | + |
| 80 | + # print a summary of the memory change |
| 81 | + print('+/-', sum(stat.size_diff for stat in cmp), file=state['log']) |
| 82 | + |
| 83 | + # report the individual changes |
| 84 | + for stat in sorted(cmp, key=lambda x: x.size_diff, reverse=True)[ |
| 85 | + :max_length |
| 86 | + ]: |
| 87 | + if stat.size_diff != 0: |
| 88 | + print(f' {stat}', file=state['log']) |
| 89 | + print('', file=state['log']) |
| 90 | + |
| 91 | + state['prev'] = new |
| 92 | + state['itt'] += 1 |
| 93 | + state['log'].flush() |
| 94 | + |
| 95 | + |
| 96 | +@shutdown |
| 97 | +async def close_log(scheduler, state): |
| 98 | + """Close the log file on shutdown.""" |
| 99 | + state['log'].close() |
0 commit comments