forked from jesperes/erlang_1brc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregate.erl
230 lines (206 loc) · 7.16 KB
/
aggregate.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
-module(aggregate).
-export([ run/1
, aggregate_measurements/2
, chunk_processor/0
, line_processor/0
, parse_float/1
]).
-include_lib("eunit/include/eunit.hrl").
run([Filename]) ->
% logger:update_primary_config(#{ level => none }),
aggregate_measurements(atom_to_list(Filename), [{bufsize, 2 * 1024 * 1024}]).
aggregate_measurements(Filename, Opts) ->
process_flag(trap_exit, true),
Start = erlang:monotonic_time(),
BufSize = proplists:get_value(bufsize, Opts),
logger:info(#{bufsize => BufSize}),
{ok, FD} = prim_file:open(Filename, [read]),
NumProcessors =
proplists:get_value(parallel, Opts, erlang:system_info(schedulers) div 2),
{ProcessorPids, AllPids} = start_processors(NumProcessors),
read_chunks(FD, 0, 0, <<>>, BufSize, ProcessorPids),
Now = erlang:monotonic_time(),
logger:info(#{label => "All chunks read, waiting for processors to finish",
elapsed_secs => (Now - Start) / 1000_000_000.0}),
Map = wait_for_completion(AllPids, #{}),
%% ?assertMatch({_, 180, _, _}, maps:get(<<"Abha">>, Map)),
Fmt = format_final_map(Map),
case proplists:get_value(no_output, Opts, false) of
true -> ok;
false ->
io:format("~ts~n", [Fmt])
end.
start_processors(NumProcs) ->
logger:info(#{label => "Starting processing pipelines",
num_pipelines => NumProcs}),
lists:foldl(
fun(_, {ProcessorPids, AllPids}) ->
LineProcessorPid = proc_lib:start_link(?MODULE, line_processor, []),
ProcessorPid = proc_lib:start_link(?MODULE, chunk_processor, []),
ProcessorPid ! {line_processor, LineProcessorPid},
LineProcessorPid ! {result_pid, self()},
{[ProcessorPid|ProcessorPids],
[ProcessorPid, LineProcessorPid|AllPids]}
end, {[], []}, lists:seq(1, NumProcs)).
read_chunks(FD, N, Offset, PrevChunk, BufSize, TargetPids) ->
TargetPid = lists:nth((N rem length(TargetPids)) + 1, TargetPids),
case prim_file:pread(FD, Offset, BufSize) of
{ok, Bin} ->
Size = byte_size(Bin),
%% Read chunks pair-wise and split them so that each processed
%% chunk is on an even newline boundary
case binary:split(Bin, <<"\n">>) of
[First, NextChunk] ->
send_chunk(<<PrevChunk/binary, First/binary>>, TargetPid),
%% sleep_if_target_pid_mql_too_long(TargetPid, 20),
read_chunks(FD, N + 1, Offset + Size, NextChunk, BufSize, TargetPids);
[Chunk] ->
send_chunk(Chunk, TargetPid),
read_chunks(FD, N + 1, Offset + Size, <<>>, BufSize, TargetPids)
end;
eof ->
%% Reached end of file, process the last chunk
send_chunk(PrevChunk, TargetPid),
lists:foreach(fun(Pid) -> Pid ! eof end, TargetPids),
ok
end.
send_chunk(Chunk, TargetPid) ->
TargetPid ! {chunk, Chunk}.
wait_for_completion([], Map) ->
logger:info(#{label => "All subprocesses finished"}),
Map;
wait_for_completion(Pids, Map) ->
receive
{'EXIT', Pid, normal} ->
wait_for_completion(Pids -- [Pid], Map);
{result, SenderPid, NewMap} ->
logger:info(#{label => "Got result",
sender => SenderPid,
result_size => maps:size(NewMap)}),
wait_for_completion(Pids, merge_location_data(Map, NewMap));
_ ->
logger:error(#{label => "Unexpected message"})
end.
merge_location_data(Map1, Map2) ->
Stations = lists:usort(maps:keys(Map1) ++ maps:keys(Map2)),
lists:foldl(
fun(Station, Map) when Station =:= '$ancestors' orelse
Station =:= '$initial_call' ->
Map;
(Station, Map) ->
case {maps:get(Station, Map1, undefined),
maps:get(Station, Map2, undefined)} of
{Data1, undefined} -> maps:put(Station, Data1, Map);
{undefined, Data2} -> maps:put(Station, Data2, Map);
{Data1, Data2} ->
{Min1, Max1, Count1, Sum1} = Data1,
{Min2, Max2, Count2, Sum2} = Data2,
maps:put(
Station,
{min(Min1, Min2),
max(Max1, Max2),
Count1 + Count2,
Sum1 + Sum2},
Map)
end
end, #{}, Stations).
format_final_map(Map) ->
"{" ++
lists:join(
", ",
lists:map(
fun({Station, {Min, Max, Count, Sum}}) ->
Mean = Sum / Count,
io_lib:format("~ts=~.1f/~.1f/~.1f",
[Station, Min/10, Mean/10, Max/10])
end, lists:sort(maps:to_list(Map))))
++ "}".
%%
%% Chunk processor: this step in the pipeline takes a binary
%% consisting of an even number of line, splits it at "\n" and ";" and
%% passes it on to the line processor.
%%
chunk_processor() ->
proc_lib:init_ack(self()),
logger:info(#{label => "Chunk processor running"}),
chunk_processor_loop(undefined).
chunk_processor_loop(Pid) ->
receive
{line_processor, LineProcessorPid} ->
chunk_processor_loop(LineProcessorPid);
{chunk, Chunk} ->
Pid ! {lines, binary:split(Chunk, [<<"\n">>, <<";">>], [global])},
chunk_processor_loop(Pid);
eof ->
logger:info(#{label => "Chunk processor finished."}),
Pid ! eof;
_ ->
logger:error(#{label => "Unexpected message"})
end.
%%
%% The line processor
%%
line_processor() ->
proc_lib:init_ack(self()),
line_processor_loop(undefined).
line_processor_loop(ResultPid) ->
receive
{result_pid, Pid} ->
line_processor_loop(Pid);
{lines, Lines} ->
process_lines(Lines),
line_processor_loop(ResultPid);
eof ->
Map = maps:from_list(get()),
ResultPid ! {result, self(), Map},
{heap_size, HeapSize} = process_info(self(), heap_size),
logger:info(#{label => "Line processor finished",
heap_size => HeapSize}),
ok;
_ ->
logger:error(#{label => "Unexpected message"})
end.
process_lines([]) ->
ok;
process_lines([<<>>]) ->
ok;
process_lines([Station, TempBin|Rest]) ->
Temp = parse_float(TempBin),
case get(Station) of
undefined ->
put(Station, { Temp % min
, Temp % max
, 1 % count
, Temp % sum
});
{OldMin, OldMax, OldCount, OldSum} ->
put(Station, { min(OldMin, Temp)
, max(OldMax, Temp)
, OldCount + 1
, OldSum + Temp
})
end,
process_lines(Rest).
%% Very specialized float-parser for floats with a single fractional
%% digit, and returns the result as an integer * 10.
-define(TO_NUM(C), (C - $0)).
parse_float(<<$-, A, B, $., C>>) ->
-1 * (?TO_NUM(A) * 100 + ?TO_NUM(B) * 10 + ?TO_NUM(C));
parse_float(<<$-, B, $., C>>) ->
-1 * (?TO_NUM(B) * 10 + ?TO_NUM(C));
parse_float(<<A, B, $., C>>) ->
?TO_NUM(A) * 100 + ?TO_NUM(B) * 10 + ?TO_NUM(C);
parse_float(<<B, $., C>>) ->
?TO_NUM(B) * 10 + ?TO_NUM(C).
-ifdef(TEST).
parse_float_test() ->
lists:foreach(fun({Bin, Exp}) ->
Float = parse_float(Bin),
?debugVal({Bin, Float}),
?assert(abs(Exp - Float) =< 0.0001)
end,
[ {<<"-0.5">>, -5}
, {<<"-10.5">>, -105}
, {<<"10.5">>, 105}
]).
-endif.