From decfcf5749a44855ab32f32baabc6da07e8b8f17 Mon Sep 17 00:00:00 2001 From: Tim Slendebroek <32385057+TimSlendebroek@users.noreply.github.com> Date: Fri, 24 Jan 2025 11:07:12 -0800 Subject: [PATCH 1/2] Fix savedir, request mem and mem usage --- src/optimization.jl | 6 +++++- src/utils_begin.jl | 11 ++++++----- src/utils_end.jl | 17 +++++++++++++---- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/optimization.jl b/src/optimization.jl index 804722b7b..1a98fa04c 100644 --- a/src/optimization.jl +++ b/src/optimization.jl @@ -32,7 +32,11 @@ function optimization_engine( # create working directory original_dir = pwd() - savedir = abspath(joinpath(save_folder, "$(generation)__$(Dates.now())__$(getpid())")) + if Sys.iswindows() + savedir = abspath(joinpath(save_folder, "$(generation)__$(join(split(string(Dates.now()),":"),"-"))__$(getpid())")) + else + savedir = abspath(joinpath(save_folder, "$(generation)__$(Dates.now())__$(getpid())")) + end mkdir(savedir) cd(savedir) diff --git a/src/utils_begin.jl b/src/utils_begin.jl index f9e938025..3cf05ca25 100644 --- a/src/utils_begin.jl +++ b/src/utils_begin.jl @@ -324,19 +324,20 @@ Determines what the maximum memory is based on the device type (apple, windows, function localhost_memory() if Sys.isapple() cmd = `sysctl hw.memsize` # for OSX - mem_size = parse(Int, match(r"\d+", readchomp(cmd)).match) / 1024^3 + mem_size = parse(Int, match(r"\d+", readchomp(cmd)).match) / 1024^3 # GiB elseif Sys.isunix() # General Unix command (including macOS and Linux) cmd = `free -b` # get memory in bytes - mem_size = parse(Int, match(r"\d+", readchomp(cmd)).match) / 1024^3 + mem_size = parse(Int, match(r"\d+", readchomp(cmd)).match) / 1024^3 # GiB elseif Sys.iswindows() # Windows command - cmd = `wmic ComputerSystem get TotalPhysicalMemory` - mem_size = parse(Int, match(r"\d+", readchomp(cmd)).match) / 1024^3 + cmd = `powershell -Command "Get-CimInstance Win32_ComputerSystem | Select-Object -ExpandProperty TotalPhysicalMemory"` + mem_bytes = parse(Int, readchomp(cmd)) + mem_size = mem_bytes / 1024^3 # GiB elseif Sys.islinux() # Linux-specific command cmd = `grep MemTotal /proc/meminfo` - mem_size = parse(Int, match(r"\d+", readchomp(cmd)).match) / 1024^2 # Linux reports in KB + mem_size = parse(Int, match(r"\d+", readchomp(cmd)).match) / 1024^2 # GiB else error("couldn't determine the mem_size") end diff --git a/src/utils_end.jl b/src/utils_end.jl index 67244282e..f213a75d3 100644 --- a/src/utils_end.jl +++ b/src/utils_end.jl @@ -808,19 +808,28 @@ A plot with the following characteristics: [], [] end end - """ get_julia_process_memory_usage() Returns memory used by current julia process """ function get_julia_process_memory_usage() - pid = getpid() - mem_info = read(`ps -p $pid -o rss=`, String) - mem_usage_kb = parse(Int, strip(mem_info)) + if Sys.iswindows() + pid = getpid() + # Use PowerShell to get the current process's WorkingSet (memory in bytes) + cmd = `powershell -Command "(Get-Process -Id $pid).WorkingSet64"` + mem_bytes_str = readchomp(cmd) + mem_bytes = parse(Int, mem_bytes_str) + return mem_bytes + else + pid = getpid() + mem_info = read(`ps -p $pid -o rss=`, String) + mem_usage_kb = parse(Int, strip(mem_info)) + end return mem_usage_kb * 1024 end + """ save(memtrace::MemTrace, filename::String="memtrace.txt") From f3803821a5c1839848ffc83c2e01ccaa8558c699 Mon Sep 17 00:00:00 2001 From: Tim ASDF Date: Wed, 29 Jan 2025 16:02:21 -0800 Subject: [PATCH 2/2] reverse isunix islinux order --- src/utils_begin.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/utils_begin.jl b/src/utils_begin.jl index 3cf05ca25..bb6ba1bc5 100644 --- a/src/utils_begin.jl +++ b/src/utils_begin.jl @@ -325,10 +325,6 @@ function localhost_memory() if Sys.isapple() cmd = `sysctl hw.memsize` # for OSX mem_size = parse(Int, match(r"\d+", readchomp(cmd)).match) / 1024^3 # GiB - elseif Sys.isunix() - # General Unix command (including macOS and Linux) - cmd = `free -b` # get memory in bytes - mem_size = parse(Int, match(r"\d+", readchomp(cmd)).match) / 1024^3 # GiB elseif Sys.iswindows() # Windows command cmd = `powershell -Command "Get-CimInstance Win32_ComputerSystem | Select-Object -ExpandProperty TotalPhysicalMemory"` @@ -338,6 +334,10 @@ function localhost_memory() # Linux-specific command cmd = `grep MemTotal /proc/meminfo` mem_size = parse(Int, match(r"\d+", readchomp(cmd)).match) / 1024^2 # GiB + elseif Sys.isunix() + # General Unix command (including macOS and Linux) + cmd = `free -b` # get memory in bytes + mem_size = parse(Int, match(r"\d+", readchomp(cmd)).match) / 1024^3 # GiB else error("couldn't determine the mem_size") end