Sunday, August 16, 2015

ERLANG etop to get erlang process info


Erlang Top is a tool for presenting information about erlang processes similar to the information presented by "top" in UNIX. - (http://www.erlang.org/doc/man/etop.html)


Command to start etop for the node test@testHost with cookie testCookie. This command shows 20 processes running with the same cookie. Command is set to sort by memory usage. Tracing is off.

/home/otpuser/lib/erlang/lib/observer-1.3/priv/bin/etop -node test@testHost -setcookie testCookie -tracing off -lines 20 -sort memory

Labels: ,

Saturday, April 11, 2015

How to stop printing integer lists as character in Erlang

Erlang used to guess whether a given list is in a printable string format and print the list as a character list. This can be happened even related with binary lists. From Erlang version R16B, function is introduced to control this behavior.

shell:string(true).   - This is the default setting (Prints integer lists as character where possible)
shell:string(false). - Prints integer lists as it is without converting it into characters.

When you call this function from a one Erlang node, change will affect to all visible nodes globally. So remember to set it to default when the requirement is over. 

Labels: , , , ,

Saturday, February 21, 2015

Like-Search for MNESIA using ERLANG

I searched every where in the Internet for a way to implement 'like-search' function using ERLANG for MNESIA (Similar to what we experience in MySQL). Unfortunately I couldn't find it. Finally my one of friend (Sisila Priyankara@Sri Lanka) wrote a function exactly which I expected.
I would like to share his function as a module with you. You can hack this module and develop as you want.

  • qlc_tbl.hrl is the place where you should mention the record definitions of the tables which are going to be used with this module.
eg :- (record definition for a table called service) 
 -record(service, {  id :: integer(),
                    name :: string(),
                    description :: string(),
                    access_code :: string(),
                    type :: atom(),
                    codec :: string(),
                    access_type :: atom(),
                    status :: atom(),
                    created_date :: tuple(),
                    created_by :: integer(),
                    port_number :: integer(),
                    svn_revision :: integer(),
                    deploy_count :: integer(),
                    company_id :: integer(),
                    department_id :: integer(),
                    dtmf_payload :: integer()
                 }).
-type service() :: #service{}. 


 
-module(like_search).

-export([search/3]).
-include_lib("stdlib/include/qlc.hrl").
-include("../conf/qlc_tbl.hrl").

-spec search(Table :: atom(), TupleElementIndex :: integer(), Word :: float() | atom() | integer() | list()) -> RecordList :: list().
%% @doc like search for a single field in a single table. Search will be done from begining
search(Table, TupleElementIndex, Word)->
    Fun = fun() ->
    qlc:eval(
    qlc:q(
    [X || X <- mnesia:table(Table), string:str(toString(element(TupleElementIndex, X)), toString(Word)) ==1]
    ))
    end,
    {atomic, RecordList} = mnesia:transaction(Fun),
    RecordList.

-spec toString(Value :: float() | atom() | integer() | list()) -> string().
toString(Value) ->
        if
        is_float(Value) ->
%                float_to_list(io_lib:format("~.2f",[Value]));
%        float_to_list(Value);
        lists:nth(1,io_lib:format("~.2f", [Value]));
        is_atom(Value) ->
                atom_to_list(Value);
        is_integer(Value) ->
                integer_to_list(Value);
        is_list(Value) ->
                Value;
        true ->
                "--"
        end.

Labels: , , , ,

Tuesday, February 10, 2015

Mnesia {error,{application_name@host_name,{already_exists,application_name@host_name}}}

When you are going to create a mnesia schema with more than 1 node in nodes list you may end up with a error looks like following 

{error,{application_name@host_name,{already_exists,application_name@host_name}}}

Eg :-
{error,{yaws_app@ussdgw_02,{already_exists,yaws_app@ussdgw_02}}}

The cause for this error might be mnesia has been started in one or more nodes in which are listed in your nodes list in mnesia:create_schema([]). Stop mnesia by calling mnesia:stop() in all listed nodes.

Labels: , , ,

Saturday, September 6, 2014

How to convert an Un-Printable_latin1_list to binary in ERLANG

I needed to convert [1,2,3,4,5,6,7,8] to <<"1,2,3,4,5,6,7,8">>. When I used list_to_binary() it returns <<1,2,3,4,5,6,7,8>> which gives me an error in encoding the above output with the rfc4627:encode().

So, I was instructed to use

case io_lib:printable_latin1_list(Value) of
            true ->
                list_to_binary(Value);
            false ->
                list_to_binary(lists:subtract(lists:concat([lists:concat([",",X])||X<-Value]),","))
        end;


When the
Value = [1,2,3,4,5,6,7,8].
return value from the case will be <<"1,2,3,4,5,6,7,8">>

If the
Value = "abcdefgh". (or a printable list)
return value will be <<"abcdefgh">>

____________________________________________________________________________
Reference & Special Thanks goes to Upul Dissanayake@Sri Lanka

Labels: , , ,

Wednesday, August 27, 2014

Retrieve similar elements in two different lists in ERLANG

If you need to compare 2 lists and take the similar or different elements can be done using the following simple method. Assume two lists as X and Y.

X = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].

Y = [2, 4, 7, 20, 50, 100].

Z = X -- Y.      %% Returns you the values in X and Y which are dissimilar.

X -- Z   %% Returns you the values in X which are similar to values in Y.

Y -- Z   %% Returns you the values in Y which are similar to values in X.

Special thanks to Sisila@Sri Lanka
 


Labels: , , ,

Thursday, July 3, 2014

How to get UNIX TIMESTAMP in ERLANG

If you want to get UNIX TIMESTAMP which means number of seconds starting from Thursday, 1 January 1970, You can use following command.

calendar:datetime_to_gregorian_seconds
(calendar:now_to_universal_time( now()))-719528*24*3600.

Labels: