Skip to content

Commit 7a43980

Browse files
committed
Merge: cpupower: Update libcpupower Python bindings
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-10/-/merge_requests/1245 Description: include updates a future MR will need to build the bindings. JIRA: https://issues.redhat.com/browse/RHEL-91191 Signed-off-by: John B. Wyatt IV <jwyatt@redhat.com> Approved-by: John Kacur <jkacur@redhat.com> Approved-by: David Arcari <darcari@redhat.com> Approved-by: CKI KWF Bot <cki-ci-bot+kwf-gitlab-com@redhat.com> Merged-by: Scott Weaver <scweaver@redhat.com>
2 parents e23aa49 + 9eedbb7 commit 7a43980

File tree

4 files changed

+78
-19
lines changed

4 files changed

+78
-19
lines changed
Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
# SPDX-License-Identifier: GPL-2.0-only
22
# Makefile for libcpupower's Python bindings
33
#
4-
# This Makefile expects you have already run the makefile for cpupower to build
5-
# the .o files in the lib directory for the bindings to be created.
4+
# This Makefile expects you have already run `make install-lib` in the lib
5+
# directory for the bindings to be created.
66

7-
CC := gcc
7+
CC ?= gcc
8+
# CFLAGS ?=
9+
LDFLAGS ?= -lcpupower
810
HAVE_SWIG := $(shell if which swig >/dev/null 2>&1; then echo 1; else echo 0; fi)
911
HAVE_PYCONFIG := $(shell if which python-config >/dev/null 2>&1; then echo 1; else echo 0; fi)
1012

11-
LIB_DIR := ../../lib
12-
PY_INCLUDE = $(firstword $(shell python-config --includes))
13-
OBJECTS_LIB = $(wildcard $(LIB_DIR)/*.o)
13+
PY_INCLUDE ?= $(firstword $(shell python-config --includes))
14+
INSTALL_DIR ?= $(shell python3 -c "import site; print(site.getsitepackages()[0])")
1415

1516
all: _raw_pylibcpupower.so
1617

1718
_raw_pylibcpupower.so: raw_pylibcpupower_wrap.o
18-
$(CC) -shared $(OBJECTS_LIB) raw_pylibcpupower_wrap.o -o _raw_pylibcpupower.so
19+
$(CC) -shared $(LDFLAGS) raw_pylibcpupower_wrap.o -o _raw_pylibcpupower.so
1920

2021
raw_pylibcpupower_wrap.o: raw_pylibcpupower_wrap.c
21-
$(CC) -fPIC -c raw_pylibcpupower_wrap.c $(PY_INCLUDE)
22+
$(CC) $(CFLAGS) $(PY_INCLUDE) -fPIC -c raw_pylibcpupower_wrap.c
2223

2324
raw_pylibcpupower_wrap.c: raw_pylibcpupower.swg
2425
ifeq ($(HAVE_SWIG),0)
@@ -28,6 +29,15 @@ else ifeq ($(HAVE_PYCONFIG),0)
2829
endif
2930
swig -python raw_pylibcpupower.swg
3031

32+
# Only installs the Python bindings
33+
install: _raw_pylibcpupower.so
34+
install -D _raw_pylibcpupower.so $(INSTALL_DIR)/_raw_pylibcpupower.so
35+
install -D raw_pylibcpupower.py $(INSTALL_DIR)/raw_pylibcpupower.py
36+
37+
uninstall:
38+
rm -f $(INSTALL_DIR)/_raw_pylibcpupower.so
39+
rm -f $(INSTALL_DIR)/raw_pylibcpupower.py
40+
3141
# Will only clean the bindings folder; will not clean the actual cpupower folder
3242
clean:
3343
rm -f raw_pylibcpupower.py raw_pylibcpupower_wrap.c raw_pylibcpupower_wrap.o _raw_pylibcpupower.so

tools/power/cpupower/bindings/python/README

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@ libcpupower (aside from the libcpupower object files).
55
requirements
66
------------
77

8-
* You need the object files in the libcpupower directory compiled by
9-
cpupower's makefile.
8+
* If you are building completely from upstream; please install libcpupower by
9+
running `make install-lib` within the cpupower directory. This installs the
10+
libcpupower.so file and symlinks needed. Otherwise, please make sure a symlink
11+
to libcpupower.so exists in your library path from your distribution's
12+
packages.
1013
* The SWIG program must be installed.
11-
* The Python's development libraries installed.
14+
* The Python's development libraries must be installed.
1215

1316
Please check that your version of SWIG is compatible with the version of Python
1417
installed on your machine by checking the SWIG changelog on their website.
1518
https://swig.org/
1619

1720
Note that while SWIG itself is GPL v3+ licensed; the resulting output,
18-
the bindings code: is permissively licensed + the license of libcpupower's .o
19-
files. For these bindings that means GPL v2.
21+
the bindings code: is permissively licensed + the license of libcpupower's
22+
library files. For these bindings that means GPL v2.
2023

2124
Please see https://swig.org/legal.html and the discussion [1] for more details.
2225

@@ -48,6 +51,31 @@ To run the test script:
4851
$ python test_raw_pylibcpupower.py
4952

5053

54+
developing/using the bindings directly
55+
--------------------------------------
56+
57+
You need to add the Python bindings directory to your $PYTHONPATH.
58+
59+
You would set the path in the Bash terminal or in the Bash profile:
60+
61+
PYTHONPATH=~/linux/tools/power/cpupower/bindings/python:$PYTHONPATH
62+
63+
This allows you to set a specific repo of the bindings to use.
64+
65+
66+
installing/uninstalling
67+
-----------------------
68+
69+
Python uses a system specific site-packages folder to look up modules to import
70+
by default. You do not need to install cpupower to use the SWIG bindings.
71+
72+
You can install and uninstall the bindings to the site-packages with:
73+
74+
sudo make install
75+
76+
sudo make uninstall
77+
78+
5179
credits
5280
-------
5381

tools/power/cpupower/bindings/python/raw_pylibcpupower.swg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ void cpufreq_put_stats(struct cpufreq_stats *stats);
134134

135135
unsigned long cpufreq_get_transitions(unsigned int cpu);
136136

137+
char *cpufreq_get_energy_performance_preference(unsigned int cpu);
138+
void cpufreq_put_energy_performance_preference(char *ptr);
139+
137140
int cpufreq_set_policy(unsigned int cpu, struct cpufreq_policy *policy);
138141

139142
int cpufreq_modify_policy_min(unsigned int cpu, unsigned long min_freq);
@@ -160,6 +163,8 @@ int cpuidle_state_disable(unsigned int cpu, unsigned int idlestate,
160163
unsigned int disable);
161164
unsigned long cpuidle_state_latency(unsigned int cpu,
162165
unsigned int idlestate);
166+
unsigned long cpuidle_state_residency(unsigned int cpu,
167+
unsigned int idlestate);
163168
unsigned long cpuidle_state_usage(unsigned int cpu,
164169
unsigned int idlestate);
165170
unsigned long long cpuidle_state_time(unsigned int cpu,

tools/power/cpupower/bindings/python/test_raw_pylibcpupower.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,38 @@
1515
print(f"cstate count error: return code: {cpu_cstates_count}")
1616

1717
"""
18-
Disable cstate (will fail if the above is 0, ex: a virtual machine)
18+
Disable cstate (will fail if the above returns is under 1, ex: a virtual machine)
1919
"""
2020
cstate_disabled = p.cpuidle_state_disable(0, 0, 1)
21-
if cpu_cstates_count == 0:
22-
print(f"CPU 0 has {cpu_cstates_count} c-states")
23-
else:
24-
print(f"cstate count error: return code: {cpu_cstates_count}")
2521

2622
match cstate_disabled:
2723
case 0:
2824
print(f"CPU state disabled")
2925
case -1:
3026
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")
3131
case _:
32-
print(f"Not documented")
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)
3338

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}")
3450

3551
# Pointer example
3652

0 commit comments

Comments
 (0)