Socket statistics

Ulf Wiger (AL/EAB) <>
Tue Apr 19 11:14:10 CEST 2005


On April 19 2005 10:18, Joe wrote:
> 
>     Good morning Gurus
> 
>     Are there any Erlang functions for obtaining socket statistics?
> 
>     I'd like something like:
> 
> 	Stats = gen_tcp:statistics(Socket),
> 
>    Stats should then contain some "amazingly interesting" 
> (sic) information.
> For example, total time of session number of packets 
> sent/received, packet losses etc. 


Can't find anything in the docs, but looking at the 
Source, I found this (below). Apparently, packet loss
is not one of the things counted.

/Uffe

=:=:=:=:=:=:=:=:=:=:=

-module(inet).

-export([getstat/1, getstat/2]).

getstat(Socket) ->
    prim_inet:getstat(Socket, stats()).

getstat(Socket,What) ->
    prim_inet:getstat(Socket, What).

=:=:=:=:=:=:=:=:=:=:=


-module(prim_inet).
-export([..., getstat/2, ...]).

...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% GETSTAT(insock(), StatList) -> {ok,StatReply} | {error, Reason}
%%
%% get socket statistics (from driver)
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

getstat(S, Stats) when port(S),list(Stats) ->
    case encode_stats(Stats) of
        {ok, Bytes} ->
            case ctl_cmd(S, ?INET_REQ_GETSTAT, Bytes) of
                {ok, Data} -> decode_stats(Data);
                Error -> Error
            end;
        Error -> Error
    end.

...

encode_stats(L) ->
    case catch enc_stats(L) of
        List when list(List) -> {ok, List};
        Error -> Error
    end.

enc_stats([H|T]) ->
    case H of
        recv_cnt  -> [?INET_STAT_RECV_CNT | enc_stats(T)];
        recv_max  -> [?INET_STAT_RECV_MAX | enc_stats(T)];
        recv_avg  -> [?INET_STAT_RECV_AVG | enc_stats(T)];
        recv_dvi  -> [?INET_STAT_RECV_DVI | enc_stats(T)];
        send_cnt  -> [?INET_STAT_SEND_CNT | enc_stats(T)];
        send_max  -> [?INET_STAT_SEND_MAX | enc_stats(T)];
        send_avg  -> [?INET_STAT_SEND_AVG  | enc_stats(T)];
        send_pend -> [?INET_STAT_SEND_PEND | enc_stats(T)];
        send_oct  -> [?INET_STAT_SEND_OCT  | enc_stats(T)];
        recv_oct  -> [?INET_STAT_RECV_OCT  | enc_stats(T)];
        _ -> throw({error, einval})
    end;
enc_stats([]) -> [].


decode_stats(Bytes) -> 
    case catch dec_stats(Bytes) of
        List when list(List) -> {ok, List};
        Error -> Error
    end.


dec_stats([?INET_STAT_SEND_OCT,X7,X6,X5,X4,X3,X2,X1,X0 | R]) ->
    Val = ?u64(X7,X6,X5,X4,X3,X2,X1,X0),
    [{send_oct, Val} | dec_stats(R)];
dec_stats([?INET_STAT_RECV_OCT,X7,X6,X5,X4,X3,X2,X1,X0 | R]) ->
    Val = ?u64(X7,X6,X5,X4,X3,X2,X1,X0),
    [{recv_oct, Val} | dec_stats(R)];
dec_stats([X,X3,X2,X1,X0 | R]) ->
    Val = ?u32(X3,X2,X1,X0),
    case X of
        ?INET_STAT_RECV_CNT  -> [{recv_cnt,Val} | dec_stats(R)];
        ?INET_STAT_RECV_MAX  -> [{recv_max,Val} | dec_stats(R)];
        ?INET_STAT_RECV_AVG  -> [{recv_avg,Val} | dec_stats(R)];
        ?INET_STAT_RECV_DVI  -> [{recv_dvi,Val} | dec_stats(R)];
        ?INET_STAT_SEND_CNT  -> [{send_cnt,Val} | dec_stats(R)];
        ?INET_STAT_SEND_MAX  -> [{send_max,Val} | dec_stats(R)];
        ?INET_STAT_SEND_AVG  -> [{send_avg,Val} | dec_stats(R)];
        ?INET_STAT_SEND_PEND -> [{send_pend,Val} | dec_stats(R)];
        _  -> throw({error, einval})
    end;
dec_stats([]) -> [].



More information about the erlang-questions mailing list