From 79fcdba98eb7c9be0fb55b41ef6fdae0c7e1c5c1 Mon Sep 17 00:00:00 2001 From: Xorg Date: Tue, 29 Sep 2015 09:40:33 +0200 Subject: [PATCH] Add function cpu_msr_driver_open_core() This function is similar to cpu_msr_driver_open(), but with core number as parameter For Linux, core number was always 0. To be able to get all core temp/voltage, we need to set the core number. --- libcpuid/exports.def | 1 + libcpuid/libcpuid.h | 16 ++++++++++++++++ libcpuid/libcpuid.sym | 1 + libcpuid/rdmsr.c | 14 +++++++++++++- 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/libcpuid/exports.def b/libcpuid/exports.def index 92f0e55..82a6ce3 100644 --- a/libcpuid/exports.def +++ b/libcpuid/exports.def @@ -28,3 +28,4 @@ cpu_msrinfo @24 cpu_msr_driver_close @25 cpu_clock_by_ic @26 cpuid_get_total_cpus @27 +cpu_msr_driver_open_core @28 diff --git a/libcpuid/libcpuid.h b/libcpuid/libcpuid.h index 8d2280a..9104475 100644 --- a/libcpuid/libcpuid.h +++ b/libcpuid/libcpuid.h @@ -390,6 +390,7 @@ typedef enum { ERR_EXTRACT = -11, /*!< "Cannot extract RDMSR driver (read only media?)" */ ERR_HANDLE = -12, /*!< "Bad handle" */ ERR_INVMSR = -13, /*!< "Invalid MSR" */ + ERR_INVCNB = -14, /*!< "Invalid core number" */ } cpu_error_t; /** @@ -804,6 +805,21 @@ void cpuid_free_cpu_list(struct cpu_list_t* list); struct msr_driver_t; struct msr_driver_t* cpu_msr_driver_open(void); +/** + * @brief Similar to \ref cpu_msr_driver_open, but accept one parameter + * + * This function works on Linux only + * + * @param core_num specify the core number for MSR. + * The first core number is 0. + * The last core number is \ref cpuid_get_total_cpus - 1. + * + * @returns a handle to the driver on success, and NULL on error. + * The error message can be obtained by calling \ref cpuid_error. + * @see cpu_error_t + */ +struct msr_driver_t* cpu_msr_driver_open_core(int core_num); + /** * @brief Reads a Model-Specific Register (MSR) * diff --git a/libcpuid/libcpuid.sym b/libcpuid/libcpuid.sym index 7a6a51c..ba74bb1 100644 --- a/libcpuid/libcpuid.sym +++ b/libcpuid/libcpuid.sym @@ -25,3 +25,4 @@ cpu_msrinfo cpu_msr_driver_close cpu_clock_by_ic cpuid_get_total_cpus +cpu_msr_driver_open_core diff --git a/libcpuid/rdmsr.c b/libcpuid/rdmsr.c index 3b5ab1a..65657e0 100644 --- a/libcpuid/rdmsr.c +++ b/libcpuid/rdmsr.c @@ -69,12 +69,24 @@ struct msr_driver_t { int fd; }; static int rdmsr_supported(void); struct msr_driver_t* cpu_msr_driver_open(void) { + return cpu_msr_driver_open_core(0); +} + +struct msr_driver_t* cpu_msr_driver_open_core(int core_num) +{ + char msr[32]; struct msr_driver_t* handle; + if(core_num < 0 && cpuid_get_total_cpus() <= core_num) + { + set_error(ERR_INVCNB); + return NULL; + } if (!rdmsr_supported()) { set_error(ERR_NO_RDMSR); return NULL; } - int fd = open("/dev/cpu/0/msr", O_RDONLY); + sprintf(msr, "/dev/cpu/%i/msr", core_num); + int fd = open(msr, O_RDONLY); if (fd < 0) { if (errno == EIO) { set_error(ERR_NO_RDMSR);