File: test_raw_pylibcpupower.py

package info (click to toggle)
linux 6.18.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,742,096 kB
  • sloc: ansic: 26,781,576; asm: 272,087; sh: 148,750; python: 79,244; makefile: 57,741; perl: 36,527; xml: 19,542; cpp: 5,911; yacc: 4,939; lex: 2,950; awk: 1,607; sed: 30; ruby: 25
file content (58 lines) | stat: -rwxr-xr-x 1,477 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0-only

import raw_pylibcpupower as p

# Simple function call

"""
Get cstate count
"""
cpu_cstates_count = p.cpuidle_state_count(0)
if cpu_cstates_count > -1:
    print(f"CPU 0 has {cpu_cstates_count} c-states")
else:
    print(f"cstate count error: return code: {cpu_cstates_count}")

"""
Disable cstate (will fail if the above returns is under 1, ex: a virtual machine)
"""
cstate_disabled = p.cpuidle_state_disable(0, 0, 1)

match cstate_disabled:
    case 0:
        print(f"CPU state disabled")
    case -1:
        print(f"Idlestate not available")
    case -2:
        print(f"Disabling is not supported by the kernel")
    case -3:
        print(f"No write access to disable/enable C-states: try using sudo")
    case _:
        print(f"Not documented: {cstate_disabled}")

"""
Test cstate is disabled
"""
is_cstate_disabled = p.cpuidle_is_state_disabled(0, 0)

match is_cstate_disabled:
    case 1:
        print(f"CPU is disabled")
    case 0:
        print(f"CPU is enabled")
    case -1:
        print(f"Idlestate not available")
    case -2:
        print(f"Disabling is not supported by kernel")
    case _:
        print(f"Not documented: {is_cstate_disabled}")

# Pointer example

topo = p.cpupower_topology()
total_cpus = p.get_cpu_topology(topo)
if total_cpus > 0:
    print(f"Number of total cpus: {total_cpus} and number of cores: {topo.cores}")
else:
    print(f"Error: could not get cpu topology")