File size: 2,336 Bytes
da58840
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
692adfc
da58840
 
 
 
 
 
 
 
 
 
 
692adfc
da58840
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import hashlib
import json
import logging
import os
import time
from contextlib import contextmanager
from typing import Any, Optional

logger = logging.getLogger(__name__)

WORKER = {"pid": None, "calls": 0, "last_end": None}


def session_tag(session_hash: Any) -> str:
    if not session_hash:
        return ""
    return hashlib.sha256(str(session_hash).encode("utf-8")).hexdigest()[:12]


def current_session_tag() -> str:
    try:
        from gradio.context import LocalContext
        return session_tag(getattr(LocalContext.request.get(None), "session_hash", ""))
    except Exception:
        return ""


def process_age_seconds() -> Optional[float]:
    try:
        with open("/proc/self/stat") as stat:
            start_ticks = int(stat.read().rsplit(")", 1)[1].split()[19])
        with open("/proc/uptime") as uptime:
            uptime_seconds = float(uptime.read().split()[0])
        return round(uptime_seconds - start_ticks / os.sysconf("SC_CLK_TCK"), 2)
    except Exception:
        return None


def emit(kind: str, fields: dict) -> None:
    try:
        logger.info("%s %s", kind, json.dumps(fields, separators=(",", ":")))
    except Exception:
        pass


def labs_event(event: str, session: str, **extra) -> None:
    emit("LABS_EVENT", {"event": event, "session": session, "t": round(time.time(), 2), **extra})


@contextmanager
def gpu_call(fn: str, session: str, texture_size: Any, **extra):
    started = time.time()
    record = {}
    try:
        pid = os.getpid()
        if WORKER["pid"] != pid:
            WORKER.update(pid=pid, calls=0, last_end=None)
        WORKER["calls"] += 1
        last_end = WORKER["last_end"]
        record = {
            "fn": fn, "pid": pid, "calls_in_worker": WORKER["calls"],
            "since_prev_call_s": None if last_end is None else round(started - last_end, 2),
            "since_worker_start_s": process_age_seconds(), "texture_size": texture_size, "session": session, **extra,
        }
    except Exception:
        pass
    ok = False
    try:
        yield
        ok = True
    finally:
        try:
            WORKER["last_end"] = time.time()
            emit("GPU_CALL", {**record, "in_gpu_s": round(WORKER["last_end"] - started, 2), "ok": ok,
                              "t": round(started, 2)})
        except Exception:
            pass