File size: 1,704 Bytes
44a2bac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import logging
import os
import platform


def setup_logger(name: str = "hf_demos", level: str | None = None) -> logging.Logger:
    logger = logging.getLogger(name)
    if logger.handlers:
        return logger  # already configured

    lvl = (level or os.environ.get("LOG_LEVEL", "INFO")).upper()
    logger.setLevel(getattr(logging, lvl, logging.INFO))

    ch = logging.StreamHandler()
    fmt = logging.Formatter(
        "%(asctime)s [%(levelname)s] [%(name)s] %(message)s", datefmt="%Y-%m-%d %H:%M:%S"
    )
    ch.setFormatter(fmt)
    logger.addHandler(ch)
    logger.propagate = False
    return logger


def log_cpu_info(logger: logging.Logger) -> None:
    """Log CPU model, core count, architecture, and platform at startup."""
    model = ""
    physical_cores: set[tuple[str, str]] = set()
    cur_physical_id = ""
    try:
        with open("/proc/cpuinfo") as f:
            for line in f:
                if line.startswith("model name") and not model:
                    model = line.split(":", 1)[1].strip()
                elif line.startswith("physical id"):
                    cur_physical_id = line.split(":", 1)[1].strip()
                elif line.startswith("core id"):
                    physical_cores.add((cur_physical_id, line.split(":", 1)[1].strip()))
    except OSError:
        model = platform.processor() or "unknown"

    logical_cores = os.cpu_count() or 0
    n_physical = len(physical_cores) if physical_cores else logical_cores

    arch = platform.machine()
    plat = platform.platform()
    logger.info(
        f"CPU: {model} | {n_physical} physical, {logical_cores} logical cores | {arch} | {plat}"
    )