Debian Codegraph @

cylc/flow/main_loop/log_main_loop.py

raw
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 """Main loop plugin for monitoring main loop plugins.
17
18 .. note::
19
20 This plugin is for Cylc developers debugging main loop operations.
21
22 If ``matplotlib`` is installed this plugin will plot results as a PDF in
23 the run directory when the workflow is shut down (cleanly).
24
25 """
26 from collections import deque
27 import json
28 from pathlib import Path
29
30 from cylc.flow.main_loop import startup, shutdown
31
32 try:
33 import matplotlib
34 matplotlib.use('Agg')
35 from matplotlib import pyplot as plt
36 PLT = True
37 except ModuleNotFoundError:
38 PLT = False
39
40
41 @startup
42 async def init(scheduler, _):
43 """Override default queue length of 1.
44
45 This allows timings to accumulate, normally only the most recent is kept.
46 """
47 plugins = scheduler.main_loop_plugins
48 for plugin in plugins['timings']:
49 plugins['timings'][plugin] = deque()
50
51
52 @shutdown
53 async def report(scheduler, _):
54 """Extract plugin function timings."""
55 data = scheduler.main_loop_plugins['timings']
56 if data:
57 data = _normalise(data)
58 _dump(data, scheduler.workflow_run_dir)
59 _plot(data, scheduler.workflow_run_dir)
60
61
62 def _normalise(data):
63 earliest_time = min((
64 start_time
65 for _, timings in data.items()
66 for start_time, duration in timings
67 ))
68 return {
69 plugin_name: [
70 (start_time - earliest_time, duration)
71 for start_time, duration in timings
72 ]
73 for (plugin_name, _), timings in data.items()
74 }
75
76
77 def _dump(data, path):
78 json.dump(
79 data,
80 Path(path, f'{__name__}.json').open('w+'),
81 indent=4
82 )
83 return True
84
85
86 def _plot(data, path):
87 if not PLT:
88 return False
89
90 _, ax1 = plt.subplots(figsize=(10, 7.5))
91 ax1.set_xlabel('Workflow Run Time (s)')
92 ax1.set_ylabel('XTrigger Run Time (s)')
93
94 for plugin_name, (timings) in data.items():
95 x_data = []
96 y_data = []
97 for start_time, duration in timings:
98 x_data.append(start_time)
99 y_data.append(duration)
100 ax1.scatter(x_data, y_data, label=plugin_name)
101
102 ax1.set_xlim(0, ax1.get_xlim()[1])
103 ax1.set_ylim(0, ax1.get_ylim()[1])
104
105 ax1.legend()
106 plt.savefig(
107 Path(path, f'{__name__}.pdf')
108 )
109 return True