Commit a14163a7 by Vicent Marti

cache: Shared meter for memory usage

parent f9774eea
...@@ -18,7 +18,9 @@ ...@@ -18,7 +18,9 @@
GIT__USE_OIDMAP GIT__USE_OIDMAP
bool git_cache__enabled = true; bool git_cache__enabled = true;
size_t git_cache__max_storage = (4 * 1024 * 1024); int64_t git_cache__max_storage = (4 * 1024 * 1024);
static git_atomic64 git_cache__current_storage = {0};
static size_t git_cache__max_object_size[8] = { static size_t git_cache__max_object_size[8] = {
0, /* GIT_OBJ__EXT1 */ 0, /* GIT_OBJ__EXT1 */
...@@ -81,6 +83,7 @@ static void clear_cache(git_cache *cache) ...@@ -81,6 +83,7 @@ static void clear_cache(git_cache *cache)
}); });
kh_clear(oid, cache->map); kh_clear(oid, cache->map);
git_atomic64_add(&git_cache__current_storage, -cache->used_memory);
cache->used_memory = 0; cache->used_memory = 0;
} }
...@@ -106,7 +109,7 @@ void git_cache_free(git_cache *cache) ...@@ -106,7 +109,7 @@ void git_cache_free(git_cache *cache)
static void cache_evict_entries(git_cache *cache) static void cache_evict_entries(git_cache *cache)
{ {
uint32_t seed = rand(); uint32_t seed = rand();
size_t evict_count = 8; int64_t evicted_memory = 0, evict_count = 8;
/* do not infinite loop if there's not enough entries to evict */ /* do not infinite loop if there's not enough entries to evict */
if (evict_count > kh_size(cache->map)) { if (evict_count > kh_size(cache->map)) {
...@@ -121,12 +124,15 @@ static void cache_evict_entries(git_cache *cache) ...@@ -121,12 +124,15 @@ static void cache_evict_entries(git_cache *cache)
git_cached_obj *evict = kh_val(cache->map, pos); git_cached_obj *evict = kh_val(cache->map, pos);
evict_count--; evict_count--;
cache->used_memory -= evict->size; evicted_memory += evict->size;
git_cached_obj_decref(evict); git_cached_obj_decref(evict);
kh_del(oid, cache->map, pos); kh_del(oid, cache->map, pos);
} }
} }
cache->used_memory -= evicted_memory;
git_atomic64_add(&git_cache__current_storage, -evicted_memory);
} }
static bool cache_should_store(git_otype object_type, size_t object_size) static bool cache_should_store(git_otype object_type, size_t object_size)
...@@ -171,7 +177,8 @@ static void *cache_store(git_cache *cache, git_cached_obj *entry) ...@@ -171,7 +177,8 @@ static void *cache_store(git_cache *cache, git_cached_obj *entry)
if (git_mutex_lock(&cache->lock) < 0) if (git_mutex_lock(&cache->lock) < 0)
return entry; return entry;
if (cache->used_memory > git_cache__max_storage) /* soften the load on the cache */
if (git_cache__current_storage.val > git_cache__max_storage)
cache_evict_entries(cache); cache_evict_entries(cache);
pos = kh_get(oid, cache->map, &entry->oid); pos = kh_get(oid, cache->map, &entry->oid);
...@@ -186,6 +193,7 @@ static void *cache_store(git_cache *cache, git_cached_obj *entry) ...@@ -186,6 +193,7 @@ static void *cache_store(git_cache *cache, git_cached_obj *entry)
kh_val(cache->map, pos) = entry; kh_val(cache->map, pos) = entry;
git_cached_obj_incref(entry); git_cached_obj_incref(entry);
cache->used_memory += entry->size; cache->used_memory += entry->size;
git_atomic64_add(&git_cache__current_storage, (int64_t)entry->size);
} }
} }
/* found */ /* found */
......
...@@ -31,11 +31,11 @@ typedef struct { ...@@ -31,11 +31,11 @@ typedef struct {
typedef struct { typedef struct {
git_oidmap *map; git_oidmap *map;
git_mutex lock; git_mutex lock;
size_t used_memory; int64_t used_memory;
} git_cache; } git_cache;
extern bool git_cache__enabled; extern bool git_cache__enabled;
extern size_t git_cache__max_storage; extern int64_t git_cache__max_storage;
int git_cache_set_max_object_size(git_otype type, size_t size); int git_cache_set_max_object_size(git_otype type, size_t size);
......
...@@ -39,7 +39,6 @@ int git_libgit2_capabilities() ...@@ -39,7 +39,6 @@ int git_libgit2_capabilities()
/* Declarations for tuneable settings */ /* Declarations for tuneable settings */
extern size_t git_mwindow__window_size; extern size_t git_mwindow__window_size;
extern size_t git_mwindow__mapped_limit; extern size_t git_mwindow__mapped_limit;
extern size_t git_odb__cache_size;
static int config_level_to_futils_dir(int config_level) static int config_level_to_futils_dir(int config_level)
{ {
...@@ -104,7 +103,7 @@ int git_libgit2_opts(int key, ...) ...@@ -104,7 +103,7 @@ int git_libgit2_opts(int key, ...)
} }
case GIT_OPT_SET_CACHE_MAX_SIZE: case GIT_OPT_SET_CACHE_MAX_SIZE:
git_cache__max_storage = va_arg(ap, size_t); git_cache__max_storage = va_arg(ap, int64_t);
break; break;
case GIT_OPT_ENABLE_CACHING: case GIT_OPT_ENABLE_CACHING:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment