Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dss fixcache leak #27

Merged
merged 2 commits into from
May 10, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions c_src/eleveldb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,12 @@ static void eleveldb_db_resource_cleanup(ErlNifEnv* env, void* arg)
// Delete any dynamically allocated memory stored in eleveldb_db_handle
eleveldb_db_handle* handle = (eleveldb_db_handle*)arg;
delete handle->db;

// Release any cache we explicitly allocated when setting up options
if (handle->options.block_cache)
{
delete handle->options.block_cache;
}
}

static void eleveldb_itr_resource_cleanup(ErlNifEnv* env, void* arg)
Expand Down
71 changes: 71 additions & 0 deletions test/cacheleak.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
%% -------------------------------------------------------------------
%%
%% eleveldb: Erlang Wrapper for LevelDB (http://code.google.com/p/leveldb/)
%%
%% Copyright (c) 2010 Basho Technologies, Inc. All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License. You may obtain
%% a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% -------------------------------------------------------------------
-module(cacheleak).

-compile(export_all).

-include_lib("eunit/include/eunit.hrl").

cacheleak_test_() ->
{timeout, 300*60, fun() ->
[] = os:cmd("rm -rf /tmp/eleveldb.cacheleak.test"),
Blobs = [{<<I:128/unsigned>>, compressible_bytes(10240)} ||
I <- lists:seq(1, 10000)],
cacheleak_loop(10, Blobs, 300000)
end}.

%% It's very important for this test that the data is compressible. Otherwise,
%% the file will be mmaped, and nothing will fill up the cache.
compressible_bytes(Count) ->
list_to_binary([0 || _I <- lists:seq(1, Count)]).

cacheleak_loop(0, _Blobs, _MaxFinalRSS) ->
ok;
cacheleak_loop(Count, Blobs, MaxFinalRSS) ->
%% We spawn a process to open a LevelDB instance and do a series of
%% reads/writes to fill up the cache. When the process exits, the LevelDB
%% ref will get GC'd and we can re-evaluate the memory footprint of the
%% process to make sure everything got cleaned up as expected.
F = fun() ->

{ok, Ref} = eleveldb:open("/tmp/eleveldb.cacheleak.test",
[{create_if_missing, true},
{cache_size, 83886080}]),
[ok = eleveldb:put(Ref, I, B, []) || {I, B} <- Blobs],
eleveldb:fold(Ref, fun({_K, _V}, A) -> A end, [], [{fill_cache, true}]),
[{ok, B} = eleveldb:get(Ref, I, []) || {I, B} <- Blobs],
io:format(user, "RSS1: ~p\n", [rssmem()])
end,
{_Pid, Mref} = spawn_monitor(F),
receive
{'DOWN', Mref, process, _, _} ->
ok
end,
RSS = rssmem(),
?assert(MaxFinalRSS > RSS),
cacheleak_loop(Count-1, Blobs, MaxFinalRSS).

rssmem() ->
Cmd = io_lib:format("ps -o rss= ~s", [os:getpid()]),
S = string:strip(os:cmd(Cmd), both),
{I, _} = string:to_integer(S),
I.