• 十六进制字符串转为二进制

    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。选项有

    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
    + link: 父进程与子进程建立连接
+ monitor: 父进程监控子进程
+ {priority, Level}: 设置新的进程的优先级。等价于在新进程中执行process_flag(priority, Level)。区别在于,spawn的时候设置优先级,优先级会在进程被选择运行之前就设置好了。
+ {fullsweep_after, Number}:只是用在调整性能上。Erlang的运行系统,使用的是分代的垃圾回收机制。用一个『old heap』保存至少存活了一个垃圾回收周期的数据,当old heap空间不够时,就会执行垃圾回收。
+ {min_heap_size, Size},只是用在调整性能上。设置heap的最小size,单位是word。将这个值设置的比系统默认的大的话,会加速一些进程运行,因为会减少垃圾回收的执行次数。但是太大会导致内存不够,减慢系统运行。
+ {min_bin_vheap_size, VSize},只是用在调整性能上。 + 交叉引用工具:xref。xref通过分析函数的调用和定义,发现函数、模块、应用和版本之间的依赖关系。这个工具可以帮助查看是否有函数名输入错误。在用rebar打包时,可以用rebar xref执行。 + 获得ps命令得到的消耗的内存 ```erlang
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命令得到的消耗的内存 ```erlang
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的核数 ```erlang
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命令得到的虚拟内存消耗 ```erlang
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命令得到的虚拟内存消耗 ```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.
``` + 获得erlang运行时的进程信息 ```erlang
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()].
  • eralng gen_server:call

这是一个同步调用,pid1通过erlang:send()发送给pid2消息后,receive等待返回,这时候pid1处于阻塞状态。pid2是一个gen_server行为模式的进程,loop函数接收到消息后,进行相应处理,并返回。

  • gen_server:cast

直接用erlang:send发送'$gen_cast'消息。

Erlang常用代码段的更多相关文章

  1. PyTorch常用代码段整理合集

    PyTorch常用代码段整理合集 转自:知乎 作者:张皓 众所周知,程序猿在写代码时通常会在网上搜索大量资料,其中大部分是代码段.然而,这项工作常常令人心累身疲,耗费大量时间.所以,今天小编转载了知乎 ...

  2. Java常用代码段 - 未完待续

    记录一些自己写项目常用的代码段. 格式化常用日期格式 Date date = new Date(System.currentTimeMillis()); DateFormat d3 = DateFor ...

  3. PyTorch 常用代码段整理

    基础配置 检查 PyTorch 版本 torch.__version__               # PyTorch version torch.version.cuda              ...

  4. JavaScript常用代码段

    总结一下在各种地方看到的还有自己使用的一些实用代码 1)区分IE和非IE浏览器 if(!+[1,]){ alert("这是IE浏览器"); } else{ alert(" ...

  5. C#获取本地IP地址[常用代码段]

    获得当前机器的IP代码,假设本地主机为单网卡 string strHostName = Dns.GetHostName(); //得到本机的主机名 IPHostEntry ipEntry = Dns. ...

  6. php常用代码段

    点击换验证码 <a href=" src="{:U('Reglog/vcode')}" /></a> TP上一条下一条 $prev=$artica ...

  7. SQL常用代码段

    --STUFF 函数将字符串插入另一字符串.它在第一个字符串中从开始位置删除指定长度的字符:然后将第二个字符串插入第一个字符串的开始位置. STUFF ( character_expression , ...

  8. PHP常用代码段:

    1.PHP加密解密   function encryptDecrypt($key, $string, $decrypt){      if($decrypt){          $decrypted ...

  9. php 常用代码段

    1.写文件 $fp = fopen("jsapi_ticket.json", "w+"); fwrite($fp, $str); fclose($fp); 2. ...

随机推荐

  1. POJ 3237 Tree (树链拆分)

    主题链接~~> 做题情绪:了. 解题思路: 主要注意如何区间更新就ok了 . 树链剖分就是树上的线段树. 代码: #include<iostream> #include<sst ...

  2. 随着MapReduce job实现去加重,多种输出文件夹

    总结以往的工作中遇到的一个问题. 背景: 操作和维护与scribe从apacheserver一再被推到日志记录,所以在这里ETL处理正在进行的重.有根据业务的输出类型是用于多文件夹一个需求.方便挂分区 ...

  3. net开源cms系统

    .net开源cms系统推荐 内容目录: 提起开源cms,大家第一想到的是php的cms,因为php开源的最早,也最为用户和站长们认可,随着各大cms系统的功能的不断完善和各式各样的开源cms的出现,. ...

  4. C# DateTime结构的常用方法

    在项目开发中,经常会碰到日期处理.比如查询中,可能会经常遇到按时间段查询,有时会默认取出一个月的数据.当我们提交数据时,会需要记录当前日期,等等.下面就看看一些常用的方法. 首先,DateTime是一 ...

  5. 杭州电 1052 Tian Ji -- The Horse Racing(贪婪)

    http://acm.hdu.edu.cn/showproblem.php? pid=1052 Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS ...

  6. 两年前实习时的文档——MMC学习总结

    1概述 驱动程序实际上是硬件与应用程序之间的中间层.在Linux操作系统中,设备驱动程序对各种不同的设备提供了一致的訪问接口,把设备映射成一个特殊的设备文件,用户程序能够像其它文件一样对设备文件进行操 ...

  7. 【CTO辩论】移动创业大军:谁斗争or变更代理

    众创时代.英雄辈出. 但千军万马过独木桥,竞争厮杀也异常残酷.有人说,这个时代不宜创业,由于技术门槛高了.推广难度高了.盈利模式没了.创业变重了.玩法变了...... 也有人说,时势造英雄.天时地利人 ...

  8. SQL Server 数据库没有有效全部者的三种解决的方法

    问题:     开发的过程中,操作系统出了问题.决定重装系统.可是没有将SQL Server中的数据库文件分离出来,直接将系统格了.在新系统数据库中附加了数据库文件,一切还算正常.但当打开数据库关系图 ...

  9. WebAPI上传大文件

    今天在研究WebAPI的上传与下载,作为Rest的框架,更多是面向资源,就其本身来说,是不会涉及也不应该涉及到大文件的处理,具体多大呢,也就是ASP.NET的限值2G. ASP.NET的pipelin ...

  10. (大数据工程师学习路径)第四步 SQL基础课程----其他(基础练习到此为止)

    一.准备 在正式开始本内容之前,需要先从github下载相关代码,搭建好一个名为mysql_shiyan的数据库(有三张表:department,employee,project),并向其中插入数据. ...