R-ps/0001-Get-physical-CPU-count-from-lscpu-if-available.patch
2019-03-22 22:30:17 -04:00

52 lines
1.2 KiB
Diff

From 49bb399e608ab564892a43a830ffd554ed240111 Mon Sep 17 00:00:00 2001
From: Elliott Sales de Andrade <quantum.analyst@gmail.com>
Date: Mon, 18 Mar 2019 00:13:47 -0400
Subject: [PATCH] Get physical CPU count from lscpu if available.
Signed-off-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
---
R/linux.R | 23 ++++++++++++++++++++++-
1 file changed, 22 insertions(+), 1 deletion(-)
diff --git a/R/linux.R b/R/linux.R
index add8043..0d44689 100644
--- a/R/linux.R
+++ b/R/linux.R
@@ -140,7 +140,20 @@ psl__decode_address <- function(addr, family) {
}
}
-ps_cpu_count_physical_linux <- function() {
+psl__cpu_count_from_lscpu <- function() {
+ lines <- system("lscpu -p=core", intern = TRUE)
+ cores = list()
+
+ for (l in lines) {
+ if (!str_starts_with(l, "#")) {
+ cores[str_strip(l)] <- 1
+ }
+ }
+
+ length(cores)
+}
+
+psl__cpu_count_from_cpuinfo <- function() {
lines <- readLines("/proc/cpuinfo")
mapping = list()
current = list()
@@ -165,3 +178,11 @@ ps_cpu_count_physical_linux <- function() {
sum(as.integer(unlist(mapping)))
}
+
+ps_cpu_count_physical_linux <- function() {
+ if (Sys.which("lscpu") != "") {
+ psl__cpu_count_from_lscpu()
+ } else {
+ psl__cpu_count_from_cpuinfo()
+ }
+}
--
2.20.1