檢視原始碼 instrument (runtime_tools v2.1.1)

用於檢測的分析與工具函式

模組 instrument 包含用於研究 Erlang 執行時系統中資源使用情況的支援。目前,僅能研究記憶體的配置。

注意

由於此模組會檢查執行時系統的內部細節,因此不同版本之間的差異可能很大。我們不對此模組提供相容性保證。

另請參閱

erts_alloc(3), erl(1)

摘要

型別

依據其 OriginType 分組的已配置區塊大小(包含其標頭)摘要。

區塊大小的直方圖,其中每個區間的上限是前一個區間的兩倍。

AllocatorType 是使用此載體的分配器類型。

函式

傳回系統中所有已標記配置的摘要,可選擇依分配器類型和排程器 ID 進行篩選。

等同於 carriers(#{})

傳回系統中所有載體的摘要,可選擇依分配器類型和排程器 ID 進行篩選。

型別

連結到此型別

allocation_origin()

檢視原始碼 (未匯出)
-type allocation_origin() :: atom() | mfa() | pid() | port().
連結到此型別

allocation_summary()

檢視原始碼 (未匯出)
-type allocation_summary() ::
          {HistogramStart :: non_neg_integer(),
           UnscannedSize :: non_neg_integer(),
           Allocations :: #{Origin :: allocation_origin() => #{Type :: atom() => block_histogram()}}}.

依據其 OriginType 分組的已配置區塊大小(包含其標頭)摘要。

Origin 通常是配置區塊的 NIF 或驅動程式,如果無法判斷則為 'system'。

Type 是區塊所屬的配置類別,例如 db_termmessagebinary。這些類別對應於 erl_alloc.types 中的類別。

如果一個或多個載體無法在不損害系統回應能力的情況下完整掃描,則 UnscannedSize 是必須跳過的位元組數。

連結到此型別

block_histogram()

檢視原始碼 (未匯出)
-type block_histogram() :: tuple().

區塊大小的直方圖,其中每個區間的上限是前一個區間的兩倍。

第一個區間的上限由傳回直方圖的函式提供,而最後一個區間沒有上限。

例如,以下直方圖在 128-256 位元組大小之間有 40 個 (message) 區塊,在 256-512 位元組之間有 78 個區塊,在 512-1024 位元組之間有 2 個區塊,在 1-2KB 之間有 2 個區塊。

> instrument:allocations(#{ histogram_start => 128, histogram_width => 15 }).
{ok, {128, 0, #{ message => {0,40,78,2,2,0,0,0,0,0,0,0,0,0,0}, ... } }}
連結到此型別

carrier_info_list()

檢視原始碼 (未匯出)
-type carrier_info_list() ::
          {HistogramStart :: non_neg_integer(),
           Carriers ::
               [{AllocatorType :: atom(),
                 InPool :: boolean(),
                 TotalSize :: non_neg_integer(),
                 UnscannedSize :: non_neg_integer(),
                 Allocations ::
                     [{Type :: atom(), Count :: non_neg_integer(), Size :: non_neg_integer()}],
                 FreeBlocks :: block_histogram()}]}.

AllocatorType 是使用此載體的分配器類型。

InPool 指示載體是否在遷移池中。

TotalSize 是載體的總大小,包括其標頭。

Allocations 是載體中已配置區塊的摘要。請注意,當不同分配器類型之間共用載體池時,載體可能包含多種不同的區塊類型(請參閱 erts_alloc 文件以瞭解更多詳細資訊)。

FreeBlocks 是載體中可用區塊大小的直方圖。

如果無法在不損害系統回應能力的情況下完整掃描載體,則 UnscannedSize 是必須跳過的位元組數。

函式

連結到此函式

allocations()

檢視原始碼 (自 OTP 21.0 起)
-spec allocations() -> {ok, Result} | {error, Reason}
                     when Result :: allocation_summary(), Reason :: not_enabled.

等同於 allocations(#{})

連結到此函式

allocations(Options)

檢視原始碼 (自 OTP 21.0 起)
-spec allocations(Options) -> {ok, Result} | {error, Reason}
                     when
                         Result :: allocation_summary(),
                         Reason :: not_enabled,
                         Options ::
                             #{scheduler_ids => [non_neg_integer()],
                               allocator_types => [atom()],
                               histogram_start => pos_integer(),
                               histogram_width => pos_integer(),
                               flags => [per_process | per_port | per_mfa]}.

傳回系統中所有已標記配置的摘要,可選擇依分配器類型和排程器 ID 進行篩選。

預設情況下,只有二進位檔以及由 NIF 和驅動程式進行的配置會被標記,但這可以使用 +M<S>atags 模擬器選項在每個分配器的基礎上進行配置。

如果指定的分配器類型未啟用,則呼叫將失敗並顯示 {error, not_enabled}

可以使用以下選項

  • allocator_types - 要搜尋的分配器類型。

    當不同分配器類型之間的載體遷移已啟用時,指定特定分配器類型可能會導致奇怪的結果:您可能會看到意外的類型(例如,搜尋 binary_alloc 時出現程序堆積),或者如果區塊所在的載體已遷移到其他類型的分配器,則可能會看到比預期更少的區塊。

    預設為所有 alloc_util 分配器。

  • scheduler_ids - 將搜尋其分配器實例的排程器 ID。排程器 ID 為 0 將參考未繫結到任何特定排程器的全域實例。預設為所有排程器和全域實例。

  • histogram_start - 配置區塊大小直方圖中第一個區間的上限。預設值為 128。

  • histogram_width - 配置區塊大小直方圖中的區間數。預設值為 18。

  • flags - 控制如何分組輸出,例如按每個程序顯示配置(如果可能),而不僅僅是按 NIF/驅動程式顯示。預設值為 []

範例

> instrument:allocations(#{ histogram_start => 128, histogram_width => 15 }).
{ok,{128,0,
     #{udp_inet =>
           #{driver_event_state => {0,0,0,0,0,0,0,0,0,1,0,0,0,0,0}},
       system =>
           #{heap => {0,0,0,0,20,4,2,2,2,3,0,1,0,0,1},
             db_term => {271,3,1,52,80,1,0,0,0,0,0,0,0,0,0},
             code => {0,0,0,5,3,6,11,22,19,20,10,2,1,0,0},
             binary => {18,0,0,0,7,0,0,1,0,0,0,0,0,0,0},
             message => {0,40,78,2,2,0,0,0,0,0,0,0,0,0,0},
             ... }
       spawn_forker =>
           #{driver_select_data_state =>
                 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}},
       ram_file_drv => #{drv_binary => {0,0,0,0,0,0,1,0,0,0,0,0,0,0,0}},
       prim_file =>
           #{process_specific_data => {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
             nif_trap_export_entry => {0,4,0,0,0,0,0,0,0,0,0,0,0,0,0},
             monitor_extended => {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
             drv_binary => {0,0,0,0,0,0,1,0,3,5,0,0,0,1,0},
             binary => {0,4,0,0,0,0,0,0,0,0,0,0,0,0,0}},
       prim_buffer =>
           #{nif_internal => {0,4,0,0,0,0,0,0,0,0,0,0,0,0,0},
             binary => {0,4,0,0,0,0,0,0,0,0,0,0,0,0,0}}}}}
連結到此函式

carriers()

檢視原始碼 (自 OTP 21.0 起)
-spec carriers() -> {ok, Result} | {error, Reason}
                  when Result :: carrier_info_list(), Reason :: not_enabled.

等同於 carriers(#{})

連結到此函式

carriers(Options)

檢視原始碼 (自 OTP 21.0 起)
-spec carriers(Options) -> {ok, Result} | {error, Reason}
                  when
                      Result :: carrier_info_list(),
                      Reason :: not_enabled,
                      Options ::
                          #{scheduler_ids => [non_neg_integer()],
                            allocator_types => [atom()],
                            histogram_start => pos_integer(),
                            histogram_width => pos_integer()}.

傳回系統中所有載體的摘要,可選擇依分配器類型和排程器 ID 進行篩選。

如果指定的分配器類型未啟用,則呼叫將失敗並顯示 {error, not_enabled}

可以使用以下選項

  • allocator_types - 要搜尋的分配器類型。預設為所有 alloc_util 分配器。

  • scheduler_ids - 將搜尋其分配器實例的排程器 ID。排程器 ID 為 0 將參考未繫結到任何特定排程器的全域實例。預設為所有排程器和全域實例。

  • histogram_start - 可用區塊大小直方圖中第一個區間的上限。預設值為 512。

  • histogram_width - 可用區塊大小直方圖中的區間數。預設值為 14。

範例

> instrument:carriers(#{ histogram_start => 512, histogram_width => 8 }).
{ok,{512,
     [{driver_alloc,false,262144,0,
                    [{driver_alloc,1,32784}],
                    {0,0,0,0,0,0,0,1}},
      {binary_alloc,false,32768,0,
                    [{binary_alloc,15,4304}],
                    {3,0,0,0,1,0,0,0}},
      {...}|...]}}