diff options
| -rw-r--r-- | src/vm_memory_monitor.erl | 26 |
1 files changed, 10 insertions, 16 deletions
diff --git a/src/vm_memory_monitor.erl b/src/vm_memory_monitor.erl index 4e33c02545..9acf1354e9 100644 --- a/src/vm_memory_monitor.erl +++ b/src/vm_memory_monitor.erl @@ -195,17 +195,20 @@ get_mem_limit(MemFraction, TotalMemory) -> %%---------------------------------------------------------------------------- %% Internal Helpers %%---------------------------------------------------------------------------- +cmd(Command) -> + [Exec| _Rest] = string:tokens(Command, " "), + case os:find_executable(Exec) of + false -> throw({command_not_found, Exec}); + _ -> ok + end, + os:cmd(Command). %% get_total_memory(OS) -> Total %% Windows and Freebsd code based on: memsup:get_memory_usage/1 %% Original code was part of OTP and released under "Erlang Public License". get_total_memory({unix,darwin}) -> - File = os:cmd("/usr/bin/vm_stat"), - case string:str(File, " not found\n") of - 0 -> ok; - _ -> throw({command_not_found, "/usr/bin/vm_stat"}) - end, + File = cmd("/usr/bin/vm_stat"), Lines = string:tokens(File, "\n"), Dict = dict:from_list(lists:map(fun parse_line_mach/1, Lines)), [PageSize, Inactive, Active, Free, Wired] = @@ -233,11 +236,7 @@ get_total_memory({unix, linux}) -> dict:fetch('MemTotal', Dict); get_total_memory({unix, sunos}) -> - File = os:cmd("/usr/sbin/prtconf"), - case string:str(File, "No such file or directory\n") of - 0 -> ok; - _ -> throw({command_not_found, "/usr/sbin/prtconf"}) - end, + File = cmd("/usr/sbin/prtconf"), Lines = string:tokens(File, "\n"), Dict = dict:from_list(lists:map(fun parse_line_sunos/1, Lines)), dict:fetch('Memory size', Dict); @@ -288,12 +287,7 @@ parse_line_sunos(Line) -> end. freebsd_sysctl(Def) -> - Result = os:cmd("/sbin/sysctl -n " ++ Def), - case string:str(Result, " not found\n") of - 0 -> ok; - _ -> throw({command_not_found, "/sbin/sysctl"}) - end, - list_to_integer(Result -- "\n"). + list_to_integer(cmd("/sbin/sysctl -n " ++ Def) -- "\n"). %% file:read_file does not work on files in /proc as it seems to get %% the size of the file first and then read that many bytes. But files |
