Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/tools/power/cpupower/bindings/python/test_raw_pylibcpupower.py
26299 views
1
#!/usr/bin/env python3
2
# SPDX-License-Identifier: GPL-2.0-only
3
4
import raw_pylibcpupower as p
5
6
# Simple function call
7
8
"""
9
Get cstate count
10
"""
11
cpu_cstates_count = p.cpuidle_state_count(0)
12
if cpu_cstates_count > -1:
13
print(f"CPU 0 has {cpu_cstates_count} c-states")
14
else:
15
print(f"cstate count error: return code: {cpu_cstates_count}")
16
17
"""
18
Disable cstate (will fail if the above returns is under 1, ex: a virtual machine)
19
"""
20
cstate_disabled = p.cpuidle_state_disable(0, 0, 1)
21
22
match 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
"""
35
Test cstate is disabled
36
"""
37
is_cstate_disabled = p.cpuidle_is_state_disabled(0, 0)
38
39
match 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
53
topo = p.cpupower_topology()
54
total_cpus = p.get_cpu_topology(topo)
55
if total_cpus > 0:
56
print(f"Number of total cpus: {total_cpus} and number of cores: {topo.cores}")
57
else:
58
print(f"Error: could not get cpu topology")
59
60