llm-bench

"""The owner trimmed the kernel config by hand and dropped CONFIG_X86_INTEL_PSTATE with the iGPU
options. The kernel falls back to acpi-cpufreq, where the powersave governor from
/etc/conf.d/cpupower (chosen for intel_pstate's HWP powersave) pins every core at 800 MHz."""
from bench.core.tools import Symlink
from bench.tasks import sleuth
from bench.tasks.sleuth_cases import _home

KERNEL = "7.3.0-tkg-bore-llvm"
CMDLINE = (f"BOOT_IMAGE=/vmlinuz-{KERNEL} root=UUID=6d3e0b6f-0b0c-4c51-9a3d-6c1a4b7e2f10 ro quiet "
           "nowatchdog split_lock_detect=off")
PS = """PID   USER     TIME  COMMAND
    1 root      0:01 init [3]
 2088 ntp       0:00 /usr/sbin/chronyd
 2140 root      0:03 /usr/sbin/NetworkManager --pid-file /run/NetworkManager.pid
 2201 root      0:00 /usr/sbin/crond
 2733 user      3:02 /usr/bin/kwin_wayland --xwayland
 8120 root      0:06 /usr/bin/python3.13 /usr/lib/python-exec/python3.13/emerge -uDN @world
 9311 portage   0:00 [llvm-core/llvm-21.1.8] sandbox /usr/lib/portage/python3.13/ebuild.sh compile
 9402 portage   0:41 ninja -v -j16 -l16
31810 portage   1:12 /usr/lib/llvm/21/bin/clang++ -O2 -march=alderlake -c lib/Target/X86/X86ISelLowering.cpp
31822 portage   0:58 /usr/lib/llvm/21/bin/clang++ -O2 -march=alderlake -c lib/Transforms/Vectorize/LoopVectorize.cpp
31840 portage   0:47 /usr/lib/llvm/21/bin/clang++ -O2 -march=alderlake -c lib/CodeGen/SelectionDAG/DAGCombiner.cpp
31851 portage   0:31 /usr/lib/llvm/21/bin/clang++ -O2 -march=alderlake -c lib/Analysis/ScalarEvolution.cpp
"""
CPUPOWER = """analyzing CPU 0:
  driver: acpi-cpufreq
  CPUs which run at the same hardware frequency: 0
  CPUs which need to have their frequency coordinated by software: 0
  maximum transition latency: 10.0 us
  hardware limits: 800 MHz - 3.70 GHz
  available frequency steps:  3.70 GHz, 3.70 GHz, 3.50 GHz, 3.30 GHz, 3.10 GHz, 2.90 GHz, 2.70 GHz, 2.50 GHz, 2.30 GHz, 2.10 GHz, 1.90 GHz, 1.70 GHz, 1.50 GHz, 1.30 GHz, 1.10 GHz, 900 MHz, 800 MHz
  available cpufreq governors: conservative ondemand userspace powersave performance schedutil
  current policy: frequency should be within 800 MHz and 3.70 GHz.
                  The governor "powersave" may decide which speed to use
                  within this range.
  current CPU frequency: 800 MHz (asserted by call to hardware)
  boost state support:
    Supported: yes
    Active: yes
"""


def context():
    files, commands = _home.host(kernel=KERNEL, cmdline=CMDLINE, driver="acpi-cpufreq",
                                 load="busy", ps=PS)
    new, old = _home.kernel_config(pstate=False, trimmed=True), _home.kernel_config()
    src = f"usr/src/linux-{KERNEL}/"
    files.update({
        "proc/config.gz": _home.gz(new),
        f"boot/config-{KERNEL}": new, f"boot/config-{KERNEL}.old": old,
        f"boot/vmlinuz-{KERNEL}": b"\x4d\x5a" + bytes(4094),
        f"boot/vmlinuz-{KERNEL}.old": b"\x4d\x5a" + bytes(4094),
        src + ".config": new, src + ".config.old": old,
        src + "Makefile": "VERSION = 7\nPATCHLEVEL = 3\nSUBLEVEL = 0\nEXTRAVERSION =\nNAME = Baby Opossum Posse\n",
        "usr/src/linux": Symlink(f"linux-{KERNEL}"),
    })
    commands.update({"cpupower": "Usage:\tcpupower [-d|--debug] [-c|--cpu cpulist ] <command> [<args>]\n",
                     "cpupower frequency-info": CPUPOWER})
    return sleuth.snapshot(files, commands)


PSTATE = r"intel[_ -]?p[_ -]?state"

CASES = [sleuth.Case(
    case_id="cpufreq-driver", category="sleuth/kernel", context=context,
    complaint=("I rebuilt my kernel last night after trimming the config by hand: I dropped the iGPU "
               "drivers (this is a -KF, no iGPU) and a pile of drivers for hardware I don't have. "
               "Since then the machine is sluggish. The emerge running right now is taking about "
               "three times as long as usual and the desktop stutters. Temps look low. What "
               "happened?"),
    culprit=[PSTATE], reject=[r"thermal|throttl|i915|microcode|mitigation"],
    fix=[r"X86_INTEL_PSTATE\s*=\s*y",
         r"(re-?enable|enable|restore|rebuild|build|turn\s+(back\s+)?on|add\s+(it\s+)?back|re-?add|"
         r"select|set)\b[^.\n]{0,80}" + PSTATE,
         PSTATE + r"\b[^.\n]{0,60}\b(back|=\s*y|built[- ]in|enabled)"],
    key=["acpi-cpufreq", "CONFIG_X86_INTEL_PSTATE is not set"],
    expert=["cat /sys/devices/system/cpu/cpufreq/policy0/scaling_driver",
            "zcat /proc/config.gz | grep INTEL_PSTATE"],
    reference={"culprit": "CONFIG_X86_INTEL_PSTATE was dropped from the trimmed kernel config",
               "root_cause": "Without intel_pstate the kernel falls back to acpi-cpufreq, where the "
                             "powersave governor set by /etc/conf.d/cpupower pins every core at the "
                             "800 MHz minimum.",
               "fix": "Set CONFIG_X86_INTEL_PSTATE=y again, rebuild and reboot; until then "
                      "cpupower frequency-set -g schedutil."},
    wrong=[{"culprit": "powersave governor pinning the CPUs at 800 MHz"},
           {"culprit": "thermal throttling of the package"},
           {"culprit": "CONFIG_SCHED_MC_PRIO missing, so tasks land on the E-cores"}],
)]