# lcnt

###
Module

lcnt

###
Module Summary

A runtime system Lock Profiling tool.

###
Since

Module lcnt was introduced in OTP R13B04.

###
Description

The lcnt module is used to profile the internal ethread locks in the Erlang Runtime System. With lcnt enabled, internal counters in the runtime system are updated each time a lock is taken. The counters stores information about the number of acquisition tries and the number of collisions that has occurred during the acquisition tries. The counters also record the waiting time a lock has caused for a blocked thread when a collision has occurred.

The data produced by the lock counters will give an estimate on how well the runtime system will behave from a parallelizable view point for the scenarios tested. This tool was mainly developed to help Erlang runtime developers iron out potential and generic bottlenecks.

Locks in the emulator are named after what type of resource they protect and where in the emulator they are initialized, those are lock 'classes'. Most of those locks are also instantiated several times, and given unique identifiers, to increase locking granularity. Typically an instantiated lock protects a disjunct set of the resource, for example ets tables, processes or ports. In other cases it protects a specific range of a resource, for example pix_lock which protects index to process mappings, and is given a unique number within the class. A unique lock in lcnt is referenced by a name (class) and an identifier: {Name, Id}.

Some locks in the system are static and protects global resources, for example bif_timers and the run_queue locks. Other locks are dynamic and not necessarily long lived, for example process locks and ets-table locks. The statistics data from short lived locks can be stored separately when the locks are deleted. This behavior is by default turned off to save memory but can be turned on via lcnt:rt_opt({copy_save, true}). The lcnt:apply/1,2,3 functions enables this behavior during profiling.

###
Exports

####
start() -> {ok, Pid} | {error, {already_started, Pid}}
OTP R13B04

### Types

Pid = pid()

Starts the lock profiler server. The server only act as a medium for the user and performs filtering and printing of data collected by lcnt:collect/1.

####
stop() -> ok
OTP R13B04

Stops the lock profiler server.

####
collect() -> ok
OTP R13B04

Same as collect(node()).

####
collect(Node) -> ok
OTP R13B04

### Types

Node = node()

Collects lock statistics from the runtime system. The function starts a server if it is not already started. It then populates the server with lock statistics. If the server held any lock statistics data before the collect then that data is lost.

####
clear() -> ok
OTP R13B04

Same as clear(node()).

####
clear(Node) -> ok
OTP R13B04

### Types

Node = node()

Clears the internal lock statistics from the runtime system. This does not clear the data on the server only on runtime system. All counters for static locks are zeroed, all dynamic locks currently alive are zeroed and all saved locks now destroyed are removed. It also resets the duration timer.

####
conflicts() -> ok
OTP R13B04

Same as conflicts([]).

####
conflicts(Options) -> ok
OTP R13B04

### Types

Options = [option()]
option() =
{sort, Sort :: sort()} |
{reverse, boolean()} |
{locations, boolean()} |
{thresholds, Thresholds :: [threshold()]} |
{print,
PrintOptions :: [print() | {print(), integer() >= 0}]} |
{max_locks, MaxLocks :: integer() >= 0 | none} |
{combine, boolean()}
print() =
colls | duration | entry | id | name | ratio | time | tries |
type
sort() =
colls | entry | id | name | ratio | time | tries | type
threshold() =
{colls, integer() >= 0} |
{time, integer() >= 0} |
{tries, integer() >= 0}

Prints a list of internal locks and its statistics.

For option description, see lcnt:inspect/2.

####
locations() -> ok
OTP R13B04

Same as locations([]).

####
locations(Options) -> ok
OTP R13B04

### Types

Options = [option()]
option() =
{sort, Sort :: sort()} |
{reverse, boolean()} |
{locations, boolean()} |
{thresholds, Thresholds :: [threshold()]} |
{print,
PrintOptions :: [print() | {print(), integer() >= 0}]} |
{max_locks, MaxLocks :: integer() >= 0 | none} |
{combine, boolean()}
print() =
colls | duration | entry | id | name | ratio | time | tries |
type
sort() =
colls | entry | id | name | ratio | time | tries | type
threshold() =
{colls, integer() >= 0} |
{time, integer() >= 0} |
{tries, integer() >= 0}

Prints a list of internal lock counters by source code locations.

For option description, see lcnt:inspect/2.

####
inspect(Lock) -> ok
OTP R13B04

### Types

Lock = Name | {Name, Id | [Id]}
Name = atom() | pid() | port()
Id = atom() | integer() | pid() | port()

Same as inspect(Lock, []).

####
inspect(Lock, Options) -> ok
OTP R13B04

### Types

Lock = Name | {Name, Id | [Id]}
Name = atom() | pid() | port()
Id = atom() | integer() | pid() | port()
Options = [option()]
option() =
{sort, Sort :: sort()} |
{reverse, boolean()} |
{locations, boolean()} |
{thresholds, Thresholds :: [threshold()]} |
{print,
PrintOptions :: [print() | {print(), integer() >= 0}]} |
{max_locks, MaxLocks :: integer() >= 0 | none} |
{combine, boolean()}
print() =
colls | duration | entry | id | name | ratio | time | tries |
type
sort() =
colls | entry | id | name | ratio | time | tries | type
threshold() =
{colls, integer() >= 0} |
{time, integer() >= 0} |
{tries, integer() >= 0}

Prints a list of internal lock counters for a specific lock.

Lock Name and Id for ports and processes are interchangeable with the use of lcnt:swap_pid_keys/0 and is the reason why pid() and port() options can be used in both Name and Id space. Both pids and ports are special identifiers with stripped creation and can be recreated with lcnt:pid/2,3 and lcnt:port/1,2.

Option description:

{combine, boolean()}
  Combine the statistics from different instances of a lock class.
  Default: true
{locations, boolean()}
  Print the statistics by source file and line numbers.
  Default: false
{max_locks, MaxLocks}
  Maximum number of locks printed or no limit with none.
  Default: 20
{print, PrintOptions}
  Printing options:
  name
    Named lock or named set of locks (classes). The same name used for initializing the lock in the VM.
  id
    Internal id for set of locks, not always unique. This could be table name for ets tables (db_tab), port id for ports, integer identifiers for allocators, etc.
  type
    Type of lock: rw_mutex, mutex, spinlock, rw_spinlock or proclock.
  entry
    In combination with {locations, true} this option prints the lock operations source file and line number entry-points along with statistics for each entry.
  tries
    Number of acquisitions of this lock.
  colls
    Number of collisions when a thread tried to acquire this lock. This is when a trylock is EBUSY, a write try on read held rw_lock, a try read on write held rw_lock, a thread tries to lock an already locked lock. (Internal states supervises this).
  ratio
    The ratio between the number of collisions and the number of tries (acquisitions) in percentage.
  time
    Accumulated waiting time for this lock. This could be greater than actual wall clock time, it is accumulated for all threads. Trylock conflicts does not accumulate time.
  duration
    Percentage of accumulated waiting time of wall clock time. This percentage can be higher than 100% since accumulated time is from all threads.
  Default: [name,id,tries,colls,ratio,time,duration]
{reverse, boolean()}
  Reverses the order of sorting.
  Default: false
{sort, Sort}
  Column sorting orders.
  Default: time
{thresholds, Thresholds}
  Filtering thresholds. Anything values above the threshold value are passed through.
  Default: [{tries, 0}, {colls, 0}, {time, 0}]

####
information() -> ok
OTP R13B04

Prints lcnt server state and generic information about collected lock statistics.

####
swap_pid_keys() -> ok
OTP R13B04

Swaps places on Name and Id space for ports and processes.

####
load(Filename) -> ok
OTP R13B04

### Types

Filename = file:filename()

Restores previously saved data to the server.

####
save(Filename) -> ok
OTP R13B04

### Types

Filename = file:filename()

Saves the collected data to file.

###
Convenience functions

The following functions are used for convenience.

###
Exports

####
apply(Fun) -> term()
OTP R13B04

### Types

Fun = function()

Same as apply(Fun, []).

####
apply(Fun, Args) -> term()
OTP R13B04

### Types

Fun = function()
Args = [term()]

Clears the lock counters and then setups the instrumentation to save all destroyed locks. After setup the function is called, passing the elements in Args as arguments. When the function returns the statistics are immediately collected to the server. After the collection the instrumentation is returned to its previous behavior. The result of the applied function is returned.

Warning

This function should only be used for micro-benchmarks; it sets copy_save to true for the duration of the call, which can quickly lead to running out of memory.

####
apply(Module, Function, Args) -> term()
OTP R13B04

### Types

Module = module()
Function = atom()
Args = [term()]

Same as apply(fun() -> erlang:apply(Module, Function, Args) end).

####
pid(Id, Serial) -> pid()
OTP R13B04

### Types

Id = Serial = integer()

Same as pid(node(), Id, Serial).

####
pid(Node, Id, Serial) -> pid()
OTP R13B04

### Types

Node = node()
Id = Serial = integer()

Creates a process id with creation 0.

####
port(Id) -> port()
OTP R13B04

### Types

Id = integer()

Same as port(node(), Id).

####
port(Node, Id) -> port()
OTP R13B04

### Types

Node = node()
Id = integer()

Creates a port id with creation 0.

###
Internal runtime lock counter controllers

The following functions control the behavior of the internal counters.

###
Exports

####
rt_collect() -> [lock_counter_data()]
OTP R13B04

### Types

lock_counter_data() = term()

Same as rt_collect(node()).

####
rt_collect(Node) -> [lock_counter_data()]
OTP R13B04

### Types

Node = node()
lock_counter_data() = term()

Returns a list of raw lock counter data.

####
rt_clear() -> ok
OTP R13B04

Same as rt_clear(node()).

####
rt_clear(Node) -> ok
OTP R13B04

### Types

Node = node()

Clear the internal counters. Same as lcnt:clear(Node).

####
rt_mask() -> [category_atom()]
OTP 20.1

### Types

category_atom() = atom()

Same as rt_mask(node()).

####
rt_mask(Node) -> [category_atom()]
OTP 20.1

### Types

Node = node()
category_atom() = atom()

Refer to rt_mask/2. for a list of valid categories. All categories are enabled by default.

####
rt_mask(Categories) -> ok | {error, copy_save_enabled}
OTP 20.1

### Types

Categories = [category_atom()]
category_atom() = atom()

Same as rt_mask(node(), Categories).

####
rt_mask(Node, Categories) -> ok | {error, copy_save_enabled}
OTP 20.1

### Types

Node = node()
Categories = [category_atom()]
category_atom() = atom()

Sets the lock category mask to the given categories.

This will fail if the copy_save option is enabled; see lcnt:rt_opt/2.

Valid categories are:

- allocator
- db (ETS tables)
- debug
- distribution
- generic
- io
- process
- scheduler

This list is subject to change at any time, as is the category any given lock may belong to.

####
rt_opt(Option) -> boolean()
OTP R13B04

### Types

Option = {Type, Value :: boolean()}
Type = copy_save | process_locks

Same as rt_opt(node(), {Type, Value}).

####
rt_opt(Node, Option) -> boolean()
OTP R13B04

### Types

Node = node()
Option = {Type, Value :: boolean()}
Type = copy_save | process_locks

Option description:

{copy_save, boolean()}
  Retains the statistics of destroyed locks.
  Default: false
  Warning

  This option will use a lot of memory when enabled, which must be reclaimed with lcnt:rt_clear. Note that it makes no distinction between locks that were destroyed and locks for which counting was disabled, so enabling this option will disable changes to the lock category mask.

{process_locks, boolean()}
  Profile process locks, equal to adding process to the lock category mask; see lcnt:rt_mask/2
  Default: true

###
See Also

LCNT User's Guide