Path: blob/master/tools/power/cpupower/bindings/python/test_raw_pylibcpupower.py
26299 views
#!/usr/bin/env python31# SPDX-License-Identifier: GPL-2.0-only23import raw_pylibcpupower as p45# Simple function call67"""8Get cstate count9"""10cpu_cstates_count = p.cpuidle_state_count(0)11if cpu_cstates_count > -1:12print(f"CPU 0 has {cpu_cstates_count} c-states")13else:14print(f"cstate count error: return code: {cpu_cstates_count}")1516"""17Disable cstate (will fail if the above returns is under 1, ex: a virtual machine)18"""19cstate_disabled = p.cpuidle_state_disable(0, 0, 1)2021match cstate_disabled:22case 0:23print(f"CPU state disabled")24case -1:25print(f"Idlestate not available")26case -2:27print(f"Disabling is not supported by the kernel")28case -3:29print(f"No write access to disable/enable C-states: try using sudo")30case _:31print(f"Not documented: {cstate_disabled}")3233"""34Test cstate is disabled35"""36is_cstate_disabled = p.cpuidle_is_state_disabled(0, 0)3738match is_cstate_disabled:39case 1:40print(f"CPU is disabled")41case 0:42print(f"CPU is enabled")43case -1:44print(f"Idlestate not available")45case -2:46print(f"Disabling is not supported by kernel")47case _:48print(f"Not documented: {is_cstate_disabled}")4950# Pointer example5152topo = p.cpupower_topology()53total_cpus = p.get_cpu_topology(topo)54if total_cpus > 0:55print(f"Number of total cpus: {total_cpus} and number of cores: {topo.cores}")56else:57print(f"Error: could not get cpu topology")585960