|
| 1 | +-module(hprobe_inject). |
| 2 | +-export([test/0]). |
| 3 | + |
| 4 | +%% Per-round probe for the tcp_reactor_perf loop, as the test ran before it |
| 5 | +%% changed: five batches of 200 {active, once} rounds on one connection, then |
| 6 | +%% 200 warmup and 600 sustained rounds on a fresh one. |
| 7 | +%% |
| 8 | +%% A ticker process wakes every millisecond on the timer wheel, which does not |
| 9 | +%% go through the TCP reactor. For each round slower than 5 ms the probe prints |
| 10 | +%% the longest ticker gap that overlaps it. A slow round with a matching ticker |
| 11 | +%% gap means the whole program stopped (the host paused it); a slow round with |
| 12 | +%% no such gap means the round waited on the runtime alone. |
| 13 | +%% |
| 14 | +%% One line per run: |
| 15 | +%% old=ok|fail batches=[ms..] warm=ms sust=ms slow=[{Phase,Round,RoundUs,GapUs}] gaps=[GapUs >= 20 ms] |
| 16 | +test() -> |
| 17 | + Self = self(), |
| 18 | + Ticker = spawn(fun() -> tick(Self, erlang:monotonic_time(microsecond), []) end), |
| 19 | + A = pair(), |
| 20 | + Batches = [rounds(A, 200) || _ <- lists:seq(1, 5)], |
| 21 | + close(A), |
| 22 | + P = pair(), |
| 23 | + Warm = rounds(P, 200), |
| 24 | + Sust = rounds(P, 600), |
| 25 | + close(P), |
| 26 | + Ticker ! stop, |
| 27 | + Gaps = receive {gaps, G} -> G after 5000 -> [] end, |
| 28 | + BatchMs = [sum_us(B) div 1000 || B <- Batches], |
| 29 | + Sorted = lists:sort(BatchMs), |
| 30 | + Median = lists:nth(3, Sorted), |
| 31 | + WarmMs = sum_us(Warm) div 1000, |
| 32 | + SustMs = sum_us(Sust) div 1000, |
| 33 | + OldBatch = Median > 0 andalso lists:last(Sorted) > Median * 5, |
| 34 | + OldRatio = SustMs > max(1, WarmMs) * 9, |
| 35 | + Old = case OldBatch orelse OldRatio of true -> fail; false -> ok end, |
| 36 | + Phases = [{batch, R} || R <- Batches] ++ [{warm, Warm}, {sust, Sust}], |
| 37 | + Slow = lists:append([slow(Name, Rs, Gaps) || {Name, Rs} <- Phases]), |
| 38 | + Big = [U || {_, U} <- Gaps, U >= 20000], |
| 39 | + io:format("old=~p batches=~w warm=~p sust=~p slow=~w gaps=~w~n", |
| 40 | + [Old, BatchMs, WarmMs, SustMs, Slow, Big]), |
| 41 | + halt(0). |
| 42 | + |
| 43 | +pair() -> |
| 44 | + {ok, LSock} = gen_tcp:listen(0, [{active, false}, binary, {packet, line}]), |
| 45 | + {ok, Port} = inet:port(LSock), |
| 46 | + {ok, CSock} = gen_tcp:connect({127,0,0,1}, Port, [{active, false}, binary]), |
| 47 | + {ok, SSock} = gen_tcp:accept(LSock), |
| 48 | + ok = gen_tcp:close(LSock), |
| 49 | + {SSock, CSock}. |
| 50 | + |
| 51 | +close({S, C}) -> ok = gen_tcp:close(C), gen_tcp:close(S). |
| 52 | + |
| 53 | +sum_us(Rounds) -> lists:sum([U || {_, U} <- Rounds]). |
| 54 | + |
| 55 | +%% Rounds as {StartUs, DurationUs}. |
| 56 | +rounds(Pair, N) -> rounds(Pair, N, []). |
| 57 | +rounds(_Pair, 0, Acc) -> lists:reverse(Acc); |
| 58 | +rounds({S, C} = Pair, N, Acc) -> |
| 59 | + T0 = erlang:monotonic_time(microsecond), |
| 60 | + ok = inet:setopts(S, [{active, once}]), |
| 61 | + ok = gen_tcp:send(C, <<"P\r\n">>), |
| 62 | + inject(), |
| 63 | + receive {tcp, S, _} -> ok after 2000 -> erlang:error({timeout, N}) end, |
| 64 | + rounds(Pair, N - 1, [{T0, erlang:monotonic_time(microsecond) - T0} | Acc]). |
| 65 | + |
| 66 | +slow(Name, Rounds, Gaps) -> |
| 67 | + Indexed = lists:zip(lists:seq(1, length(Rounds)), Rounds), |
| 68 | + [{Name, I, U, overlap(T0, T0 + U, Gaps)} || {I, {T0, U}} <- Indexed, U > 5000]. |
| 69 | + |
| 70 | +%% Longest ticker gap overlapping [From, To]. |
| 71 | +overlap(From, To, Gaps) -> |
| 72 | + lists:max([0 | [G || {S, G} <- Gaps, S < To, S + G > From]]). |
| 73 | + |
| 74 | +%% Ticker: records every gap over 3 ms between millisecond wakeups. |
| 75 | +tick(Parent, Last, Acc) -> |
| 76 | + receive |
| 77 | + stop -> Parent ! {gaps, lists:reverse(Acc)} |
| 78 | + after 1 -> |
| 79 | + Now = erlang:monotonic_time(microsecond), |
| 80 | + Acc1 = case Now - Last > 3000 of |
| 81 | + true -> [{Last, Now - Last} | Acc]; |
| 82 | + false -> Acc |
| 83 | + end, |
| 84 | + tick(Parent, Now, Acc1) |
| 85 | + end. |
| 86 | + |
| 87 | +%% Control: one 100 ms delay at round 700 that is not a host pause. |
| 88 | +inject() -> |
| 89 | + G = case get(round) of undefined -> 1; X -> X + 1 end, |
| 90 | + put(round, G), |
| 91 | + case G of 700 -> timer:sleep(100); _ -> ok end. |
0 commit comments