Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge gcmalloc into ASCYLIB #13

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ lbll_pugh_no_ro:
$(MAKE) "RO_FAIL=0" src/linkedlist-pugh

lbll_lazy:
$(MAKE) src/linkedlist-lazy
$(MAKE) "GC=1" src/linkedlist-lazy

lbll_lazy_no_ro:
$(MAKE) "RO_FAIL=0" src/linkedlist-lazy
Expand Down
7 changes: 6 additions & 1 deletion common/Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ ifeq ($(ARCH_NAME), x86_64)
LDFLAGS += -m64
SSPFD = -lsspfd_x86_64
LDFLAGS += -L$(LIBSSMEM)/lib -lssmem_x86_64
LDFLAGS += -L$(LIBGCMALLOC)/lib -lgcmalloc_x86_64
endif

ifeq ($(ARCH_NAME), sun4v)
Expand Down Expand Up @@ -272,7 +273,11 @@ else
ifeq ($(GC),0)
CFLAGS += -DGC=0
else
CFLAGS += -DGC=1
ifeq ($(GC),1)
CFLAGS += -DGC=1
else
CFLAGS += -DGC=2
endif
endif
endif

Expand Down
41 changes: 41 additions & 0 deletions external/include/gcmalloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef _GCMALLOC_H_
#define _GCMALLOC_H_
#include <stdlib.h>

/* Allocate an object of specified size. */
void* gcmem_malloc(size_t size);

/*
* Free an object. It is guaranteed that the object is buffered until
* it is safe to actually reclaim memory.
*/
void gcmem_free(void* ptr);

/*
* Informs gcmalloc that the thread enters a critical section and no
* reclamation can be performed, starting from now, on new freed objects.
*/
void gcmem_unsafe_to_reclaim();

/*
* Informs gcmalloc that the thread leaves a critical section and
* will not access a shared object. Should not be called without a previous,
* corresponding gcmem_unsafe_to_reclaim().
*/
void gcmem_safe_to_reclaim();

/* Usage example:

thread_function() {
...
gmcmem_unsafe_to_reclaim();
...
void *object = extract_object_from_shared_data_strucutre();
gcmem_free(object);
...
gmcmem_safe_to_reclaim();
...
}
*/

#endif
Binary file added external/lib/libgcmalloc_x86_64.a
Binary file not shown.
24 changes: 24 additions & 0 deletions include/memalloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef _MEM_ALLOC_H
#define _MEM_ALLOC_H

#include <stdlib.h>
#include "ssmem.h"
#include "ssalloc.h"
#define MEM_MAX_ALLOCATORS 4

void memalloc_init(int id);
void memalloc_term();
void* memalloc_alloc(size_t size);
void memalloc_free(void *ptr);

/*
* Functions to be used when several allocation sizes are required.
*/
void memalloc_init_idx(unsigned int index, int id);
void memalloc_term_idx(unsigned int index);
void* memalloc_alloc_idx(unsigned int index, size_t size);
void memalloc_free_idx(int index, void *ptr);

void memalloc_unsafe_to_reclaim();
void memalloc_safe_to_reclaim();
#endif
8 changes: 6 additions & 2 deletions src/hashtable-copy/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ measurements.o:
ssalloc.o:
$(CC) $(CFLAGS) -c -o $(BUILDIR)/ssalloc.o $(PROF)/ssalloc.c

memalloc.o: ssalloc.o
$(CC) $(CFLAGS) -c -o $(BUILDIR)/memalloc.o $(BUILDIR)/ssalloc.o $(PROF)/memalloc.c


htlock.o:
$(CC) $(CFLAGS) -c -o $(BUILDIR)/htlock.o $(PROF)/htlock.c

Expand All @@ -34,8 +38,8 @@ copy_on_write.o: htlock.o clh.o
test.o: copy_on_write.h
$(CC) $(CFLAGS) -c -o $(BUILDIR)/test.o $(TEST_FILE)

main: measurements.o ssalloc.o copy_on_write.o test.o htlock.o clh.o
$(CC) $(CFLAGS) $(BUILDIR)/measurements.o $(BUILDIR)/ssalloc.o $(BUILDIR)/htlock.o $(BUILDIR)/clh.o $(BUILDIR)/copy_on_write.o $(BUILDIR)/test.o -o $(BINS) $(LDFLAGS)
main: measurements.o memalloc.o ssalloc.o copy_on_write.o test.o htlock.o clh.o
$(CC) $(CFLAGS) $(BUILDIR)/measurements.o $(BUILDIR)/memalloc.o $(BUILDIR)/ssalloc.o $(BUILDIR)/htlock.o $(BUILDIR)/clh.o $(BUILDIR)/copy_on_write.o $(BUILDIR)/test.o -o $(BINS) $(LDFLAGS)

clean:
rm -f $(BINS)
50 changes: 38 additions & 12 deletions src/hashtable-copy/copy_on_write.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,15 @@

#include "copy_on_write.h"

__thread ssmem_allocator_t* alloc;

__thread ssmem_allocator_t* allocs[MEM_MAX_ALLOCATORS];

size_t array_ll_fixed_size;

static inline volatile array_ll_t*
array_ll_new_init(size_t size)
{
array_ll_t* all;
all = ssalloc_aligned(CACHE_LINE_SIZE, sizeof(array_ll_t) + (array_ll_fixed_size * sizeof(kv_t)));
all = memalloc_alloc(sizeof(array_ll_t) + (array_ll_fixed_size * sizeof(kv_t)));
assert(all != NULL);

all->size = size;
Expand All @@ -45,7 +44,7 @@ static inline array_ll_t*
array_ll_new(size_t size)
{
array_ll_t* all;
all = ssmem_alloc(alloc, sizeof(array_ll_t) + (array_ll_fixed_size * sizeof(kv_t)));
all = memalloc_alloc(sizeof(array_ll_t) + (array_ll_fixed_size * sizeof(kv_t)));
assert(all != NULL);

all->size = size;
Expand Down Expand Up @@ -95,7 +94,7 @@ copy_on_write_new(size_t num_buckets)


sval_t
cpy_search(copy_on_write_t* set, skey_t key)
cpy_search_impl(copy_on_write_t* set, skey_t key)
{
size_t bucket = key & set->hash;

Expand All @@ -113,6 +112,14 @@ cpy_search(copy_on_write_t* set, skey_t key)
return 0;
}

sval_t
cpy_search(copy_on_write_t* set, skey_t key) {
memalloc_unsafe_to_reclaim();
sval_t r = cpy_search_impl(set, key);
memalloc_safe_to_reclaim();
return r;
}

sval_t
cpy_array_search(array_ll_t* all_cur, skey_t key)
{
Expand All @@ -129,7 +136,7 @@ cpy_array_search(array_ll_t* all_cur, skey_t key)
}

sval_t
cpy_delete(copy_on_write_t* set, skey_t key)
cpy_delete_impl(copy_on_write_t* set, skey_t key)
{
size_t bucket = key & set->hash;
array_ll_t* all_old;
Expand Down Expand Up @@ -166,20 +173,29 @@ cpy_delete(copy_on_write_t* set, skey_t key)
if (removed)
{
set->array[bucket] = all_new;
ssmem_free(alloc, (void*) all_old);
memalloc_free((void*) all_old);
}
else
{
ssmem_free(alloc, (void*) all_new);
memalloc_free((void*) all_new);
}

GL_UNLOCK(set->lock);
UNLOCK(set->lock + bucket);
return removed;
}

sval_t
cpy_delete(copy_on_write_t* set, skey_t key) {
sval_t r;
memalloc_unsafe_to_reclaim();
r = cpy_delete_impl(set, key);
memalloc_safe_to_reclaim();
return r;
}

int
cpy_insert(copy_on_write_t* set, skey_t key, sval_t val)
cpy_insert_impl(copy_on_write_t* set, skey_t key, sval_t val)
{
size_t bucket = key & set->hash;
array_ll_t* all_old;
Expand All @@ -203,7 +219,7 @@ cpy_insert(copy_on_write_t* set, skey_t key, sval_t val)
{
if (unlikely(all_old->kvs[i].key == key))
{
ssmem_free(alloc, (void*) all_new);
memalloc_free((void*) all_new);
GL_UNLOCK(set->lock);
UNLOCK(set->lock + bucket);
return 0;
Expand All @@ -216,23 +232,33 @@ cpy_insert(copy_on_write_t* set, skey_t key, sval_t val)
all_new->kvs[i].val = val;

set->array[bucket] = all_new;
ssmem_free(alloc, (void*) all_old);
memalloc_free((void*) all_old);

GL_UNLOCK(set->lock);
UNLOCK(set->lock + bucket);
return 1;
}

int
cpy_insert(copy_on_write_t* set, skey_t key, sval_t val) {
int r;
memalloc_unsafe_to_reclaim();
r = cpy_insert_impl(set, key, val);
memalloc_safe_to_reclaim();
return r;
}

size_t
copy_on_write_size(copy_on_write_t* set)
{
size_t s = 0;
int i;
memalloc_unsafe_to_reclaim();
for (i = 0; i < set->num_buckets; i++)
{
s += set->array[i]->size;
}

memalloc_safe_to_reclaim();
return s;
};

Expand Down
3 changes: 1 addition & 2 deletions src/hashtable-copy/copy_on_write.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
#include "common.h"
#include "utils.h"
#include "measurements.h"
#include "ssalloc.h"
#include "ssmem.h"
#include "memalloc.h"

#define DEFAULT_ALTERNATE 0
#define DEFAULT_EFFECTIVE 1
Expand Down
12 changes: 3 additions & 9 deletions src/hashtable-copy/test_simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,7 @@ test(void* thread)
init_clh_thread(&clh_local_p);
#endif

#if GC == 1
alloc = (ssmem_allocator_t*) malloc(sizeof(ssmem_allocator_t));
assert(alloc != NULL);
ssmem_alloc_init_fs_size(alloc, SSMEM_DEFAULT_MEM_SIZE, SSMEM_GC_FREE_SET_SIZE, ID);
#endif
memalloc_init(ID);

RR_INIT(phys_id);
barrier_cross(&barrier);
Expand Down Expand Up @@ -250,10 +246,7 @@ test(void* thread)
EXEC_IN_DEC_ID_ORDER_END(&barrier);

SSPFDTERM();
#if GC == 1
ssmem_term();
free(alloc);
#endif
memalloc_term();
THREAD_END();
pthread_exit(NULL);
}
Expand All @@ -263,6 +256,7 @@ main(int argc, char **argv)
{
set_cpu(0);
ssalloc_init();
memalloc_init(num_threads);
seeds = seed_rand();

struct option long_options[] = {
Expand Down
7 changes: 5 additions & 2 deletions src/linkedlist-harris/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ measurements.o:
ssalloc.o:
$(CC) $(CFLAGS) -c -o $(BUILDIR)/ssalloc.o $(PROF)/ssalloc.c

memalloc.o: ssalloc.o
$(CC) $(CFLAGS) -c -o $(BUILDIR)/memalloc.o $(BUILDIR)/ssalloc.o $(PROF)/memalloc.c

linkedlist.o:
$(CC) $(CFLAGS) -c -o $(BUILDIR)/linkedlist.o linkedlist.c

Expand All @@ -29,8 +32,8 @@ intset.o: linkedlist.h harris.h
test.o: linkedlist.h harris.h intset.h
$(CC) $(CFLAGS) -c -o $(BUILDIR)/test.o $(TEST_FILE)

main: measurements.o ssalloc.o linkedlist.o harris.o harris.o intset.o test.o $(TMILB)
$(CC) $(CFLAGS) $(BUILDIR)/measurements.o $(BUILDIR)/ssalloc.o $(BUILDIR)/linkedlist.o $(BUILDIR)/harris.o $(BUILDIR)/intset.o $(BUILDIR)/test.o -o $(BINS) $(LDFLAGS)
main: measurements.o memalloc.o ssalloc.o linkedlist.o harris.o harris.o intset.o test.o $(TMILB)
$(CC) $(CFLAGS) $(BUILDIR)/measurements.o $(BUILDIR)/memalloc.o $(BUILDIR)/ssalloc.o $(BUILDIR)/linkedlist.o $(BUILDIR)/harris.o $(BUILDIR)/intset.o $(BUILDIR)/test.o -o $(BINS) $(LDFLAGS)

clean:
-rm -f $(BINS)
11 changes: 5 additions & 6 deletions src/linkedlist-harris/harris.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ harris_search(intset_t *set, skey_t key, node_t **left_node)
{
node_t* free = cur;
cur = (node_t*) get_unmarked_ref((long) cur->next);
ssmem_free(alloc, (void*) free);
memalloc_free((void*) free);
}
while (cur != right_node);
#endif
Expand Down Expand Up @@ -185,7 +185,7 @@ harris_insert(intset_t *set, skey_t key, sval_t val)
#if GC == 1
if (unlikely(newnode != NULL))
{
ssmem_free(alloc, (void*) newnode);
memalloc_free((void*) newnode);
}
#endif
return 0;
Expand Down Expand Up @@ -242,10 +242,7 @@ harris_delete(intset_t *set, skey_t key)

if (likely(ATOMIC_CAS_MB(&left_node->next, right_node, right_node_next)))
{
#if GC == 1
ssmem_free(alloc, (void*) get_unmarked_ref((long) right_node));
#endif
;
memalloc_free((void*) get_unmarked_ref((long) right_node));
}
else
{
Expand All @@ -261,12 +258,14 @@ set_size(intset_t *set)
int size = 0;
node_t* node;

memalloc_unsafe_to_reclaim();
/* We have at least 2 elements */
node = (node_t*) get_unmarked_ref((long) set->head->next);
while ((node_t*) get_unmarked_ref((long) node->next) != NULL)
{
if (!is_marked_ref((long) node->next)) size++;
node = (node_t*) get_unmarked_ref((long) node->next);
}
memalloc_safe_to_reclaim();
return size;
}
Loading