Skip to content

Commit 5c47d3d

Browse files
authored
Merge pull request #696 from libgit2/cache-info
Add reader methods for cache information
2 parents 128c8d5 + d9a84b4 commit 5c47d3d

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

ext/rugged/rugged_settings.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,37 @@ static VALUE rb_git_get_option(VALUE self, VALUE option)
135135
}
136136
}
137137

138+
/*
139+
* call-seq:
140+
* Rugged::Settings.max_cache_size -> max cache size
141+
*
142+
* Returns the maximum amount of memory the cache will consume.
143+
*/
144+
static VALUE rb_git_get_max_cache_size(VALUE mod) {
145+
size_t val;
146+
size_t max;
147+
git_libgit2_opts(GIT_OPT_GET_CACHED_MEMORY, &val, &max);
148+
return SIZET2NUM(max);
149+
}
150+
151+
/*
152+
* call-seq:
153+
* Rugged::Settings.used_cache_size -> used cache size
154+
*
155+
* Returns the amount of memory the cache is currently consuming.
156+
*/
157+
static VALUE rb_git_get_used_cache_size(VALUE mod) {
158+
size_t val;
159+
size_t max;
160+
git_libgit2_opts(GIT_OPT_GET_CACHED_MEMORY, &val, &max);
161+
return SIZET2NUM(val);
162+
}
163+
138164
void Init_rugged_settings(void)
139165
{
140166
VALUE rb_cRuggedSettings = rb_define_class_under(rb_mRugged, "Settings", rb_cObject);
141167
rb_define_module_function(rb_cRuggedSettings, "[]=", rb_git_set_option, 2);
142168
rb_define_module_function(rb_cRuggedSettings, "[]", rb_git_get_option, 1);
169+
rb_define_module_function(rb_cRuggedSettings, "max_cache_size", rb_git_get_max_cache_size, 0);
170+
rb_define_module_function(rb_cRuggedSettings, "used_cache_size", rb_git_get_used_cache_size, 0);
143171
}

test/settings_test.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require "test_helper"
2+
3+
class SettingsTest < Rugged::TestCase
4+
def scrub_stack size
5+
return if size == 0
6+
scrub_stack size - 1
7+
end
8+
9+
def test_used_cache_size
10+
# Repo objects hold on to the cache, so make sure all unreferenced repo objects
11+
# get GC'd
12+
scrub_stack 50
13+
GC.start
14+
15+
size = Rugged::Settings.used_cache_size
16+
repo = FixtureRepo.from_libgit2("attr")
17+
diff = repo.diff("605812a", "370fe9ec22", :context_lines => 1, :interhunk_lines => 1)
18+
19+
# cache size should grow
20+
assert_operator size, :<, Rugged::Settings.used_cache_size
21+
end
22+
23+
def test_max_cache_size
24+
# We don't assert anything about the default max cache size because it is
25+
# an implementation detail (libgit2 should be allowed to change the
26+
# default size without breaking these tests).
27+
assert Rugged::Settings.max_cache_size
28+
end
29+
end

0 commit comments

Comments
 (0)