Skip to content

Commit 4ae4111

Browse files
committed
Test read ahead by tracing file reads (chunk iterator)
1 parent 182cb04 commit 4ae4111

File tree

2 files changed

+82
-16
lines changed

2 files changed

+82
-16
lines changed

test/osiris_log_SUITE.erl

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -396,47 +396,61 @@ iterator_read_chunk_with_read_ahead(Config) ->
396396
{ok, R} = osiris_log:init_offset_reader(0, RConf),
397397
Tests =
398398
[
399-
fun(#{w := W0, r := R0}) ->
400-
%% first chunk, there won't be any data size hints in the reader
399+
fun(write, #{w := W0}) ->
401400
EntriesRev = [<<"hi">>, <<"ho">>],
402401
{_, W1} = write_committed(EntriesRev, W0),
402+
W1;
403+
(read, #{r := R0, tracer := T}) ->
404+
%% first chunk, there won't be any data size hints in the reader
403405
{ok, H, I0, R1} = osiris_log:chunk_iterator(R0),
404406
{{_, <<"ho">>}, I1} = osiris_log:iterator_next(I0),
405407
{{_, <<"hi">>}, I2} = osiris_log:iterator_next(I1),
406408
?assertMatch(end_of_chunk, osiris_log:iterator_next(I2)),
407-
{H, W1, R1}
409+
?assertEqual(2, length(osiris_tracer:calls(T))),
410+
{H, R1}
408411
end,
409-
fun(#{w := W0, r := R0}) ->
412+
fun(write, #{w := W0}) ->
410413
%% this one will be read ahead
411414
EntriesRev = [<<"foo">>, <<"bar">>],
412415
{_, W1} = write_committed(EntriesRev, W0),
416+
W1;
417+
(read, #{r := R0, tracer := T}) ->
413418
{ok, H, I0, R1} = osiris_log:chunk_iterator(R0),
414419
{{_, <<"bar">>}, I1} = osiris_log:iterator_next(I0),
415420
{{_, <<"foo">>}, I2} = osiris_log:iterator_next(I1),
416421
?assertMatch(end_of_chunk, osiris_log:iterator_next(I2)),
417-
{H, W1, R1}
422+
?assertEqual(0, length(osiris_tracer:calls(T))),
423+
{H, R1}
418424
end,
419-
fun(#{w := W0, r := R0}) ->
425+
fun(write, #{w := W0}) ->
420426
%% this one will be read ahead
421-
E1 = rand:bytes(RAL - 100),
427+
E1 = binary:copy(<<"b">>, RAL - 200),
422428
EntriesRev = [E1 , <<"aaa">>],
423429
{_, W1} = write_committed(EntriesRev, W0),
430+
W1;
431+
(read, #{r := R0, tracer := T}) ->
432+
E1 = binary:copy(<<"b">>, RAL - 200),
424433
{ok, H, I0, R1} = osiris_log:chunk_iterator(R0),
425434
{{_, <<"aaa">>}, I1} = osiris_log:iterator_next(I0),
426435
{{_, E1}, I2} = osiris_log:iterator_next(I1),
427436
?assertMatch(end_of_chunk, osiris_log:iterator_next(I2)),
428-
{H, W1, R1}
437+
?assertEqual(0, length(osiris_tracer:calls(T))),
438+
{H, R1}
429439
end,
430-
fun(#{w := W0, r := R0}) ->
440+
fun(write, #{w := W0}) ->
431441
%% this one is too big to be read ahead
432-
E1 = rand:bytes(RAL * 2),
442+
E1 = binary:copy(<<"b">>, RAL * 2),
433443
EntriesRev = [E1 , <<"aaa">>],
434444
{_, W1} = write_committed(EntriesRev, W0),
445+
W1;
446+
(read, #{r := R0, tracer := T}) ->
447+
E1 = binary:copy(<<"b">>, RAL * 2),
435448
{ok, H, I0, R1} = osiris_log:chunk_iterator(R0),
436449
{{_, <<"aaa">>}, I1} = osiris_log:iterator_next(I0),
437450
{{_, E1}, I2} = osiris_log:iterator_next(I1),
438451
?assertMatch(end_of_chunk, osiris_log:iterator_next(I2)),
439-
{H, W1, R1}
452+
?assertEqual(2, length(osiris_tracer:calls(T))),
453+
{H, R1}
440454
end
441455
],
442456

@@ -2172,11 +2186,18 @@ init_reader(data, Conf) ->
21722186
osiris_log:init_data_reader({0, empty}, Conf).
21732187

21742188
run_read_ahead_tests(Tests, RType, FSize, Wr0, Rd0) ->
2175-
lists:foldl(fun(F, Acc) ->
2176-
{H, W, R0} = F(Acc),
2177-
R1 = update_read(H, R0),
2178-
#{w => W, r => R1, rtype => RType, fsize => FSize}
2179-
end, #{w => Wr0, r => Rd0, rtype => RType, fsize => FSize}, Tests).
2189+
W = lists:foldl(fun(F, Acc) ->
2190+
W = F(write, Acc),
2191+
#{w => W}
2192+
end, #{w => Wr0}, Tests),
2193+
R = lists:foldl(fun(F, Acc) ->
2194+
Tracer = osiris_tracer:start({file, pread, '_'}),
2195+
{_, R0} = F(read, Acc#{tracer => Tracer}),
2196+
osiris_tracer:stop(Tracer),
2197+
% R1 = update_read(H, R0),
2198+
#{r => R0, rtype => RType, fsize => FSize}
2199+
end, #{r => Rd0, rtype => RType, fsize => FSize}, Tests),
2200+
maps:merge(W, R).
21802201

21812202
-spec update_read(map(), osiris_log:state()) -> osiris_log:state().
21822203
update_read(#{chunk_id := ChId,

test/osiris_tracer.erl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
%% This Source Code Form is subject to the terms of the Mozilla Public
2+
%% License, v. 2.0. If a copy of the MPL was not distributed with this
3+
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
%%
5+
%% Copyright (c) 2007-2025 Broadcom. All Rights Reserved. The term Broadcom refers to Broadcom Inc. and/or its subsidiaries.
6+
%%
7+
8+
-module(osiris_tracer).
9+
10+
-record(?MODULE,
11+
{
12+
calls = [] :: list()
13+
}).
14+
15+
-export([start/1, loop/1, calls/1, stop/1]).
16+
17+
-spec start(erlang:trace_pattern_mfa()) -> pid().
18+
start(MFA) ->
19+
P = spawn(?MODULE, loop, [#?MODULE{}]),
20+
erlang:trace(self(), true, [call, {tracer, P}]),
21+
erlang:trace_pattern(MFA, true, [global]),
22+
P.
23+
24+
loop(#?MODULE{calls = Calls} = S) ->
25+
receive
26+
{trace, _Pid, call, {Module, Function, Arguments}} ->
27+
loop(#?MODULE{calls = [{Module, Function, Arguments} | Calls]});
28+
{P, calls} ->
29+
P ! lists:reverse(Calls),
30+
loop(S);
31+
stop ->
32+
ok;
33+
_ ->
34+
loop(S)
35+
end.
36+
37+
calls(P) ->
38+
P ! {self(), calls},
39+
receive
40+
Calls -> Calls
41+
end.
42+
43+
stop(P) ->
44+
erlang:trace(self(), false, [call]),
45+
P ! stop.

0 commit comments

Comments
 (0)