From nobody Thu Dec 18 12:13:45 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C958513C9CA for ; Wed, 24 Jul 2024 22:13:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721859238; cv=none; b=teCChZYxVZf0OdkwtoOGX9S2mWrMN8Ohks/WzHYJ9x7bHvh+kwrZ++fl3xqQ7aD35i7q3XQLkQQfS/OH1nx+D5aupt1RW2q9C1r0duI2ZJiK4Jks+JMw/JR+Zr8b1iy7zpDWltfyoPk6tVUDWxKb6/jfNCS0Ry5QJRCWsYPscZ0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721859238; c=relaxed/simple; bh=rdxqSma22UUNhO8GRP1xfHjICj9ttsvc4m76NncJc0k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=W3YI5Nm1jU5J7BgsIYQSrMkGIl9EG5kVs6q74Ys4MiFDLvh/gIUi+dTiLWVBUb7ir6YcOsqbn6aIRMg5K42GBFY5+beLvPN3xNiV+yN8bV6YiiptrhFIh56iuvTEVE/G8Yl8wqwZeWkrbF2fgQiHsZWEh6pa6ekKMRFut33ieeA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=F3giVy6D; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="F3giVy6D" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721859235; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ZZDN0EpYdGH4QTVcef68WhZG9zzbZoHSE92tNxLJUpI=; b=F3giVy6DvfyHEElymKk+N/hm5curq56zJ+UrSvGKveAey49fN/ote/sueBWW1XQAbCEdNV 7GZp9SADX3MB8COqaNj7atK7dVQ0au8ijFOmtD514Uz0gbIQdx9j+umovQgRoWc3wWI4tg MyF6txWrh8tLk7f8Jur9v9nw4mU+PNg= Received: from mail-ua1-f71.google.com (mail-ua1-f71.google.com [209.85.222.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-344-fDWMgqDlOu-mHs6qCGbNuA-1; Wed, 24 Jul 2024 18:13:54 -0400 X-MC-Unique: fDWMgqDlOu-mHs6qCGbNuA-1 Received: by mail-ua1-f71.google.com with SMTP id a1e0cc1a2514c-8200332b4f2so79951241.0 for ; Wed, 24 Jul 2024 15:13:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721859234; x=1722464034; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZZDN0EpYdGH4QTVcef68WhZG9zzbZoHSE92tNxLJUpI=; b=es2fsAT5JeTgtJQPvwT2aFF9cfda0Jpl+aM/buuXOydrbOytfqZGNuiSCO7juSw8IV H9x0yc3VCaie1iwvhEt0t//TexZdEiu07Hg4g4hkB4RrT5r/HQv8ObuOqnsXgQykpjyQ a9OWmrw3z9gVbHT7dWbFeB0odNtA6ffS6bnUXUXXu+eLBUbfd1/Qy2jJQd/Q5g9veIJz Mupb8nWrXuheIBVZF4v0g/CZdzKhzapFutlyqYaR2F36478xTGhp/VzWg3ttpZHvMwFU FoFm8962oYuQ/ivqVjfbDvxcoBpyLLp0pNRIagUocFe0WWLON8qUyoQYzlmNtNqduncr n7tA== X-Forwarded-Encrypted: i=1; AJvYcCUL4fKK2NEMXkLwdplqMSoqtTlzYMRRlkCpTC/C16D0kq2a0EQ94IIdf5R419oh8RUx/vmx+aN7q1mNsvFf2adrobLlYXvyr179I00J X-Gm-Message-State: AOJu0YxGe46NrY/DsSsLr5QRZGMs/olzFe24lSF61OpvVJhWAjS9CpKK KWwuTARv4jat3ArU08dflBNRYQHIjo4qN/rH/0IS7Y+QcGnYcsQmveRzPEyURFHSjq55f+scuWn GdUjTQick9ZBT1J88jyQyFoSxf7aPCmWl1aZ6wGcleHbjfGPHDoLlIM9Wgm3MOw== X-Received: by 2002:a05:6102:2c86:b0:492:8dbc:c1c with SMTP id ada2fe7eead31-493d9aceda8mr167987137.16.1721859233639; Wed, 24 Jul 2024 15:13:53 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFcd3gUwhhsm4W+Vb5EQ8SWvV970VSd0d0iC1pmnTuNlKfecuyjyVo3PW17Zk2UTrUJmcQwlg== X-Received: by 2002:a05:6102:2c86:b0:492:8dbc:c1c with SMTP id ada2fe7eead31-493d9aceda8mr167960137.16.1721859233335; Wed, 24 Jul 2024 15:13:53 -0700 (PDT) Received: from hatbackup.redhat.com ([71.217.44.209]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7a1d7435227sm7542685a.82.2024.07.24.15.13.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jul 2024 15:13:53 -0700 (PDT) From: "John B. Wyatt IV" To: linux-pm@vger.kernel.org, "Thomas Renninger" , "Shuah Khan" , "Shuah Khan" Cc: "John B. Wyatt IV" , linux-kernel@vger.kernel.org, "John Kacur" , "Tomas Glozar" , "John B. Wyatt IV" Subject: [PATCH 1/2] Add SWIG bindings files for libcpupower Date: Wed, 24 Jul 2024 18:11:19 -0400 Message-ID: <20240724221122.54601-2-jwyatt@redhat.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240724221122.54601-1-jwyatt@redhat.com> References: <20240724221122.54601-1-jwyatt@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. A latter commit will demonstrate taking a SWIG .i file and the libcpupower object files to generate the bindings. Note that while SWIG itself is GPL v3+ licensed; the resulting output, the bindings code, is permissively licensed. Please see https://swig.org/legal.html for more details. Signed-off-by: John B. Wyatt IV Signed-off-by: John B. Wyatt IV --- .../power/cpupower/bindings/python/.gitignore | 8 ++++ tools/power/cpupower/bindings/python/Makefile | 31 +++++++++++++ tools/power/cpupower/bindings/python/README | 33 ++++++++++++++ .../bindings/python/raw_pylibcpupower.i | 45 +++++++++++++++++++ 4 files changed, 117 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 diff --git a/tools/power/cpupower/bindings/python/.gitignore b/tools/power/= cpupower/bindings/python/.gitignore new file mode 100644 index 000000000000..b83e0f5e26a0 --- /dev/null +++ b/tools/power/cpupower/bindings/python/.gitignore @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: GPL-2.0-only +__pycache__/ +*.c +*.o +*.so +*.py +!test_raw_pylibcpupower.py +!raw_pylibcpupower.i # git keeps ignoring this file diff --git a/tools/power/cpupower/bindings/python/Makefile b/tools/power/cp= upower/bindings/python/Makefile new file mode 100644 index 000000000000..a6c779333f8d --- /dev/null +++ b/tools/power/cpupower/bindings/python/Makefile @@ -0,0 +1,31 @@ +# SPDX-License-Identifier: GPL-2.0-only +# Makefile for libcpupower 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=3Dgcc + +LIB_DIR =3D ../../lib +BIND_DIR =3D . +PY_INCLUDE :=3D $(firstword $(shell python-config --includes)) +#PY_INCLUDE =3D $(shell python-config --includes | awk '{ print $1 }') + +OBJECTS_LIB =3D $(wildcard $(LIB_DIR)/*.o) +OBJECTS_BIND =3D $(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_pylibcpupow= er.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_wra= p.o _raw_pylibcpupower.so diff --git a/tools/power/cpupower/bindings/python/README b/tools/power/cpup= ower/bindings/python/README new file mode 100644 index 000000000000..a2663a2d23d1 --- /dev/null +++ b/tools/power/cpupower/bindings/python/README @@ -0,0 +1,33 @@ + +This folder contains the necessary files to build Python bindings for libc= pupower. + +To begin, first build the object files for libcpupower by running make in +the cpupower directory. + +Next you will need to install SWIG. Using Fedora: + +sudo dnf install swig + +Please check that your version of SWIG is compatible with the version of +Python installed on your machine by checking the SWIG changelog on their +website. https://swig.org/ + +Then return to the directory this README is in to run: + +make + +To run the test script: + +python test_raw_pylibcpupower.py + +Note that while SWIG itself is GPL v3+ licensed; the resulting output, +the bindings code, is permissively licensed. Please see +https://swig.org/legal.html for more details. + +Original Bindings Author: +John B. Wyatt IV +jwyatt@redhat.com +sageofredondo@gmail.com + +Copyright (C) 2024 Red Hat + diff --git a/tools/power/cpupower/bindings/python/raw_pylibcpupower.i b/too= ls/power/cpupower/bindings/python/raw_pylibcpupower.i new file mode 100644 index 000000000000..9a920ecf1f47 --- /dev/null +++ b/tools/power/cpupower/bindings/python/raw_pylibcpupower.i @@ -0,0 +1,45 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +%module raw_pylibcpupower +%{ +#include "../../lib/cpupower_intern.h" +#include "../../lib/acpi_cppc.h" +#include "../../lib/cpufreq.h" +#include "../../lib/cpuidle.h" +#include "../../lib/cpupower.h" +#include "../../lib/powercap.h" +%} + +/* + * cpuidle.h + */ + +unsigned int cpuidle_state_count(unsigned int cpu); +int cpuidle_state_disable(unsigned int cpu, unsigned int idlestate, unsign= ed int disable); + +/* + * cpupower.h + */ + +struct cpupower_topology { + /* Amount of CPU cores, packages and threads per core in the system */ + unsigned int cores; + unsigned int pkgs; + unsigned int threads; /* per core */ + + /* Array gets mallocated with cores entries, holding per core info */ + struct cpuid_core_info *core_info; +}; + +struct cpuid_core_info { + int pkg; + int core; + int cpu; + + /* flags */ + unsigned int is_online:1; +}; + +int get_cpu_topology(struct cpupower_topology *cpu_top); +void cpu_topology_release(struct cpupower_topology cpu_top); +int cpupower_is_cpu_online(unsigned int cpu); --=20 2.45.2 From nobody Thu Dec 18 12:13:45 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B898113CA93 for ; Wed, 24 Jul 2024 22:13:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721859241; cv=none; b=UvaypUDpghTSeAmsVpemQcgX/B8yWH1JnHHcYTgwwcPwdvVP0q3zPpTEEx/mwivX6SJOx3uyMpdjTiC01IgHQJYJg/ViMDyhHG1RlIBYvEGmlhfRT5leugTibqDio7XyIgib9QVbA5YWleehTbdta43UlFtCy32/hhtMOfsA4Nw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721859241; c=relaxed/simple; bh=gNmWzbPCwsNn3iBECMmvE1pa/3xdm645kUt+trqbGcc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QhwbNcGKqMusfyuQCKJzGZn0ZvG0/+HEf3aMZWonwu0ZmF7qZ42zRyZbGwthwlV4gM1l5EobF+oRQ9ueP7VfEn8mju35NEmw2t+XxmhjNXCZ4zMD8V/k0M7ZZcy1We6GBcoyXjsY6Bt1ZxlpwAYN/SeqyuGpc0orT5gMCgQjMX8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=arbF3inG; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="arbF3inG" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721859238; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1oDlIFLL5CHDliwMOUVKHCIO32QiYYQzCXXQNVqBa5o=; b=arbF3inG6LF6vrqTDNZU73E+EAsZnpTEXJn32lEYzbonfHwZQNy0lyTs/rkNC+JslvfyfT c7ct3fBIe2Zp1sPe2V2AvFdmnvAL7jiuhWGKQGOWJoYsKMpGT0i441jIgxx3V5NU9b//TM lTvywpez78Ije+VDziitsnPVvSd+mPQ= Received: from mail-oo1-f69.google.com (mail-oo1-f69.google.com [209.85.161.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-695-oKyiQaJnPN2PyKLIqirkxw-1; Wed, 24 Jul 2024 18:13:57 -0400 X-MC-Unique: oKyiQaJnPN2PyKLIqirkxw-1 Received: by mail-oo1-f69.google.com with SMTP id 006d021491bc7-5d1f7855cd7so243079eaf.1 for ; Wed, 24 Jul 2024 15:13:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721859235; x=1722464035; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1oDlIFLL5CHDliwMOUVKHCIO32QiYYQzCXXQNVqBa5o=; b=Cud7+VL2jLzmEUQ9Tj5Dfn3T/lvdBLRIITJxfcfeulPSqbfAMvaZC2V+ByWTbdJR7a wW/ApyUnt6xnaWHL3AbDuXT1gWimi+EoSrAK4/3y2LKzw+bKhJbehTDCtGIpn/Cqg6Vk GoFHnAu+BLX9/b2HzWCziAHPsDNUUbBXStTqKDy8ZL2ewuTGWJqDea8RDg2DuLSKBSKr BZOVtmHU9OvfYfPgolslPlskNYw/L/ud6Rnb+mwMnEAPoIAS6+TnG908mtoH0CrB3drx E6eD0DfGq4bhUb9/C+tX7eVl9zytY1AkQGsR4v9rlKuXFR+hg11qaD5DwpqzMzlVXr28 pbWg== X-Forwarded-Encrypted: i=1; AJvYcCX9thwbCOlVMFyfs1EfWPm15c+4+ggOMVbMT7Cpq/QHSeXpVqDpMKRoeQtApfe4GrsRARnxfHU64drdlm+Ku66EYRUVj/f6XjpVKvXz X-Gm-Message-State: AOJu0YwAduT19VOdefoGZOqBwB7Jvta8QZ8BdaN/YSftvn9poczYHEWu AhAXNmm6ghvsL9cbjNkiFKmJpDm+8am4KnIefTDHothe2q9MFvKssrVDNIaDFpZeCl+GLhAbbPo /kz1g7Z6cGiPssURcxxiCZn9YxYI6K9/5QyNPiP48HPSd+zBRkON6nHZ3axX8HSGY9m2yZarE X-Received: by 2002:a05:6359:4595:b0:1a8:c0fd:cf2d with SMTP id e5c5f4694b2df-1acf8869b8fmr161039955d.9.1721859235481; Wed, 24 Jul 2024 15:13:55 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF8XXO039xnVzDHimc3GzVZUVBr2GcfRiEvX7FSDvCGjlyRhlzijS5vlgD8K63bsxdP19pT4Q== X-Received: by 2002:a05:6359:4595:b0:1a8:c0fd:cf2d with SMTP id e5c5f4694b2df-1acf8869b8fmr161037355d.9.1721859235139; Wed, 24 Jul 2024 15:13:55 -0700 (PDT) Received: from hatbackup.redhat.com ([71.217.44.209]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7a1d7435227sm7542685a.82.2024.07.24.15.13.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jul 2024 15:13:54 -0700 (PDT) From: "John B. Wyatt IV" To: linux-pm@vger.kernel.org, "Thomas Renninger" , "Shuah Khan" , "Shuah Khan" Cc: "John B. Wyatt IV" , linux-kernel@vger.kernel.org, "John Kacur" , "Tomas Glozar" , "John B. Wyatt IV" Subject: [PATCH 2/2] Include test_raw_pylibcpupower.py Date: Wed, 24 Jul 2024 18:11:20 -0400 Message-ID: <20240724221122.54601-3-jwyatt@redhat.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240724221122.54601-1-jwyatt@redhat.com> References: <20240724221122.54601-1-jwyatt@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This script demonstrates how to make use of, and tests, the bindings. The .i file currently only binds a limited subsection of functionality with the bindings. Signed-off-by: John B. Wyatt IV Signed-off-by: John B. Wyatt IV --- .../bindings/python/test_raw_pylibcpupower.py | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100755 tools/power/cpupower/bindings/python/test_raw_pylibcpup= ower.py diff --git a/tools/power/cpupower/bindings/python/test_raw_pylibcpupower.py= b/tools/power/cpupower/bindings/python/test_raw_pylibcpupower.py new file mode 100755 index 000000000000..3d6f62b9556a --- /dev/null +++ b/tools/power/cpupower/bindings/python/test_raw_pylibcpupower.py @@ -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 =3D 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 =3D p.cpuidle_state_disable(0, 0, 1) +if cpu_cstates_count =3D=3D 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 =3D p.cpupower_topology() +total_cpus =3D 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") --=20 2.45.2