erlang运行期的自省机制

erlang运行期最值得称道的地方之一就是完备的自省机制,也就是说你可以通过这些信息了解整个系统运行的方方面面,给系统的调试,排错,调优,运营提供非常大的便利。在beam的c实现代码中至少1/4的代码在为这个目标服务,信息非常的到位详细,这和爱立信作为商业公司运营交换机的需求有很大的关系。自省分为2个层面的:提供erts运行期信息的和用户进程相关的信息。包括一下一个基础设施:system_flag, system_info,system_profile,system_monitor,erts_debug,the Erlang crash dumps,trace. 以及在otp的os_mon,snmp.

[size=large]system_flag主要用来微调erts的性能相关参数。
erlang:system_flag(fullsweep_after, Number)[/size]
Number is a non-negative integer which indicates how many times generational garbages collections can be done without forcing a fullsweep collection. The value applies to new processes; processes already running are not affected.
In low-memory systems (especially without virtual memory), setting the value to 0 can help to conserve memory.
An alternative way to set this value is through the (operating system) environment variable ERL_FULLSWEEP_AFTER.
erlang:system_flag(min_heap_size, MinHeapSize)
Sets the default minimum heap size for processes. The size is given in words. The new min_heap_size only effects processes spawned after the change of min_heap_size has been made. The min_heap_size can be set for individual processes by use of spawn_opt/N or process_flag/2.
erlang:system_flag(multi_scheduling, BlockState)
BlockState = block | unblock
If multi-scheduling is enabled, more than one scheduler thread is used by the emulator. Multi-scheduling can be blocked. When multi-scheduling has been blocked, only one scheduler thread will schedule Erlang processes.
If BlockState =:= block, multi-scheduling will be blocked. If BlockState =:= unblock and no-one else is blocking multi-scheduling and this process has only blocked one time, multi-scheduling will be unblocked. One process can block multi-scheduling multiple times. If a process has blocked multiple times, it has to unblock exactly as many times as it has blocked before it has released its multi-scheduling block. If a process that has blocked multi-scheduling exits, it will release its blocking of multi-scheduling.
The return values are disabled, blocked, or enabled. The returned value describes the state just after the call to erlang:system_flag(multi_scheduling, BlockState) has been made. The return values are described in the documentation of erlang:system_info(multi_scheduling).
NOTE: Blocking of multi-scheduling should normally not be needed. If you feel that you need to block multi-scheduling, think through the problem at least a couple of times again. Blocking multi-scheduling should only be used as a last resort since it will most likely be a very inefficient way to solve the problem.
See also erlang:system_info(multi_scheduling), erlang:system_info(multi_scheduling_blockers), and erlang:system_info(schedulers).
erlang:system_flag(trace_control_word, TCW)
Sets the value of the node's trace control word to TCW. TCW should be an unsigned integer. For more information see documentation of the set_tcw function in the match specification documentation in the ERTS User's Guide.
[size=large]

system_info提供erts最基本的环境信息
erlang:system_info(Type) -> Res
[/size]
Types:

Type, Res -- see below

Returns various information about the current system (emulator) as specified by Type:

allocated_areas
Returns a list of tuples with information about miscellaneous allocated memory areas.
Each tuple contains an atom describing type of memory as first element and amount of allocated memory in bytes as second element. In those cases when there is information present about allocated and used memory, a third element is present. This third element contains the amount of used memory in bytes.
erlang:system_info(allocated_areas) is intended for debugging, and the content is highly implementation dependent. The content of the results will therefore change when needed without prior notice.
Note: The sum of these values is not the total amount of memory allocated by the emulator. Some values are part of other values, and some memory areas are not part of the result. If you are interested in the total amount of memory allocated by the emulator see erlang:memory/0,1.
allocator
Returns {Allocator, Version, Features, Settings}.
Types:

* Allocator = undefined | elib_malloc | glibc
* Version = [int()]
* Features = [atom()]
* Settings = [{Subsystem, [{Parameter, Value}]}]
* Subsystem = atom()
* Parameter = atom()
* Value = term()

Explanation:

* Allocator corresponds to the malloc() implementation used. If Allocator equals undefined, the malloc() implementation used could not be identified. Currently elib_malloc and glibc can be identified.
* Version is a list of integers (but not a string) representing the version of the malloc() implementation used.
* Features is a list of atoms representing allocation features used.
* Settings is a list of subsystems, their configurable parameters, and used values. Settings may differ between different combinations of platforms, allocators, and allocation features. Memory sizes are given in bytes.

See also "System Flags Effecting erts_alloc" in erts_alloc(3).
alloc_util_allocators
Returns a list of the names of all allocators using the ERTS internal alloc_util framework as atoms. For more information see the "the alloc_util framework" section in the erts_alloc(3) documentation.
{allocator, Alloc}
Returns information about the specified allocator. As of erts version 5.6.1 the return value is a list of {instance, InstanceNo, InstanceInfo} tuples where InstanceInfo contains information about a specific instance of the allocator. If Alloc is not a recognized allocator, undefined is returned. If Alloc is disabled, false is returned.
Note: The information returned is highly implementation dependent and may be changed, or removed at any time without prior notice. It was initially intended as a tool when developing new allocators, but since it might be of interest for others it has been briefly documented.
The recognized allocators are listed in erts_alloc(3). After reading the erts_alloc(3) documentation, the returned information should more or less speak for itself. But it can be worth explaining some things. Call counts are presented by two values. The first value is giga calls, and the second value is calls. mbcs, and sbcs are abbreviations for, respectively, multi-block carriers, and single-block carriers. Sizes are presented in bytes. When it is not a size that is presented, it is the amount of something. Sizes and amounts are often presented by three values, the first is current value, the second is maximum value since the last call to erlang:system_info({allocator, Alloc}), and the third is maximum value since the emulator was started. If only one value is present, it is the current value. fix_alloc memory block types are presented by two values. The first value is memory pool size and the second value used memory size.
{allocator_sizes, Alloc}
Returns various size information for the specified allocator. The information returned is a subset of the information returned by erlang:system_info({allocator, Alloc}).
c_compiler_used
Returns a two-tuple describing the C compiler used when compiling the runtime system. The first element is an atom describing the name of the compiler, or undefined if unknown. The second element is a term describing the version of the compiler, or undefined if unknown.
check_io
Returns a list containing miscellaneous information regarding the emulators internal I/O checking. Note, the content of the returned list may vary between platforms and over time. The only thing guaranteed is that a list is returned.
compat_rel
Returns the compatibility mode of the local node as an integer. The integer returned represents the Erlang/OTP release which the current emulator has been set to be backward compatible with. The compatibility mode can be configured at startup by using the command line flag +R, see erl(1).
creation
Returns the creation of the local node as an integer. The creation is changed when a node is restarted. The creation of a node is stored in process identifiers, port identifiers, and references. This makes it (to some extent) possible to distinguish between identifiers from different incarnations of a node. Currently valid creations are integers in the range 1..3, but this may (probably will) change in the future. If the node is not alive, 0 is returned.
debug_compiled
Returns true if the emulator has been debug compiled; otherwise, false.
dist
Returns a binary containing a string of distribution information formatted as in Erlang crash dumps. For more information see the "How to interpret the Erlang crash dumps" chapter in the ERTS User's Guide.
dist_ctrl
Returns a list of tuples {Node, ControllingEntity}, one entry for each connected remote node. The Node is the name of the node and the ControllingEntity is the port or pid responsible for the communication to that node. More specifically, the ControllingEntity for nodes connected via TCP/IP (the normal case) is the socket actually used in communication with the specific node.
driver_version
Returns a string containing the erlang driver version used by the runtime system. It will be on the form "<major ver>.<minor ver>".
elib_malloc
If the emulator uses the elib_malloc memory allocator, a list of two-element tuples containing status information is returned; otherwise, false is returned. The list currently contains the following two-element tuples (all sizes are presented in bytes):

{heap_size, Size}
Where Size is the current heap size.
{max_alloced_size, Size}
Where Size is the maximum amount of memory allocated on the heap since the emulator started.
{alloced_size, Size}
Where Size is the current amount of memory allocated on the heap.
{free_size, Size}
Where Size is the current amount of free memory on the heap.
{no_alloced_blocks, No}
Where No is the current number of allocated blocks on the heap.
{no_free_blocks, No}
Where No is the current number of free blocks on the heap.
{smallest_alloced_block, Size}
Where Size is the size of the smallest allocated block on the heap.
{largest_free_block, Size}
Where Size is the size of the largest free block on the heap.

fullsweep_after
Returns {fullsweep_after, int()} which is the fullsweep_after garbage collection setting used by default. For more information see garbage_collection described below.
garbage_collection
Returns a list describing the default garbage collection settings. A process spawned on the local node by a spawn or spawn_link will use these garbage collection settings. The default settings can be changed by use of system_flag/2. spawn_opt/4 can spawn a process that does not use the default settings.
global_heaps_size
Returns the current size of the shared (global) heap.
heap_sizes
Returns a list of integers representing valid heap sizes in words. All Erlang heaps are sized from sizes in this list.
heap_type
Returns the heap type used by the current emulator. Currently the following heap types exist:

private
Each process has a heap reserved for its use and no references between heaps of different processes are allowed. Messages passed between processes are copied between heaps.
shared
One heap for use by all processes. Messages passed between processes are passed by reference.
hybrid
A hybrid of the private and shared heap types. A shared heap as well as private heaps are used.

info
Returns a binary containing a string of miscellaneous system information formatted as in Erlang crash dumps. For more information see the "How to interpret the Erlang crash dumps" chapter in the ERTS User's Guide.
kernel_poll
Returns true if the emulator uses some kind of kernel-poll implementation; otherwise, false.
loaded
Returns a binary containing a string of loaded module information formatted as in Erlang crash dumps. For more information see the "How to interpret the Erlang crash dumps" chapter in the ERTS User's Guide.
logical_processors
Returns the number of logical processors detected on the system as an integer or the atom unknown if the emulator wasn't able to detect any.
machine
Returns a string containing the Erlang machine name.
modified_timing_level
Returns the modified timing level (an integer) if modified timing has been enabled; otherwise, undefined. See the +T command line flag in the documentation of the erl(1) command for more information on modified timing.
multi_scheduling
Returns disabled, blocked, or enabled. A description of the return values:

disabled
The emulator has only one scheduler thread. The emulator does not have SMP support, or have been started with only one scheduler thread.
blocked
The emulator has more than one scheduler thread, but all scheduler threads but one have been blocked, i.e., only one scheduler thread will schedule Erlang processes and execute Erlang code.
enabled
The emulator has more than one scheduler thread, and no scheduler threads have been blocked, i.e., all available scheduler threads will schedule Erlang processes and execute Erlang code.

See also erlang:system_flag(multi_scheduling, BlockState), erlang:system_info(multi_scheduling_blockers), and erlang:system_info(schedulers).
multi_scheduling_blockers
Returns a list of PIDs when multi-scheduling is blocked; otherwise, the empty list. The PIDs in the list is PIDs of the processes currently blocking multi-scheduling. A PID will only be present once in the list, even if the corresponding process has blocked multiple times.
See also erlang:system_flag(multi_scheduling, BlockState), erlang:system_info(multi_scheduling), and erlang:system_info(schedulers).
otp_release
Returns a string containing the OTP release number.
process_count
Returns the number of processes currently existing at the local node as an integer. The same value as length(processes()) returns.
process_limit
Returns the maximum number of concurrently existing processes at the local node as an integer. This limit can be configured at startup by using the command line flag +P, see erl(1).
procs
Returns a binary containing a string of process and port information formatted as in Erlang crash dumps. For more information see the "How to interpret the Erlang crash dumps" chapter in the ERTS User's Guide.
scheduler_id
Returns the scheduler id (SchedulerId) of the scheduler thread that the calling process is executing on. SchedulerId is a positive integer; where 1 <= SchedulerId <= erlang:system_info(schedulers). See also erlang:system_info(schedulers).
schedulers
Returns the number of scheduler threads used by the emulator. A scheduler thread schedules Erlang processes and Erlang ports, and execute Erlang code and Erlang linked in driver code.
The number of scheduler threads is determined at emulator boot time and cannot be changed after that.
See also erlang:system_info(scheduler_id), erlang:system_flag(multi_scheduling, BlockState), erlang:system_info(multi_scheduling), and and erlang:system_info(multi_scheduling_blockers).
smp_support
Returns true if the emulator has been compiled with smp support; otherwise, false.
system_version
Returns a string containing the emulator type and version as well as some important properties such as the size of the thread pool, etc.
system_architecture
Returns a string containing the processor and OS architecture the emulator is built for.
threads
Returns true if the emulator has been compiled with thread support; otherwise, false is returned.
thread_pool_size
Returns the number of async threads in the async thread pool used for asynchronous driver calls (driver_async()) as an integer.
trace_control_word
Returns the value of the node's trace control word. For more information see documentation of the function get_tcw in "Match Specifications in Erlang", ERTS User's Guide.
version
Returns a string containing the version number of the emulator.
wordsize
Returns the word size in bytes as an integer, i.e. on a 32-bit architecture 4 is returned, and on a 64-bit architecture 8 is returned.

[size=large]system_monitor主要用来调优.
erlang:system_monitor(MonitorPid, [Option]) -> MonSettings[/size]

Types:

MonitorPid = pid()
Option = {long_gc, Time} | {large_heap, Size} | busy_port | busy_dist_port
Time = Size = int()
MonSettings = {OldMonitorPid, [Option]}
OldMonitorPid = pid()

Sets system performance monitoring options. MonitorPid is a local pid that will receive system monitor messages, and the second argument is a list of monitoring options:

{long_gc, Time}
If a garbage collection in the system takes at least Time wallclock milliseconds, a message {monitor, GcPid, long_gc, Info} is sent to MonitorPid. GcPid is the pid that was garbage collected and Info is a list of two-element tuples describing the result of the garbage collection. One of the tuples is {timeout, GcTime} where GcTime is the actual time for the garbage collection in milliseconds. The other tuples are tagged with heap_size, heap_block_size, stack_size, mbuf_size, old_heap_size, and old_heap_block_size. These tuples are explained in the documentation of the gc_start trace message (see erlang:trace/3). New tuples may be added, and the order of the tuples in the Info list may be changed at any time without prior notice.
{large_heap, Size}
If a garbage collection in the system results in the allocated size of a heap being at least Size words, a message {monitor, GcPid, large_heap, Info} is sent to MonitorPid. GcPid and Info are the same as for long_gc above, except that the tuple tagged with timeout is not present. Note: As of erts version 5.6 the monitor message is sent if the sum of the sizes of all memory blocks allocated for all heap generations is equal to or larger than Size. Previously the monitor message was sent if the memory block allocated for the youngest generation was equal to or larger than Size.
busy_port
If a process in the system gets suspended because it sends to a busy port, a message {monitor, SusPid, busy_port, Port} is sent to MonitorPid. SusPid is the pid that got suspended when sending to Port.
busy_dist_port
If a process in the system gets suspended because it sends to a process on a remote node whose inter-node communication was handled by a busy port, a message {monitor, SusPid, busy_dist_port, Port} is sent to MonitorPid. SusPid is the pid that got suspended when sending through the inter-node communication port Port.

[size=large]system_profile也是提供些erts进程和port的信息
erlang:system_profile(ProfilerPid, Options) -> ProfilerSettings[/size]

Types:

ProfilerSettings -> {ProfilerPid, Options} | undefined
ProfilerPid = pid() | port()
Options = [Option]
Option = runnable_procs | runnable_ports | scheduler | exclusive

Sets system profiler options. ProfilerPid is a local pid or port that will receive profiling messages. The receiver is excluded from all profiling. The second argument is a list of profiling options:

runnable_procs
If a process is put into or removed from the runqueue a message, {profile, Pid, State, Mfa, Ts}, is sent to ProfilerPid. Running processes that is reinsertet into the runqueue after completing its reductions does not trigger this message.
runnable_ports
If a port is put into or removed from the runqueue a message, {profile, Port, State, 0, Ts}, is sent to ProfilerPid.
scheduler
If a scheduler is put to sleep or awoken a message, {profile, scheduler, Id, State, NoScheds, Ts}, is sent to ProfilerPid.
exclusive
If a synchronous call to a port from a process is done, the calling process is considered not runnable during the call runtime to the port. The calling process is notified as inactive and subsequently active when the port callback return

[size=large]erts_debug提供最详尽的内部数据信息
erts_debug:get_internal_state()未公开[/size]

node_and_dist_references
DbTable_words
next_pid
next_port
check_io_debug
available_internal_state
monitoring_nodes
link_list
monitor_list
channel_number
have_pending_exit

crash dump则是个无价之宝 可以用erlang:halt来强行产生系统的运行映像,分析系统的运行状态。

其他的几个机制如trace文档都写得很清楚,我就不抄书了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值