Skip to contentSkip to conversation

Add SWIG Bindings to libcpupower

PATCH Superseded John B. Wyatt IV 2 participants 9 messages Aug 27, 2024 – Sep 4, 2024 CPU power monitoring

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

John B. Wyatt IV

author maintainer
#0 lore raw

v2 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 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):
Add SWIG bindings files for libcpupower
Implement dummy function for SWIG to accept the full library
definitions
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 v2 1/4 Add SWIG bindings files for 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.

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 v2:
- Implemented the full bindings from libcpupower headers
- Addressed review comments about doc and makefile
- Small style and other fixes

---
 .../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
author maintainer
#2 lore raw

PATCH v2 2/4 Implement dummy function for SWIG to accept the full library definitions

In reply to John B. Wyatt IV

There was a symbol listed in the powercap.h file that was not implemented that caused SWIG to give a symbol not found error, despite not being used.

As suggested, implement a dummy function that returns 0 so SWIG accepts the .o files.

---

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 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
#3 lore raw

PATCH v2 3/4 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 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
#4 lore raw

PATCH v2 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 v2:
- Added as requested.

---
 MAINTAINERS | 3 +++
 1 file changed, 3 insertions(+)
MAINTAINERS +3 -0
index f328373463b0..80d5475c943b 100644
@@ -5819,6 +5819,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
#5 lore raw
In reply to John B. Wyatt IV
Quoted 54 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 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):
Add SWIG bindings files for libcpupower
Implement dummy function for SWIG to accept the full library
definitions
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

Couple of things to address:

1. I noticed none of the patches have the subsystem prefix:
pm:cpupower is the right prefix for patch subject for all the patches except the MAINTAINERS file

I will pull the fix "Implement dummy function for SWIG to accept the full library" Patch 2 in your series.

I want this subject changed to just fix as it is a problem irrespective of SWIG - we have a missing function. Subject would be as follows:

""pm:cpupower: Add missing powercap_set_enabled() stub function"

Make this the first patch in the series. I will send this up for Linux 6.11-rc7 or Linux 6.12-rc1

Depending on how the timelines for merge window work, expect the series to land in Linux 6.13-rc1. I would prefer to delay it anyway so we can get some soaking in next.

thanks,
-- Shuah

maintainer
#6 lore raw
Quoted 47 lines · Shuah Khan
Quoted 46 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 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):
   Add SWIG bindings files for libcpupower
   Implement dummy function for SWIG to accept the full library definitions
   Include test_raw_pylibcpupower.py
   MAINTAINERS: Add Maintainers for SWIG Python bindings

Quoted 15 lines
Quoted 7 lines

  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 +

Quoted 7 lines
  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

Noticed Rafael isn't on this thread. Adding now to keep him in the loop.

cpupower pull request go through Rafael's tree.

Quoted 12 lines

Couple of things to address:

1. I noticed none of the patches have the subsystem prefix:
  pm:cpupower is the right prefix for patch subject for all the patches except the MAINTAINERS file

I will pull the fix  "Implement dummy function for SWIG to accept the full library" Patch 2 in your series.

I want this subject changed to just fix as it is a problem irrespective of SWIG - we have a missing function. Subject would be as follows:

Quoted 9 lines

""pm:cpupower: Add missing powercap_set_enabled() stub function"

Make this the first patch in the series. I will send this up for Linux 6.11-rc7 or Linux 6.12-rc1

Depending on how the timelines for merge window work, expect the series to land in Linux 6.13-rc1. I would prefer to delay it anyway so we can get some soaking in next.

thanks,
-- Shuah

author maintainer
#7 lore raw
In reply to Shuah Khan
Quoted 12 lines · Shuah Khan
Quoted 1 line · John B. Wyatt IV

[snipped]

Couple of things to address:

1. I noticed none of the patches have the subsystem prefix:
pm:cpupower is the right prefix for patch subject for all the patches except the MAINTAINERS file

I will pull the fix "Implement dummy function for SWIG to accept the full library" Patch 2 in your series.

Quoted 7 lines

I want this subject changed to just fix as it is a problem irrespective of SWIG - we have a missing function. Subject would be as follows:

""pm:cpupower: Add missing powercap_set_enabled() stub function"

Make this the first patch in the series. I will send this up for Linux 6.11-rc7 or Linux 6.12-rc1

Understood.

Quoted 3 lines

Depending on how the timelines for merge window work, expect the series to land in Linux 6.13-rc1. I would prefer to delay it anyway so we can get some soaking in next.

Thank you, I appreciate it.

What kind of soaking are you looking for? Is there anything I can do to help?

Signature · 4 lines
Sincerely,
John Wyatt
Software Engineer, Core Kernel
Red Hat
maintainer
#8 lore raw
Quoted 23 lines · John B. Wyatt IV
Quoted 12 lines · Shuah Khan
Quoted 1 line · John B. Wyatt IV

[snipped]

Couple of things to address:

1. I noticed none of the patches have the subsystem prefix:
pm:cpupower is the right prefix for patch subject for all the patches except the MAINTAINERS file

I will pull the fix "Implement dummy function for SWIG to accept the full library" Patch 2 in your series.

Quoted 7 lines

I want this subject changed to just fix as it is a problem irrespective of SWIG - we have a missing function. Subject would be as follows:

""pm:cpupower: Add missing powercap_set_enabled() stub function"

Make this the first patch in the series. I will send this up for Linux 6.11-rc7 or Linux 6.12-rc1

Understood.

Quoted 9 lines
Quoted 3 lines

Depending on how the timelines for merge window work, expect the series to land in Linux 6.13-rc1. I would prefer to delay it anyway so we can get some soaking in next.

Thank you, I appreciate it.

What kind of soaking are you looking for? Is there anything I can do to help?

It is more the timing than anything you can do. We are at rc6. Please do send the series with the changes I requested.

thanks,
-- Shuah