十六进制字符串转为二进制app
hex_to_bin(Bin) -> hex2bin(Bin). hex2bin(Bin) when is_binary(Bin) -> hex2bin(binary_to_list(Bin)); hex2bin([]) -> <<>>; hex2bin([X, Y | Rest]) -> <<(erlang:list_to_integer([X], 16) * 16 + erlang:list_to_integer([Y], 16)):8, (hex2bin(Rest))/binary>>.
二进制转为十六进制字符串函数
bin2hex(B) -> bin2hex(B, lower). bin2hex(B, LowerOrUpper) when is_binary(B) -> bin2hex(binary_to_list(B), LowerOrUpper); bin2hex(L, upper) -> LH0 = lists:map(fun(X) -> erlang:integer_to_list(X, 16) end, L), LH = lists:map(fun([X, Y]) -> [X, Y]; ([X]) -> [$0, X] end, LH0), lists:flatten(LH); bin2hex(B, lower) -> H = bin2hex(B, upper), string:to_lower(H).
反编译代码
有时候线上出问题的时候,须要查看线上运行的代码,这时候就用到反编译了。工具
decompile(Mod) -> {ok, {_, [{abstract_code, {_, AC}}]}} = beam_lib:chunks(code:which(Mod), [abstract_code]), try io:format("~s~n", [erl_prettypr:format(erl_syntax:form_list(AC))]) catch io:format("~ts~n", [erl_prettypr:format(erl_syntax:form_list(AC))]) end.
分裂进程
erlang分裂进程的函数是erlang:spawn。选项有oop
spawn(Fun) -> pid() spawn(Node, Fun) -> pid() %若是Node不存在,返回一个无用的pid spawn(Module, Function, Args) -> pid() spawn(Node, Module, Function, Args) -> pid() spawn_link(Fun) -> pid() spawn_link(Node, Fun) -> pid() spawn_link(Module, Function, Args) -> pid() spawn_link(Node, Module, Function, Args) -> pid() spawn_monitor(Fun) -> {pid(), reference()} spawn_monitor(Module, Function, Args) -> {pid(), reference()} spawn_opt(Fun, Options) -> pid() | {pid(), reference()} spawn_opt(Node, Fun, Options) -> pid() | {pid(), reference()} spawn_opt(Module, Function, Args, Options) -> pid() | {pid(), reference()} spawn_opt(Node, Module, Function, Args, Options) -> pid() | {pid(), reference()} Options = [Option] Option = link | monitor | {priority, Level :: priority_level()} | {fullsweep_after, Number :: integer() >= 0} | {min_heap_size, Size :: integer() >= 0} | {min_bin_vheap_size, VSize :: integer() >= 0} priority_level() = low | normal | high | max
交叉引用工具:xref。xref经过分析函数的调用和定义,发现函数、模块、应用和版本之间的依赖关系。这个工具能够帮助查看是否有函数名输入错误。在用rebar打包时,能够用rebar xref执行。性能
得到ps命令获得的消耗的内存ui
get_ps_real_memory() -> Cmd = "pid=`echo $$`;p=`ps -ef | awk -v pid=$pid '$2 == pid {print $3}'`;mem=`ps -o rss -p $p | grep -v RSS`;r=$(($mem/1024));echo $r", os:cmd(Cmd), Result = os:cmd(Cmd), string:substr(Result, 1, length(Result) - 1).
得到top命令获得的消耗的内存spa
get_top_real_memory() -> Cmd = "pid=`echo $$`;p=`ps -ef | awk -v pid=$pid '$2 == pid {print $3}'`;mem=`cat /proc/$p/stat | awk '{print $24}'`;r=$(($mem*4096/1024/1024));echo $r", Result = os:cmd(Cmd), case length(Result) > 1 of true -> string:substr(Result, 1, length(Result) -1); false -> 0 end.
得到当前cpu的核数code
get_cpu_count() -> Cmd = "cat /proc/cpuinfo | grep processor | wc -l", R = os:cmd(Cmd), case string:to_integer(R) of {Count, _} when is_integer(Count) -> Count; {error, _} -> 0 end.
得到ps命令获得的虚拟内存消耗orm
get_ps_virtual_memory() -> Cmd = "pid=`echo $$`;p=`ps -ef | awk -v pid=$pid '$2 == pid {print $3}'`;mem=`ps -o vsz -p $p | grep -v VSZ`;r=$(($mem/1024));echo $r", Result = os:cmd(Cmd), string:substr(Result, 1, length(Result) - 1).
+得到top命令获得的虚拟内存消耗server
```erlang get_top_virtual_memory() -> Cmd = "pid=`echo $$`;p=`ps -ef | awk -v pid=$pid '$2 == pid {print $3}'`;mem=`cat /proc/$p/stat | awk '{print $24}'`;r=$(($mem*4096/1024/1024));echo $r", Result = os:cmd(Cmd), case length(Result) > 1 of true -> string:substr(Result, 1, length(Result) - 1); false -> 0 end. ```
process_infos() -> filelib:ensure_dir("./log/"), File = "./log/processes_infos.log", {ok, Fd} = file:open(File, [write, raw, binary, append]), Fun = fun(Pi) -> Info = io_lib:format("=>~p \n\n",[Pi]), case filelib:is_file(File) of true -> file:write(Fd, Info); false -> file:close(Fd), {ok, NewFd} = file:open(File, [write, raw, binary, append]), file:write(NewFd, Info) end, timer:sleep(20) end, [Fun(erlang:process_info(P)) || P <- erlang:processes()].
这是一个同步调用,pid1经过erlang:send()发送给pid2消息后,receive等待返回,这时候pid1处于阻塞状态。pid2是一个gen_server行为模式的进程,loop函数接收到消息后,进行相应处理,并返回。
直接用erlang:send发送'$gen_cast'消息。