檢視原始碼 eprof (工具 v4.1.1)
Erlang 的時間分析工具
模組 eprof
提供一組函數,用於 Erlang 程式的時間分析,以找出執行時間的使用方式。分析是使用 Erlang 的追蹤 BIF 完成的。當開始分析時,會針對指定的一組程序啟用本地函數呼叫的追蹤,並在停止分析時停用。
使用 Eprof 時,預期程式執行速度會變慢。
摘要
函數
如果 TypeOpts
是一個 atom,則假定它是一個模組名稱,並且此呼叫等同於 analyze(TypeOpts, [])
。
當分析停止時,呼叫此函數以顯示結果。
呼叫此函數以確保 analyze/0,1,2
顯示的結果會列印到檔案 File
以及螢幕上。
如果 FunRootset
是一個 fun,則此呼叫等同於 profile([], FunRootset)
。
如果 Arg1
是一個 fun 且 Arg2
是一個列表,則此呼叫等同於 profile([], Arg1, {'_','_','_'}, Arg2)
。
此函數產生一個程序,該程序應用一個 fun 或一個函數,然後開始分析產生的程序以及 Rootset
中的程序(以及從它們產生的任何新程序)。
此函數產生一個程序 P
,該程序 apply(Module, Function, Args)
,然後開始分析 P
和 Rootset
中的程序(以及從它們產生的任何新程序)。
啟動 Eprof 伺服器,該伺服器保存收集到的資料的內部狀態。
開始分析 Rootset
中的程序(以及從它們產生的任何新程序)。
停止 Eprof 伺服器。
停止使用 start_profiling/1
或 profile/1
開始的分析。
類型
函數
-spec analyze() -> ok | nothing_to_analyze.
等同於 analyze(procs, [])
。
-spec analyze(TypeOpts) -> ok | nothing_to_analyze when TypeOpts :: analyze_type().
如果 TypeOpts
是一個 atom,則假定它是一個模組名稱,並且此呼叫等同於 analyze(TypeOpts, [])
。
否則,如果 TypeOpts
是一個列表,則假定它是一個選項列表,並且此呼叫等同於 analyze(procs, TypeOpts)
。
-spec analyze(Type, Options) -> ok | nothing_to_analyze when Type :: analyze_type(), Options :: [Option], Option :: {filter, Filter} | {sort, Sort}, Filter :: [{calls, non_neg_integer()} | {time, float()}], Sort :: time | calls | mfa.
當分析停止時,呼叫此函數以顯示結果。
如果 Type
是 procs
,則每個函數所花費的時間會針對每個分析的程序分別顯示。
如果 Type
是 total
,則每個函數所花費的時間會針對每個分析的程序合併顯示。
時間以總時間的百分比和絕對時間(以微秒為單位)顯示。
-spec log(File) -> ok when File :: atom() | file:filename().
呼叫此函數以確保 analyze/0,1,2
顯示的結果會列印到檔案 File
以及螢幕上。
-spec profile(Fun) -> {ok, Value} | {error, Reason} when Fun :: fun(() -> term()), Value :: term(), Reason :: term(); (Rootset) -> profiling | {error, Reason} when Rootset :: [atom() | pid()], Reason :: term().
如果 FunRootset
是一個 fun,則此呼叫等同於 profile([], FunRootset)
。
如果 FunRootset
是一個列表,則假定它是一個 Rootset
,並且此呼叫等同於 start_profiling(Rootset)
。
-spec profile(Fun, Options) -> {ok, Value} | {error, Reason} when Fun :: fun(() -> term()), Options :: [set_on_spawn | {set_on_spawn, boolean()}], Value :: term(), Reason :: term(); (Rootset, Fun) -> {ok, Value} | {error, Reason} when Rootset :: [atom() | pid()], Fun :: fun(() -> term()), Value :: term(), Reason :: term().
如果 Arg1
是一個 fun 且 Arg2
是一個列表,則此呼叫等同於 profile([], Arg1, {'_','_','_'}, Arg2)
。
如果 Arg1
是一個列表且 Arg2
是一個 fun,則此呼叫等同於 profile(Arg1, Arg2, {'_','_','_'}, Arg1)
。
-spec profile(Rootset, Module, Function, Args) -> {ok, Value} | {error, Reason} when Rootset :: [atom() | pid()], Module :: module(), Function :: atom(), Args :: [term()], Value :: term(), Reason :: term(); (Rootset, Fun, Pattern, Options) -> {ok, Value} | {error, Reason} when Rootset :: [atom() | pid()], Fun :: fun(() -> term()), Pattern :: trace_pattern_mfa(), Options :: [set_on_spawn | {set_on_spawn, boolean()}], Value :: term(), Reason :: term().
此函數產生一個程序,該程序應用一個 fun 或一個函數,然後開始分析產生的程序以及 Rootset
中的程序(以及從它們產生的任何新程序)。
如果 Arg1
是一個 fun,則預期 Arg2
是一個追蹤模式,而 Arg3
是一個選項列表。在這種情況下,此呼叫等同於
profile(Rootset, erlang, apply, [Arg1, []], Arg2, Arg3)
如果 Arg1
是一個 atom,則假定 Arg1
是一個模組名稱,Arg2
是該模組中函數的名稱,而 Arg3
是呼叫該函數時要使用的參數列表。在這種情況下,此呼叫等同於
profile(Rootset, Arg1, Arg2, Arg3, {'_','_','_'}, [{set_on_spawn, true}])
-spec profile(Rootset, Module, Function, Args, Pattern) -> {ok, Value} | {error, Reason} when Rootset :: [atom() | pid()], Module :: module(), Function :: atom(), Args :: [term()], Pattern :: trace_pattern_mfa(), Value :: term(), Reason :: term().
等同於 profile(Rootset, Module, Function, Args, Pattern, [{set_on_spawn, true}])
。
-spec profile(Rootset, Module, Function, Args, Pattern, Options) -> {ok, Value} | {error, Reason} when Rootset :: [atom() | pid()], Module :: module(), Function :: atom(), Args :: [term()], Pattern :: trace_pattern_mfa(), Options :: [set_on_spawn | {set_on_spawn, boolean()}], Value :: term(), Reason :: term().
此函數產生一個程序 P
,該程序 apply(Module, Function, Args)
,然後開始分析 P
和 Rootset
中的程序(以及從它們產生的任何新程序)。
Rootset
是一個 pid 和註冊名稱的列表。
有關任何分析程序中的活動資訊都儲存在 Eprof 資料庫中。
如果可以為 P
和 Rootset
中的所有程序啟用追蹤,則當 Fun()
/apply
返回值 Value
時,函數會返回 {ok,Value}
,如果 Fun()
/apply
因為退出原因 Reason
而失敗,則會返回 {error,Reason}
。否則,它會立即返回 {error, Reason}
。
set_on_spawn
選項將為 rootset 中的程序產生的所有程序啟動呼叫時間追蹤。這是預設行為。
程式設計師必須確保作為參數提供的函數是真正同步的,並且在函數傳回值後沒有任何工作繼續進行。
-spec start() -> {ok, Pid} | {error, Reason} when Pid :: pid(), Reason :: {already_started, Pid}.
啟動 Eprof 伺服器,該伺服器保存收集到的資料的內部狀態。
-spec start_profiling(Rootset, Pattern) -> profiling | {error, Reason} when Rootset :: [atom() | pid()], Pattern :: trace_pattern_mfa(), Reason :: term().
-spec start_profiling(Rootset, Pattern, Options) -> profiling | {error, Reason} when Rootset :: [atom() | pid()], Pattern :: trace_pattern_mfa(), Options :: [set_on_spawn | {set_on_spawn, boolean()}], Reason :: term().
開始分析 Rootset
中的程序(以及從它們產生的任何新程序)。
有關任何分析程序中的活動資訊都儲存在 Eprof 資料庫中。
Rootset
是一個 pid 和註冊名稱的列表。
如果可以為 Rootset
中的所有程序啟用追蹤,則函數會返回 profiling
,否則返回 error
。
可以選擇一種模式來縮小分析範圍。例如,可以選擇一個特定的模組,並且只會分析在該模組中執行的程式碼。
set_on_spawn
選項將為 rootset 中的程序產生的所有程序啟動呼叫時間追蹤。這是預設行為。
-spec stop() -> stopped.
停止 Eprof 伺服器。
-spec stop_profiling() -> profiling_stopped | profiling_already_stopped.
停止使用 start_profiling/1
或 profile/1
開始的分析。