Skip to contentSkip to conversation

RAPL: Use Powercap sysfs

PATCH New Srinivas Pandruvada 1 participant 3 messages Jun 5, 2014

lore
Share & tools
lore

Srinivas Pandruvada

author
#0 lore raw

PATCH 1/3 RAPL: Use Powercap sysfs

Linux Powercap sysfs with Intel RAPL driver exists from 3.13.rc1. Use this to estimate RAPL domain power instead of MSRs. Since these MSRs are non-architectural, they can be changed from processor model to model. But by using powercap sysfs, kernel driver will take care about processor specific variations.

---
 src/cpu/rapl/rapl_interface.cpp | 91 ++++++++++++++++++++++++++++++++++++++++-
 src/cpu/rapl/rapl_interface.h   |  7 +++-
 2 files changed, 96 insertions(+), 2 deletions(-)
src/cpu/rapl/rapl_interface.cpp +90 -1
index 6dd7b26..a19e0a9 100644
@@ -27,6 +27,8 @@
 #include <unistd.h>
 #include <errno.h>
 #include <math.h>
+#include <stdlib.h>
+#include <dirent.h>
 #include "lib.h"
 #include "rapl_interface.h"
 
@@ -68,7 +70,11 @@
 #define PP0_DOMAIN_PRESENT	0x04
 #define PP1_DOMAIN_PRESENT	0x08
 
-c_rapl_interface::c_rapl_interface(int cpu) :
+c_rapl_interface::c_rapl_interface(const char *dev_name, int cpu) :
+	powercap_sysfs_present(false),
+	powercap_core_path(),
+	powercap_uncore_path(),
+	powercap_dram_path(),
 	measurment_interval(def_sampling_interval),
 	first_cpu(cpu),
 	last_pkg_energy_status(0.0),
@@ -78,10 +84,63 @@ c_rapl_interface::c_rapl_interface(int cpu) :
 {
 	uint64_t value;
 	int ret;
+	string package_path;
+	DIR *dir;
+	struct dirent *entry;
 
 	RAPL_INFO_PRINT("RAPL device for cpu %d\n", cpu);
 
 	rapl_domains = 0;
+
+	if (dev_name) {
+		string base_path = "/sys/class/powercap/intel-rapl/";
+		if ((dir = opendir(base_path.c_str())) != NULL) {
+			while ((entry = readdir(dir)) != NULL) {
+				string path = base_path + entry->d_name + "/name";
+				string str = read_sysfs_string(path);
+				if (str.length() > 0) {
+					if (str == dev_name) {
+						package_path = base_path + entry->d_name + "/";
+						powercap_sysfs_present = true;
+						rapl_domains |= PKG_DOMAIN_PRESENT;
+						break;
+					}
+				}
+			}
+			closedir(dir);
+		}
+	}
+
+	if (powercap_sysfs_present) {
+		if ((dir = opendir(package_path.c_str())) != NULL) {
+			while ((entry = readdir(dir)) != NULL) {
+				string path = package_path + entry->d_name;
+				string str = read_sysfs_string(path + "/name");
+				if (str.length() > 0) {
+					if (str == "core") {
+						rapl_domains |= PP0_DOMAIN_PRESENT;
+						powercap_core_path = path + "/";
+					}
+					else if (str == "dram") {
+						rapl_domains |= DRAM_DOMAIN_PRESENT;
+						powercap_dram_path = path + "/";
+					}
+					else if (str == "uncore") {
+						rapl_domains |= PP1_DOMAIN_PRESENT;
+						powercap_uncore_path = path + "/";
+					}
+				}
+			}
+			closedir(dir);
+		}
+
+		RAPL_INFO_PRINT("RAPL Using PowerCap Sysfs : Domain Mask %x\n",
+														rapl_domains);
+		return;
+	}
+
+	// Fallback to using MSRs
+
 	// presence of each domain
 	// Check presence of PKG domain
 	ret = read_msr(first_cpu, MSR_PKG_ENERY_STATUS, &value);
@@ -312,6 +371,16 @@ int c_rapl_interface::get_dram_energy_status(double *status)
 		return -1;
 	}
 
+	if (powercap_sysfs_present) {
+		string str = read_sysfs_string(powercap_dram_path + "energy_uj");
+		if (str.length() > 0) {
+			*status =  atof(str.c_str()) / 1000000; // uj to Js
+			return 0;
+		}
+
+		return -EINVAL;
+	}
+
 	ret = read_msr(first_cpu, MSR_DRAM_ENERY_STATUS, &value);
 	if(ret < 0)
 	{
@@ -393,6 +462,16 @@ int c_rapl_interface::get_pp0_energy_status(double *status)
 		return -1;
 	}
 
+	if (powercap_sysfs_present) {
+		string str = read_sysfs_string(powercap_core_path + "energy_uj");
+		if (str.length() > 0) {
+			*status = atof(str.c_str()) / 1000000; // uj to Js
+			return 0;
+		}
+
+		return -EINVAL;
+	}
+
 	ret = read_msr(first_cpu, MSR_PP0_ENERY_STATUS, &value);
 	if(ret < 0)
 	{
@@ -471,6 +550,16 @@ int c_rapl_interface::get_pp1_energy_status(double *status)
 		return -1;
 	}
 
+	if (powercap_sysfs_present) {
+		string str = read_sysfs_string(powercap_uncore_path + "energy_uj");
+		if (str.length() > 0) {
+			*status =  atof(str.c_str()) / 1000000; // uj to Js
+			return 0;
+		}
+
+		return -EINVAL;
+	}
+
 	ret = read_msr(first_cpu, MSR_PP1_ENERY_STATUS, &value);
 	if(ret < 0)
 	{
src/cpu/rapl/rapl_interface.h +6 -1
index be863e2..744f595 100644
@@ -28,6 +28,11 @@ class c_rapl_interface
 {
 private:
 	static const int def_sampling_interval = 1; //In seconds
+	bool powercap_sysfs_present;
+	string powercap_core_path;
+	string powercap_uncore_path;
+	string powercap_dram_path;
+
 	unsigned char rapl_domains;
 	int measurment_interval;
 	int first_cpu;
@@ -45,7 +50,7 @@ private:
 	int write_msr(int cpu, unsigned int idx, uint64_t val);
 
 public:
-	c_rapl_interface(int cpu = 0);
+	c_rapl_interface(const char *dev_name = "package-0", int cpu = 0);
 
 	int get_rapl_power_unit(uint64_t *value);
 	double get_power_unit();
Signature · 1 line
1.7.11.7
author
#1 lore raw

PATCH 2/3 Use package name to identify domains

Here the cpu is identifying a package id. Currently the RAPL package name is not used, but by changing to powercap sysfs package name, it can be used to identify RAPL package domains in a multi processor system.

---
 src/cpu/cpu.cpp              | 4 ++--
 src/cpu/cpu_rapl_device.cpp  | 4 ++--
 src/cpu/dram_rapl_device.cpp | 4 ++--
 3 files changed, 6 insertions(+), 6 deletions(-)
src/cpu/cpu.cpp +2 -2
index b3b6166..37cc409 100644
@@ -82,14 +82,14 @@ static class abstract_cpu * new_package(int package, int cpu, char * vendor, int
 	cpudev = new class cpudevice(_("cpu package"), packagename, ret);
 	all_devices.push_back(cpudev);
 
-	sprintf(packagename, _("cpu rapl package %i"), cpu);
+	sprintf(packagename, _("package-%i"), cpu);
 	cpu_rapl_dev = new class cpu_rapl_device(cpudev, _("cpu rapl package"), packagename, ret);
 	if (cpu_rapl_dev->device_present())
 		all_devices.push_back(cpu_rapl_dev);
 	else
 		delete cpu_rapl_dev;
 
-	sprintf(packagename, _("dram rapl package %i"), cpu);
+	sprintf(packagename, _("package-%i"), cpu);
 	dram_rapl_dev = new class dram_rapl_device(cpudev, _("dram rapl package"), packagename, ret);
 	if (dram_rapl_dev->device_present())
 		all_devices.push_back(dram_rapl_dev);
src/cpu/cpu_rapl_device.cpp +2 -2
index be10d9e..357a1c8 100644
@@ -33,9 +33,9 @@ cpu_rapl_device::cpu_rapl_device(cpudevice *parent, const char *classname, const
 	  device_valid(false)
 {
 	if (_cpu)
-		rapl = new c_rapl_interface(cpu->get_first_cpu());
+		rapl = new c_rapl_interface(dev_name, cpu->get_first_cpu());
 	else
-		rapl = new c_rapl_interface(0);
+		rapl = new c_rapl_interface();
 	last_time = time(NULL);
 	if (rapl->pp0_domain_present()) {
 		device_valid = true;
src/cpu/dram_rapl_device.cpp +2 -2
index fd31cb5..36d47a2 100644
@@ -34,9 +34,9 @@ dram_rapl_device::dram_rapl_device(cpudevice *parent, const char *classname, con
 	  device_valid(false)
 {
 	if (_cpu)
-		rapl = new c_rapl_interface(cpu->get_first_cpu());
+		rapl = new c_rapl_interface(dev_name, cpu->get_first_cpu());
 	else
-		rapl = new c_rapl_interface(0);
+		rapl = new c_rapl_interface();
 	last_time = time(NULL);
 	if (rapl->dram_domain_present()) {
 		device_valid = true;
Signature · 1 line
1.7.11.7
author
#2 lore raw

PATCH 3/3 Update README with configs

In reply to Srinivas Pandruvada

Update powercap sysfs and Intel RAPL driver config entries as suggested configs.

---
 README | 4 ++++
 1 file changed, 4 insertions(+)
README +4 -0
index 135bd6f..cd38cc5 100644
@@ -47,6 +47,10 @@ CONFIG_X86_MSR
 ACPI_PROCFS_POWER
 CONFIG_DEBUG_FS
 
+Use these configs from linux-3.13.rc1
+CONFIG_POWERCAP
+CONFIG_INTEL_RAPL
+
 The patches in the patches/ sub-directory are required for PowerTOP to function
 fully.
 
Signature · 1 line
1.7.11.7