Skip to content

Commit

Permalink
fix bug; simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
felix021 committed Apr 24, 2014
1 parent a44f6e8 commit 3c91f57
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 43 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
======
Expand Down
20 changes: 11 additions & 9 deletions hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
48 changes: 20 additions & 28 deletions shmht.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#define _LARGEFILE64_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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++)
{
Expand All @@ -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;
}

Expand Down

0 comments on commit 3c91f57

Please sign in to comment.