Skip to contentSkip to conversation

Add SWIG Bindings to libcpupower

PATCH In review John B. Wyatt IV 4 participants 10 messages Sep 5, 2024 – Sep 23, 2024 CPU power monitoring

lore
Share & tools
lore

John B. Wyatt IV

author maintainer
#0 lore raw

v3 cover Add SWIG Bindings to libcpupower

SWIG is a tool packaged in Fedora and other distros that can generate bindings from C and C++ code for several languages including Python, Perl, and Go. Providing bindings for scripting languages is a common feature to make use of libraries more accessible to more users and programs. My team specifically wants to expand the features of rteval. rteval is a Python program used to measure real time performance. We wanted to test the effect of enabling some levels of idle-stat to see how it affects latency, and didn't want to reinvent the wheel. Since SWIG requires the .o files created by libcpupower at compilation it makes sense to include this in the cpupower directory so that others can make use of them.

The V3 of this patchset includes:
* renaming header messages as requested and adding people to Cc as requested
* moving the stub (dummy) commit to the front of the patchset
* small punctuation fixes

The V2 of this patchset includes:
* the full definition of libcpupower headers that is needed for the bindings
* dummy implementation in C of a function listed in the header of libcpupower (requested by Shuah Khan)
* test_raw_pylibcpupower.py demonstrates an example of using the bindings
* adding myself and John Kacur to the cpupower section of the maintainers file (requested by Shuah Khan)
* addressed review comments about doc, makefile, and maintainers file
* small style and other fixes

The name raw_pylibcpupower is used because a wrapper pylibcpupower may be needed to make the bindings more 'pythonic' in the future. The bindings folder is used because Go or Perl bindings may be useful for other users in the future.

Note that while SWIG itself is GPL v3+ licensed; the resulting output, the bindings code, has the same license as the .o files used to generate the bindings (GPL v2 only). Please see
https://swig.org/legal.html
and
https://lore.kernel.org/linux-pm/Zqv9BOjxLAgyNP5B@hatbackup/#t
for more details on the license.

Sincerely,
John Wyatt
Software Engineer, Core Kernel
Red Hat

John B. Wyatt IV (4):
pm:cpupower: Add missing powercap_set_enabled() stub function pm:cpupower: Add SWIG bindings files for libcpupower pm:cpupower: Include test_raw_pylibcpupower.py
MAINTAINERS: Add Maintainers for SWIG Python bindings

 MAINTAINERS                                   |   3 +
 .../power/cpupower/bindings/python/.gitignore |   8 +
 tools/power/cpupower/bindings/python/Makefile |  31 +++
 tools/power/cpupower/bindings/python/README   |  59 +++++
 .../bindings/python/raw_pylibcpupower.i       | 247 ++++++++++++++++++
 .../bindings/python/test_raw_pylibcpupower.py |  42 +++
 tools/power/cpupower/lib/powercap.c           |   8 +
 7 files changed, 398 insertions(+)
 create mode 100644 tools/power/cpupower/bindings/python/.gitignore
 create mode 100644 tools/power/cpupower/bindings/python/Makefile
 create mode 100644 tools/power/cpupower/bindings/python/README
 create mode 100644 tools/power/cpupower/bindings/python/raw_pylibcpupower.i
 create mode 100755 tools/power/cpupower/bindings/python/test_raw_pylibcpupower.py
Signature · 1 line
2.46.0
author maintainer
#1 lore raw

PATCH v3 1/4 pm:cpupower: Add missing powercap_set_enabled() stub function

There was a symbol listed in the powercap.h file that was not implemented. Implement it with a stub return of 0.

Programs like SWIG require that functions that are defined in the headers be implemented.

---

Changes in v3:
- Renamed commit header, moved commit to be the first in the patchset as requested by Shuah Khan. Adjusted commit message body to match.
- Added pm:cpupower prefix to header and Rafael to Cc

Changes in v2:
- Implemented the function so SWIG will accept the header definition

---
 tools/power/cpupower/lib/powercap.c | 8 ++++++++
 1 file changed, 8 insertions(+)
tools/power/cpupower/lib/powercap.c +8 -0
index a7a59c6bacda..94a0c69e55ef 100644
@@ -77,6 +77,14 @@ int powercap_get_enabled(int *mode)
 	return sysfs_get_enabled(path, mode);
 }
 
+/*
+ * TODO: implement function. Returns dummy 0 for now.
+ */
+int powercap_set_enabled(int mode)
+{
+	return 0;
+}
+
 /*
  * Hardcoded, because rapl is the only powercap implementation
 - * this needs to get more generic if more powercap implementations
Signature · 1 line
2.46.0
author maintainer
#2 lore raw

PATCH v3 2/4 pm:cpupower: Add SWIG bindings files for libcpupower

In reply to John B. Wyatt IV

SWIG is a tool packaged in Fedora and other distros that can generate bindings from C and C++ code for several languages including Python, Perl, and Go.

These bindings allows users to easily write scripts that use and extend libcpupower's functionality. Currently, only Python is provided in the makefile, but additional languages may be added if there is demand.

Added suggestions from Shuah Khan for the README and license discussion.

Note that while SWIG itself is GPL v3+ licensed; the resulting output, the bindings code, is permissively licensed + the license of the .o files. Please see
https://swig.org/legal.html and [1] for more details.

[1]
https://lore.kernel.org/linux-pm/Zqv9BOjxLAgyNP5B@hatbackup/

---

Changes in v3:
- Reordered patch series for cpupower stub implementation to be first.
- Added pm:cpupower prefix to header and Rafael to Cc.

Changes in v2:
- Implemented the function so SWIG will accept the header definition

---
 .../power/cpupower/bindings/python/.gitignore |   8 +
 tools/power/cpupower/bindings/python/Makefile |  31 +++
 tools/power/cpupower/bindings/python/README   |  59 +++++
 .../bindings/python/raw_pylibcpupower.i       | 247 ++++++++++++++++++
 4 files changed, 345 insertions(+)
 create mode 100644 tools/power/cpupower/bindings/python/.gitignore
 create mode 100644 tools/power/cpupower/bindings/python/Makefile
 create mode 100644 tools/power/cpupower/bindings/python/README
 create mode 100644 tools/power/cpupower/bindings/python/raw_pylibcpupower.i
tools/power/cpupower/bindings/python/.gitignore new file +8 -0
new file mode 100644
index 000000000000..5c9a1f0212dd
@@ -0,0 +1,8 @@
+__pycache__/
+raw_pylibcpupower_wrap.c
+*.o
+*.so
+*.py
+!test_raw_pylibcpupower.py
+# git keeps ignoring this file, use git add -f raw_libcpupower.i
+!raw_pylibcpupower.i
tools/power/cpupower/bindings/python/Makefile new file +31 -0
new file mode 100644
index 000000000000..d0418f902795
@@ -0,0 +1,31 @@
+# SPDX-License-Identifier: GPL-2.0-only
+# Makefile for libcpupower's Python bindings
+#
+# This Makefile expects you have already run the makefile for cpupower to build
+# the .o files in the lib directory for the bindings to be created.
+
+CC=gcc
+
+LIB_DIR = ../../lib
+BIND_DIR = .
+PY_INCLUDE := $(firstword $(shell python-config --includes))
+#PY_INCLUDE = $(shell python-config --includes | awk '{ print $1 }')
+
+OBJECTS_LIB = $(wildcard $(LIB_DIR)/*.o)
+OBJECTS_BIND = $(wildcard $(BIND_DIR)/*.o)
+
+all: _raw_pylibcpupower.so
+
+_raw_pylibcpupower.so: raw_pylibcpupower_wrap.o
+	$(CC) -shared $(OBJECTS_LIB) raw_pylibcpupower_wrap.o -o _raw_pylibcpupower.so # raw_pylibcpupower_wrap.o
+#	$(CC) -shared $(OBJECTS_BIND) $(OBJECTS_LIB) -o _raw_pylibcpupower.so # raw_pylibcpupower_wrap.o
+
+raw_pylibcpupower_wrap.o: raw_pylibcpupower_wrap.c
+	$(CC) -fPIC -c raw_pylibcpupower_wrap.c $(PY_INCLUDE)
+
+raw_pylibcpupower_wrap.c: raw_pylibcpupower.i
+	swig -python raw_pylibcpupower.i
+
+# Will only clean the bindings folder; will not clean the actual cpupower folder
+clean:
+	rm -f raw_pylibcpupower.py raw_pylibcpupower_wrap.c raw_pylibcpupower_wrap.o _raw_pylibcpupower.so
tools/power/cpupower/bindings/python/README new file +59 -0

59 lines not on this page · raw message

tools/power/cpupower/bindings/python/raw_pylibcpupower.i new file +247 -0

247 lines not on this page · raw message

Signature · 1 line
2.46.0
#3 lore raw

Re: [PATCH v3 2/4] pm:cpupower: Add SWIG bindings files for libcpupower

Hi,

Quoted 31 lines · John B. Wyatt IV

SWIG is a tool packaged in Fedora and other distros that can generate bindings from C and C++ code for several languages including Python, Perl, and Go.

These bindings allows users to easily write scripts that use and extend libcpupower's functionality. Currently, only Python is provided in the makefile, but additional languages may be added if there is demand.

Added suggestions from Shuah Khan for the README and license discussion.

Note that while SWIG itself is GPL v3+ licensed; the resulting output, the bindings code, is permissively licensed + the license of the .o files. Please see
https://swig.org/legal.html and [1] for more details.

[1]
https://lore.kernel.org/linux-pm/Zqv9BOjxLAgyNP5B@hatbackup/

---

Changes in v3:
- Reordered patch series for cpupower stub implementation to be first.
- Added pm:cpupower prefix to header and Rafael to Cc.

Changes in v2:
- Implemented the function so SWIG will accept the header definition

Quoted 10 lines
---
 .../power/cpupower/bindings/python/.gitignore |   8 +
 tools/power/cpupower/bindings/python/Makefile |  31 +++
 tools/power/cpupower/bindings/python/README   |  59 +++++
 .../bindings/python/raw_pylibcpupower.i       | 247 ++++++++++++++++++
 4 files changed, 345 insertions(+)
 create mode 100644 tools/power/cpupower/bindings/python/.gitignore
 create mode 100644 tools/power/cpupower/bindings/python/Makefile
 create mode 100644 tools/power/cpupower/bindings/python/README
 create mode 100644 tools/power/cpupower/bindings/python/raw_pylibcpupower.i

This file is deleted when running "make mrproper".

$ git reset --hard HEAD
$ make mrproper
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory)
deleted: tools/power/cpupower/bindings/python/raw_pylibcpupower.i

That is a bit more than annoying when trying to switch branches after that happened.

Guenter

#4 lore raw

Re: [PATCH v3 2/4] pm:cpupower: Add SWIG bindings files for libcpupower

Quoted 6 lines · Guenter Roeck
Quoted 2 lines · John B. Wyatt IV
 .../bindings/python/raw_pylibcpupower.i       | 247 ++++++++++++++++++

This file is deleted when running "make mrproper".

Yes, please don't create files called '*.i", "*.s" or "*.o". We assume they are generated files (the result of the preprocessor, the compiler, and the assembler respectively).

And yeah, obviously "*.i" and "*.s" and not _usually_ seen, because they are only steps on the way to "*.o", so maybe they aren't _quite_ as obvious as some "*.o" file would be, but they are very much standard naming, and we have build rules for them (ie "make kernel/exit.i" will create the pre-processed result of exit.c).

Linus

author maintainer
#5 lore raw

Re: [PATCH v3 2/4] pm:cpupower: Add SWIG bindings files for libcpupower

Quoted 12 lines · Linus Torvalds
Quoted 6 lines · Guenter Roeck
Quoted 2 lines · John B. Wyatt IV
 .../bindings/python/raw_pylibcpupower.i       | 247 ++++++++++++++++++

This file is deleted when running "make mrproper".

Yes, please don't create files called '*.i", "*.s" or "*.o". We assume they are generated files (the result of the preprocessor, the compiler, and the assembler respectively).

Quoted 6 lines

And yeah, obviously "*.i" and "*.s" and not _usually_ seen, because they are only steps on the way to "*.o", so maybe they aren't _quite_ as obvious as some "*.o" file would be, but they are very much standard naming, and we have build rules for them (ie "make kernel/exit.i" will create the pre-processed result of exit.c).

My apologies. This was reported and fixed with this patch by renaming the file to '.swg'. '.swg' is the second filename extension used by SWIG.

https://lore.kernel.org/linux-pm/[email protected]/

Signature · 4 lines
Sincerely,
John Wyatt
Software Engineer, Core Kernel
Red Hat
maintainer
#6 lore raw

Re: [PATCH v3 2/4] pm:cpupower: Add SWIG bindings files for libcpupower

Quoted 20 lines · John B. Wyatt IV
Quoted 12 lines · Linus Torvalds
Quoted 6 lines · Guenter Roeck
Quoted 2 lines · John B. Wyatt IV
  .../bindings/python/raw_pylibcpupower.i       | 247 ++++++++++++++++++

This file is deleted when running "make mrproper".

Yes, please don't create files called '*.i", "*.s" or "*.o". We assume they are generated files (the result of the preprocessor, the compiler, and the assembler respectively).

Quoted 6 lines

And yeah, obviously "*.i" and "*.s" and not _usually_ seen, because they are only steps on the way to "*.o", so maybe they aren't _quite_ as obvious as some "*.o" file would be, but they are very much standard naming, and we have build rules for them (ie "make kernel/exit.i" will create the pre-processed result of exit.c).

Quoted 6 lines

My apologies. This was reported and fixed with this patch by renaming the file to '.swg'. '.swg' is the second filename extension used by SWIG.

https://lore.kernel.org/linux-pm/[email protected]/

It was quickly found and fixed. I will send this to Rafael to get this into rc1.

thanks,
-- Shuah

author maintainer
#7 lore raw

PATCH v3 3/4 pm:cpupower: Include test_raw_pylibcpupower.py

In reply to John B. Wyatt IV

This script demonstrates how to make use of, and tests, the bindings.

In the future, this script could become part of a larger test suite to test the bindings and libcpupower.

---

Changes in v3:
- Small puncations below fixed.
- Reordered patch series for cpupower stub implementation to be first.
- Added pm:cpupower prefix to header and Rafael to Cc.

Changes in v2:
- None.

---
 .../bindings/python/test_raw_pylibcpupower.py | 42 +++++++++++++++++++
 1 file changed, 42 insertions(+)
 create mode 100755 tools/power/cpupower/bindings/python/test_raw_pylibcpupower.py
tools/power/cpupower/bindings/python/test_raw_pylibcpupower.py new file +42 -0
new file mode 100755
index 000000000000..3d6f62b9556a
@@ -0,0 +1,42 @@
+#!/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 is 0, ex: a virtual machine)
+"""
+cstate_disabled = p.cpuidle_state_disable(0, 0, 1)
+if cpu_cstates_count == 0:
+    print(f"CPU 0 has {cpu_cstates_count} c-states")
+else:
+    print(f"cstate count error: return code: {cpu_cstates_count}")
+
+match cstate_disabled:
+    case 0:
+        print(f"CPU state disabled")
+    case -1:
+        print(f"Idlestate not available")
+    case _:
+        print(f"Not documented")
+
+
+# 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")
Signature · 1 line
2.46.0
author maintainer
#8 lore raw

PATCH v3 4/4 MAINTAINERS: Add Maintainers for SWIG Python bindings

In reply to John B. Wyatt IV

Adding myself as the primary maintainer and John Kacur as the backup maintainer for the libcpupower SWIG generated Python bindings.

---

Changes in v3:
- Added Rafael to Cc.

Changes in v2:
- Added as requested.

---
 MAINTAINERS | 3 +++
 1 file changed, 3 insertions(+)
MAINTAINERS +3 -0
index 62a00f9471c1..1b2bbc8c5adb 100644
@@ -5821,6 +5821,9 @@ CPU POWER MONITORING SUBSYSTEM
 M:	Thomas Renninger <[email protected]>
 M:	Shuah Khan <[email protected]>
 M:	Shuah Khan <[email protected]>
+M:	John B. Wyatt IV <[email protected]>
+M:	John B. Wyatt IV <[email protected]>
+M:	John Kacur <[email protected]>
 L:	[email protected]
 S:	Maintained
 F:	tools/power/cpupower/
Signature · 1 line
2.46.0
maintainer
#9 lore raw
In reply to John B. Wyatt IV
Quoted 59 lines · John B. Wyatt IV

SWIG is a tool packaged in Fedora and other distros that can generate bindings from C and C++ code for several languages including Python, Perl, and Go. Providing bindings for scripting languages is a common feature to make use of libraries more accessible to more users and programs. My team specifically wants to expand the features of rteval. rteval is a Python program used to measure real time performance. We wanted to test the effect of enabling some levels of idle-stat to see how it affects latency, and didn't want to reinvent the wheel. Since SWIG requires the .o files created by libcpupower at compilation it makes sense to include this in the cpupower directory so that others can make use of them.

The V3 of this patchset includes:
* renaming header messages as requested and adding people to Cc as requested
* moving the stub (dummy) commit to the front of the patchset
* small punctuation fixes

The V2 of this patchset includes:
* the full definition of libcpupower headers that is needed for the bindings
* dummy implementation in C of a function listed in the header of libcpupower (requested by Shuah Khan)
* test_raw_pylibcpupower.py demonstrates an example of using the bindings
* adding myself and John Kacur to the cpupower section of the maintainers file (requested by Shuah Khan)
* addressed review comments about doc, makefile, and maintainers file
* small style and other fixes

The name raw_pylibcpupower is used because a wrapper pylibcpupower may be needed to make the bindings more 'pythonic' in the future. The bindings folder is used because Go or Perl bindings may be useful for other users in the future.

Note that while SWIG itself is GPL v3+ licensed; the resulting output, the bindings code, has the same license as the .o files used to generate the bindings (GPL v2 only). Please see
https://swig.org/legal.html
and
https://lore.kernel.org/linux-pm/Zqv9BOjxLAgyNP5B@hatbackup/#t
for more details on the license.

Sincerely,
John Wyatt
Software Engineer, Core Kernel
Red Hat

John B. Wyatt IV (4):
pm:cpupower: Add missing powercap_set_enabled() stub function pm:cpupower: Add SWIG bindings files for libcpupower pm:cpupower: Include test_raw_pylibcpupower.py
MAINTAINERS: Add Maintainers for SWIG Python bindings

  MAINTAINERS                                   |   3 +
  .../power/cpupower/bindings/python/.gitignore |   8 +
  tools/power/cpupower/bindings/python/Makefile |  31 +++
  tools/power/cpupower/bindings/python/README   |  59 +++++
  .../bindings/python/raw_pylibcpupower.i       | 247 ++++++++++++++++++
  .../bindings/python/test_raw_pylibcpupower.py |  42 +++
  tools/power/cpupower/lib/powercap.c           |   8 +
  7 files changed, 398 insertions(+)
Quoted 6 lines

create mode 100644 tools/power/cpupower/bindings/python/.gitignore create mode 100644 tools/power/cpupower/bindings/python/Makefile create mode 100644 tools/power/cpupower/bindings/python/README create mode 100644 tools/power/cpupower/bindings/python/raw_pylibcpupower.i create mode 100755 tools/power/cpupower/bindings/python/test_raw_pylibcpupower.py

Applied the series for Linux 6.12-rc1 to

https://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux.git cpupower branch

I chose to walk through the steps without installing SWIG to see what make would do.

Some things to improve:

Running make in tools/power/cpupower/bindings/python directory without installing SWIG gives me the following errors:

make: python-config: No such file or directory swig -python raw_pylibcpupower.i
make: swig: No such file or directory
make: *** [Makefile:27: raw_pylibcpupower_wrap.c] Error 127

I think it would be good to provide better help message walking user through installing the dependencies.

Documentation/Makefile is a good example to look at for useful message on the dependencies to be installed.

You can send me patch on top of
https://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux.git cpupower branch

thanks,
-- Shuah