Skip to content

Commit 1b2e2e3

Browse files
committed
Sort statistics (WIP)
1 parent 7e9fd56 commit 1b2e2e3

1 file changed

Lines changed: 25 additions & 4 deletions

File tree

src/tr.erl

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
reduce_call_trees/1,
3030
top_call_trees/0, top_call_trees/1, top_call_trees/2,
3131
print_sorted_call_stat/2,
32-
sorted_call_stat/1,
32+
sorted_call_stat/1, sorted_call_stat/2,
3333
call_stat/1, call_stat/2]).
3434

3535
%% API - utilities
@@ -181,6 +181,13 @@
181181
-type tb_acc_list() :: [[tr()]].
182182
-type tb_acc() :: tb_acc_tree() | tb_acc_list().
183183

184+
-type sorted_call_stat_options() :: #{sort_by => call_stat_field()}.
185+
%% Options for sorted call statistics.
186+
%%
187+
%% `sort_by' is the field name to sort by (default: `acc_time').
188+
189+
-type call_stat_field() :: count | acc_time | own_time.
190+
184191
-type call() :: {call, {module(), atom(), list()}}.
185192
-type result() :: {return | exception, any()}. % Result of a function call.
186193
-type simple_tr() :: call() | result().
@@ -482,7 +489,14 @@ print_sorted_call_stat(KeyF, Limit) ->
482489
%% @see call_stat/1
483490
-spec sorted_call_stat(selector(Key)) -> [{Key, call_count(), acc_time(), own_time()}].
484491
sorted_call_stat(KeyF) ->
485-
lists:reverse(sort_by_time(call_stat(KeyF))).
492+
sorted_call_stat(KeyF, #{}).
493+
494+
%% @doc TODO
495+
-spec sorted_call_stat(selector(Key), sorted_call_stat_options()) ->
496+
[{Key, call_count(), acc_time(), own_time()}].
497+
sorted_call_stat(KeyF, Options) ->
498+
SortBy = maps:get(sort_by, Options, acc_time),
499+
lists:reverse(sort_call_stat(SortBy, call_stat(KeyF))).
486500

487501
%% @doc Returns call time statistics for traces selected from `tab()'.
488502
%%
@@ -1018,8 +1032,15 @@ filter_range(_PrefF, #tr{event = Event}, _State, _MaxDepth) when ?is_msg(Event)
10181032

10191033
%% Call stat
10201034

1021-
sort_by_time(MapStat) ->
1022-
lists:keysort(3, [{Key, Count, AccTime, OwnTime} || {Key, {Count, AccTime, OwnTime}} <- maps:to_list(MapStat)]).
1035+
-spec sort_call_stat(call_stat_field(), #{Key => {call_count(), acc_time(), own_time()}}) ->
1036+
[{Key, call_count(), acc_time(), own_time()}].
1037+
sort_call_stat(SortBy, MapStat) ->
1038+
Rows = [{Key, Count, AccTime, OwnTime} || {Key, {Count, AccTime, OwnTime}} <- maps:to_list(MapStat)],
1039+
lists:keysort(sort_by_pos(SortBy), Rows).
1040+
1041+
sort_by_pos(count) -> 2;
1042+
sort_by_pos(acc_time) -> 3;
1043+
sort_by_pos(own_time) -> 4.
10231044

10241045
call_stat_step(_KeyF, #tr{event = Event}, State) when ?is_msg(Event) ->
10251046
State;

0 commit comments

Comments
 (0)