From: Vitaliy Makkoveev Subject: Re: improvement of perfpolicy auto To: tech@openbsd.org Date: Thu, 15 May 2025 21:26:42 +0300 On Sun, Apr 27, 2025 at 03:29:42PM +0200, Kirill A. Korinsky wrote: > tech@, > > here's an improvement to how perfpolicy auto works. > > Short story: Instead of only using min and max speeds, it now utilizes > all supported by CPU speeds. > > Long story: I've improved how idle time is computed. This change > switches to using p_tu.tu_runtime, which seems like the right way, at > least based on my understanding. This allows implementing something > similar to the concept behind Linux's ondemand: the CPU load within a > time window determines the target performance level. Since we don't have > per-CPU cpu_setperf, it uses the highest required frequency across all > CPUs as the target. > > I've personally been using this approach since September, simetimes > polish it, and it works quite well. I'm seeing the CPU works at > different speeds, as expected. > > Tests? Feedback? Ok? > I like this diff. Without it my machine (dmesg below) with 'hw.perfpolicy=auto' runs at 400 Mhz and can't be used for my normal workflow. As the addition in the 'hw.perfpolicy=auto' sound is broken, it plays the middle frequencies very quiet. This diff fixes it. I don't like to use this machine with 'hw.perfpolicy=high' because it does permanent cooler on/off with hight frequency. So, I want this diff be pushed forward. Can someone from this area provide feedback? OpenBSD 7.7-current (GENERIC.MP) #12: Wed May 14 20:17:26 MSK 2025 otto@obsd-amd64-build.local:/home/otto/src/sys/arch/amd64/compile/GENERIC.MP real mem = 16853643264 (16072MB) avail mem = 16316157952 (15560MB) random: good seed from bootblocks mpath0 at root scsibus0 at mpath0: 256 targets mainbus0 at root bios0 at mainbus0: SMBIOS rev. 3.4 @ 0x43c9b000 (145 entries) bios0: vendor American Megatrends International, LLC. version "E15H1IMS.10F" date 07/05/2023 bios0: Micro-Star International Co., Ltd. Modern 15 B12M efi0 at bios0: UEFI 2.8 efi0: American Megatrends rev 0x50018 acpi0 at bios0: ACPI 6.4 acpi0: sleep states S0ix S3 S4 S5 acpi0: tables DSDT FACP SSDT FIDT SLIC SSDT SSDT SSDT SSDT SSDT HPET APIC MCFG SSDT UEFI LPIT SSDT SSDT DBGP DBG2 SSDT NHLT DMAR SSDT SSDT SSDT SSDT BGRT PHAT WSMT FPDT BGRT acpi0: wakeup devices PEGP(S4) PEG2(S4) PEGP(S4) PEGP(S4) PEG0(S4) PEGP(S4) RP09(S4) PXSX(S4) RP10(S4) PXSX(S4) RP11(S4) PXSX(S4) RP12(S4) PXSX(S4) RP13(S4) PXSX(S4) [...] acpitimer0 at acpi0: 3579545 Hz, 24 bits acpihpet0 at acpi0: 19200000 Hz acpimadt0 at acpi0 addr 0xfee00000: PC-AT compat cpu0 at mainbus0: apid 0 (boot processor) cpu0: 12th Gen Intel(R) Core(TM) i7-1255U, 4690.01 MHz, 06-9a-04, patch 00000436 cpu0: cpuid 1 edx=bfebfbff ecx=77fafbff cpu0: cpuid 6 eax=df8ff7 ecx=409 cpu0: cpuid 7.0 ebx=239c27eb ecx=984007ac edx=fc18c410 cpu0: cpuid a vers=5, gp=6, gpwidth=48, ff=3, ffwidth=48 cpu0: cpuid d.1 eax=f cpu0: cpuid 80000001 edx=2c100800 ecx=121 cpu0: cpuid 80000007 edx=100 cpu0: msr 10a=1488fd6b cpu0: 48KB 64b/line 12-way D-cache, 32KB 64b/line 8-way I-cache, 1MB 64b/line 10-way L2 cache, 12MB 64b/line 12-way L3 cache cpu0: smt 0, core 0, package 0 mtrr: Pentium Pro MTRR support, 10 var ranges, 88 fixed ranges cpu0: apic clock running at 38MHz cpu0: mwait min=64, max=64, C-substates=0.2.0.2.0.1.0.1, IBE cpu1 at mainbus0: apid 1 (application processor) cpu1: 12th Gen Intel(R) Core(TM) i7-1255U, 4690.02 MHz, 06-9a-04, patch 00000436 cpu1: smt 1, core 0, package 0 cpu2 at mainbus0: apid 8 (application processor) cpu2: 12th Gen Intel(R) Core(TM) i7-1255U, 4091.46 MHz, 06-9a-04, patch 00000436 cpu2: smt 0, core 4, package 0 cpu3 at mainbus0: apid 9 (application processor) cpu3: 12th Gen Intel(R) Core(TM) i7-1255U, 4091.44 MHz, 06-9a-04, patch 00000436 cpu3: smt 1, core 4, package 0 cpu4 at mainbus0: apid 16 (application processor) cpu4: 12th Gen Intel(R) Core(TM) i7-1255U, 3192.29 MHz, 06-9a-04, patch 00000436 cpu4: 32KB 64b/line 8-way D-cache, 64KB 64b/line 8-way I-cache, 2MB 64b/line 16-way L2 cache, 12MB 64b/line 12-way L3 cache cpu4: smt 0, core 8, package 0 cpu5 at mainbus0: apid 18 (application processor) cpu5: 12th Gen Intel(R) Core(TM) i7-1255U, 2993.10 MHz, 06-9a-04, patch 00000436 cpu5: smt 0, core 9, package 0 cpu6 at mainbus0: apid 20 (application processor) cpu6: 12th Gen Intel(R) Core(TM) i7-1255U, 2793.56 MHz, 06-9a-04, patch 00000436 cpu6: smt 0, core 10, package 0 cpu7 at mainbus0: apid 22 (application processor) cpu7: 12th Gen Intel(R) Core(TM) i7-1255U, 2793.56 MHz, 06-9a-04, patch 00000436 cpu7: smt 0, core 11, package 0 cpu8 at mainbus0: apid 24 (application processor) cpu8: 12th Gen Intel(R) Core(TM) i7-1255U, 2593.89 MHz, 06-9a-04, patch 00000436 cpu8: smt 0, core 12, package 0 cpu9 at mainbus0: apid 26 (application processor) cpu9: 12th Gen Intel(R) Core(TM) i7-1255U, 2494.25 MHz, 06-9a-04, patch 00000436 cpu9: smt 0, core 13, package 0 cpu10 at mainbus0: apid 28 (application processor) cpu10: 12th Gen Intel(R) Core(TM) i7-1255U, 2394.48 MHz, 06-9a-04, patch 00000436 cpu10: smt 0, core 14, package 0 cpu11 at mainbus0: apid 30 (application processor) cpu11: 12th Gen Intel(R) Core(TM) i7-1255U, 2394.48 MHz, 06-9a-04, patch 00000436 cpu11: smt 0, core 15, package 0 ioapic0 at mainbus0: apid 2 pa 0xfec00000, version 20, 120 pins acpimcfg0 at acpi0 acpimcfg0: addr 0xc0000000, bus 0-255 acpiprt0 at acpi0: bus 0 (PC00) acpiprt1 at acpi0: bus -1 (PEG2) acpiprt2 at acpi0: bus -1 (PEG0) acpiprt3 at acpi0: bus 1 (RP09) acpiprt4 at acpi0: bus -1 (RP10) acpiprt5 at acpi0: bus -1 (RP11) acpiprt6 at acpi0: bus -1 (RP12) acpiprt7 at acpi0: bus -1 (RP13) acpiprt8 at acpi0: bus -1 (RP14) acpiprt9 at acpi0: bus -1 (RP15) acpiprt10 at acpi0: bus -1 (RP16) acpiprt11 at acpi0: bus -1 (RP01) acpiprt12 at acpi0: bus -1 (RP02) acpiprt13 at acpi0: bus -1 (RP03) acpiprt14 at acpi0: bus -1 (RP04) acpiprt15 at acpi0: bus -1 (RP05) acpiprt16 at acpi0: bus -1 (RP06) acpiprt17 at acpi0: bus -1 (RP07) acpiprt18 at acpi0: bus -1 (RP08) acpiprt19 at acpi0: bus -1 (RP17) acpiprt20 at acpi0: bus -1 (RP18) acpiprt21 at acpi0: bus -1 (RP19) acpiprt22 at acpi0: bus -1 (RP20) acpiprt23 at acpi0: bus -1 (RP21) acpiprt24 at acpi0: bus -1 (RP22) acpiprt25 at acpi0: bus -1 (RP23) acpiprt26 at acpi0: bus -1 (RP24) acpiprt27 at acpi0: bus -1 (RP25) acpiprt28 at acpi0: bus -1 (RP26) acpiprt29 at acpi0: bus -1 (RP27) acpiprt30 at acpi0: bus -1 (RP28) acpiec0 at acpi0 acpiec0: Not running on HW-Reduced ACPI type 134 acpipci0 at acpi0 PC00: 0x00000000 0x00000011 0x00000001 "MSNB1001" at acpi0 not configured acpiac0 at acpi0: AC unit online acpibat0 at acpi0: BAT1 model "BIF0_9" type LION oem "MSI" acpibtn0 at acpi0: LID0(wakeup) "PNP0C14" at acpi0 not configured "INTC1046" at acpi0 not configured "PNP0C50" at acpi0 not configured "CUST0000" at acpi0 not configured "ACPI000E" at acpi0 not configured pchgpio0 at acpi0 GPI0 addr 0xfd6e0000/0x10000 0xfd6d0000/0x10000 0xfd6a0000/0x10000 0xfd690000/0x10000 irq 14, 360 pins acpibtn1 at acpi0: SLPB acpicpu0 at acpi0: C3(200@1048 mwait.1@0x60), C2(350@127 mwait.1@0x21), C1(1000@1 mwait.1), PSS acpicpu1 at acpi0: C3(200@1048 mwait.1@0x60), C2(350@127 mwait.1@0x21), C1(1000@1 mwait.1), PSS acpicpu2 at acpi0: C3(200@1048 mwait.1@0x60), C2(350@127 mwait.1@0x21), C1(1000@1 mwait.1), PSS acpicpu3 at acpi0: C3(200@1048 mwait.1@0x60), C2(350@127 mwait.1@0x21), C1(1000@1 mwait.1), PSS acpicpu4 at acpi0: C3(200@1048 mwait.1@0x60), C2(350@127 mwait.1@0x21), C1(1000@1 mwait.1), PSS acpicpu5 at acpi0: C3(200@1048 mwait.1@0x60), C2(350@127 mwait.1@0x21), C1(1000@1 mwait.1), PSS acpicpu6 at acpi0: C3(200@1048 mwait.1@0x60), C2(350@127 mwait.1@0x21), C1(1000@1 mwait.1), PSS acpicpu7 at acpi0: C3(200@1048 mwait.1@0x60), C2(350@127 mwait.1@0x21), C1(1000@1 mwait.1), PSS acpicpu8 at acpi0: C3(200@1048 mwait.1@0x60), C2(350@127 mwait.1@0x21), C1(1000@1 mwait.1), PSS acpicpu9 at acpi0: C3(200@1048 mwait.1@0x60), C2(350@127 mwait.1@0x21), C1(1000@1 mwait.1), PSS acpicpu10 at acpi0: C3(200@1048 mwait.1@0x60), C2(350@127 mwait.1@0x21), C1(1000@1 mwait.1), PSS acpicpu11 at acpi0: C3(200@1048 mwait.1@0x60), C2(350@127 mwait.1@0x21), C1(1000@1 mwait.1), PSS "PNP0C14" at acpi0 not configured "PNP0C14" at acpi0 not configured acpibtn2 at acpi0: PWRB(wakeup) "INTC1070" at acpi0 not configured "ACPI0011" at acpi0 not configured intelpmc0 at acpi0: PEPD state 0: 0x7f:1:2:0x00:0x0000000000000060 counter: 0x7f:64:0:0x00:0x0000000000000632 frequency: 0 state 1: 0x7f:1:2:0x00:0x0000000000000060 counter: 0x00:32:0:0x03:0x00000000fe00193c frequency: 8197 "INTC1041" at acpi0 not configured acpipwrres0 at acpi0: PXP_, resource for PEG2, PEGP, MINI acpipwrres1 at acpi0: BTRT acpipwrres2 at acpi0: PXTC, resource for TPD0 acpipwrres3 at acpi0: PTPL acpipwrres4 at acpi0: WRST acpipwrres5 at acpi0: PAUD, resource for HDAS acpitz0 at acpi0 acpitz0: critical temperature is 100 degC acpipwrres6 at acpi0: PIN_ acpivideo0 at acpi0: GFX0 acpivout0 at acpivideo0: DD1F acpivout1 at acpivideo0: DD2F cpu0: using VERW MDS workaround cpu0: Enhanced SpeedStep 4690 MHz: speeds: 2501, 2500, 2400, 2300, 2100, 2000, 1700, 1600, 1500, 1300, 1100, 1000, 900, 700, 600, 400 MHz pci0 at mainbus0 bus 0 0:31:5: mem address conflict 0xfe010000/0x1000 pchb0 at pci0 dev 0 function 0 "Intel Core 12G Host" rev 0x04 inteldrm0 at pci0 dev 2 function 0 "Intel Graphics" rev 0x0c drm0 at inteldrm0 inteldrm0: msi, ALDERLAKE_P, gen 12 "Intel Core 12G DTT" rev 0x04 at pci0 dev 4 function 0 not configured "Intel Core 12G GNA" rev 0x04 at pci0 dev 8 function 0 not configured "Intel Core 12G CL" rev 0x01 at pci0 dev 10 function 0 not configured xhci0 at pci0 dev 20 function 0 "Intel 600 Series xHCI" rev 0x01: msi, xHCI 1.20 usb0 at xhci0: USB revision 3.0 uhub0 at usb0 configuration 1 interface 0 "Intel xHCI root hub" rev 3.00/1.00 addr 1 "Intel 600 Series SRAM" rev 0x01 at pci0 dev 20 function 2 not configured iwx0 at pci0 dev 20 function 3 "Intel Wi-Fi 6 AX211" rev 0x01, msix dwiic0 at pci0 dev 21 function 0 "Intel 600 Series I2C" rev 0x01: apic 2 int 27 iic0 at dwiic0 ihidev0 at iic0 addr 0x15 gpio 83, vendor 0x4f3 product 0x3282, PNP0C50 ihidev0: 92 report ids imt0 at ihidev0: clickpad, 5 contacts wsmouse0 at imt0 mux 0 ims0 at ihidev0 reportid 1: 2 buttons wsmouse1 at ims0 mux 0 hid at ihidev0 reportid 5 not configured hid at ihidev0 reportid 6 not configured hid at ihidev0 reportid 7 not configured hid at ihidev0 reportid 11 not configured hid at ihidev0 reportid 12 not configured hid at ihidev0 reportid 13 not configured ims1 at ihidev0 reportid 14: 0 buttons wsmouse2 at ims1 mux 0 hid at ihidev0 reportid 92 not configured "Intel 600 Series HECI" rev 0x01 at pci0 dev 22 function 0 not configured ppb0 at pci0 dev 29 function 0 "Intel 600 Series PCIE" rev 0x01: msi pci1 at ppb0 bus 1 nvme0 at pci1 dev 0 function 0 "Micron Technology NVMe" rev 0x03: msix, NVMe 1.4 nvme0: Micron_2400_MTFDKBA512QFM, firmware V3MA001, serial 22303B5E46EF scsibus1 at nvme0: 2 targets, initiator 0 sd0 at scsibus1 targ 1 lun 0: sd0: 488386MB, 512 bytes/sector, 1000215216 sectors pcib0 at pci0 dev 31 function 0 "Intel 600 Series eSPI" rev 0x01 azalia0 at pci0 dev 31 function 3 "Intel 600 Series HD Audio" rev 0x01: msi azalia0: codecs: Realtek ALC256 audio0 at azalia0 ichiic0 at pci0 dev 31 function 4 "Intel 600 Series SMBus" rev 0x01: apic 2 int 16 iic1 at ichiic0 "Intel 600 Series SPI" rev 0x01 at pci0 dev 31 function 5 not configured isa0 at pcib0 isadma0 at isa0 pckbc0 at isa0 port 0x60/5 irq 1 irq 12 pckbd0 at pckbc0 (kbd slot) wskbd0 at pckbd0: console keyboard pcppi0 at isa0 port 0x61 spkr0 at pcppi0 vmm0 at mainbus0: VMX/EPT efifb at mainbus0 not configured ugen0 at uhub0 port 5 "Generic USB2.0-CRW" rev 2.00/39.60 addr 2 uvideo0 at uhub0 port 7 configuration 1 interface 0 "SunplusIT Inc HD Webcam" rev 2.01/3.01 addr 3 video0 at uvideo0 uhidev0 at uhub0 port 8 configuration 1 interface 0 "CHICONY USB Keyboard" rev 1.10/3.00 addr 4 uhidev0: iclass 3/1 ukbd0 at uhidev0: 8 variable keys, 6 key codes wskbd1 at ukbd0 mux 1 uhidev1 at uhub0 port 8 configuration 1 interface 1 "CHICONY USB Keyboard" rev 1.10/3.00 addr 4 uhidev1: iclass 3/0, 3 report ids uhid0 at uhidev1 reportid 1: input=1, output=0, feature=0 uhid1 at uhidev1 reportid 2: input=1, output=0, feature=2 ucc0 at uhidev1 reportid 3: 19 usages, 14 keys, enum wskbd2 at ucc0 mux 1 uhidev2 at uhub0 port 9 configuration 1 interface 0 "Logitech Wireless Receiver" rev 1.10/3.08 addr 5 uhidev2: iclass 3/1, 5 report ids ums0 at uhidev2 reportid 1: 5 buttons, Z dir wsmouse3 at ums0 mux 0 uhid2 at uhidev2 reportid 5: input=0, output=0, feature=7 ugen1 at uhub0 port 10 "Intel Bluetooth" rev 2.01/0.02 addr 6 vscsi0 at root scsibus2 at vscsi0: 256 targets softraid0 at root scsibus3 at softraid0: 256 targets sd1 at scsibus3 targ 1 lun 0: sd1: 488385MB, 512 bytes/sector, 1000213631 sectors root on sd1a (0f9a87214e4f739d.a) swap on sd1b dump on sd1b drm:pid0:ct_send *ERROR* [drm] *ERROR* GT0: GUC: CT: No response for request 0x4000 (fence 1) drm:pid0:intel_guc_ct_send *ERROR* [drm] *ERROR* GT0: GUC: CT: Sending action 0x4000 failed (0xffffffffffffffc4e) status=0 drm:pid0:intel_huc_auth *ERROR* [drm] *ERROR* GT0: HuC: all workloads authentication failed 0xffffffffffffffc4e drm:pid31535:ct_handle_response *ERROR* [drm] *ERROR* GT0: GUC: CT: Unsolicited response message: len 1, data 0xf0000000 (fence 1, last 1) drm:pid31535:ct_handle_hxg *ERROR* [drm] *ERROR* GT0: GUC: CT: Failed to handle HXG message (0xfffffffffffffffee) 0xffff800001c5d3b8h drm:pid31535:ct_handle_msg *ERROR* [drm] *ERROR* GT0: GUC: CT: Failed to process CT message (0xfffffffffffffffee) 0xffff800001c5d3b4h inteldrm0: 1920x1080, 32bpp wsdisplay0 at inteldrm0 mux 1: console (std, vt100 emulation), using wskbd0 wskbd1: connecting to wsdisplay0 wskbd2: connecting to wsdisplay0 wsdisplay0: screen 1-5 added (std, vt100 emulation) iwx0: hw rev 0x370, fw 77.f92b5fed.0, address 14:75:5b:49:fc:f0 drm:pid35471:drm_atomic_helper_wait_for_flip_done *ERROR* [drm] *ERROR* [CRTC:82:pipe A] flip_done timed out > > Index: sys/kern/sched_bsd.c > =================================================================== > RCS file: /home/cvs/src/sys/kern/sched_bsd.c,v > diff -u -p -r1.99 sched_bsd.c > --- sys/kern/sched_bsd.c 10 Mar 2025 09:28:56 -0000 1.99 > +++ sys/kern/sched_bsd.c 27 Apr 2025 11:02:11 -0000 > @@ -575,16 +575,21 @@ current_perfpolicy(void) > return (hw_power) ? perfpolicy_on_ac : perfpolicy_on_battery; > } > > +#define PERFPOL_AUTO_HW_POWER_HIGH 80 > +#define PERFPOL_AUTO_AC_POWER_HIGH 95 > +#define PERFPOL_AUTO_INTERVAL_MS 100 > + > void > setperf_auto(void *v) > { > - static uint64_t *idleticks, *totalticks; > - static int downbeats; > - int i, j = 0; > - int speedup = 0; > + static struct timespec last, *idletimes; > + int i = 0; > + int speedup = 0, load = 0, speed = 0; > + uint64_t idle, window; > CPU_INFO_ITERATOR cii; > struct cpu_info *ci; > - uint64_t idle, total, allidle = 0, alltotal = 0; > + struct timespec now, ts, its; > + struct schedstate_percpu *spc; > > if (!perfpolicy_dynamic()) > return; > @@ -597,49 +602,68 @@ setperf_auto(void *v) > goto faster; > } > > - if (!idleticks) > - if (!(idleticks = mallocarray(ncpusfound, sizeof(*idleticks), > + if (!idletimes) > + if (!(idletimes = mallocarray(ncpusfound, sizeof(*idletimes), > M_DEVBUF, M_NOWAIT | M_ZERO))) > return; > - if (!totalticks) > - if (!(totalticks = mallocarray(ncpusfound, sizeof(*totalticks), > - M_DEVBUF, M_NOWAIT | M_ZERO))) { > - free(idleticks, M_DEVBUF, > - sizeof(*idleticks) * ncpusfound); > - return; > - } > + > + nanouptime(&now); > + timespecsub(&now, &last, &ts); > + last = now; > + window = TIMESPEC_TO_NSEC(&ts) / 1000000ULL; > + > CPU_INFO_FOREACH(cii, ci) { > if (!cpu_is_online(ci)) > continue; > - total = 0; > - for (i = 0; i < CPUSTATES; i++) { > - total += ci->ci_schedstate.spc_cp_time[i]; > - } > - total -= totalticks[j]; > - idle = ci->ci_schedstate.spc_cp_time[CP_IDLE] - idleticks[j]; > - if (idle < total / 3) > - speedup = 1; > - alltotal += total; > - allidle += idle; > - idleticks[j] += idle; > - totalticks[j] += total; > - j++; > + > + spc = &ci->ci_schedstate; > + > + ts = spc->spc_runtime; > + its = spc->spc_idleproc->p_tu.tu_runtime; > + > + if (ci->ci_curproc == spc->spc_idleproc && > + timespeccmp(&ts, &spc->spc_runtime, ==) && > + timespeccmp(&now, &ts, >)) > + timespecsub(&now, &ts, &ts); > + else > + timespecclear(&ts); > + > + timespecsub(&its, &idletimes[i], &its); > + timespecadd(&idletimes[i], &its, &idletimes[i]); > + > + timespecadd(&ts, &its, &ts); > + > + idle = TIMESPEC_TO_NSEC(&ts) / 1000000ULL; > + > + if (idle <= 0) > + speed = 100; > + > + if (idle > window) > + load = 0; > + else > + load = (100 * (window - idle)) / window; > + > + if (load > speed) > + speed = load; > + > + i++; > } > - if (allidle < alltotal / 2) > + > + if (hw_power && speed >= PERFPOL_AUTO_HW_POWER_HIGH) > + speedup = 1; > + else if (speed >= PERFPOL_AUTO_AC_POWER_HIGH) > speedup = 1; > - if (speedup && downbeats < 5) > - downbeats++; > > if (speedup && perflevel != 100) { > faster: > perflevel = 100; > cpu_setperf(perflevel); > - } else if (!speedup && perflevel != 0 && --downbeats <= 0) { > - perflevel = 0; > + } else if (!speedup && speed != perflevel) { > + perflevel = speed; > cpu_setperf(perflevel); > } > > - timeout_add_msec(&setperf_to, 100); > + timeout_add_msec(&setperf_to, PERFPOL_AUTO_INTERVAL_MS); > } > > int >