From 1d6dfee19245ce1d02162b8f2889c16cac057b37 Mon Sep 17 00:00:00 2001 From: Gregory Burd Date: Thu, 14 Mar 2013 13:27:59 -0400 Subject: [PATCH 1/2] Cache sizing is critical for performance. This change guesses at a reasonable setting for the WiredTiger cache size at runtime. This cache is shared across all vnodes regarless of how many are active at any given time. The algorithm is: max(1GB, 1/3 (RAM - Beam RSS size)). We don't enable direct_io on purpose and data will be double buffered in WiredTiger's cache and the filesystem buffer cache. This turns out to be faster than direct I/O despite wasting a bit of RAM. --- src/riak_kv_wterl_backend.erl | 44 ++++++++++++++++++++++++++++++++--- src/temp_riak_kv_backend.erl | 4 +++- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/riak_kv_wterl_backend.erl b/src/riak_kv_wterl_backend.erl index 4f9e0bd..eeaf4f8 100644 --- a/src/riak_kv_wterl_backend.erl +++ b/src/riak_kv_wterl_backend.erl @@ -24,6 +24,8 @@ -behavior(temp_riak_kv_backend). -author('Steve Vinoski '). +-compile([{parse_transform, lager_transform}]). + %% KV Backend API -export([api_version/0, capabilities/1, @@ -111,7 +113,7 @@ start(Partition, Config) -> {logging, true}, {transactional, true}, {session_max, SessionMax}, - {cache_size, "2GB"}, + {cache_size, size_cache(Config)}, {sync, false} %% {verbose, %% ["block", "shared_cache", "ckpt", "evict", @@ -135,8 +137,7 @@ start(Partition, Config) -> session=SRef, partition=Partition}}; {error, ConnReason}=ConnError -> - lager:error("Failed to start wterl backend: ~p\n", - [ConnReason]), + lager:error("Failed to start wterl backend: ~p\n", [ConnReason]), ConnError end; Error -> @@ -476,6 +477,43 @@ fetch_status(Cursor, {ok, Stat}, Acc) -> [What,Val|_] = [binary_to_list(B) || B <- binary:split(Stat, [<<0>>], [global])], fetch_status(Cursor, wterl:cursor_next_value(Cursor), [{What,Val}|Acc]). +size_cache(Config) -> + Size = + case app_helper:get_prop_or_env(cache_size, Config, wterl) of + {ok, Value} -> + Value; + undefined -> + RunningApps = application:which_applications(), + FinalGuess = + case proplists:is_defined(sasl, RunningApps) andalso + proplists:is_defined(os_mon, RunningApps) of + true -> + Memory = memsup:get_system_memory_data(), + TotalRAM = proplists:get_value(system_total_memory, Memory), + FreeRAM = proplists:get_value(free_memory, Memory), + UsedByBeam = proplists:get_value(total, erlang:memory()), + Target = ((TotalRAM - UsedByBeam) div 3), + FirstGuess = (Target - (Target rem (1024 * 1024))), + SecondGuess = + case FirstGuess > FreeRAM of + true -> FreeRAM - (FreeRAM rem (1024 * 1024)); + _ -> FirstGuess + end, + case SecondGuess < 1073741824 of %% < 1GB? + true -> "1GB"; + false -> + ThirdGuess = SecondGuess div (1024 * 1024), + integer_to_list(ThirdGuess) ++ "MB" + end; + false -> + "1GB" + end, + application:set_env(wt, cache_size, FinalGuess), + lager:warning("Using best-guess cache size of ~p for WiredTiger storage backend.", [FinalGuess]), + FinalGuess + end, + Size. + %% =================================================================== %% EUnit tests %% =================================================================== diff --git a/src/temp_riak_kv_backend.erl b/src/temp_riak_kv_backend.erl index 6763692..5c27461 100644 --- a/src/temp_riak_kv_backend.erl +++ b/src/temp_riak_kv_backend.erl @@ -272,7 +272,9 @@ empty_check({Backend, State}) -> }. setup({BackendMod, Config}) -> - %% Start the backend + lager:start(), + application:start(sasl), + application:start(os_mon), {ok, S} = BackendMod:start(42, Config), {BackendMod, S}. -- 2.45.2 From ef3e161f125af4bc6f152722f5f32ee1cc325e28 Mon Sep 17 00:00:00 2001 From: Gregory Burd Date: Thu, 14 Mar 2013 13:33:34 -0400 Subject: [PATCH 2/2] Embelish rebar's config a bit. --- rebar.config | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/rebar.config b/rebar.config index 66e81cd..f379e05 100644 --- a/rebar.config +++ b/rebar.config @@ -1,6 +1,39 @@ %%-*- mode: erlang -*- %% ex: ft=erlang ts=4 sw=4 et -{port_specs, [{"priv/wterl.so", ["c_src/*.c"]}]}. + +{require_otp_vsn, "R1[456]"}. + +{cover_enabled, true}. + +{eunit_opts, [verbose, {report, {eunit_surefire, [{dir, "."}]}}]}. + +{erl_opts, [%{d,'DEBUG',true}, + debug_info, + fail_on_warning, + warn_unused_vars, + warn_export_all, + warn_shadow_vars, + warn_unused_import, + warn_unused_function, + warn_bif_clash, + warn_unused_record, + warn_deprecated_function, + warn_obsolete_guard, + warn_export_vars, + warn_exported_vars, + warn_untyped_record, + {parse_transform, lager_transform} + %warn_missing_spec, + %strict_validation + ]}. + +{xref_checks, [undefined_function_calls]}. + +{deps, [ + {lager, "2.*", {git, "git://github.com/basho/lager", {branch, "master"}}} + ]}. + +{port_specs, [{"priv/wt.so", ["c_src/*.c"]}]}. {port_env, [ {"DRV_CFLAGS", "$DRV_CFLAGS -Werror -I c_src/system/include"}, -- 2.45.2