lcnt(3)
okay bestie, this is lcnt(3) from the Erlang Module Definition part of the manual, and honestly? it's giving runtime-nerd royalty π grab a lil snack π because we're about to get into internal lock profiling together.
β¨ NAME β¨
lcnt β a runtime system Lock Profiling tool. That's the whole name, short and iconic.
πΈ DESCRIPTION πΈ
so the lcnt module is your go-to for profiling the internal ethread locks in the Erlang Runtime System. When lcnt is enabled, internal counters in the runtime system get updated every single time a lock is taken β like, every time, no skips π
Those counters store info about the number of acquisition tries and the number of collisions that 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 point of view for the scenarios tested. This tool was mainly developed to help Erlang runtime developers iron out potential and generic bottlenecks β lowkey a public service fr fr.
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 protect 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 it can be turned on via lcnt:rt_opt({copy_save, true}). The lcnt:apply/1,2,3 functions enable this behavior during profiling.
π See Also π
- LCNT User's Guide
π¦ DATA TYPES π¦
the building blocks, the little icons of this whole situation β¨
category_atom() :: atom().
lock_counter_data() :: term().
option() ::
{sort, Sort :: sort()} |
{reverse, boolean()} |
{locations, boolean()} |
{thresholds, Thresholds :: [threshold()]} |
{print,
PrintOptions :: [print() | {print(), non_neg_integer()}]} |
{max_locks, MaxLocks :: non_neg_integer() | none} |
{combine, boolean()}.
print() ::
colls | duration | entry | id | name | ratio | time | tries |
type.
sort() :: colls | entry | id | name | ratio | time | tries | type.
threshold() ::
{colls, non_neg_integer()} |
{time, non_neg_integer()} |
{tries, non_neg_integer()}.
and that's just the setup bestie β the functions are coming next, keep scrolling π
β¨ FUNCTIONS β¨
okay bestie, welcome to the function buffet. every single one of these is here, no skips. let's get into it. π
apply π§Ό
apply(Fun, Args) -> term() when Fun :: fun(), Args :: [term()].
Sets up lock counters, applies Fun with Args, and cleans up. π§Ό
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 setscopy_savetotruefor the duration of the call, which can quickly lead to running out of memory.
clear π§Ή
clear(Node) -> ok when Node :: node().
Clears the internal lock statistics from the runtime system. β¨
This clears the data in the runtime system but not in server. 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.
collect π
collect(Node) -> ok when 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. π
conflicts βοΈ
conflicts(Options) -> ok when Options :: [option()].
Prints a list of internal locks and its statistics.
For option description, see lcnt:inspect/2.
information βΉοΈ
information() -> ok.
Prints lcnt server state and generic information about collected lock statistics.
inspect π
inspect(Lock, Options) -> ok
when
Lock :: Name | {Name, Id | [Id]},
Name :: atom() | pid() | port(),
Id :: atom() | integer() | pid() | port(),
Options :: [option()].
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/0 and port/0 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 withnone. 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_spinlockorproclock.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 isEBUSY, 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}]
load π
load(Filename) -> ok when Filename :: file:filename().
Restores previously saved data to the server.
locations π
locations(Options) -> ok when Options :: [option()].
Prints a list of internal lock counters by source code locations.
For option description, see lcnt:inspect/2.
pid π
pid(Node, Id, Serial) -> pid()
when Node :: node(), Id :: integer(), Serial :: integer().
Creates a process id with creation 0.
port π
port(Node, Id) -> port() when Node :: node(), Id :: integer().
Creates a port id with creation 0.
rt_clear π§½
rt_clear(Node) -> ok when Node :: node().
Clear the internal counters.
Equivalent to lcnt:clear(Node).
rt_collect π₯
rt_collect(Node) -> [lock_counter_data()] when Node :: node().
Returns a list of raw lock counter data.
rt_mask π
rt_mask() -> [category_atom()].
rt_mask(Node) -> [category_atom()] when Node :: node();
(Categories) -> ok | {error, copy_save_enabled}
when Categories :: [category_atom()].
Sets the current lock category mask for the current node or retrieves the current mask for a remote node.
If Arg is an atom, it is assumed to be a node, and this call returns the current lock category mask for node Arg.
If Arg is a list, this call is equivalent to rt_mask(node(), Arg).
rt_mask(Node, Categories) π
rt_mask(Node, Categories) -> ok | {error, copy_save_enabled}
when Node :: node(), Categories :: [category_atom()].
Sets the lock category mask according to Categories on node Node.
This call will fail if the copy_save option is enabled; see lcnt:rt_opt/2.
Valid categories are:
allocatordb(ETS tables)debugdistributiongenericioprocessscheduler
This list is subject to change at any time, as is the category any given lock belongs to. π
rt_opt βοΈ
rt_opt(Node, Option) -> boolean()
when
Node :: node(),
Option :: {Type, Value :: boolean()},
Type :: copy_save | process_locks.
Sets a single option on node Node.
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/0,1. 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 addingprocessto the lock category mask; seelcnt:rt_mask/2. Default:true
save πΎ
save(Filename) -> ok when Filename :: file:filename().
Saves the collected data to file.
start π
start() -> {ok, Pid} | {error, {already_started, Pid}}
when 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 π
stop() -> ok.
Stops the lock profiler server.
swap_pid_keys π
swap_pid_keys() -> ok.
Swaps places on Name and Id space for ports and processes.
π From your friends at Ericsson AB Β· tools 4.2.2 Β· lcnt(3) π
π¦ where this page comes from
- ubuntu stonking: erlang-doc 1:29.0.6+dfsg-1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms