File: activities.py

package info (click to toggle)
pg-activity 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,144 kB
  • sloc: python: 3,902; sql: 1,067; sh: 5; makefile: 2
file content (351 lines) | stat: -rw-r--r-- 11,955 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
from __future__ import annotations

import builtins
import os
import time
from collections.abc import Sequence
from typing import TypeVar
from warnings import catch_warnings, simplefilter

import attr
import psutil

from .types import (
    BlockingProcess,
    IOCounter,
    LoadAverage,
    LocalRunningProcess,
    MemoryInfo,
    RunningProcess,
    SortKey,
    SwapInfo,
    SystemProcess,
    WaitingProcess,
)


def sys_get_proc(pid: int) -> SystemProcess | None:
    """Return a SystemProcess instance matching given pid or None if access with psutil
    is not possible.
    """
    try:
        psproc = psutil.Process(pid)
        meminfo = psproc.memory_info()
        mem_percent = psproc.memory_percent()
        cpu_percent = psproc.cpu_percent(interval=0)
        cpu_times = psproc.cpu_times()
        io_counters = psproc.io_counters()
        status_iow = str(psproc.status())
    except (psutil.NoSuchProcess, psutil.AccessDenied):
        return None

    return SystemProcess(
        meminfo=meminfo,
        io_read=IOCounter(io_counters.read_count, io_counters.read_bytes),
        io_write=IOCounter(io_counters.write_count, io_counters.write_bytes),
        io_time=time.time(),
        mem_percent=mem_percent,
        cpu_percent=cpu_percent,
        cpu_times=cpu_times,
        read_delta=0,
        write_delta=0,
        io_wait=(status_iow == psutil.STATUS_DISK_SLEEP),
        psutil_proc=psproc,
    )


def ps_complete(
    pg_processes: Sequence[RunningProcess],
    processes: dict[int, SystemProcess],
    fs_blocksize: int,
) -> tuple[list[LocalRunningProcess], IOCounter, IOCounter]:
    """Transform the sequence of 'pg_processes' (RunningProcess) as LocalRunningProcess
    with local system information from the 'processes' map. Return LocalRunningProcess
    list, as well as read and write IO counters.

    The 'processes' map is updated in place.
    """
    local_procs = []
    read_bytes_delta = 0.0
    write_bytes_delta = 0.0
    read_count_delta = 0
    write_count_delta = 0
    n_io_time = time.time()
    for pg_proc in pg_processes:
        pid = pg_proc.pid
        new_proc = sys_get_proc(pid)
        if new_proc is None:
            continue
        try:
            # Getting information from the previous loop
            proc = processes[pid]
        except KeyError:
            # No previous information about this process
            proc = new_proc
        else:
            # Update old process with new information
            mem_percent = proc.mem_percent
            cpu_percent = proc.cpu_percent
            if proc.psutil_proc is not None:
                try:
                    mem_percent = proc.psutil_proc.memory_percent()
                    cpu_percent = proc.psutil_proc.cpu_percent(interval=0)
                except (psutil.NoSuchProcess, psutil.AccessDenied):
                    pass
            proc = attr.evolve(
                proc,
                io_wait=new_proc.io_wait,
                read_delta=(
                    (new_proc.io_read.bytes - proc.io_read.bytes)
                    / (n_io_time - proc.io_time)
                ),
                write_delta=(
                    (new_proc.io_write.bytes - proc.io_write.bytes)
                    / (n_io_time - proc.io_time)
                ),
                io_read=new_proc.io_read,
                io_write=new_proc.io_write,
                io_time=n_io_time,
                mem_percent=mem_percent,
                cpu_percent=cpu_percent,
            )

            # Global io counters
            read_bytes_delta += proc.read_delta
            write_bytes_delta += proc.write_delta

        processes[pid] = proc

        local_procs.append(
            LocalRunningProcess.from_process(
                pg_proc,
                cpu=proc.cpu_percent,
                mem=proc.mem_percent,
                read=proc.read_delta,
                write=proc.write_delta,
                io_wait=proc.io_wait,
            )
        )

    # store io counters
    if read_bytes_delta > 0:
        read_count_delta += int(read_bytes_delta / fs_blocksize)
    if write_bytes_delta > 0:
        write_count_delta += int(write_bytes_delta / fs_blocksize)

    io_read = IOCounter(count=read_count_delta, bytes=int(read_bytes_delta))
    io_write = IOCounter(count=write_count_delta, bytes=int(write_bytes_delta))

    return local_procs, io_read, io_write


T = TypeVar("T", RunningProcess, WaitingProcess, BlockingProcess, LocalRunningProcess)


def sorted(processes: list[T], *, key: SortKey, reverse: bool = False) -> list[T]:
    """Return processes sorted.

    >>> from ipaddress import IPv4Interface, ip_address

    PostgreSQL 13+
    >>> processes = [
    ...     LocalRunningProcess(
    ...         pid="6240",
    ...         xmin="1234",
    ...         application_name="pgbench",
    ...         database="pgbench",
    ...         user="postgres",
    ...         client=ip_address("127.0.0.2"),
    ...         cpu=0.1,
    ...         mem=0.993_254_939_413_836,
    ...         read=0.1,
    ...         write=0.282_725_318_098_656_75,
    ...         state="idle in transaction",
    ...         query="UPDATE pgbench_accounts SET abalance = abalance + 3062 WHERE aid = 1932841;",
    ...         encoding="UTF-8",
    ...         duration=0.1,
    ...         wait="ClientRead",
    ...         io_wait=False,
    ...         query_leader_pid=6240,
    ...         is_parallel_worker=False,
    ...     ),
    ...     LocalRunningProcess(
    ...         pid="6239",
    ...         xmin="2345",
    ...         application_name="pgbench",
    ...         database="pgbench",
    ...         user="postgres",
    ...         client=IPv4Interface("192.0.2.5/24"),
    ...         cpu=0.1,
    ...         mem=0.994_254_939_413_836,
    ...         read=0.1,
    ...         write=0.282_725_318_098_656_75,
    ...         state="idle in transaction",
    ...         query="UPDATE pgbench_accounts SET abalance = abalance + 141 WHERE aid = 7289374;",
    ...         encoding=None,
    ...         duration=0.1,
    ...         wait="ClientRead",
    ...         io_wait=False,
    ...         query_leader_pid=6239,
    ...         is_parallel_worker=False,
    ...     ),
    ...     LocalRunningProcess(
    ...         pid="6228",
    ...         xmin="3456",
    ...         application_name="pgbench",
    ...         database="pgbench",
    ...         user="postgres",
    ...         client=ip_address("2001:db8::"),
    ...         cpu=0.2,
    ...         mem=1.024_758_418_061_11,
    ...         read=0.2,
    ...         write=0.113_090_128_201_154_74,
    ...         state="active",
    ...         query="UPDATE pgbench_accounts SET abalance = abalance + 3062 WHERE aid = 1932841;",
    ...         encoding="UTF-8",
    ...         duration=0.1,
    ...         wait=False,
    ...         io_wait=False,
    ...         query_leader_pid=6240,
    ...         is_parallel_worker=True,
    ...     ),
    ... ]

    >>> processes = sorted(processes, key=SortKey.cpu, reverse=True)
    >>> [p.pid for p in processes]
    ['6228', '6240', '6239']
    >>> processes = sorted(processes, key=SortKey.mem)
    >>> [p.pid for p in processes]
    ['6240', '6239', '6228']

    When using the 'duration' sort key, processes are also sorted by ascending
    (query_leader_pid, is_parallel_worker).
    >>> processes = sorted(processes, key=SortKey.duration, reverse=True)
    >>> [p.pid for p in processes]
    ['6239', '6240', '6228']

    PostgreSQL 12- (query_leader_pid is None)
    >>> processes = [
    ...     LocalRunningProcess(
    ...         pid="6240",
    ...         xmin="1234",
    ...         application_name="pgbench",
    ...         database="pgbench",
    ...         user="postgres",
    ...         client=ip_address("192.168.1.2"),
    ...         cpu=0.1,
    ...         mem=0.993_254_939_413_836,
    ...         read=0.1,
    ...         write=0.282_725_318_098_656_75,
    ...         state="idle in transaction",
    ...         query="UPDATE pgbench_accounts SET abalance = abalance + 3062 WHERE aid = 1932841;",
    ...         encoding=None,
    ...         duration=0.1,
    ...         wait="ClientRead",
    ...         io_wait=False,
    ...         query_leader_pid=None,
    ...         is_parallel_worker=False,
    ...     ),
    ...     LocalRunningProcess(
    ...         pid="6239",
    ...         xmin="2345",
    ...         application_name="pgbench",
    ...         database="pgbench",
    ...         user="postgres",
    ...         client=ip_address("0000:0000:0000:0000:0000:0abc:0007:0def"),
    ...         cpu=0.1,
    ...         mem=0.994_254_939_413_836,
    ...         read=0.1,
    ...         write=0.282_725_318_098_656_75,
    ...         state="idle in transaction",
    ...         query="UPDATE pgbench_accounts SET abalance = abalance + 141 WHERE aid = 7289374;",
    ...         encoding="UTF-8",
    ...         duration=0.1,
    ...         wait="ClientRead",
    ...         io_wait=False,
    ...         query_leader_pid=None,
    ...         is_parallel_worker=False,
    ...     ),
    ...     LocalRunningProcess(
    ...         pid="6228",
    ...         xmin="3456",
    ...         application_name="pgbench",
    ...         database="pgbench",
    ...         user="postgres",
    ...         client=None,
    ...         cpu=0.2,
    ...         mem=1.024_758_418_061_11,
    ...         read=0.2,
    ...         write=0.113_090_128_201_154_74,
    ...         state="active",
    ...         query="UPDATE pgbench_accounts SET abalance = abalance + 3062 WHERE aid = 1932841;",
    ...         encoding="latin1",
    ...         duration=0.1,
    ...         wait=False,
    ...         io_wait=False,
    ...         query_leader_pid=None,
    ...         is_parallel_worker=True,
    ...     ),
    ... ]

    >>> processes = sorted(processes, key=SortKey.duration, reverse=True)
    >>> [p.pid for p in processes]
    ['6240', '6239', '6228']
    """

    # If we filter by duration, we also need to filter by ascending
    # (query_leader_pid, is_parallel_worker):
    # * for pg13+: query_leader_pid = coalesce(leader_pid, pid)
    # * for pg12-: query_leader_pid = Null / None
    # Note: parallel_worker have the same "duration" as their leader.
    if key == SortKey.duration:
        processes = builtins.sorted(
            processes,
            key=lambda p: (p.query_leader_pid, p.is_parallel_worker),
            reverse=False,
        )

    return builtins.sorted(
        processes,
        key=lambda p: getattr(p, key.name) or 0,  # TODO: avoid getattr()
        reverse=reverse,
    )


def update_max_iops(max_iops: int, read_count: float, write_count: float) -> int:
    """Update 'max_iops' value from read_count/write_count.

    >>> update_max_iops(45657, 123, 888)
    45657
    >>> update_max_iops(3, 123, 888)
    1011
    """
    return max(int(read_count + write_count), max_iops)


def get_load_average() -> tuple[float, float, float]:
    """Get load average"""
    return os.getloadavg()


def get_mem_swap() -> tuple[MemoryInfo, SwapInfo]:
    """Get memory and swap usage"""
    with catch_warnings():
        simplefilter("ignore", RuntimeWarning)
        phymem = psutil.virtual_memory()
        vmem = psutil.swap_memory()
    # 'buffers' and 'cached' attributes are not available on some systems (e.g. OSX)
    buffers = getattr(phymem, "buffers", 0)
    cached = getattr(phymem, "cached", 0)
    mem_used = phymem.total - (phymem.free + buffers + cached)
    return (
        MemoryInfo(mem_used, buffers + cached, phymem.free, phymem.total),
        SwapInfo(vmem.used, vmem.free, vmem.total),
    )


def mem_swap_load() -> tuple[MemoryInfo, SwapInfo, LoadAverage]:
    """Read memory, swap and load average from Data object."""
    memory, swap = get_mem_swap()
    load = LoadAverage(*get_load_average())
    return memory, swap, load