1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-09 15:10:29 +02:00

Refactor to separate gcbench from gc

This commit is contained in:
Andy Wingo 2022-03-11 10:17:05 +01:00
parent 77ac530360
commit f57a1b8a55
10 changed files with 132 additions and 101 deletions

View file

@ -1,4 +1,4 @@
TESTS=GCBench # MT_GCBench MT_GCBench2 TESTS=gcbench # MT_GCBench MT_GCBench2
COLLECTORS=bdw semi mark-sweep parallel-mark-sweep COLLECTORS=bdw semi mark-sweep parallel-mark-sweep
CC=gcc CC=gcc

11
bdw.h
View file

@ -16,19 +16,16 @@
struct context {}; struct context {};
enum alloc_kind { NODE, DOUBLE_ARRAY };
typedef void (*field_visitor)(struct context *, void **ref);
#define GC_HEADER /**/ #define GC_HEADER /**/
static inline void* allocate(struct context *cx, enum alloc_kind kind, static inline void* allocate(struct context *cx, enum alloc_kind kind,
size_t size) { size_t size) {
// memset to 0 by the collector.
switch (kind) { switch (kind) {
case NODE: case ALLOC_KIND_NODE:
// cleared to 0 by the collector.
return GC_malloc(size); return GC_malloc(size);
case DOUBLE_ARRAY: case ALLOC_KIND_DOUBLE_ARRAY:
// warning: not cleared!
return GC_malloc_atomic(size); return GC_malloc_atomic(size);
} }
abort(); abort();

17
gc.h Normal file
View file

@ -0,0 +1,17 @@
#ifndef GC_H_
#define GC_H_
#if defined(GC_BDW)
#include "bdw.h"
#elif defined(GC_SEMI)
#include "semi.h"
#elif defined(GC_MARK_SWEEP)
#include "mark-sweep.h"
#elif defined(GC_PARALLEL_MARK_SWEEP)
#define GC_PARALLEL_MARK 1
#include "mark-sweep.h"
#else
#error unknown gc
#endif
#endif // GC_H_

30
gcbench-types.h Normal file
View file

@ -0,0 +1,30 @@
#ifndef GCBENCH_TYPES_H
#define GCBENCH_TYPES_H
#include "inline.h"
#define FOR_EACH_HEAP_OBJECT_KIND(M) \
M(node, Node, NODE) \
M(double_array, DoubleArray, DOUBLE_ARRAY)
#define DECLARE_NODE_TYPE(name, Name, NAME) \
struct Name; \
typedef struct Name Name;
FOR_EACH_HEAP_OBJECT_KIND(DECLARE_NODE_TYPE)
#undef DECLARE_NODE_TYPE
#define DEFINE_ENUM(name, Name, NAME) ALLOC_KIND_##NAME,
enum alloc_kind {
FOR_EACH_HEAP_OBJECT_KIND(DEFINE_ENUM)
};
#undef DEFINE_ENUM
#define DEFINE_METHODS(name, Name, NAME) \
static inline size_t name##_size(Name *obj) ALWAYS_INLINE; \
static inline void visit_##name##_fields(Name *obj,\
void (*visit)(void **loc, void *visit_data), \
void *visit_data) ALWAYS_INLINE;
FOR_EACH_HEAP_OBJECT_KIND(DEFINE_METHODS)
#undef DEFINE_METHODS
#endif // GCBENCH_TYPES_H

View file

@ -43,19 +43,8 @@
#include <sys/time.h> #include <sys/time.h>
#include "assert.h" #include "assert.h"
#include "gcbench-types.h"
#if defined(GC_BDW) #include "gc.h"
#include "bdw.h"
#elif defined(GC_SEMI)
#include "semi.h"
#elif defined(GC_MARK_SWEEP)
#include "mark-sweep.h"
#elif defined(GC_PARALLEL_MARK_SWEEP)
#define GC_PARALLEL_MARK 1
#include "mark-sweep.h"
#else
#error unknown gc
#endif
static const int kStretchTreeDepth = 18; // about 16Mb static const int kStretchTreeDepth = 18; // about 16Mb
static const int kLongLivedTreeDepth = 16; // about 4Mb static const int kLongLivedTreeDepth = 16; // about 4Mb
@ -76,21 +65,23 @@ typedef struct DoubleArray {
double values[0]; double values[0];
} DoubleArray; } DoubleArray;
static inline size_t node_size(void *obj) { static inline size_t node_size(Node *obj) {
return sizeof(Node); return sizeof(Node);
} }
static inline size_t double_array_size(void *obj) { static inline size_t double_array_size(DoubleArray *array) {
DoubleArray *array = obj;
return sizeof(*array) + array->length * sizeof(double); return sizeof(*array) + array->length * sizeof(double);
} }
static inline void visit_node_fields(struct context *cx, void *obj, static inline void
field_visitor visit) { visit_node_fields(Node *node,
Node *node = obj; void (*visit)(void **loc, void *visit_data),
visit(cx, (void**)&node->left); void *visit_data) {
visit(cx, (void**)&node->right); visit((void**)&node->left, visit_data);
visit((void**)&node->right, visit_data);
} }
static inline void visit_double_array_fields(struct context *cx, void *obj, static inline void
field_visitor visit) { visit_double_array_fields(DoubleArray *obj,
void (*visit)(void **loc, void *visit_data),
void *visit_data) {
} }
typedef HANDLE_TO(Node) NodeHandle; typedef HANDLE_TO(Node) NodeHandle;
@ -98,13 +89,14 @@ typedef HANDLE_TO(DoubleArray) DoubleArrayHandle;
static Node* allocate_node(struct context *cx) { static Node* allocate_node(struct context *cx) {
// memset to 0 by the collector. // memset to 0 by the collector.
return allocate(cx, NODE, sizeof (Node)); return allocate(cx, ALLOC_KIND_NODE, sizeof (Node));
} }
static struct DoubleArray* allocate_double_array(struct context *cx, static struct DoubleArray* allocate_double_array(struct context *cx,
size_t size) { size_t size) {
// note, not memset to 0 by the collector. // note, we might allow the collector to leave this data uninitialized.
DoubleArray *ret = allocate(cx, DOUBLE_ARRAY, sizeof (double) * size); DoubleArray *ret = allocate(cx, ALLOC_KIND_DOUBLE_ARRAY,
sizeof(DoubleArray) + sizeof (double) * size);
ret->length = size; ret->length = size;
return ret; return ret;
} }

7
inline.h Normal file
View file

@ -0,0 +1,7 @@
#ifndef INLINE_H
#define INLINE_H
#define ALWAYS_INLINE __attribute__((always_inline))
#define NEVER_INLINE __attribute__((noinline))
#endif // INLINE_H

View file

@ -6,6 +6,7 @@
#include "assert.h" #include "assert.h"
#include "debug.h" #include "debug.h"
#include "inline.h"
#include "precise-roots.h" #include "precise-roots.h"
#ifdef GC_PARALLEL_MARK #ifdef GC_PARALLEL_MARK
#include "parallel-marker.h" #include "parallel-marker.h"
@ -162,20 +163,11 @@ get_small_object_freelist(struct context *cx, enum small_object_size kind) {
#define GC_HEADER uintptr_t _gc_header #define GC_HEADER uintptr_t _gc_header
enum alloc_kind { NODE, DOUBLE_ARRAY };
typedef void (*field_visitor)(struct context *, void **ref);
static inline size_t node_size(void *obj) __attribute__((always_inline));
static inline size_t double_array_size(void *obj) __attribute__((always_inline));
static inline void visit_node_fields(struct context *cx, void *obj, field_visitor visit) __attribute__((always_inline));
static inline void visit_double_array_fields(struct context *cx, void *obj, field_visitor visit) __attribute__((always_inline));
static inline void clear_memory(uintptr_t addr, size_t size) { static inline void clear_memory(uintptr_t addr, size_t size) {
memset((char*)addr, 0, size); memset((char*)addr, 0, size);
} }
static void collect(struct context *cx) __attribute__((noinline)); static void collect(struct context *cx) NEVER_INLINE;
static inline uint8_t* mark_byte(struct context *cx, struct gcobj *obj) { static inline uint8_t* mark_byte(struct context *cx, struct gcobj *obj) {
uintptr_t granule = (((uintptr_t) obj) - cx->heap_base) / GRANULE_SIZE; uintptr_t granule = (((uintptr_t) obj) - cx->heap_base) / GRANULE_SIZE;
@ -193,12 +185,12 @@ static inline int mark_object(struct context *cx, struct gcobj *obj) {
static void process(struct context *cx, struct gcobj *obj) { static void process(struct context *cx, struct gcobj *obj) {
switch (tag_live_alloc_kind(obj->tag)) { switch (tag_live_alloc_kind(obj->tag)) {
case NODE: #define SCAN_OBJECT(name, Name, NAME) \
visit_node_fields(cx, obj, marker_visit); case ALLOC_KIND_##NAME: \
break; visit_##name##_fields((Name*)obj, marker_visit, cx); \
case DOUBLE_ARRAY: break;
visit_double_array_fields(cx, obj, marker_visit); FOR_EACH_HEAP_OBJECT_KIND(SCAN_OBJECT)
break; #undef SCAN_OBJECT
default: default:
abort (); abort ();
} }
@ -215,7 +207,7 @@ static void collect(struct context *cx) {
DEBUG("start collect #%ld:\n", cx->count); DEBUG("start collect #%ld:\n", cx->count);
marker_prepare(cx); marker_prepare(cx);
for (struct handle *h = cx->roots; h; h = h->next) for (struct handle *h = cx->roots; h; h = h->next)
marker_visit_root(cx, &h->v); marker_visit_root(&h->v, cx);
marker_trace(cx, process); marker_trace(cx, process);
marker_release(cx); marker_release(cx);
DEBUG("done marking\n"); DEBUG("done marking\n");
@ -301,12 +293,12 @@ static size_t live_object_granules(struct gcobj *obj) {
return 1; return 1;
size_t bytes; size_t bytes;
switch (tag_live_alloc_kind (obj->tag)) { switch (tag_live_alloc_kind (obj->tag)) {
case NODE: #define COMPUTE_SIZE(name, Name, NAME) \
bytes = node_size(obj); case ALLOC_KIND_##NAME: \
break; bytes = name##_size((Name*)obj); \
case DOUBLE_ARRAY: break;
bytes = double_array_size(obj); FOR_EACH_HEAP_OBJECT_KIND(COMPUTE_SIZE)
break; #undef COMPUTE_SIZE
default: default:
abort (); abort ();
} }

View file

@ -7,6 +7,7 @@
#include "assert.h" #include "assert.h"
#include "debug.h" #include "debug.h"
#include "inline.h"
// The Chase-Lev work-stealing deque, as initially described in "Dynamic // The Chase-Lev work-stealing deque, as initially described in "Dynamic
// Circular Work-Stealing Deque" (Chase and Lev, SPAA'05) // Circular Work-Stealing Deque" (Chase and Lev, SPAA'05)
@ -236,22 +237,23 @@ static void marker_release(struct context *cx) {
} }
struct gcobj; struct gcobj;
static inline void marker_visit(struct context *cx, void **loc) __attribute__((always_inline)); static inline void marker_visit(void **loc, void *mark_data) ALWAYS_INLINE;
static inline void marker_trace(struct context *cx, static inline void marker_trace(struct context *cx,
void (*)(struct context *, struct gcobj *)) void (*)(struct context *, struct gcobj *))
__attribute__((always_inline)); ALWAYS_INLINE;
static inline int mark_object(struct context *cx, static inline int mark_object(struct context *cx,
struct gcobj *obj) __attribute__((always_inline)); struct gcobj *obj) ALWAYS_INLINE;
static inline void static inline void
marker_visit(struct context *cx, void **loc) { marker_visit(void **loc, void *mark_data) {
struct context *cx = mark_data;
struct gcobj *obj = *loc; struct gcobj *obj = *loc;
if (obj && mark_object(cx, obj)) if (obj && mark_object(cx, obj))
mark_deque_push(&context_marker(cx)->deque, (uintptr_t)obj); mark_deque_push(&context_marker(cx)->deque, (uintptr_t)obj);
} }
static inline void static inline void
marker_visit_root(struct context *cx, void **loc) { marker_visit_root(void **loc, struct context *cx) {
marker_visit(cx, loc); marker_visit(loc, cx);
} }
static inline void static inline void
marker_trace(struct context *cx, marker_trace(struct context *cx,

55
semi.h
View file

@ -23,22 +23,13 @@ static uintptr_t align_up(uintptr_t addr, size_t align) {
#define GC_HEADER uintptr_t _gc_header #define GC_HEADER uintptr_t _gc_header
enum alloc_kind { NODE, DOUBLE_ARRAY };
typedef void (*field_visitor)(struct context *, void **ref);
static inline size_t node_size(void *obj) __attribute__((always_inline));
static inline size_t double_array_size(void *obj) __attribute__((always_inline));
static inline void visit_node_fields(struct context *cx, void *obj, field_visitor visit) __attribute__((always_inline));
static inline void visit_double_array_fields(struct context *cx, void *obj, field_visitor visit) __attribute__((always_inline));
static inline void clear_memory(uintptr_t addr, size_t size) { static inline void clear_memory(uintptr_t addr, size_t size) {
memset((char*)addr, 0, size); memset((char*)addr, 0, size);
} }
static void collect(struct context *cx, size_t bytes) __attribute__((noinline)); static void collect(struct context *cx, size_t bytes) NEVER_INLINE;
static void process(struct context *cx, void **loc); static void visit(void **loc, void *visit_data);
static void flip(struct context *cx) { static void flip(struct context *cx) {
uintptr_t split = cx->base + (cx->size >> 1); uintptr_t split = cx->base + (cx->size >> 1);
@ -55,12 +46,12 @@ static void flip(struct context *cx) {
static void* copy(struct context *cx, uintptr_t kind, void *obj) { static void* copy(struct context *cx, uintptr_t kind, void *obj) {
size_t size; size_t size;
switch (kind) { switch (kind) {
case NODE: #define COMPUTE_SIZE(name, Name, NAME) \
size = node_size(obj); case ALLOC_KIND_##NAME: \
break; size = name##_size(obj); \
case DOUBLE_ARRAY: break;
size = double_array_size(obj); FOR_EACH_HEAP_OBJECT_KIND(COMPUTE_SIZE)
break; #undef COMPUTE_SIZE
default: default:
abort (); abort ();
} }
@ -75,14 +66,12 @@ static uintptr_t scan(struct context *cx, uintptr_t grey) {
void *obj = (void*)grey; void *obj = (void*)grey;
uintptr_t kind = *(uintptr_t*) obj; uintptr_t kind = *(uintptr_t*) obj;
switch (kind) { switch (kind) {
case NODE: #define SCAN_OBJECT(name, Name, NAME) \
visit_node_fields(cx, obj, process); case ALLOC_KIND_##NAME: \
return grey + align_up (node_size(obj), ALIGNMENT); visit_##name##_fields((Name*)obj, visit, cx); \
break; return grey + align_up(name##_size((Name*)obj), ALIGNMENT);
case DOUBLE_ARRAY: FOR_EACH_HEAP_OBJECT_KIND(SCAN_OBJECT)
visit_double_array_fields(cx, obj, process); #undef SCAN_OBJECT
return grey + align_up (double_array_size(obj), ALIGNMENT);
break;
default: default:
abort (); abort ();
} }
@ -91,15 +80,18 @@ static uintptr_t scan(struct context *cx, uintptr_t grey) {
static void* forward(struct context *cx, void *obj) { static void* forward(struct context *cx, void *obj) {
uintptr_t header_word = *(uintptr_t*)obj; uintptr_t header_word = *(uintptr_t*)obj;
switch (header_word) { switch (header_word) {
case NODE: #define CASE_ALLOC_KIND(name, Name, NAME) \
case DOUBLE_ARRAY: case ALLOC_KIND_##NAME:
FOR_EACH_HEAP_OBJECT_KIND(CASE_ALLOC_KIND)
#undef CASE_ALLOC_KIND
return copy(cx, header_word, obj); return copy(cx, header_word, obj);
default: default:
return (void*)header_word; return (void*)header_word;
} }
} }
static void process(struct context *cx, void **loc) { static void visit(void **loc, void *visit_data) {
struct context *cx = visit_data;
void *obj = *loc; void *obj = *loc;
if (obj != NULL) if (obj != NULL)
*loc = forward(cx, obj); *loc = forward(cx, obj);
@ -109,7 +101,7 @@ static void collect(struct context *cx, size_t bytes) {
flip(cx); flip(cx);
uintptr_t grey = cx->hp; uintptr_t grey = cx->hp;
for (struct handle *h = cx->roots; h; h = h->next) for (struct handle *h = cx->roots; h; h = h->next)
process(cx, &h->v); visit(&h->v, cx);
// fprintf(stderr, "pushed %zd bytes in roots\n", cx->hp - grey); // fprintf(stderr, "pushed %zd bytes in roots\n", cx->hp - grey);
while(grey < cx->hp) while(grey < cx->hp)
grey = scan(cx, grey); grey = scan(cx, grey);
@ -134,8 +126,9 @@ static inline void* allocate(struct context *cx, enum alloc_kind kind,
void *ret = (void *)addr; void *ret = (void *)addr;
uintptr_t *header_word = ret; uintptr_t *header_word = ret;
*header_word = kind; *header_word = kind;
if (kind == NODE) // FIXME: Allow allocator to avoid initializing pointerless memory?
clear_memory(addr + sizeof(uintptr_t), size - sizeof(uintptr_t)); // if (kind == NODE)
clear_memory(addr + sizeof(uintptr_t), size - sizeof(uintptr_t));
return ret; return ret;
} }
} }

View file

@ -49,7 +49,7 @@ mark_queue_put(struct mark_queue *q, size_t idx, uintptr_t x) {
q->buf[idx & (q->size - 1)] = x; q->buf[idx & (q->size - 1)] = x;
} }
static int mark_queue_grow(struct mark_queue *q) __attribute__((noinline)); static int mark_queue_grow(struct mark_queue *q) NEVER_INLINE;
static int static int
mark_queue_grow(struct mark_queue *q) { mark_queue_grow(struct mark_queue *q) {
@ -125,22 +125,23 @@ static void marker_release(struct context *cx) {
} }
struct gcobj; struct gcobj;
static inline void marker_visit(struct context *cx, void **loc) __attribute__((always_inline)); static inline void marker_visit(void **loc, void *mark_data) ALWAYS_INLINE;
static inline void marker_trace(struct context *cx, static inline void marker_trace(struct context *cx,
void (*)(struct context *, struct gcobj *)) void (*)(struct context *, struct gcobj *))
__attribute__((always_inline)); ALWAYS_INLINE;
static inline int mark_object(struct context *cx, static inline int mark_object(struct context *cx,
struct gcobj *obj) __attribute__((always_inline)); struct gcobj *obj) ALWAYS_INLINE;
static inline void static inline void
marker_visit(struct context *cx, void **loc) { marker_visit(void **loc, void *mark_data) {
struct context *cx = mark_data;
struct gcobj *obj = *loc; struct gcobj *obj = *loc;
if (obj && mark_object(cx, obj)) if (obj && mark_object(cx, obj))
mark_queue_push(&context_marker(cx)->queue, obj); mark_queue_push(&context_marker(cx)->queue, obj);
} }
static inline void static inline void
marker_visit_root(struct context *cx, void **loc) { marker_visit_root(void **loc, struct context *cx) {
marker_visit(cx, loc); marker_visit(loc, cx);
} }
static inline void static inline void
marker_trace(struct context *cx, marker_trace(struct context *cx,