Skip to content

Commit

Permalink
add tool shmcache_dump
Browse files Browse the repository at this point in the history
  • Loading branch information
yuqing committed Jun 23, 2018
1 parent 36dd319 commit cccdeb1
Show file tree
Hide file tree
Showing 5 changed files with 349 additions and 56 deletions.
91 changes: 76 additions & 15 deletions src/shm_hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,23 +271,76 @@ int shm_ht_clear(struct shmcache_context *context)
return ht_count;
}

int shm_ht_to_array(struct shmcache_context *context,
struct shmcache_hentry_array *array)
static bool shm_ht_match_key(struct shmcache_match_key_info *key_info,
struct shm_hash_entry *entry)
{
int loop;
int start;

if (entry->key_len < key_info->length) {
return false;
}
switch (key_info->op_type) {
case SHMCACHE_MATCH_KEY_OP_EXACT:
return key_info->length == entry->key_len &&
memcmp(key_info->key, entry->key, key_info->length) == 0;
case SHMCACHE_MATCH_KEY_OP_LEFT:
return memcmp(key_info->key, entry->key, key_info->length) == 0;
case SHMCACHE_MATCH_KEY_OP_RIGHT:
return memcmp(key_info->key, entry->key +
(entry->key_len - key_info->length), key_info->length) == 0;
case SHMCACHE_MATCH_KEY_OP_ANYWHERE:
loop = entry->key_len - key_info->length + 1;
for (start=0; start<loop; start++) {
if (memcmp(key_info->key, entry->key + start,
key_info->length) == 0)
{
return true;
}
}
return false;
default:
return false;
}
}

#define HT_KEY_MATCHED(key_info, entry) \
(key_info == NULL || shm_ht_match_key(key_info, entry))

int shm_ht_to_array_ex(struct shmcache_context *context,
struct shmcache_hentry_array *array,
struct shmcache_match_key_info *key_info,
const int offset, const int count)
{
int64_t entry_offset;
struct shm_hash_entry *src;
struct shmcache_hash_entry *dest;
char *value_data;
int row_count;
int i;
int bytes;
int current_time;

if (context->memory->hashtable.count == 0) {
if (offset < 0) {
logError("file: "__FILE__", line: %d, "
"invalid offset: %d", __LINE__, offset);
array->count = 0;
array->entries = NULL;
return EINVAL;
}

row_count = context->memory->hashtable.count - offset;
if (count > 0 && count < row_count) {
row_count = count;
}

if (row_count <= 0) {
array->count = 0;
array->entries = NULL;
return 0;
}

bytes = sizeof(struct shmcache_hash_entry) * context->memory->hashtable.count;
bytes = sizeof(struct shmcache_hash_entry) * row_count;
array->entries = (struct shmcache_hash_entry *)malloc(bytes);
if (array->entries == NULL) {
logError("file: "__FILE__", line: %d, "
Expand All @@ -296,23 +349,28 @@ int shm_ht_to_array(struct shmcache_context *context,
return ENOMEM;
}

current_time = time(NULL);
i = 0;
entry_offset = shm_list_first(context);
while (entry_offset > 0 && i < offset) {
src = shm_get_hentry_ptr(context, entry_offset);
if (HT_ENTRY_IS_VALID(src, current_time) &&
HT_KEY_MATCHED(key_info, src))
{
++i;
}
entry_offset = shm_list_next(context, entry_offset);
}

memset(array->entries, 0, bytes);
array->count = 0;
dest = array->entries;
current_time = time(NULL);

entry_offset = shm_list_first(context);
while (entry_offset > 0) {
if (array->count == context->memory->hashtable.count) {
logError("file: "__FILE__", line: %d, "
"exceeds hash table count: %d",
__LINE__, context->memory->hashtable.count);
shm_ht_free_array(array);
return EFAULT;
}

src = shm_get_hentry_ptr(context, entry_offset);
if (!HT_ENTRY_IS_VALID(src, current_time)) {
if (!(HT_ENTRY_IS_VALID(src, current_time) &&
HT_KEY_MATCHED(key_info, src)))
{
entry_offset = shm_list_next(context, entry_offset);
continue;
}
Expand Down Expand Up @@ -340,6 +398,9 @@ int shm_ht_to_array(struct shmcache_context *context,
entry_offset = shm_list_next(context, entry_offset);
dest++;
array->count++;
if (array->count == row_count) {
break;
}
}

return 0;
Expand Down
34 changes: 32 additions & 2 deletions src/shm_hashtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@
#define HT_ENTRY_IS_VALID(entry, current_time) \
(entry->expires == 0 || entry->expires >= current_time)

#define SHMCACHE_MATCH_KEY_OP_EXACT 0
#define SHMCACHE_MATCH_KEY_OP_LEFT 1
#define SHMCACHE_MATCH_KEY_OP_RIGHT 2
#define SHMCACHE_MATCH_KEY_OP_ANYWHERE \
(SHMCACHE_MATCH_KEY_OP_LEFT | SHMCACHE_MATCH_KEY_OP_RIGHT)

struct shmcache_match_key_info {
char *key;
int length;
int op_type;
};

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -97,10 +109,28 @@ hashtable entry to array
parameters:
context: the context pointer
array: the array, should call shm_ht_free_array after use
key_info: the match key info, NULL for match all
offset: start offset, based 0
count: row count limit
return error no, 0 for success, != 0 for fail
*/
int shm_ht_to_array(struct shmcache_context *context,
struct shmcache_hentry_array *array);
int shm_ht_to_array_ex(struct shmcache_context *context,
struct shmcache_hentry_array *array,
struct shmcache_match_key_info *key_info,
const int offset, const int count);

/**
hashtable entry to array
parameters:
context: the context pointer
array: the array, should call shm_ht_free_array after use
return error no, 0 for success, != 0 for fail
*/
static inline int shm_ht_to_array(struct shmcache_context *context,
struct shmcache_hentry_array *array)
{
return shm_ht_to_array_ex(context, array, NULL, 0, 0);
}

/**
free the array return by shm_ht_to_array
Expand Down
39 changes: 1 addition & 38 deletions src/tests/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,6 @@
#include "shmcache/shm_hashtable.h"
#include "shmcache/shmcache.h"

static int test_dump(struct shmcache_context *context, const int dump_count)
{
int result;
int i;
struct shmcache_hentry_array array;
struct shmcache_hash_entry *entry;
struct shmcache_hash_entry *end;

if ((result=shm_ht_to_array(context, &array)) != 0) {
return result;
}

printf("entry count: %d\n", array.count);
end = array.entries + array.count;
i = 0;
for (entry=array.entries; entry<end; entry++) {
printf("%d. key: %.*s, value(%d): %.*s\n",
++i, entry->key.length, entry->key.data,
entry->value.length, entry->value.length,
entry->value.data);
if (dump_count > 0 && i == dump_count) {
break;
}
}

shm_ht_free_array(&array);
return 0;
}

int main(int argc, char *argv[])
{
#define MAX_VALUE_SIZE (8 * 1024)
Expand All @@ -54,7 +25,7 @@ int main(int argc, char *argv[])
struct shmcache_hash_entry *entries;
struct shmcache_hash_entry tmp;
int bytes;
int ttl = 60;
int ttl = 300;
int i, k;
int i1, i2;

Expand All @@ -70,14 +41,6 @@ int main(int argc, char *argv[])
return result;
}

if (argc > 1 && strcmp(argv[1], "dump") == 0) {
int dump_count = 0;
if (argc > 2) {
dump_count = atoi(argv[2]);
}
return test_dump(&context, dump_count);
}

bytes = sizeof(struct shmcache_hash_entry) * KEY_COUNT;
entries = (struct shmcache_hash_entry *)malloc(bytes);
if (entries == NULL) {
Expand Down
2 changes: 1 addition & 1 deletion src/tools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LIB_PATH = -lfastcommon -lshmcache -lpthread -ldl
TARGET_PATH = $(DESTDIR)/usr/local/bin/

TARGET_PRGS = shmcache_set shmcache_get shmcache_delete shmcache_remove_all \
shmcache_stats
shmcache_stats shmcache_dump

ALL_PRGS = $(TARGET_PRGS)

Expand Down
Loading

0 comments on commit cccdeb1

Please sign in to comment.