Skip to content

Commit

Permalink
Fix savedir, request mem and mem usage
Browse files Browse the repository at this point in the history
  • Loading branch information
TimSlendebroek committed Jan 24, 2025
1 parent 889df80 commit e3e0c6f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/optimization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
11 changes: 6 additions & 5 deletions src/utils_begin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 12 additions & 4 deletions src/utils_end.jl
Original file line number Diff line number Diff line change
Expand Up @@ -808,19 +808,27 @@ 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))
return mem_usage_kb * 1024
end


"""
save(memtrace::MemTrace, filename::String="memtrace.txt")
Expand Down

0 comments on commit e3e0c6f

Please sign in to comment.