Get system memory information from julia
Asked Answered
C

1

6

Is there a nice way to get the current system information in julia (my use case here is memory but also interested in basically anything information that I could get from running top on linux).

This is what I have at the moment: (basically just getting the output of `free -m`)<- I can't get this to let me escape backticks and keep code highlighting...

import Base.DataFmt: readdlm_string, invalid_dlm

"""
    getmeminfo()
Returns (in MB) A tuple of containing:
    - Memory(total, used, buffer, available)
    - Swap(total, used, free)
"""
function getmeminfo()
    memstats = readdlm_string(readstring(`free -m`),invalid_dlm(Char), Int, '\n', true, Dict())
    return Tuple{Array{Int,1},Array{Int,1}}((memstats[2,[2;3;6;7]], memstats[3,[2;3;4]]))
end

Is there something in Base or any better ideas?

Citrine answered 4/3, 2017 at 17:51 Comment(2)
On Linux (maybe Mac) you can also look at cat /proc/meminfo (free is also Unix specific)Pruritus
ahh yeah, readdlm("/proc/meminfo") is much more succinct...Citrine
L
18

The built-in Sys module contains functions dedicated to retrieving system information.

julia> VERSION
v"1.0.0"

julia> Sys.total_memory() / 2^20
8071.77734375

julia> Sys.free_memory() / 2^20
5437.46484375

julia> Sys.CPU_NAME
"haswell"

julia> Sys.
ARCH              KERNEL             WORD_SIZE          eval               isexecutable       set_process_title
BINDIR            MACHINE            __init__           free_memory        islinux            total_memory
CPU_NAME          SC_CLK_TCK         _cpu_summary       get_process_title  isunix             uptime
CPU_THREADS       STDLIB             _show_cpuinfo      include            iswindows          which
CPUinfo           UV_cpu_info_t      cpu_info           isapple            loadavg            windows_version
JIT               WINDOWS_VISTA_VER  cpu_summary        isbsd              maxrss
julia> # Above after pressing Tab key twice

While it does not support all of the information provided by top, it will hopefully provide the information you are looking for.

Loan answered 5/3, 2017 at 15:17 Comment(2)
thought there had to be something like this!Citrine
Note: those commands return the amount of memory in bytes, so dividing by 2^20 gives you the available memory in MB.Sociopath

© 2022 - 2024 — McMap. All rights reserved.