ERLANG KERNEL APP

KERNEL应用中包含了所有代码运行的时候所必须的ERLANG运行时系统:文件服务、代码服务等等。

所有的ERLANG系统启动时都将首先启动KERNEL应用,ERLANG/OTP的最小系统是由KERNEL和STDLIB所构成。

KERNEL应用中包含以下的功能:

日志处理器

两个标准日志处理器都定义在KERNEL应用中,它们分别在Kernel User's Guidelogger_std_h(3)logger_disk_log_h(3)手册中。

系统信号事件处理器

异步的操作系统信号可能通过在KERNEL事件管理器1中注册erl_signal_server 来接收/订阅。默认安装的信号处理器可以处理以下的信号:

  • sigusr1

    接收到此信号时,默认的处理器将会停止ERLANG虚拟机,并产生CRASHDUMP 文件,同时向标准输出写入“Received SIGUSR1”。等价于调用 erlang:halt("Received SIGUSR1")

    [xshumeng@xshumengs-MacBook-Pro doc.en]$ ps -ef | grep beam
    501 64346   941   0  2:20PM ttys001    0:00.00 grep --color=auto beam
    501 64330 64255   0  2:20PM ttys002    0:00.45 /usr/local/Cellar/erlang/20.3.6/lib/erlang/erts-9.3.1/bin/beam.smp ...
    [xshumeng@xshumengs-MacBook-Pro doc.en]$ kill -s SIGUSR1 64330
    [xshumeng@xshumengs-MacBook-Pro Gitcenter]$ erl
    Erlang/OTP 20 [erts-9.3.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
    
    Eshell V9.3.1  (abort with ^G)
    1> Received SIGUSR1
    
    Crash dump is being written to: erl_crash.dump...done
    [xshumeng@xshumengs-MacBook-Pro Gitcenter]$ ll
    total 744
    drwxr-xr-x   3 xshumeng  staff      96 Jul 21 14:17 .
    drwxr-xr-x@ 13 xshumeng  staff     416 Jul 21 14:16 ..
    -rw-r-----   1 xshumeng  staff  376949 Jul 21 14:17 erl_crash.dump
  • sigquit

    接收到此信号时,默认的处理器将会立即停止ERLANG虚拟机。等价于调用 erlang:halt()

    [xshumeng@xshumengs-MacBook-Pro doc.en]$ ps -ef | grep beam
    501 64473   941   0  2:31PM ttys001    0:00.00 grep --color=auto beam
    501 64464 64255   0  2:31PM ttys002    0:00.43 /usr/local/Cellar/erlang/20.3.6/lib/erlang/erts-9.3.1/bin/beam.smp ...
    [xshumeng@xshumengs-MacBook-Pro doc.en]$ kill -s SIGQUIT 64464
    [xshumeng@xshumengs-MacBook-Pro Gitcenter]$ erl
    Erlang/OTP 20 [erts-9.3.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
    
    Eshell V9.3.1  (abort with ^G)
    1> [xshumeng@xshumengs-MacBook-Pro Gitcenter]$
  • sigterm

    接收到此信号时,默认的处理器通常会终止(terminate)ERLANG虚拟机,等价于调用init:stop()

    [xshumeng@xshumengs-MacBook-Pro doc.en]$ ps -ef | grep beam
    501 64431   941   0  2:29PM ttys001    0:00.00 grep --color=auto beam
    501 64420 64255   0  2:29PM ttys002    0:00.44 /usr/local/Cellar/erlang/20.3.6/lib/erlang/erts-9.3.1/bin/beam.smp ...
    [xshumeng@xshumengs-MacBook-Pro doc.en]$ kill -s SIGTERM 64420
    [xshumeng@xshumengs-MacBook-Pro Gitcenter]$ erl
    Erlang/OTP 20 [erts-9.3.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
    
    Eshell V9.3.1  (abort with ^G)
    1>
    =INFO REPORT==== 21-Jul-2018::14:29:49 ===
    SIGTERM received - shutting down
    [xshumeng@xshumengs-MacBook-Pro Gitcenter]$

事件

当事件处理器添加到erl_signal_server时,必须处理以下事件:

  • sighup

    Hangup detected on controlling terminal or death of controlling process

  • sigquit

    来自键盘输入的退出

  • sigabrt

    来自abort的终止信号

  • sigalrm

    来自alarm的计时器信号

  • sigterm

    终止信号

  • sigusr1

    用户定义的信号12

  • sigusr2

    用户定义的信号23

  • sigchld

    子进程已停止(stopped)或已终止(terminated)

  • sigstop

    停止进程

  • sigtstp

    Stop typed at terminal

    设置系统信号量参考os:set_signal/2

应用配置

下面所列的配置参数都是为KERNEL应用而定义的,更多的关于配置参数的信息参考app(4)

  • distributed = [Distrib]

    指定哪些应用是分布式的和允许它们在哪些节点上运行。在这个参数中:

    • Distrib = {App,Nodes} | {App,Time,Nodes}
    • App = atom()
    • Time = integer()>0
    • Nodes = [node() | {node(),...,node()}]

    关于此参数,参见application:load/2

  • dist_auto_connect = Value

    指定节点是否自动连接。如果不指定这个参数,节点将会一直自动连接,比如:当一条信息发送到另一个节点时。Value 的值有:

    • never

      节点之间从不自动建立连接,必须以显示的明确方式建立节点之间的连接。参见net_kernel(3)

    • once

      节点之间自动建立连接,但是每个节点仅一次自动建立连接。如果节点退出,此后它将需要以显示的明确方式建立节点之间的连接。参见net_kernel(3)

  • permissions = [Perm]

    指定当应用启动时的默认许可,在此参数中:

    • Perm = {ApplName,Bool}
    • ApplName = atom()
    • Bool = boolean()

    关于许可方面的信息,参见application:permit/2

  • logger = [Config]

    指定Logger除主要日志级别外的配置,主要日志级别在下面的logger_level 中指定,并且此配置不与SASL Error Logging产生冲突,它的配置将在下面的logger_sasl_compatible中指定。

    关于logger的配置参数信息,参见《KERNEL用户手册》中Logging章节。

  • logger_level = Level

    为Logger指定主要日志级别。Log events with the same, or a more severe level, pass through the primary log level check. See section Logging in the Kernel User's Guide for more information about Logger and log levels.

    Level = emergency | alert | critical | error | warning | notice | info | debug | all | none

    在系统运行时改变主要日志级别,可以调用: logger:set_primary_config(level, Level)

    默认级别是:info

  • logger_sasl_compatible = true | false

    指定在ERLANG/OTP-21.0之前是否向后兼容SASL错误日志记录器。(Specifies if Logger behaves backwards compatible with the SASL error logging functionality from releases prior to Erlang/OTP 21.0.)

    如果这个参数设置为true值,默认的Logger处理器将不会记录任何进程和宕机或者监督者的报告。如果随后启动SASL应用,将会添加名为sasl的Logger处理器,它将参照SASL配置参数 sasl_error_loggersasl_errlog_type来记录这些事件。

    关于SASL配置参数的详细信息,参见SASL手册中: Deprecated Error Logger Event Handlers and Configuration

    查看SASL用户手册中SASL Error Logging章节和KERNEL用户手册中关于SASL 的章节Backwards Compatibility with error_logger信息,以了解Logger 如何向后兼容SASL应用。

    默认值是:false

    注意

    If this parameter is set to true, sasl_errlog_type indicates that progress reports shall be logged, and the configured primary log level is notice or more severe, then SASL automatically sets the primary log level to info. That is, this setting can potentially overwrite the value of the Kernel configuration parameter logger_level. This is to allow progress reports, which have log level info, to be forwarded to the handlers.

  • global_groups = [GroupTuple]

    定义全局GROUPS,参见global_group(3)。这个参数中:

    • GroupTuple = {GroupName, [Node]} | {GroupName, PublishType, [Node]}
    • GroupName = atom()
    • PublishType = normal | hidden
    • Node = node()
  • inet_default_connect_options = [{Opt, Val}]

    指定连接到sockets的默认参数,参见inet(3)

  • inet_default_listen_options = [{Opt, Val}]

    指定listen和accept的sockets的默认参数,参见inet(3)

  • {inet_dist_use_interface, ip_address()}

    如果ERLANG节点所在主机具有多张网卡接口,这个参数指定将在哪一张网卡上监听端口。ip_address()类型的定义,参见inet(3)

  • {inet_dist_listen_min, First}{inet_dist_listen_max, Last}

    指定分布式ERLANG节点上用于分布式通讯的socket监听端口号区段First … Last。

  • {inet_dist_listen_options, Opts}

    指定当分布式ERLANG节点打开监听时使用的额外的socket参数列表。参见gen_tcp:listen/2

  • {inet_dist_connect_options, Opts}

    指定当分布式ERLANG节点连接到其他节点时使用的额外的socket参数列表。参见gen_tcp:connect/4

  • inet_parse_error_log = silent

    如果设置此参数,那么在各种网络配置文件中发生错误并跳过时,将不会产生任何日志事件。

  • inetrc = Filename

    指定网络用户配置文件名称(字符串类型),详细信息,参见ERTS用户手册中Inet Configuration章节。

  • net_setuptime = SetupTime

    SetupTime必须时确定的整型或浮点型数字,其解释为在连接到其他分布式 ERLANG节点时的每个网络操作所允许的最大时间。其最大值允许为120,此设置超出最大允许值时将默认使用最大允许值(120)。当此值没有被设置或者设置错误(比如设置参数非数值型值)时,将使用默认值7秒。

    Notice that this value does not limit the total connection setup time, but rather each individual network operation during the connection setup and handshake.

  • net_ticktime = TickTime

    Specifies the net_kernel tick time. TickTime is specified in seconds. Once every TickTime/4 second, all connected nodes are ticked (if anything else is written to a node). If nothing is received from another node within the last four tick times, that node is considered to be down. This ensures that nodes that are not responding, for reasons such as hardware errors, are considered to be down.

    The time T, in which a node that is not responding is detected, is calculated as MinT < T < MaxT, where:

    MinT = TickTime - TickTime / 4
    MaxT = TickTime + TickTime / 4

    TickTime defaults to 60 (seconds). Thus, 45 < T < 75 seconds.

    Notice that all communicating nodes are to have the same TickTime value specified.

    Normally, a terminating node is detected immediately.

  • shutdown_timeout = integer() | infinity

    指定在分布式ERLANG节点关闭期间,application_controller等待应用终止的时间。如果超时application_controller将会强制杀死悬停应用的 application_master。如果此设置没有被指定,默认值将是infinity。

  • sync_nodes_mandatory = [NodeName]

    指定想要正确启动当前节点时,哪些其他节点必须是活着的状态。如果该设置集合中的某些节点没有在指定时间内启动,当前节点将停止启动。如果此设置没有被指定,默认值将是[]。

  • sync_nodes_optional = [NodeName]

    指定想要正确启动当前节点时,哪些其他节点可以是活着的状态。如果该设置集合中的某些节点没有在指定时间内启动,当前节点将依然启动。如果此设置没有被指定,默认值将是空列表。

  • sync_nodes_timeout = integer() | infinity

    指定当前节点等待强制性的和可选性的节点启动的时间,如果此设置没有被指定,不会执行节点同步。此选项保证了全局事物的同步。

  • start_dist_ac = true | false

    指定是否启动dist_ac服务。此设置为true时启动dist_ac服务,否则不启动。对于分布式的应用系统,此设置将被设置为true值。

  • start_boot_server = true | false

    如果此设置为true值,将会启动boot_server服务(参见 erl_boot_server(3))。在嵌入式系统中使用此服务时,该配置参数将被设置为true值。

    默认值:false

  • boot_server_slaves = [SlaveIP]

    如果配置参数start_boot_server的值是true,该配置参数可用于初始化 boot_server服务,boot_server服务初始化时将使用该配置参数值(IP地址集合)做参数。

    SlaveIP = string() | atom | {integer(),integer(),integer(),integer()},
    where 0 <= integer() <=255.

    下面是原子、元组和字符串形式的从节点IP地址示例:

    '150.236.16.70', "150,236,16,70", {150,236,16,70}.

    默认值:空集合([])

  • start_disk_log = true | false

    如果此配置参数为true,将会启动disk_log_server服务(参见 disk_log(3))。在嵌入式系统中使用此服务时,该配置参数将被设置为 true值。

    默认值:false

  • start_pg2 = true | false

    如果此配置参数为true,将会启动pg2服务(参见pg2(3))。在嵌入式系统中使用此服务时,该配置参数将被设置为true值。

    默认值:false

  • start_timer = true | false

    如果此配置参数为true,将会启动timer_server服务(参见timer(3))。在嵌入式系统中使用此服务时,该配置参数将被设置为true值。

    默认值:false

  • shell_history = enabled | disabled

    指定在使用ERLANG SHELL时,是否将SHELL历史写入磁盘。

  • shell_history_drop = [string()]

    指定某些命令的执行不记录在SHELL历史中,比如["q().", "init:stop()."],这些命令的执行将不会记录在SHELL历史中。

  • shell_history_file_bytes = integer()

    指定ERLANG SHELL历史记录中将记录多少字节的数据,默认情况下此配置参数将设置为512KB,最小值是50KB。

  • shell_history_path = string()

    指定ERLANG SHELL历史记录文件存储位置。默认使用用户缓存目录,调用函数filename:basedir(user_cache, "erlang-history").查看。

  • shutdown_func = {Mod, Func}

    Mod = atom()
    Func = atom()

    设置当application_controller开始终止时调用的函数。该函数将以 Mod:Func(Reason)的形式调用,其中Reason是 application_controller终止的原因,并且为保证application_controller 正确终止,Mod:Func(Reason)必须尽快返回。

  • source_search_rules = [DirRule] | [SuffixRule]

    DirRule = {ObjDirSuffix,SrcDirSuffix}
    SuffixRule = {ObjSuffix,SrcSuffix,[DirRule]}
    ObjDirSuffix = string()
    SrcDirSuffix = string()
    ObjSuffix = string()
    SrcSuffix = string()

    指定资源文件搜索4规则列表。如果此配置参数设置为非空列表,它将替换掉默认的规则列表。规则的描述可以是简单的文件夹后缀对,比如 {"ebin", "src"},它将使用filelib:find_file/2函数进行搜索。或者使用依赖于文件扩展名的这种格式[{".beam", ".erl", [{"ebin", "src"}]},它将使用filelib:find_source/2函数进行搜索。两种规则可以混合使用。

    The interpretation of ObjDirSuffix and SrcDirSuffix is as follows: if the end of the directory name where an object is located matches ObjDirSuffix, then the name created by replacing ObjDirSuffix with SrcDirSuffix is expanded by calling filelib:wildcard/1, and the first regular file found among the matches is the source file.

Deprecated Configuration Parameters

在ERLANG/OTP 21.0中添加了新的日志API,此版本之前的error_logger事件管理器和事件处理器在这依然有效,但是默认情况下不再使用。

下面的应用配置参数依然可以设置使用,但是只在Logger相关的配置参数没有设置时有效。

  • error_logger

    通过将默认的logger_std_h类型设置为相同的值来替换。

    例如:

    erl -kernel logger '[{handler,default,logger_std_h,#{config=>#{type=>{file,"/tmp/erlang.log"}}}}]'
  • error_logger_format_depth

    通过设置默认处理器的格式化器的depth参数来替换。

    例如:

    erl -kernel logger '[{handler,default,logger_std_h,#{formatter=>{logger_formatter,#{legacy_header=>true,template=>[{logger_formatter,header},"\n",msg,"\n"],depth=>10}}}]'

    更多信息参见:Backwards compatibility with error_logger

Footnotes:

2

保留给用户使用的信号

3

保留给用户使用的信号

4

使用filelib:find_file/2filelib:find_source/2进行文件搜索

Date: 2018-07-21 Sat 13:41

Author: xshumeng

Email: xue.shumeng@yahoo.com

Created: 2023-08-28 Mon 14:16