From e045582ab1ccb8d0363228903608d66482530c58 Mon Sep 17 00:00:00 2001 From: OlgaOvcharenko Date: Wed, 2 Aug 2023 01:23:11 +0200 Subject: [PATCH] [SYSTEMDS-3602] Settings warning memory (OS X) Add OS X mem check (for Mac). Closes #1871 --- .../apache/sysds/utils/SettingsChecker.java | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/apache/sysds/utils/SettingsChecker.java b/src/main/java/org/apache/sysds/utils/SettingsChecker.java index dd92583a371..210263813ba 100644 --- a/src/main/java/org/apache/sysds/utils/SettingsChecker.java +++ b/src/main/java/org/apache/sysds/utils/SettingsChecker.java @@ -21,6 +21,8 @@ import java.io.BufferedReader; import java.io.FileReader; +import java.io.IOException; +import java.nio.charset.StandardCharsets; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -61,10 +63,13 @@ else if(JRE_Mem_Byte < Logging_Limit && JRE_Mem_Byte * 10 < Sys_Mem_Byte) { } private static long maxMemMachine() { - if("Linux".equals(System.getProperty("os.name"))) { + String sys = System.getProperty("os.name"); + if("Linux".equals(sys)) { return maxMemMachineLinux(); } - // TODO add windows. + else if(sys.contains("Mac OS")) { + return maxMemMachineOSX(); + } else { return -1; } @@ -78,11 +83,24 @@ private static long maxMemMachineLinux() { return Long.parseLong(currentLine.split(":")[1].split("kB")[0].strip()); } catch(Exception e) { - LOG.error(e); + e.printStackTrace(); return -1; } } + private static long maxMemMachineOSX() { + try { + String command = "sysctl hw.memsize"; + Runtime rt = Runtime.getRuntime(); + Process pr = rt.exec(command); + String memStr = new String(pr.getInputStream().readAllBytes(), StandardCharsets.UTF_8); + return Long.parseLong(memStr.trim().substring(12, memStr.length()-1)); + } + catch(IOException e) { + throw new RuntimeException(e); + } + } + /** * Converts a number of bytes in a long to a human readable string with GB, MB, KB and B. *