From 664ed2a6fd2b324bb6b56db3d3eca853cfda8f61 Mon Sep 17 00:00:00 2001 From: Lukas Larsson Date: Fri, 12 Sep 2014 16:38:00 +0200 Subject: erts: Add microstate accounting Microstate accounting is a way to track which state the different threads within ERTS are in. The main usage area is to pin point performance bottlenecks by checking which states the threads are in and then from there figuring out why and where to optimize. Since checking whether microstate accounting is on or off is relatively expensive if done in a short loop only a few of the states are enabled by default and more states can be enabled through configure. I've done some benchmarking and the overhead with it turned off is not noticible and with it on it is a fraction of a percent. If you enable the extra states, depending on the benchmark, the ovehead when turned off is about 1% and when turned on somewhere inbetween 5-15%. OTP-12345 --- erts/doc/src/erlang.xml | 177 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 164 insertions(+), 13 deletions(-) (limited to 'erts/doc/src/erlang.xml') diff --git a/erts/doc/src/erlang.xml b/erts/doc/src/erlang.xml index 6915b35fcb..ddca492040 100644 --- a/erts/doc/src/erlang.xml +++ b/erts/doc/src/erlang.xml @@ -5870,6 +5870,146 @@ true + Information about microstate accounting. + + +

+ Microstate accounting can be used to measure how much time the Erlang + runtime system spends doing various tasks. It is designed to be as + lightweight as possible, but there will be some overhead when this + is enabled. Microstate accounting is meant to be a profiling tool + to help figure out performance bottlenecks. + To start/stop/reset microstate_accounting you use + the system_flag + + microstate_accounting. +

+

+ erlang:statistics(microstate_accounting) returns a list of maps + representing some of the OS threads within ERTS. Each map contains + type and id fields that can be used to identify what + thread it is, and also a counters field that contains data about how + much time has been spent in the various states.

+
+> erlang:statistics(microstate_accounting).
+[#{counters => #{aux => 1899182914,
+                 check_io => 2605863602,
+                 emulator => 45731880463,
+                 gc => 1512206910,
+                 other => 5421338456,
+                 port => 221631,
+                 sleep => 5150294100},
+   id => 1,
+   type => scheduler}|...]
+        
+

The time unit is the same as returned by + + os:perf_counter/0. + So to convert it to milliseconds you could do something like this:

+
+lists:map(
+  fun(#{ counters := Cnt } = M) ->
+          MsCnt = maps:map(fun(_K, PerfCount) ->
+                                   erlang:convert_time_unit(PerfCount, perf_counter, 1000)
+                           end, Cnt),
+         M#{ counters := MsCnt }
+  end, erlang:statistics(microstate_accounting)).
+        
+

+ It is important to note that these values are not guaranteed to be + the exact time spent in each state. This is because of various + optimisation done in order to keep the overhead as small as possible. +

+ +

Currently the following MSAcc_Thread_Type are available:

+ + scheduler + The main execution threads that do most of the work. + asyncAsync threads are used by various + linked-in drivers (mainly the file drivers) do offload non-cpu + intensive work. + auxTakes care of any work that is not + specifically assigned to a scheduler. + +

Currently the following MSAcc_Thread_States are available. + All states are exclusive, meaning that a thread cannot be in two states + at once. So if you add the numbers of all counters in a thread + you will get the total run-time for that thread.

+ + aux + Time spent handling auxiliary jobs. + check_io + Time spent checking for new I/O events. + emulator + Time spent executing erlang processes. + gc + Time spent doing garbage collection. When extra states are + enabled this is the time spent doing non-fullsweep garbage + collections. + other + Time spent doing unaccounted things. + port + Time spent executing ports. + sleep + Time spent sleeping. + +

It is possible to add more fine grained MSAcc_Thread_States + through configure. + (e.g. ./configure --with-microstate-accounting=extra). + Enabling these states will cause a performance degradation when + microstate accounting is turned off and increase the overhead when + it is turned on.

+ + alloc + Time spent managing memory. Without extra states this time is + spread out over all other states. + bif + Time spent in bifs. Without extra states this time is part of + the emulator state. + busy_wait + Time spent busy waiting. This is also the state where a + scheduler no longer reports that it is active when using + + erlang:statistics(scheduler_wall_time). + So if you add all other states but this and sleep and then divide that + by all time in the thread you should get something very similar to the + scheduler_wall_time fraction. Without extra states this time is part + of the other state. + ets + Time spent executing ETS bifs. Without extra states this time is + part of the emulator state. + gc_full + Time spent doing fullsweep garbage collection. Without extra + states this time is part of the gc state. + nif + Time spent in nifs. Without extra states this time is part of + the emulator state. + send + Time spent sending messages (processes only). Without extra + states this time is part of the emulator state. + timers + Time spent managing timers. Without extra states this time is + part of the other state. + +

There is a utility module called + msacc in + runtime_tools that can be used to more easily analyse these + statistics.

+ +

+ Returns undefined if the system flag + + microstate_accounting + is turned off. +

+

The list of thread information is unsorted and may appear in + different order between calls.

+

The threads and states are subject to change without any + prior notice.

+
+
+ + Information about reductions. @@ -5887,7 +6027,7 @@ true - + Information about the run-queues.

@@ -5903,7 +6043,7 @@ true - + Information about the run-queue lengths.

@@ -5923,7 +6063,7 @@ true - + Information about runtime.

Returns information about runtime, in milliseconds.

@@ -5938,7 +6078,7 @@ true
- + Information about each schedulers work time. @@ -6009,7 +6149,7 @@ ok - + Information about active processes and ports.

@@ -6027,7 +6167,7 @@ ok - + Information about the run-queue lengths.

@@ -6046,7 +6186,7 @@ ok - + Information about wall clock.

Returns information about wall clock. wall_clock can @@ -6280,6 +6420,17 @@ ok + Set system flag microstate_accounting +

+ Turns on/off microstate accounting measurements. By passing reset it is possible to reset + all counters to 0.

+

For more information see, + erlang:statistics(microstate_accounting). +

+
+
+ + Sets system flag min_heap_size.

Sets the default minimum heap size for processes. The size @@ -6294,7 +6445,7 @@ ok - + Sets system flag min_bin_vheap_size.

Sets the default minimum binary virtual heap size for @@ -6311,7 +6462,7 @@ ok - + Sets system flag multi_scheduling.

@@ -6349,7 +6500,7 @@ ok - + Sets system flag scheduler_bind_type. @@ -6467,7 +6618,7 @@ ok - + Sets system flag scheduler_wall_time.

Turns on or off scheduler wall time measurements.

@@ -6477,7 +6628,7 @@ ok
- + Sets system flag schedulers_online.

@@ -6502,7 +6653,7 @@ ok - + Sets system flag trace_control_word.

Sets the value of the node trace control word to -- cgit v1.2.3