Skip to content

Commit

Permalink
CLEANUP : reflected jhpark816 review
Browse files Browse the repository at this point in the history
  • Loading branch information
jooho812 committed Aug 18, 2017
1 parent c110481 commit bd57797
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 276 deletions.
46 changes: 32 additions & 14 deletions engines/COMMON/mblock_allocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,32 +171,33 @@ bool mblock_list_alloc(uint32_t blck_cnt, mem_block_t **head_blk, mem_block_t **
total_mblocks += new_cnt;
//pthread_mutex_unlock(&pool_mutex);
if (alloc_cnt < blck_cnt) {
mblock_list_free(alloc_cnt, *head_blk, *tail_blk);
mblock_list_free(alloc_cnt, head_blk, tail_blk);
return false;
}
}

return true;
}

void mblock_list_free(uint32_t blck_cnt, mem_block_t *head_blk, mem_block_t *tail_blk) {
void mblock_list_free(uint32_t blck_cnt, mem_block_t **head_blk, mem_block_t **tail_blk) {
//mem_block_t *bye = NULL;
//mem_block_t *bye_helper = NULL;

//pthread_mutex_lock(&pool_mutex);
if (head_blk == NULL || blck_cnt == 0)
if (*head_blk == NULL || blck_cnt == 0)
return;

assert(pool_tail == NULL || pool_tail->next == NULL);
assert(tail_blk->next == NULL);
assert((*tail_blk)->next == NULL);

if (pool_head == NULL) {
pool_head = head_blk;
pool_head = *head_blk;
} else {
pool_tail->next = head_blk;
pool_tail->next = *head_blk;
}
pool_tail = tail_blk;
pool_tail = *tail_blk;

*head_blk = *tail_blk = NULL;
free_mblocks += blck_cnt;
assert(free_mblocks <= total_mblocks);

Expand Down Expand Up @@ -226,12 +227,29 @@ void mblock_list_free(uint32_t blck_cnt, mem_block_t *head_blk, mem_block_t *tai

bool eblk_prepare(eblock_result_t *result, uint32_t elem_count) {
assert(elem_count > 0);
uint32_t blkcnt = ((elem_count - 1) / EITEMS_PER_BLOCK) + 1;
if (!mblock_list_alloc(blkcnt, &result->head_blk, &result->last_blk)) {
result->elem_cnt = 0;
return false;
uint32_t blkcnt;
if (result->head_blk == NULL) { // empty block
blkcnt = ((elem_count - 1) / EITEMS_PER_BLOCK) + 1;
if (!mblock_list_alloc(blkcnt, &result->head_blk, &result->last_blk)) {
result->elem_cnt = 0;
return false;
}
result->tail_blk = NULL;
} else {
mem_block_t *tmp_head;
mem_block_t *tmp_last;
uint32_t curr_blkcnt = result->blck_cnt;
uint32_t curr_elemcnt = result->elem_cnt;
blkcnt = ((result->elem_cnt + elem_count - 1) / EITEMS_PER_BLOCK) + 1;
if (blkcnt > curr_blkcnt) { // need append block
if (!mblock_list_alloc((blkcnt - curr_blkcnt), &tmp_head, &tmp_last)) {
result->elem_cnt = curr_elemcnt;
return false;
}
result->last_blk->next = tmp_head;
result->last_blk = tmp_last;
}
}
result->tail_blk = NULL;
result->blck_cnt = blkcnt;
return true;
}
Expand All @@ -246,13 +264,13 @@ void eblk_truncate(eblock_result_t *result) {
uint32_t used_nblks = ((result->elem_cnt - 1) / EITEMS_PER_BLOCK) + 1;
uint32_t free_nblks = result->blck_cnt - used_nblks;

mblock_list_free(free_nblks, free_head, free_tail);
mblock_list_free(free_nblks, &free_head, &free_tail);
result->tail_blk->next = NULL;
result->last_blk = result->tail_blk;
result->blck_cnt -= free_nblks;
}
} else { /* ENGINE_ELEM_ENOENT case */
mblock_list_free(result->blck_cnt, result->head_blk, result->last_blk);
mblock_list_free(result->blck_cnt, &result->head_blk, &result->last_blk);
result->head_blk = result->tail_blk = result->last_blk = NULL;
result->elem_cnt = result->blck_cnt = 0;
}
Expand Down
2 changes: 1 addition & 1 deletion engines/COMMON/mblock_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void mblock_allocator_destroy(void);
void mblock_allocator_stats(mblock_stats *blk_stat);

bool mblock_list_alloc(uint32_t blck_cnt, mem_block_t **head_blk, mem_block_t **tail_blk);
void mblock_list_free(uint32_t blck_cnt, mem_block_t *head_blk, mem_block_t *tail_blk);
void mblock_list_free(uint32_t blck_cnt, mem_block_t **head_blk, mem_block_t **tail_blk);

bool eblk_prepare(eblock_result_t *result, uint32_t elem_count);
void eblk_truncate(eblock_result_t *result);
Expand Down
25 changes: 1 addition & 24 deletions engines/default/default_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ default_btree_elem_get(ENGINE_HANDLE* handle, const void* cookie,
ret = btree_elem_get(engine, key, nkey, bkrange, efilter,
offset, req_count, delete, drop_if_empty,
#ifdef USE_EBLOCK_RESULT
eblk_ret, EBLOCK_RESULT_SINGLE,
eblk_ret,
#else
(btree_elem_item**)eitem_array, eitem_count,
#endif
Expand All @@ -914,26 +914,6 @@ default_btree_elem_get(ENGINE_HANDLE* handle, const void* cookie,
return ret;
}

#ifdef USE_EBLOCK_RESULT
static ENGINE_ERROR_CODE
default_btree_elem_mget(ENGINE_HANDLE* handle, const void* cookie,
const token_t *key_tokens, const uint32_t numkeys,
const bkey_range *bkrange, const eflag_filter *efilter,
const uint32_t offset, const uint32_t req_count,
eblock_result_t *eblk_ret,
uint32_t *access_count, uint16_t vbucket)
{
struct default_engine *engine = get_handle(handle);
ENGINE_ERROR_CODE ret;
VBUCKET_GUARD(engine, vbucket);

ret = btree_elem_mget(engine, key_tokens, numkeys, bkrange,
efilter, offset, req_count,
eblk_ret, access_count);
return ret;
}
#endif

static ENGINE_ERROR_CODE
default_btree_elem_count(ENGINE_HANDLE* handle, const void* cookie,
const void* key, const int nkey,
Expand Down Expand Up @@ -1658,9 +1638,6 @@ create_instance(uint64_t interface, GET_SERVER_API get_server_api,
.btree_elem_delete = default_btree_elem_delete,
.btree_elem_arithmetic = default_btree_elem_arithmetic,
.btree_elem_get = default_btree_elem_get,
#ifdef USE_EBLOCK_RESULT
.btree_elem_mget = default_btree_elem_mget,
#endif
.btree_elem_count = default_btree_elem_count,
.btree_posi_find = default_btree_posi_find,
.btree_posi_find_with_get = default_btree_posi_find_with_get,
Expand Down
96 changes: 25 additions & 71 deletions engines/default/items.c
Original file line number Diff line number Diff line change
Expand Up @@ -4477,7 +4477,7 @@ static ENGINE_ERROR_CODE do_btree_elem_get(struct default_engine *engine, btree_
const int bkrtype, const bkey_range *bkrange, const eflag_filter *efilter,
const uint32_t offset, const uint32_t count, const bool delete,
#ifdef USE_EBLOCK_RESULT
eblock_result_t *eblk_ret, EBLOCK_RESULT_TYPE eblk_type,
eblock_result_t *eblk_ret,
#else
btree_elem_item **elem_array, uint32_t *elem_count,
#endif
Expand All @@ -4498,17 +4498,8 @@ static ENGINE_ERROR_CODE do_btree_elem_get(struct default_engine *engine, btree_
}

#ifdef USE_EBLOCK_RESULT
if (eblk_type == EBLOCK_RESULT_SINGLE) {
if (!eblk_prepare(eblk_ret, (count > 0 && count < info->ccnt) ? count : info->ccnt))
return ENGINE_ENOMEM;
} else { /* EBLOCK_RESULT_MULTI */
if (EBLOCK_HEAD(eblk_ret) == NULL) {
uint32_t max_cnt = (EBLOCK_NKEY_COUNT(eblk_ret) > 1) ? max_btree_size : info->ccnt;
uint32_t need_size = (count > 0 && count < max_cnt) ? count : max_cnt;
if (!eblk_prepare(eblk_ret, need_size * EBLOCK_NKEY_COUNT(eblk_ret)))
return ENGINE_ENOMEM;
}
}
if (!eblk_prepare(eblk_ret, (count > 0 && count < info->ccnt) ? count : info->ccnt))
return ENGINE_ENOMEM;
#endif

assert(info->root->ndepth < BTREE_MAX_DEPTH);
Expand Down Expand Up @@ -4650,8 +4641,7 @@ static ENGINE_ERROR_CODE do_btree_elem_get(struct default_engine *engine, btree_
*access_count = tot_access;

#ifdef USE_EBLOCK_RESULT
if (eblk_type == EBLOCK_RESULT_SINGLE)
eblk_truncate(eblk_ret);
eblk_truncate(eblk_ret);
#else
*elem_count = tot_found;
#endif
Expand Down Expand Up @@ -7251,7 +7241,7 @@ ENGINE_ERROR_CODE btree_elem_get(struct default_engine *engine,
const uint32_t offset, const uint32_t req_count,
const bool delete, const bool drop_if_empty,
#ifdef USE_EBLOCK_RESULT
eblock_result_t *eblk_ret, EBLOCK_RESULT_TYPE eblk_type,
eblock_result_t *eblk_ret,
#else
btree_elem_item **elem_array, uint32_t *elem_count,
#endif
Expand Down Expand Up @@ -7281,8 +7271,8 @@ ENGINE_ERROR_CODE btree_elem_get(struct default_engine *engine,
}
#ifdef USE_EBLOCK_RESULT
ret = do_btree_elem_get(engine, info, bkrtype, bkrange, efilter,
offset, req_count, delete, eblk_ret,
eblk_type, access_count, &potentialbkeytrim);
offset, req_count, delete,
eblk_ret, access_count, &potentialbkeytrim);
#else
ret = do_btree_elem_get(engine, info, bkrtype, bkrange, efilter,
offset, req_count, delete, elem_array,
Expand Down Expand Up @@ -7317,44 +7307,6 @@ ENGINE_ERROR_CODE btree_elem_get(struct default_engine *engine,
return ret;
}

#ifdef USE_EBLOCK_RESULT
ENGINE_ERROR_CODE btree_elem_mget(struct default_engine *engine,
const token_t *key_tokens, const uint32_t numkeys,
const bkey_range *bkrange, const eflag_filter *efilter,
const uint32_t offset, const uint32_t req_count,
eblock_result_t *eblk_ret,
uint32_t *access_count)
{
assert (EBLOCK_HEAD(eblk_ret) == NULL);
ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
int k;
uint32_t cur_elem_count = 0;
uint32_t cur_access_count = 0;

for (k = 0; k < numkeys; k++) {
ret = btree_elem_get(engine, key_tokens[k].value, key_tokens[k].length,
bkrange, efilter, offset, req_count, false, false,
eblk_ret, EBLOCK_RESULT_MULTI, &cur_access_count,
&eblk_ret->flags[k], &eblk_ret->trimmed[k]);
if (ret == ENGINE_ENOMEM) { /* eblk_prepare failed */
break;
} else {
eblk_ret->ret[k] = ret;
eblk_ret->cur_ecnt[k] = EBLOCK_ELEM_COUNT(eblk_ret) - cur_elem_count;
cur_elem_count = EBLOCK_ELEM_COUNT(eblk_ret);
access_count += cur_access_count;
cur_access_count = 0;
if (--EBLOCK_NKEY_COUNT(eblk_ret) < 1) {
ret = ENGINE_SUCCESS;
if (EBLOCK_HEAD(eblk_ret) != NULL)
eblk_truncate(eblk_ret);
}
}
}
return ret;
}
#endif

ENGINE_ERROR_CODE btree_elem_count(struct default_engine *engine,
const char *key, const size_t nkey,
const bkey_range *bkrange, const eflag_filter *efilter,
Expand Down Expand Up @@ -7946,25 +7898,27 @@ static void do_coll_elem_release(struct default_engine *engine, eitem *item, EIT
pthread_mutex_unlock(&engine->cache_lock);
} else if (type == EITEM_TYPE_BLOCK) {
eblock_result_t *eblk_ret = (eblock_result_t *)item;
uint32_t elem_count = EBLOCK_ELEM_COUNT(eblk_ret);
uint32_t lock_count, i, j;
eblock_scan_t eblk_sc;
eitem *elem;

EBLOCK_SCAN_INIT(eblk_ret, &eblk_sc);
for (i = 0; i < elem_count; i += lock_count) {
lock_count = (elem_count-i) < 100
? (elem_count-i) : 100;
pthread_mutex_lock(&engine->cache_lock);
for (j = 0; j < lock_count; j++) {
EBLOCK_SCAN_NEXT(&eblk_sc, elem);
elem_release_func(engine, elem);
if (eblk_ret->head_blk != NULL) { //validate check
uint32_t elem_count = EBLOCK_ELEM_COUNT(eblk_ret);
uint32_t lock_count, i, j;
eblock_scan_t eblk_sc;
eitem *elem;

EBLOCK_SCAN_INIT(eblk_ret, &eblk_sc);
for (i = 0; i < elem_count; i += lock_count) {
lock_count = (elem_count-i) < 100
? (elem_count-i) : 100;
pthread_mutex_lock(&engine->cache_lock);
for (j = 0; j < lock_count; j++) {
EBLOCK_SCAN_NEXT(&eblk_sc, elem);
elem_release_func(engine, elem);
}
pthread_mutex_unlock(&engine->cache_lock);
}
pthread_mutex_lock(&engine->cache_lock);
mblock_list_free(eblk_ret->blck_cnt, &eblk_ret->head_blk, &eblk_ret->tail_blk);
pthread_mutex_unlock(&engine->cache_lock);
}
pthread_mutex_lock(&engine->cache_lock);
mblock_list_free(eblk_ret->blck_cnt, eblk_ret->head_blk, eblk_ret->tail_blk);
pthread_mutex_unlock(&engine->cache_lock);
}
}
#endif
Expand Down
11 changes: 1 addition & 10 deletions engines/default/items.h
Original file line number Diff line number Diff line change
Expand Up @@ -567,22 +567,13 @@ ENGINE_ERROR_CODE btree_elem_get(struct default_engine *engine,
const uint32_t offset, const uint32_t req_count,
const bool delete, const bool drop_if_empty,
#ifdef USE_EBLOCK_RESULT
eblock_result_t *eblk_ret, EBLOCK_RESULT_TYPE eblk_type,
eblock_result_t *eblk_ret,
#else
btree_elem_item **elem_array, uint32_t *elem_count,
#endif
uint32_t *access_count,
uint32_t *flags, bool *dropped_trimmed);

#ifdef USE_EBLOCK_RESULT
ENGINE_ERROR_CODE btree_elem_mget(struct default_engine *engine,
const token_t *key_tokens, const uint32_t numkeys,
const bkey_range *bkrange, const eflag_filter *efilter,
const uint32_t offset, const uint32_t req_count,
eblock_result_t *eblk_ret,
uint32_t *access_count);
#endif

ENGINE_ERROR_CODE btree_elem_count(struct default_engine *engine,
const char *key, const size_t nkey,
const bkey_range *bkrange, const eflag_filter *efilter,
Expand Down
16 changes: 0 additions & 16 deletions engines/demo/demo_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -587,19 +587,6 @@ Demo_btree_elem_get(ENGINE_HANDLE* handle, const void* cookie,
return ENGINE_ENOTSUP;
}

#ifdef USE_EBLOCK_RESULT
static ENGINE_ERROR_CODE
Demo_btree_elem_mget(ENGINE_HANDLE* handle, const void*cookie,
const token_t *key_tokens, uint32_t numkeys,
const bkey_range *bkrange, const eflag_filter *efilter,
const uint32_t offset, const uint32_t req_count,
eblock_result_t *eblk_ret,
uint32_t *access_count, uint16_t vbucket)
{
return ENGINE_ENOTSUP;
}
#endif

static ENGINE_ERROR_CODE
Demo_btree_elem_count(ENGINE_HANDLE* handle, const void* cookie,
const void* key, const int nkey,
Expand Down Expand Up @@ -871,9 +858,6 @@ create_instance(uint64_t interface, GET_SERVER_API get_server_api,
.btree_elem_delete = Demo_btree_elem_delete,
.btree_elem_arithmetic = Demo_btree_elem_arithmetic,
.btree_elem_get = Demo_btree_elem_get,
#ifdef USE_EBLOCK_RESULT
.btree_elem_mget = Demo_btree_elem_mget,
#endif
.btree_elem_count = Demo_btree_elem_count,
.btree_posi_find = Demo_btree_posi_find,
.btree_posi_find_with_get = Demo_btree_posi_find_with_get,
Expand Down
12 changes: 0 additions & 12 deletions include/memcached/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -563,18 +563,6 @@ extern "C" {
uint32_t* access_count, uint32_t* flags,
bool* dropped_trimmed, uint16_t vbucket);

#ifdef USE_EBLOCK_RESULT
ENGINE_ERROR_CODE (*btree_elem_mget)(ENGINE_HANDLE* handle, const void* cookie,
const token_t *key_tokens,
const uint32_t numkeys,
const bkey_range *bkrange,
const eflag_filter *efilter,
const uint32_t offset,
const uint32_t req_count,
eblock_result_t *eblk_ret,
uint32_t *access_count, uint16_t vbucket);
#endif

ENGINE_ERROR_CODE (*btree_elem_count)(ENGINE_HANDLE* handle, const void* cookie,
const void* key, const int nkey,
const bkey_range *bkrange,
Expand Down
Loading

0 comments on commit bd57797

Please sign in to comment.