|
1 | 1 | # container_engines.py - container engine types and selection funcs |
2 | 2 |
|
3 | 3 | import os |
| 4 | +import logging |
4 | 5 |
|
5 | 6 | from typing import Tuple, List, Optional, Dict |
6 | 7 |
|
7 | | -from .call_wrappers import call_throws, CallVerbosity |
| 8 | +from .call_wrappers import call_throws, call, CallVerbosity |
8 | 9 | from .context import CephadmContext |
9 | 10 | from .container_engine_base import ContainerEngine |
10 | 11 | from .constants import ( |
|
13 | 14 | MIN_PODMAN_VERSION, |
14 | 15 | PIDS_LIMIT_UNLIMITED_PODMAN_VERSION, |
15 | 16 | ) |
| 17 | +from .data_utils import with_units_to_int |
16 | 18 | from .exceptions import Error |
17 | 19 |
|
18 | 20 |
|
| 21 | +logger = logging.getLogger() |
| 22 | + |
| 23 | + |
19 | 24 | class Podman(ContainerEngine): |
20 | 25 | EXE = 'podman' |
21 | 26 |
|
@@ -189,3 +194,57 @@ def pull_command( |
189 | 194 | if os.path.exists('/etc/ceph/podman-auth.json'): |
190 | 195 | cmd.append('--authfile=/etc/ceph/podman-auth.json') |
191 | 196 | return cmd |
| 197 | + |
| 198 | + |
| 199 | +def _container_mem_usage( |
| 200 | + ctx: CephadmContext, |
| 201 | + *, |
| 202 | + container_path: str = '', |
| 203 | + verbosity: CallVerbosity = CallVerbosity.QUIET, |
| 204 | +) -> Tuple[str, str, int]: |
| 205 | + container_path = container_path or ctx.container_engine.path |
| 206 | + out, err, code = call( |
| 207 | + ctx, |
| 208 | + [ |
| 209 | + container_path, |
| 210 | + 'stats', |
| 211 | + '--format', |
| 212 | + '{{.ID}},{{.MemUsage}}', |
| 213 | + '--no-stream', |
| 214 | + ], |
| 215 | + verbosity=verbosity, |
| 216 | + ) |
| 217 | + return out, err, code |
| 218 | + |
| 219 | + |
| 220 | +def _parse_mem_usage(code: int, out: str) -> Tuple[int, Dict[str, int]]: |
| 221 | + # keep track of memory usage we've seen |
| 222 | + seen_memusage = {} # type: Dict[str, int] |
| 223 | + seen_memusage_cid_len = 0 |
| 224 | + if not code: |
| 225 | + for line in out.splitlines(): |
| 226 | + (cid, usage) = line.split(',') |
| 227 | + (used, limit) = usage.split(' / ') |
| 228 | + try: |
| 229 | + seen_memusage[cid] = with_units_to_int(used) |
| 230 | + if not seen_memusage_cid_len: |
| 231 | + seen_memusage_cid_len = len(cid) |
| 232 | + except ValueError: |
| 233 | + logger.info( |
| 234 | + 'unable to parse memory usage line\n>{}'.format(line) |
| 235 | + ) |
| 236 | + pass |
| 237 | + return seen_memusage_cid_len, seen_memusage |
| 238 | + |
| 239 | + |
| 240 | +def parsed_container_mem_usage( |
| 241 | + ctx: CephadmContext, |
| 242 | + *, |
| 243 | + container_path: str = '', |
| 244 | + verbosity: CallVerbosity = CallVerbosity.QUIET, |
| 245 | +) -> Tuple[int, Dict[str, int]]: |
| 246 | + """Return memory useage values parsed from the container engine's container status.""" |
| 247 | + out, _, code = _container_mem_usage( |
| 248 | + ctx, container_path=container_path, verbosity=verbosity |
| 249 | + ) |
| 250 | + return _parse_mem_usage(code, out) |
0 commit comments