Skip to content

Fix savedir, request mem and mem usage #803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

# 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())"))

Check warning on line 36 in src/optimization.jl

View check run for this annotation

Codecov / codecov/patch

src/optimization.jl#L35-L36

Added lines #L35 - L36 were not covered by tests
else
savedir = abspath(joinpath(save_folder, "$(generation)__$(Dates.now())__$(getpid())"))

Check warning on line 38 in src/optimization.jl

View check run for this annotation

Codecov / codecov/patch

src/optimization.jl#L38

Added line #L38 was not covered by tests
end
mkdir(savedir)
cd(savedir)

Expand Down
17 changes: 9 additions & 8 deletions src/utils_begin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -324,19 +324,20 @@
function localhost_memory()
if Sys.isapple()
cmd = `sysctl hw.memsize` # for OSX
mem_size = parse(Int, match(r"\d+", readchomp(cmd)).match) / 1024^3
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

Check warning on line 327 in src/utils_begin.jl

View check run for this annotation

Codecov / codecov/patch

src/utils_begin.jl#L327

Added line #L327 was not covered by tests
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

Check warning on line 332 in src/utils_begin.jl

View check run for this annotation

Codecov / codecov/patch

src/utils_begin.jl#L330-L332

Added lines #L330 - L332 were not covered by tests
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
elseif Sys.isunix()

Check warning on line 337 in src/utils_begin.jl

View check run for this annotation

Codecov / codecov/patch

src/utils_begin.jl#L336-L337

Added lines #L336 - L337 were not covered by tests
# 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

Check warning on line 340 in src/utils_begin.jl

View check run for this annotation

Codecov / codecov/patch

src/utils_begin.jl#L339-L340

Added lines #L339 - L340 were not covered by tests
else
error("couldn't determine the mem_size")
end
Expand Down
17 changes: 13 additions & 4 deletions src/utils_end.jl
Original file line number Diff line number Diff line change
Expand Up @@ -808,19 +808,28 @@
[], []
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()

Check warning on line 818 in src/utils_end.jl

View check run for this annotation

Codecov / codecov/patch

src/utils_end.jl#L817-L818

Added lines #L817 - L818 were not covered by tests
# 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

Check warning on line 823 in src/utils_end.jl

View check run for this annotation

Codecov / codecov/patch

src/utils_end.jl#L820-L823

Added lines #L820 - L823 were not covered by tests
else
pid = getpid()
mem_info = read(`ps -p $pid -o rss=`, String)
mem_usage_kb = parse(Int, strip(mem_info))

Check warning on line 827 in src/utils_end.jl

View check run for this annotation

Codecov / codecov/patch

src/utils_end.jl#L825-L827

Added lines #L825 - L827 were not covered by tests
end
return mem_usage_kb * 1024
end


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

Expand Down