erlang/otp — pull request #523 by garazdawi (2014-10-29 10:48:29+00:00)
Title: Lukas/erts/msacc

The pull request introduces two new features: os:perf_counter/0 and erlang:statistics(microstate_accounting). They are placed in the same pull request as microstate accounting depends on os:perf_counter.

# Performance counter

The performance counter can be used to get very low overhead time stamping of events. You can either use the `os:perf_counter/0` call to get a counter in native resolution, or you can use `os:perf_counter/1` to get it in the resolution you specify. i.e. `os:perf_counter(1000)` returns a timestamp in 1/1000 s resolution.

# 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.

Example usage:

```

1> msacc:start(1000).
ok
2> msacc:print().
Average thread real-time : 1000513 us
Accumulated system run-time : 2213 us
Average scheduler run-time : 1076 us

Thread aux check_io emulator gc other port sleep

Stats per thread:
async( 0) 0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 100.00%
async( 1) 0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 100.00%
aux( 1) 0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 99.99%
scheduler( 1) 0.00% 0.03% 0.13% 0.00% 0.01% 0.00% 99.82%
scheduler( 2) 0.00% 0.00% 0.00% 0.00% 0.03% 0.00% 99.97%

Stats per type:
async 0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 100.00%
aux 0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 99.99%
scheduler 0.00% 0.02% 0.06% 0.00% 0.02% 0.00% 99.89%
ok
```

For more details see the documentation.