Skip to content

Commit 8285b3d

Browse files
jcpetruzzafacebook-github-bot
authored andcommitted
Batch pid => threadId mappings
Summary: # Context The DAP specification wants to use numeric "threadIds" to identify "threads", which correspond to Erlang processes. This means that we need to keep a mapping between pids and these threadIds. For this, we have a dedicated gen_server that will given a pid will give us a new threadId, if it was never seen, or the previous one used. # Problem For the "threads" request, we need to get the list of processes, and map their pids to threadIds. So far, we were doing it one by one, which, when the number of processes is high, introduces a noticeable overhead # This diff We add a way to convert all the pids to threadIds in one operation, and use it to implement the "threads" request Reviewed By: TheGeorge Differential Revision: D72171773 fbshipit-source-id: e1fb01514155cfeffa863dceee54abedab1cfc78
1 parent 3f1b0b5 commit 8285b3d

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

edb/src/edb_dap_id_mappings.erl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ This module implements the mapping fo PIDs to thread-ids, etc, generically.
3737

3838
%% API
3939
-export([start_link_thread_ids_server/0, start_link_frame_ids_server/0, start_link_var_reference_ids_server/0]).
40-
-export([pid_to_thread_id/1, thread_id_to_pid/1]).
40+
-export([pid_to_thread_id/1, pids_to_thread_ids/1, thread_id_to_pid/1]).
4141
-export([pid_frame_to_frame_id/1, frame_id_to_pid_frame/1]).
4242
-export([var_reference_to_frame_scope/1, frame_scope_to_var_reference/1]).
4343
-export([reset/0]).
@@ -89,6 +89,10 @@ start_link_var_reference_ids_server() ->
8989
pid_to_thread_id(Pid) when is_pid(Pid) ->
9090
gen_server:call(?THREAD_IDS_SERVER, {get_id, Pid}).
9191

92+
-spec pids_to_thread_ids([pid()]) -> #{pid() => id()}.
93+
pids_to_thread_ids(Pids) ->
94+
gen_server:call(?THREAD_IDS_SERVER, {get_ids, Pids}).
95+
9296
-spec thread_id_to_pid(id()) -> {ok, pid()} | {error, not_found}.
9397
thread_id_to_pid(Id) when is_integer(Id) ->
9498
gen_server:call(?THREAD_IDS_SERVER, {from_id, Id}).
@@ -150,11 +154,23 @@ init(ServerType) ->
150154

151155
-spec handle_call
152156
({get_id, A}, gen_server:from(), state(A)) -> {reply, id(), state(A)};
157+
({get_ids, [A]}, gen_server:from(), state(A)) -> {reply, #{A => id()}, state(A)};
153158
({from_id, id()}, gen_server:from(), state(A)) -> {reply, {ok, A} | {error, not_found}, state(A)};
154159
(reset, gen_server:from(), state(A)) -> {reply, ok, state(A)}.
155160
handle_call({get_id, A}, _From, State0) ->
156161
{Id, State1} = id_mapping_get_id(A, State0),
157162
{reply, Id, State1};
163+
handle_call({get_ids, As}, _From, State0) ->
164+
{Ids, State1} = lists:foldl(
165+
fun(A, {IdsN, StateN}) ->
166+
{Id, StateN_plus_1} = id_mapping_get_id(A, StateN),
167+
IdsN_plus_1 = IdsN#{A => Id},
168+
{IdsN_plus_1, StateN_plus_1}
169+
end,
170+
{#{}, State0},
171+
As
172+
),
173+
{reply, Ids, State1};
158174
handle_call({from_id, Id}, _From, State0) ->
159175
Result = id_mapping_from_id(Id, State0),
160176
{reply, Result, State0};

edb/src/edb_dap_request_threads.erl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,21 @@ parse_arguments(Args) ->
5353
Args :: arguments().
5454
handle(#{state := attached}, _Args) ->
5555
ProcessesInfo = edb:processes([message_queue_len, pid_string, registered_name]),
56-
Threads = [thread(Pid, Info) || Pid := Info <- ProcessesInfo],
56+
ThreadIds = edb_dap_id_mappings:pids_to_thread_ids(maps:keys(ProcessesInfo)),
57+
Threads = [thread(Pid, ThreadIds, Info) || Pid := Info <- ProcessesInfo],
5758
#{response => edb_dap_request:success(#{threads => Threads})};
5859
handle(_UnexpectedState, _) ->
5960
edb_dap_request:unexpected_request().
6061

6162
%% ------------------------------------------------------------------
6263
%% Helpers
6364
%% ------------------------------------------------------------------
64-
-spec thread(pid(), edb:process_info()) -> thread().
65-
thread(Pid, Info) ->
66-
Id = edb_dap_id_mappings:pid_to_thread_id(Pid),
65+
-spec thread(Pid, ThreadIds, Info) -> thread() when
66+
Pid :: pid(),
67+
ThreadIds :: #{pid() => edb_dap:thread_id()},
68+
Info :: edb:process_info().
69+
thread(Pid, ThreadIds, Info) ->
70+
Id = maps:get(Pid, ThreadIds),
6771
#{
6872
id => Id,
6973
name => thread_name(Info)

edb/test/edb_dap_id_mappings_SUITE.erl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,12 @@ test_it_works(_Config) ->
4949
% Assigns a new id to each pid, only if needed
5050
ThreadId1 = 1 = edb_dap_id_mappings:pid_to_thread_id(Pid1),
5151
ThreadId2 = 2 = edb_dap_id_mappings:pid_to_thread_id(Pid2),
52-
ThreadId3 = 3 = edb_dap_id_mappings:pid_to_thread_id(Pid3),
53-
ThreadId4 = 4 = edb_dap_id_mappings:pid_to_thread_id(Pid4),
52+
#{
53+
Pid1 := ThreadId1,
54+
Pid2 := ThreadId2,
55+
Pid3 := 3 = ThreadId3,
56+
Pid4 := 4 = ThreadId4
57+
} = edb_dap_id_mappings:pids_to_thread_ids([Pid1, Pid2, Pid3, Pid4]),
5458

5559
ThreadId1 = edb_dap_id_mappings:pid_to_thread_id(Pid1),
5660
ThreadId2 = edb_dap_id_mappings:pid_to_thread_id(Pid2),

0 commit comments

Comments
 (0)