From 3c91f579778c62ccad8a3bd5e98a1ef1b5475fa5 Mon Sep 17 00:00:00 2001 From: felix021 Date: Thu, 24 Apr 2014 22:48:20 +0800 Subject: [PATCH] fix bug; simplify code --- README.md | 12 ++++++------ hashtable.c | 20 +++++++++++--------- shmht.c | 48 ++++++++++++++++++++---------------------------- 3 files changed, 37 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 479f4d9..0af6546 100644 --- a/README.md +++ b/README.md @@ -11,16 +11,16 @@ Performance capacity=200M, 64 bytes key/value tests, tested on (Xeon E5-2670 0 @ 2.60GHz, 128GB ram) * hashtable.c (raw hash table in c, tested on `malloc`ed memory) - set: 0.93 Million iops - get: 2.35 Million iops +> set: 0.93 Million iops +> get: 2.35 Million iops * HashTable.py (simple wrapper) - set: 250k iops - get: 145k iops +> set: 250k iops +> get: 145k iops * Cacher.py (cached wrapper, with serialized) - set: 180k iops (write_through), 83k iops (writ_back) - get: 135k iops (write_through), 73k iops (writ_back) +> set: 180k iops (write\_through), 83k iops (writ\_back) +> get: 135k iops (write\_through), 73k iops (writ\_back) Notice ====== diff --git a/hashtable.c b/hashtable.c index 0a4aeab..acb9162 100644 --- a/hashtable.c +++ b/hashtable.c @@ -50,15 +50,6 @@ static inline void fill_ht_str(ht_str *s, const char *str, const u_int32 size) { memcpy(s->str, str, size); } -static void dump_ht_str(ht_str *s) { - if (s) { - printf("%u: %*s\n", s->size, (int)s->size, s->str); - } - else { - printf("(nil)\n"); - } -} - static unsigned int ht_get_prime_by(size_t capacity) { unsigned i = 0; capacity *= 2; @@ -241,6 +232,17 @@ int ht_destroy(hashtable *ht) { } /* + +//commented out together with 'main' to eliminate compiler's complaint +static void dump_ht_str(ht_str *s) { + if (s) { + printf("%u: %*s\n", s->size, (int)s->size, s->str); + } + else { + printf("(nil)\n"); + } +} + int main() { size_t capacity = 500000; printf("%u\n", ht_get_prime_by(capacity)); diff --git a/shmht.c b/shmht.c index 187033b..04621f1 100644 --- a/shmht.c +++ b/shmht.c @@ -1,3 +1,5 @@ +#define _LARGEFILE64_SOURCE + #include #include #include @@ -57,7 +59,6 @@ static PyObject * shmht_open(PyObject *self, PyObject *args) { int fd = 0; size_t mem_size = 0; - void *base_addr = NULL; hashtable *ht = NULL; const char *name; @@ -80,24 +81,30 @@ static PyObject * shmht_open(PyObject *self, PyObject *args) if (force_init == 0) { //try to load from existing shmht mem_size = sizeof(hashtable); if (buf.st_size >= sizeof(hashtable)) { //may be valid - base_addr = mmap(NULL, sizeof(hashtable), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); - if (base_addr == MAP_FAILED) { + ht = mmap(NULL, sizeof(hashtable), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); + if (ht == MAP_FAILED) { PyErr_Format(shmht_error, "mmap failed, map_size=sizeof(hashtable)=%lu: [%d] %s", mem_size, errno, strerror(errno)); goto create_failed; } - if (ht_is_valid((hashtable *)base_addr)) { - ht = (hashtable *)base_addr; + if (ht_is_valid(ht)) { if (capacity != 0 && capacity != ht->orig_capacity) { PyErr_Format(shmht_error, "please specify the 3rd arg(force_init=1) to overwrite an existing shmht"); goto create_failed; } capacity = ht->orig_capacity; //loaded capacity } + munmap(ht, sizeof(hashtable)); + ht = NULL; } } + if (capacity == 0) { + PyErr_Format(shmht_error, "please specify 'capacity' when you try to create a shmht"); + goto create_failed; + } + mem_size = ht_memory_size(capacity); if (buf.st_size < mem_size) { @@ -112,29 +119,14 @@ static PyObject * shmht_open(PyObject *self, PyObject *args) } } - if (base_addr == NULL) { - if (capacity == 0) { - PyErr_Format(shmht_error, "please specify 'capacity' when you try to create a shmht"); - goto create_failed; - } - base_addr = mmap(NULL, mem_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); - if (base_addr == MAP_FAILED) { - PyErr_Format(shmht_error, "mmap failed, mem_size=%lu: [%d] %s", - mem_size, errno, strerror(errno)); - goto create_failed; - } - } - else { - void *new_base_addr = mremap(base_addr, sizeof(hashtable), mem_size, MREMAP_MAYMOVE); - if (new_base_addr == MAP_FAILED) { - PyErr_Format(shmht_error, "mremap failed, mem_size=%lu: [%d] %s", - mem_size, errno, strerror(errno)); - goto create_failed; - } - base_addr = new_base_addr; + ht = mmap(NULL, mem_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); + if (ht == MAP_FAILED) { + PyErr_Format(shmht_error, "mmap failed, mem_size=%lu: [%d] %s", + mem_size, errno, strerror(errno)); + goto create_failed; } - ht = ht_init(base_addr, capacity, force_init); + ht_init(ht, capacity, force_init); int count; for (count = 0; count < max_ht_map_entries; count++) { @@ -156,8 +148,8 @@ static PyObject * shmht_open(PyObject *self, PyObject *args) create_failed: if (fd >= 0) close(fd); - if (base_addr != NULL) - munmap(base_addr, mem_size); + if (ht != NULL) + munmap(ht, mem_size); return NULL; }