-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Historically memcached module uses slab cache created by Tarantool. Fibers get access to it via call of function cord_slab_cache() that is a part of Tarantool C API. The problem is happen when memcached and Tarantool use different versions of Small library where ABI is broken. Issues #59 and #96 are examples of such problems. We decided use independent slab arena in memcached module to avoid such problems in future and this patch implements it. Below I'll describe what is important in switching to own arena. memcached module uses different types of Small's allocators (ibuf and obuf used for every network connection, one slab cache used per fiber). There are two important things with used Small's allocators: 1. some allocators depends on other and sequence of allocations should be correct. README in small source code repository has a clear description of relationships between allocators [1]. 2. some allocators are thread-safe and others not. slab arena is shared between all memcached instances, when slab cache allocated on top of slab arena is not thread-safe and must be used in scope of fiber. memcached performs memory allocations and deallocations on different stages and I would list all these stages explicitly: - memcached module initialization - memcached module deinitialization - memcached instance gc - memcached instance initialization - memcached instance start - memcached instance stop - memcached instance deinitialization Most part of these cases covered in tests test/common/instance_api.test.lua and test/common/module_api.test.lua that were added in previous commits. The exception here is gc that was checked manually. In tests function is_port_open() has been replaced to memcached minimalistic client that do set value, get value with the same key and compare both values. This check is more rigorous than check port openess. 1. https://github.com/tarantool/small Fixes #96
- Loading branch information
Showing
6 changed files
with
124 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,58 @@ | ||
#include <tarantool/module.h> | ||
#include <small/slab_cache.h> | ||
#include <small/quota.h> | ||
|
||
static struct slab_arena arena; | ||
static struct slab_cache slabc; | ||
static struct quota quota; | ||
|
||
void | ||
memcached_slab_arena_create() | ||
{ | ||
static __thread bool inited = false; | ||
if (inited) return; | ||
size_t quota_size = QUOTA_MAX; | ||
quota_init("a, quota_size); | ||
/* Parameters are the same as in src/lib/core/memory.c:memory_init() */ | ||
const size_t SLAB_SIZE = 4 * 1024 * 1024; | ||
slab_arena_create(&arena, "a, 0, SLAB_SIZE, SLAB_ARENA_PRIVATE); | ||
say_info("creating arena with %zu bytes...", quota_size); | ||
inited = 1; | ||
} | ||
|
||
void | ||
memcached_slab_arena_destroy() | ||
{ | ||
static __thread bool freed = false; | ||
if (freed) return; | ||
say_info("destroying arena..."); | ||
slab_arena_destroy(&arena); | ||
freed = 1; | ||
} | ||
|
||
void | ||
memcached_slab_cache_create() | ||
{ | ||
static __thread bool inited = false; | ||
if (inited) return; | ||
slab_cache_set_thread(&slabc); | ||
slab_cache_create(&slabc, &arena); | ||
say_info("allocate slab cache with slab size %u", arena.slab_size); | ||
inited = 1; | ||
} | ||
|
||
void | ||
memcached_slab_cache_destroy() | ||
{ | ||
static __thread bool freed = false; | ||
if (freed) return; | ||
say_info("destroying slab cache"); | ||
slab_cache_destroy(&slabc); | ||
freed = 1; | ||
} | ||
|
||
struct slab_cache * | ||
memcached_slab_cache() | ||
{ | ||
return &slabc; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,19 @@ | ||
#ifndef ALLOC_H_INCLUDED | ||
#define ALLOC_H_INCLUDED | ||
|
||
void | ||
memcached_slab_arena_create(); | ||
|
||
void | ||
memcached_slab_arena_destroy(); | ||
|
||
void | ||
memcached_slab_cache_create(); | ||
|
||
void | ||
memcached_slab_cache_destroy(); | ||
|
||
struct slab_cache * | ||
memcached_slab_cache(); | ||
|
||
#endif /* ALLOC_H_INCLUDED */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters