Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1#!/usr/bin/env python3
2# SPDX-License-Identifier: GPL-2.0-only
3
4import raw_pylibcpupower as p
5
6# Simple function call
7
8"""
9Get cstate count
10"""
11cpu_cstates_count = p.cpuidle_state_count(0)
12if cpu_cstates_count > -1:
13 print(f"CPU 0 has {cpu_cstates_count} c-states")
14else:
15 print(f"cstate count error: return code: {cpu_cstates_count}")
16
17"""
18Disable cstate (will fail if the above returns is under 1, ex: a virtual machine)
19"""
20cstate_disabled = p.cpuidle_state_disable(0, 0, 1)
21
22match cstate_disabled:
23 case 0:
24 print(f"CPU state disabled")
25 case -1:
26 print(f"Idlestate not available")
27 case -2:
28 print(f"Disabling is not supported by the kernel")
29 case -3:
30 print(f"No write access to disable/enable C-states: try using sudo")
31 case _:
32 print(f"Not documented: {cstate_disabled}")
33
34"""
35Test cstate is disabled
36"""
37is_cstate_disabled = p.cpuidle_is_state_disabled(0, 0)
38
39match is_cstate_disabled:
40 case 1:
41 print(f"CPU is disabled")
42 case 0:
43 print(f"CPU is enabled")
44 case -1:
45 print(f"Idlestate not available")
46 case -2:
47 print(f"Disabling is not supported by kernel")
48 case _:
49 print(f"Not documented: {is_cstate_disabled}")
50
51# Pointer example
52
53topo = p.cpupower_topology()
54total_cpus = p.get_cpu_topology(topo)
55if total_cpus > 0:
56 print(f"Number of total cpus: {total_cpus} and number of cores: {topo.cores}")
57else:
58 print(f"Error: could not get cpu topology")