1. 程式人生 > >系統技術非業餘研究 » 如何檢視節點的可用控制代碼數目和已用控制代碼數

系統技術非業餘研究 » 如何檢視節點的可用控制代碼數目和已用控制代碼數

很多同學在使用erlang的過程中, 碰到了很奇怪的問題, 後來查明都是檔案控制代碼不夠用了, 因為系統預設的是每個程序1024. 所以我們有必要在程式執行的時候, 瞭解這些資訊, 以便診斷和預警.
下面的這個程式就演示了這個如何檢視節點的可用控制代碼數目和已用控制代碼數的功能.

首先確保你已經安裝了lsof, 我的系統是ubuntu可以這樣安裝.

[email protected]:~# apt-get -y install lsof   
[email protected]:~# cat fd.erl
-module(fd).
-export([start/0]).

get_total_fd_ulimit() ->
    {MaxFds, _} = string:to_integer(os:cmd("ulimit -n")),
    MaxFds.

get_total_fd() -> get_total_fd(os:type()).

get_total_fd({unix, Os})
  when Os =:= linux orelse
       Os =:= darwin orelse
       Os =:= freebsd orelse Os =:= sunos ->
    get_total_fd_ulimit();
get_total_fd(_) -> unknown.

get_used_fd_lsof() ->
    Lsof = os:cmd("lsof -d \"0-9999999\" -lna -p " ++
                  os:getpid()),
    string:words(Lsof, $\n).

get_used_fd() -> get_used_fd(os:type()).

get_used_fd({unix, Os})
  when Os =:= linux orelse
       Os =:= darwin orelse Os =:= freebsd ->
    get_used_fd_lsof();
get_used_fd(_) -> unknown.

start()->
    io:format("total fd: ~p~n"
              "used fd: ~p~n", [get_total_fd(), get_used_fd()]),
    halt(0).
[email protected]:~# erlc fd.erl
[email protected]:~# ulimit -n 1024
[email protected]:~# erl -noshell -s fd      
total fd: 1024
used fd: 10
[email protected]:~# ulimit -n 10240   
[email protected]:~# erl -noshell -s fd
total fd: 10240
used fd: 10
[email protected]:~# 

收工!

Post Footer automatically generated by wp-posturl plugin for wordpress.