upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/scripts/summarize-frame-log.py
blob: 95af1c584c3ee3dc1c68f77f81c0afc1e9265865 (plain)
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
"""Summarize Darner diagnostic frame samples without printing raw log lines."""

import argparse
from bisect import bisect_left
from datetime import datetime
import json
import re
from statistics import median

FRAME = re.compile(
    r"\b(\d+(?:\.\d+)?) fps, (\d+(?:\.\d+)?) ms/frame"
    r" \((\d+(?:\.\d+)?) preparing:.*?\),\s*(\d+) draws/frame,"
    r" dropped \d+ (?:without a host pipeline|draws or dispatches) \((\d+) being made\)"
)
ROOM = re.compile(
    r"the texture queue's room:.*?\+ (\d+) MB kept for render targets"
    r" = \d+ MB against a line of \d+ MB.*?: (.+)"
)
UPLOADS = re.compile(r"texture uploads: (\d+) recorded, (\d+) left out")
TIMESTAMP = re.compile(r"^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(?:\.\d+)?Z")
MAX_OFFSET_SECONDS = 0.5


def read_samples(lines):
    frames, budgets, uploads = [], [], []
    for line in lines:
        match = FRAME.search(line)
        budget = ROOM.search(line)
        upload = UPLOADS.search(line) if "frame account per frame:" in line else None
        if not (match or budget or upload):
            continue
        stamp = TIMESTAMP.match(line)
        if not stamp:
            continue
        try:
            time = datetime.fromisoformat(stamp[0].replace("Z", "+00:00")).timestamp()
        except ValueError:
            continue
        if match:
            fps, milliseconds, preparing, draws, building = match.groups()
            frames.append((time, float(fps), float(milliseconds), float(preparing),
                           int(draws), int(building)))
        if budget:
            budgets.append((time, (int(budget[1]), budget[2].startswith("no room"))))
        if upload:
            uploads.append((time, tuple(map(int, upload.groups()))))
    return frames, budgets, uploads


def correlate(frames, records):
    """Return at most one diagnostic record per frame window."""
    records = sorted(records)
    stamps = [record[0] for record in records]
    matched = []
    for frame in frames:
        time = frame[0]
        index = bisect_left(stamps, time)
        nearest = min(records[max(0, index - 1):index + 1],
                      key=lambda record: abs(record[0] - time), default=None)
        if nearest is not None and abs(nearest[0] - time) <= MAX_OFFSET_SECONDS:
            matched.append(nearest[1])
    return matched


def summarize(lines, min_draws=1000, slow_ms=200, start=0, end=None):
    frames, budgets, uploads = read_samples(lines)
    if not frames:
        return {"samples": 0}
    origin = frames[0][0]
    world = [frame for frame in frames
             if frame[4] >= min_draws and frame[0] - origin >= start
             and (end is None or frame[0] - origin < end)]
    if not world:
        return {"samples": 0}
    slow = [frame for frame in world if frame[2] >= slow_ms]
    room = correlate(world, budgets)
    slow_room = correlate(slow, budgets)
    slow_uploads = correlate(slow, uploads)

    return {
        "samples": len(world),
        "median_fps": round(median(frame[1] for frame in world), 2),
        "median_frame_ms": round(median(frame[2] for frame in world), 2),
        "median_prepare_ms": round(median(frame[3] for frame in world), 2),
        "slow_threshold_ms": slow_ms,
        "slow_samples": len(slow),
        "slow_samples_with_zero_pipelines_building": sum(frame[5] == 0 for frame in slow),
        "budget_samples": len(room),
        "median_target_reserve_mb": median(reserved for reserved, _ in room) if room else None,
        "slow_samples_with_budget": len(slow_room),
        "slow_samples_with_texture_queue_blocked": sum(blocked for _, blocked in slow_room),
        "slow_samples_with_upload_counts": len(slow_uploads),
        "median_slow_uploads_left_out": median(skipped for _, skipped in slow_uploads)
        if slow_uploads else None,
    }


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("log")
    parser.add_argument("--min-draws", type=int, default=1000)
    parser.add_argument("--slow-ms", type=float, default=200)
    parser.add_argument("--start", type=float, default=0, help="seconds after the first FPS sample")
    parser.add_argument("--end", type=float, help="exclusive seconds after the first FPS sample")
    args = parser.parse_args()
    if args.min_draws < 0 or args.slow_ms <= 0 or args.start < 0:
        parser.error("min-draws and start must be nonnegative, and slow-ms positive")
    if args.end is not None and args.end <= args.start:
        parser.error("end must be greater than start")
    try:
        with open(args.log, encoding="utf-8-sig", errors="replace") as log:
            report = summarize(log, args.min_draws, args.slow_ms, args.start, args.end)
    except OSError:
        parser.exit(1, "Could not read the log.\n")
    print(json.dumps(report, indent=2))


if __name__ == "__main__":
    main()