Debian Codegraph @

ansible_collections/community/general/plugins/callback/cgroup_memory_recap.py

raw
1 # Copyright (c) 2018 Matt Martz <[email protected]>
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 # Make coding more python3-ish
6 from __future__ import annotations
7
8 DOCUMENTATION = r"""
9 author: Unknown (!UNKNOWN)
10 name: cgroup_memory_recap
11 type: aggregate
12 requirements:
13 - whitelist in configuration
14 - cgroups
15 short_description: Profiles maximum memory usage of tasks and full execution using cgroups
16 description:
17 - This is an Ansible callback plugin that profiles maximum memory usage of Ansible and individual tasks, and displays a
18 recap at the end using cgroups.
19 notes:
20 - Requires ansible to be run from within a C(cgroup), such as with C(cgexec -g memory:ansible_profile ansible-playbook ...).
21 - This C(cgroup) should only be used by Ansible to get accurate results.
22 - To create the C(cgroup), first use a command such as C(sudo cgcreate -a ec2-user:ec2-user -t ec2-user:ec2-user -g memory:ansible_profile).
23 options:
24 max_mem_file:
25 required: true
26 description: Path to cgroups C(memory.max_usage_in_bytes) file. Example V(/sys/fs/cgroup/memory/ansible_profile/memory.max_usage_in_bytes).
27 type: str
28 env:
29 - name: CGROUP_MAX_MEM_FILE
30 ini:
31 - section: callback_cgroupmemrecap
32 key: max_mem_file
33 cur_mem_file:
34 required: true
35 description: Path to C(memory.usage_in_bytes) file. Example V(/sys/fs/cgroup/memory/ansible_profile/memory.usage_in_bytes).
36 type: str
37 env:
38 - name: CGROUP_CUR_MEM_FILE
39 ini:
40 - section: callback_cgroupmemrecap
41 key: cur_mem_file
42 """
43
44 import threading
45 import time
46
47 from ansible.plugins.callback import CallbackBase
48
49
50 class MemProf(threading.Thread):
51 """Python thread for recording memory usage"""
52
53 def __init__(self, path, obj=None):
54 threading.Thread.__init__(self)
55 self.obj = obj
56 self.path = path
57 self.results = []
58 self.running = True
59
60 def run(self):
61 while self.running:
62 with open(self.path) as f:
63 val = f.read()
64 self.results.append(int(val.strip()) / 1024 / 1024)
65 time.sleep(0.001)
66
67
68 class CallbackModule(CallbackBase):
69 CALLBACK_VERSION = 2.0
70 CALLBACK_TYPE = "aggregate"
71 CALLBACK_NAME = "community.general.cgroup_memory_recap"
72 CALLBACK_NEEDS_WHITELIST = True
73
74 def __init__(self, display=None):
75 super().__init__(display)
76
77 self._task_memprof = None
78
79 self.task_results = []
80
81 def set_options(self, task_keys=None, var_options=None, direct=None):
82 super().set_options(task_keys=task_keys, var_options=var_options, direct=direct)
83
84 self.cgroup_max_file = self.get_option("max_mem_file")
85 self.cgroup_current_file = self.get_option("cur_mem_file")
86
87 with open(self.cgroup_max_file, "w+") as f:
88 f.write("0")
89
90 def _profile_memory(self, obj=None):
91 prev_task = None
92 results = None
93 try:
94 self._task_memprof.running = False
95 results = self._task_memprof.results
96 prev_task = self._task_memprof.obj
97 except AttributeError:
98 pass
99
100 if obj is not None:
101 self._task_memprof = MemProf(self.cgroup_current_file, obj=obj)
102 self._task_memprof.start()
103
104 if results is not None:
105 self.task_results.append((prev_task, max(results)))
106
107 def v2_playbook_on_task_start(self, task, is_conditional):
108 self._profile_memory(task)
109
110 def v2_playbook_on_stats(self, stats):
111 self._profile_memory()
112
113 with open(self.cgroup_max_file) as f:
114 max_results = int(f.read().strip()) / 1024 / 1024
115
116 self._display.banner("CGROUP MEMORY RECAP")
117 self._display.display(f"Execution Maximum: {max_results:0.2f}MB\n\n")
118
119 for task, memory in self.task_results:
120 self._display.display(f"{task.get_name()} ({task._uuid}): {memory:0.2f}MB")