Archive

Archive for December, 2013

Erlang R16支持带颜色的控制台

December 27th, 2013 6 comments

原创文章,转载请注明: 转载自系统技术非业余研究

本文链接地址: Erlang R16支持带颜色的控制台

Erlang通过fix tty驱动的过滤,在R16版本支持带颜色的控制台,这个特性在我们做各种监控工具高亮非常有帮助,参见R16的Readme:

Support ANSI in console Unix platforms will no longer filter control sequences to the ttsl driver thus enabling ANSI and colors in console. (Thanks to Pedram Nimreezi)

应用程序方面已经有日志系统lager率先支持“Colored terminal output (requires R16+)”

我们来演示下:

$ erl
Erlang R16B02 (erts-5.10.3) [source] [64-bit] [smp:16:16] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V5.10.3  (abort with ^G)
1> [io:fwrite("~s~s",[Level, Color])
1> ||
1> {Level, Color}<-
1> [
1> {debug,     "\e[0;38m" },
1> {info,      "\e[1;37m" },
1> {notice,    "\e[1;36m" },
1> {warning,   "\e[1;33m" },
1> {error,     "\e[1;31m" },
1> {critical,  "\e[1;35m" },
1> {alert,     "\e[1;44m" },
1> {emergency, "\e[1;41m" },
1> {eol,        "\e[0m\r\n"}
1> ]
1> ].
debuginfonoticewarningerrorcriticalalertemergencyeol
[ok,ok,ok,ok,ok,ok,ok,ok,ok]
2> 

效果如下图:erlang-color

祝玩得开心。

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

heroku_crashdumps避免crashdump覆盖

December 22nd, 2013 Comments off

原创文章,转载请注明: 转载自系统技术非业余研究

本文链接地址: heroku_crashdumps避免crashdump覆盖

erlang虚拟机挂掉的时候会产生个crashdump, 尽可能多的保存当时的现场,相关的使用和配置之前我写了不少的 博文 来介绍。
但是实际使用中的时候,每次crash的时候这个现场文件都叫erl_crash.dump,会把前一次的覆盖掉,不利于生产环境定位问题。

当然我们可以透过环境变量来配置:

ERL_CRASH_DUMP
If the emulator needs to write a crash dump, the value of this variable will be the file name of the crash dump file. If the variable is not set, the name of the crash dump file will be erl_crash.dump in the current directory

或者我们还有heroku_crashdumps项目可以帮我们更省力,项目参见这里,核心代码就这几行:

  case dumpdir() of
        undefined -> ok;
        Dir ->
            File = string:join([os:getenv("INSTANCE_NAME"),
                                "boot",
                                datetime(now)], "_"),
            DumpFile = filename:join(Dir, File),
            error_logger:info_msg("at=setup_crashdumps dumpfile=~p", [DumpFile]),
            os:putenv("ERL_CRASH_DUMP", DumpFile)
    end.

它的价值在于以application方式来进行的,有二个好处:
1. 容易打包。因为包括rebar在内的打包工具都是以app为单位的。
2. 使用起来更简单。 crashdump自动会把日期时间加上去。

具体使用够简单,参见项目说明,我就不罗嗦了,我来演示下效果:

$ git clone https://github.com/heroku/heroku_crashdumps.git
Initialized empty Git repository in /home/chuba/heroku_crashdumps/heroku_crashdumps/.git/
remote: Counting objects: 11, done.
remote: Compressing objects: 100% (8/8), done.
cremote: Total 11 (delta 2), reused 11 (delta 2)
Unpacking objects: 100% (11/11), done.
$ cd heroku_crashdumps/
$ ./rebar compile
==> heroku_crashdumps (compile)
Compiled src/heroku_crashdumps_app.erl

$ ERL_CRASH_DUMP="." INSTANCE_NAME="erl_crash.dump"  erl -pa ebin
Erlang R16B02 (erts-5.10.3) [source] [64-bit] [smp:16:16] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V5.10.3  (abort with ^G)
1> heroku_crashdumps_app:start().

=INFO REPORT==== 22-Dec-2013::16:12:41 ===
at=setup_crashdumps dumpfile="./erl_crash.dump_boot_2013-12-22T08:12:41+00:00"true
2> 
BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
       (v)ersion (k)ill (D)b-tables (d)istribution
A

Crash dump was written to: ./erl_crash.dump_boot_2013-12-22T08:12:41+00:00
Crash dump requested by userAborted
$ ls ./erl_crash.dump_boot_2013-12-22T08:12:41+00:00
./erl_crash.dump_boot_2013-12-22T08:12:41+00:00

小结:偷懒是美德!

祝玩得开心!

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

Erlang R16B03发布,R17已发力

December 21st, 2013 2 comments

原创文章,转载请注明: 转载自系统技术非业余研究

本文链接地址: Erlang R16B03发布,R17已发力

Erlang R16B03发布了,通常03版本是bug fix版本,进入生产版本,官方的说明如下:

OTP R16B03 is a service release with mostly a number of small corrections and user contributions. But there are some new functions worth mentioning as well, here are some of them:

A new memory allocation feature called “super carrier” has been introduced. It can for example be used for pre-allocation of all memory that the runtime system should be able to use. It is enabled by passing the +MMscs (size in MB) command line argument. For more information see the documentation of the +MMsco, +MMscrfsd, +MMscrpm, +MMscs, +MMusac, and, +Mlpm command line arguments in the erts_alloc(3) documentation.
The LDAP client (eldap application) now supports the start_tls operation. This upgrades an existing tcp connection to encryption using TLS, see eldap:start_tls/2 and /3.
The FTP client (inets application) now supports FTP over TLS (ftps).

其中最大的改进就是super carrier, 参见我之前写的博文, 这个特性在于专机专用的系统,内存的使用效率就高很多,同时这个版本对多个核心之间内存倒腾的效率和利用率也高很多,值得大家去用。内存的利用率和碎片率通常不会引起大家的关注,在生产的服务器中,这点非常值得关注。

R16B03发布了后,官方马不停蹄的就进入R17的开发。其中最大的期待就是语言方面的改进,包括eep37和map数据结构。 eep37特性已经进入master, commit在这里, 该特性的具体描述在这里.

简单的说:就是你过去在shell下写如下的fun过去是不可能的。

fun Fact(N) when N > 0 ->
            N * Fact(N - 1);
        Fact(0) ->
            1
end.

但是我们又经常需要这样的匿名函数,比如spawn函数,很不方便。 eep37就是解决这样的事情的。

我们来演示下,首先安装R17:

$ kerl build git git://github.com/erlang/otp.git master r17 && kerl install r17  r17
$ r17/bin/erl   
Erlang/OTP 17.0-rc0 [erts-6.0] [source-7d4e5e2] [64-bit] [smp:16:16] [async-threads:10] [hipe] [kernel-poll:false] [type-assertions] [debug-compiled] [lock-checking] [systemtap]

Eshell V6.0  (abort with ^G)
1> fun Fact(N) when N > 0 ->
1>             N * Fact(N - 1);
1>         Fact(0) ->
1>             1
1>     end.
#Fun<erl_eval.29.42696066>
2> F=e(-1).
#Fun<erl_eval.29.42696066>
3> F(10).
3628800

eep37这个特性涉及到的改变还是很大的:语言规格,编译器,调试器,dialyzer, tracer, shell, emacs插件等等,全系列的改变,所以要放在R17里面来,虽然看提交,代码早在1年前准备好了。我们再来体验下:

$ cat eep37.erl 
-module(eep37).

-compile(export_all).

-spec self() -> fun(() -> fun()).
self() ->
    fun Self() -> Self end.

-spec fact() -> fun((non_neg_integer()) -> non_neg_integer()).
fact() ->
    fun Fact(N) when N > 0 ->
            N * Fact(N - 1);
        Fact(0) ->
            1
    end.

$ r17/bin/erl 
Erlang/OTP 17.0-rc0 [erts-6.0] [source-7d4e5e2] [64-bit] [smp:16:16] [async-threads:10] [hipe] [kernel-poll:false] [type-assertions] [debug-compiled] [lock-checking] [systemtap]

Eshell V6.0  (abort with ^G)
1> eep37:fact().    
#Fun<eep37.1.16748913>
2> F=e(-1).
#Fun<eep37.1.16748913>
3> F(10).
3628800
4> 

小结: 语言也在不停的让用户爽。

祝玩得开心!

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