aboutsummaryrefslogtreecommitdiffstats
path: root/lib/kernel/src/disk_log_1.erl
diff options
context:
space:
mode:
authorRichard Carlsson <[email protected]>2016-11-01 15:36:57 +0100
committerRichard Carlsson <[email protected]>2016-11-29 10:31:43 +0100
commiteda5c4859f561fae7d3a3b74f19dd8596c212966 (patch)
treec957eb3248ceb4618fe062af90d28201faf58a48 /lib/kernel/src/disk_log_1.erl
parent5a8a1a963edd6d6dbb35bf615a4bad7d77041443 (diff)
downloadotp-eda5c4859f561fae7d3a3b74f19dd8596c212966.tar.gz
otp-eda5c4859f561fae7d3a3b74f19dd8596c212966.tar.bz2
otp-eda5c4859f561fae7d3a3b74f19dd8596c212966.zip
Improve caching in disk_log
Avoid starting timers for flushing when the written data is empty or larger than the max cache size. Previously, a single huge write to an empty cache would be put in the cache until the next write or the timer event. Also increase the cache size from 16K to 64K.
Diffstat (limited to 'lib/kernel/src/disk_log_1.erl')
-rw-r--r--lib/kernel/src/disk_log_1.erl32
1 files changed, 22 insertions, 10 deletions
diff --git a/lib/kernel/src/disk_log_1.erl b/lib/kernel/src/disk_log_1.erl
index 2e61363aa6..d83c30f35f 100644
--- a/lib/kernel/src/disk_log_1.erl
+++ b/lib/kernel/src/disk_log_1.erl
@@ -1416,24 +1416,36 @@ open_truncate(FileName) ->
%%% Functions that access files, and throw on error.
--define(MAX, 16384). % bytes
-define(TIMEOUT, 2000). % ms
%% -> {Reply, cache()}; Reply = ok | Error
-fwrite(#cache{c = []} = FdC, _FN, B, Size) ->
+fwrite(FdC, _FN, _B, 0) ->
+ {ok, FdC}; % avoid starting a timer for empty writes
+fwrite(#cache{fd = Fd, c = C, sz = Sz} = FdC, FileName, B, Size) ->
+ Sz1 = Sz + Size,
+ C1 = cache_append(C, B),
+ if Sz1 > ?MAX_FWRITE_CACHE ->
+ write_cache(Fd, FileName, C1);
+ true ->
+ maybe_start_timer(C),
+ {ok, FdC#cache{sz = Sz1, c = C1}}
+ end.
+
+cache_append([], B) -> B;
+cache_append(C, B) -> [C | B].
+
+%% if the cache was empty, start timer (unless it's already running)
+maybe_start_timer([]) ->
case get(write_cache_timer_is_running) of
- true ->
+ true ->
ok;
- _ ->
+ _ ->
put(write_cache_timer_is_running, true),
erlang:send_after(?TIMEOUT, self(), {self(), write_cache}),
ok
- end,
- {ok, FdC#cache{sz = Size, c = B}};
-fwrite(#cache{sz = Sz, c = C} = FdC, _FN, B, Size) when Sz < ?MAX ->
- {ok, FdC#cache{sz = Sz+Size, c = [C | B]}};
-fwrite(#cache{fd = Fd, c = C}, FileName, B, _Size) ->
- write_cache(Fd, FileName, [C | B]).
+ end;
+maybe_start_timer(_C) ->
+ ok.
fwrite_header(Fd, B, Size) ->
{ok, #cache{fd = Fd, sz = Size, c = B}}.