Commit 389e6b34 authored by John Esmet's avatar John Esmet

fixes #172 Moved memarena to util, made its public API more consistent with

the rest of the code
parent 0244c723
...@@ -52,7 +52,6 @@ set(FT_SOURCES ...@@ -52,7 +52,6 @@ set(FT_SOURCES
logfilemgr logfilemgr
logger logger
log_upgrade log_upgrade
memarena
minicron minicron
omt omt
pqueue pqueue
......
...@@ -2607,7 +2607,7 @@ serialize_rollback_log_size(ROLLBACK_LOG_NODE log) { ...@@ -2607,7 +2607,7 @@ serialize_rollback_log_size(ROLLBACK_LOG_NODE log) {
+8 //blocknum +8 //blocknum
+8 //previous (blocknum) +8 //previous (blocknum)
+8 //resident_bytecount +8 //resident_bytecount
+8 //memarena_size_needed_to_load +8 //memarena size
+log->rollentry_resident_bytecount; +log->rollentry_resident_bytecount;
return size; return size;
} }
...@@ -2628,7 +2628,7 @@ serialize_rollback_log_node_to_buf(ROLLBACK_LOG_NODE log, char *buf, size_t calc ...@@ -2628,7 +2628,7 @@ serialize_rollback_log_node_to_buf(ROLLBACK_LOG_NODE log, char *buf, size_t calc
wbuf_nocrc_BLOCKNUM(&wb, log->previous); wbuf_nocrc_BLOCKNUM(&wb, log->previous);
wbuf_nocrc_ulonglong(&wb, log->rollentry_resident_bytecount); wbuf_nocrc_ulonglong(&wb, log->rollentry_resident_bytecount);
//Write down memarena size needed to restore //Write down memarena size needed to restore
wbuf_nocrc_ulonglong(&wb, memarena_total_size_in_use(log->rollentry_arena)); wbuf_nocrc_ulonglong(&wb, toku_memarena_total_size_in_use(log->rollentry_arena));
{ {
//Store rollback logs //Store rollback logs
...@@ -2792,8 +2792,8 @@ deserialize_rollback_log_from_rbuf (BLOCKNUM blocknum, ROLLBACK_LOG_NODE *log_p, ...@@ -2792,8 +2792,8 @@ deserialize_rollback_log_from_rbuf (BLOCKNUM blocknum, ROLLBACK_LOG_NODE *log_p,
result->rollentry_resident_bytecount = rbuf_ulonglong(rb); result->rollentry_resident_bytecount = rbuf_ulonglong(rb);
size_t arena_initial_size = rbuf_ulonglong(rb); size_t arena_initial_size = rbuf_ulonglong(rb);
result->rollentry_arena = memarena_create_presized(arena_initial_size); result->rollentry_arena = toku_memarena_create_presized(arena_initial_size);
if (0) { died1: memarena_close(&result->rollentry_arena); goto died0; } if (0) { died1: toku_memarena_destroy(&result->rollentry_arena); goto died0; }
//Load rollback entries //Load rollback entries
lazy_assert(rb->size > 4); lazy_assert(rb->size > 4);
......
...@@ -99,15 +99,17 @@ PATENT RIGHTS GRANT: ...@@ -99,15 +99,17 @@ PATENT RIGHTS GRANT:
#include "ft-internal.h" #include "ft-internal.h"
#include "log.h" #include "log.h"
#include "toku_list.h" #include "toku_list.h"
#include "memarena.h"
#include "logfilemgr.h" #include "logfilemgr.h"
#include "txn.h" #include "txn.h"
#include "txn_manager.h" #include "txn_manager.h"
#include <portability/toku_pthread.h>
#include <util/omt.h>
#include "rollback_log_node_cache.h" #include "rollback_log_node_cache.h"
#include "txn_child_manager.h" #include "txn_child_manager.h"
#include <portability/toku_pthread.h>
#include <util/memarena.h>
#include <util/omt.h>
using namespace toku; using namespace toku;
// Locking for the logger // Locking for the logger
// For most purposes we use the big ydb lock. // For most purposes we use the big ydb lock.
......
...@@ -806,7 +806,7 @@ generate_rollbacks (void) { ...@@ -806,7 +806,7 @@ generate_rollbacks (void) {
DO_ROLLBACKS(lt, { DO_ROLLBACKS(lt, {
fprintf(cf, " case RT_%s:\n", lt->name); fprintf(cf, " case RT_%s:\n", lt->name);
fprintf(cf, " mem_needed = sizeof(item->u.%s) + __builtin_offsetof(struct roll_entry, u.%s);\n", lt->name, lt->name); fprintf(cf, " mem_needed = sizeof(item->u.%s) + __builtin_offsetof(struct roll_entry, u.%s);\n", lt->name, lt->name);
fprintf(cf, " CAST_FROM_VOIDP(item, malloc_in_memarena(ma, mem_needed));\n"); fprintf(cf, " CAST_FROM_VOIDP(item, toku_memarena_malloc(ma, mem_needed));\n");
fprintf(cf, " item->cmd = cmd;\n"); fprintf(cf, " item->cmd = cmd;\n");
DO_FIELDS(field_type, lt, fprintf(cf, " rbuf_ma_%s(&rc, ma, &item->u.%s.%s);\n", field_type->type, lt->name, field_type->name)); DO_FIELDS(field_type, lt, fprintf(cf, " rbuf_ma_%s(&rc, ma, &item->u.%s.%s);\n", field_type->type, lt->name, field_type->name));
fprintf(cf, " *itemp = item;\n"); fprintf(cf, " *itemp = item;\n");
...@@ -858,7 +858,7 @@ int main (int argc, const char *const argv[]) { ...@@ -858,7 +858,7 @@ int main (int argc, const char *const argv[]) {
fprintf2(cf, pf, "#include <ft/fttypes.h>\n"); fprintf2(cf, pf, "#include <ft/fttypes.h>\n");
fprintf2(cf, pf, "#include <ft/log-internal.h>\n"); fprintf2(cf, pf, "#include <ft/log-internal.h>\n");
fprintf(hf, "#include <ft/ft-internal.h>\n"); fprintf(hf, "#include <ft/ft-internal.h>\n");
fprintf(hf, "#include <ft/memarena.h>\n"); fprintf(hf, "#include <util/memarena.h>\n");
generate_enum(); generate_enum();
generate_log_struct(); generate_log_struct();
generate_dispatch(); generate_dispatch();
......
...@@ -93,12 +93,12 @@ PATENT RIGHTS GRANT: ...@@ -93,12 +93,12 @@ PATENT RIGHTS GRANT:
#ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11/760379 and to the patents and/or patent applications resulting from it." #ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11/760379 and to the patents and/or patent applications resulting from it."
#include <toku_portability.h> #include <toku_portability.h>
#include "memarena.h"
#include "toku_assert.h" #include "toku_assert.h"
#include "fttypes.h" #include "fttypes.h"
#include "memory.h" #include "memory.h"
#include <toku_htonl.h> #include <toku_htonl.h>
#include <util/memarena.h>
struct rbuf { struct rbuf {
unsigned char *buf; unsigned char *buf;
...@@ -250,7 +250,7 @@ static inline void rbuf_FILENUMS(struct rbuf *r, FILENUMS *filenums) { ...@@ -250,7 +250,7 @@ static inline void rbuf_FILENUMS(struct rbuf *r, FILENUMS *filenums) {
// 2954 // 2954
static inline void rbuf_ma_FILENUMS (struct rbuf *r, MEMARENA ma __attribute__((__unused__)), FILENUMS *filenums) { static inline void rbuf_ma_FILENUMS (struct rbuf *r, MEMARENA ma __attribute__((__unused__)), FILENUMS *filenums) {
rbuf_ma_uint32_t(r, ma, &(filenums->num)); rbuf_ma_uint32_t(r, ma, &(filenums->num));
filenums->filenums = (FILENUM *) malloc_in_memarena(ma, filenums->num * sizeof(FILENUM) ); filenums->filenums = (FILENUM *) toku_memarena_malloc(ma, filenums->num * sizeof(FILENUM) );
assert(filenums->filenums != NULL); assert(filenums->filenums != NULL);
for (uint32_t i=0; i < filenums->num; i++) { for (uint32_t i=0; i < filenums->num; i++) {
rbuf_ma_FILENUM(r, ma, &(filenums->filenums[i])); rbuf_ma_FILENUM(r, ma, &(filenums->filenums[i]));
...@@ -271,7 +271,7 @@ static inline void rbuf_ma_BYTESTRING (struct rbuf *r, MEMARENA ma, BYTESTRING ...@@ -271,7 +271,7 @@ static inline void rbuf_ma_BYTESTRING (struct rbuf *r, MEMARENA ma, BYTESTRING
bs->len = rbuf_int(r); bs->len = rbuf_int(r);
uint32_t newndone = r->ndone + bs->len; uint32_t newndone = r->ndone + bs->len;
assert(newndone <= r->size); assert(newndone <= r->size);
bs->data = (char *) memarena_memdup(ma, &r->buf[r->ndone], (size_t)bs->len); bs->data = (char *) toku_memarena_memdup(ma, &r->buf[r->ndone], (size_t)bs->len);
assert(bs->data); assert(bs->data);
r->ndone = newndone; r->ndone = newndone;
} }
......
...@@ -258,9 +258,9 @@ int toku_rollback_commit(TOKUTXN txn, LSN lsn) { ...@@ -258,9 +258,9 @@ int toku_rollback_commit(TOKUTXN txn, LSN lsn) {
} }
child_log->newest_logentry = child_log->oldest_logentry = 0; child_log->newest_logentry = child_log->oldest_logentry = 0;
// Put all the memarena data into the parent. // Put all the memarena data into the parent.
if (memarena_total_size_in_use(child_log->rollentry_arena) > 0) { if (toku_memarena_total_size_in_use(child_log->rollentry_arena) > 0) {
// If there are no bytes to move, then just leave things alone, and let the memory be reclaimed on txn is closed. // If there are no bytes to move, then just leave things alone, and let the memory be reclaimed on txn is closed.
memarena_move_buffers(parent_log->rollentry_arena, child_log->rollentry_arena); toku_memarena_move_buffers(parent_log->rollentry_arena, child_log->rollentry_arena);
} }
// each txn tries to give back at most one rollback log node // each txn tries to give back at most one rollback log node
// to the cache. All other rollback log nodes for this child // to the cache. All other rollback log nodes for this child
......
...@@ -89,15 +89,15 @@ PATENT RIGHTS GRANT: ...@@ -89,15 +89,15 @@ PATENT RIGHTS GRANT:
#ident "Copyright (c) 2007-2013 Tokutek Inc. All rights reserved." #ident "Copyright (c) 2007-2013 Tokutek Inc. All rights reserved."
#ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11/760379 and to the patents and/or patent applications resulting from it." #ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11/760379 and to the patents and/or patent applications resulting from it."
#include "rollback-ct-callbacks.h"
#include <toku_portability.h> #include <toku_portability.h>
#include <memory.h> #include <memory.h>
#include "ft-internal.h" #include "ft-internal.h"
#include "fttypes.h" #include "fttypes.h"
#include "memarena.h"
#include "rollback.h" #include "rollback.h"
#include "rollback-ct-callbacks.h"
#include <util/memarena.h>
// Address used as a sentinel. Otherwise unused. // Address used as a sentinel. Otherwise unused.
static struct serialized_rollback_log_node cloned_rollback; static struct serialized_rollback_log_node cloned_rollback;
......
...@@ -121,7 +121,7 @@ toku_find_xid_by_xid (const TXNID &xid, const TXNID &xidfind) { ...@@ -121,7 +121,7 @@ toku_find_xid_by_xid (const TXNID &xid, const TXNID &xidfind) {
} }
void *toku_malloc_in_rollback(ROLLBACK_LOG_NODE log, size_t size) { void *toku_malloc_in_rollback(ROLLBACK_LOG_NODE log, size_t size) {
return malloc_in_memarena(log->rollentry_arena, size); return toku_memarena_malloc(log->rollentry_arena, size);
} }
void *toku_memdup_in_rollback(ROLLBACK_LOG_NODE log, const void *v, size_t len) { void *toku_memdup_in_rollback(ROLLBACK_LOG_NODE log, const void *v, size_t len) {
...@@ -146,7 +146,7 @@ PAIR_ATTR ...@@ -146,7 +146,7 @@ PAIR_ATTR
rollback_memory_size(ROLLBACK_LOG_NODE log) { rollback_memory_size(ROLLBACK_LOG_NODE log) {
size_t size = sizeof(*log); size_t size = sizeof(*log);
if (log->rollentry_arena) { if (log->rollentry_arena) {
size += memarena_total_footprint(log->rollentry_arena); size += toku_memarena_total_footprint(log->rollentry_arena);
} }
return make_rollback_pair_attr(size); return make_rollback_pair_attr(size);
} }
...@@ -192,13 +192,13 @@ static void rollback_initialize_for_txn( ...@@ -192,13 +192,13 @@ static void rollback_initialize_for_txn(
log->previous = previous; log->previous = previous;
log->oldest_logentry = NULL; log->oldest_logentry = NULL;
log->newest_logentry = NULL; log->newest_logentry = NULL;
log->rollentry_arena = memarena_create(); log->rollentry_arena = toku_memarena_create();
log->rollentry_resident_bytecount = 0; log->rollentry_resident_bytecount = 0;
log->dirty = true; log->dirty = true;
} }
void make_rollback_log_empty(ROLLBACK_LOG_NODE log) { void make_rollback_log_empty(ROLLBACK_LOG_NODE log) {
memarena_close(&log->rollentry_arena); toku_memarena_destroy(&log->rollentry_arena);
rollback_empty_log_init(log); rollback_empty_log_init(log);
} }
......
...@@ -2,6 +2,7 @@ set(util_srcs ...@@ -2,6 +2,7 @@ set(util_srcs
context context
frwlock frwlock
kibbutz kibbutz
memarena
mempool mempool
partitioned_counter partitioned_counter
threadpool threadpool
......
...@@ -92,7 +92,7 @@ PATENT RIGHTS GRANT: ...@@ -92,7 +92,7 @@ PATENT RIGHTS GRANT:
#include <string.h> #include <string.h>
#include <memory.h> #include <memory.h>
#include "memarena.h" #include <util/memarena.h>
struct memarena { struct memarena {
char *buf; char *buf;
...@@ -103,7 +103,7 @@ struct memarena { ...@@ -103,7 +103,7 @@ struct memarena {
int n_other_bufs; int n_other_bufs;
}; };
MEMARENA memarena_create_presized (size_t initial_size) { MEMARENA toku_memarena_create_presized (size_t initial_size) {
MEMARENA XMALLOC(result); MEMARENA XMALLOC(result);
result->buf_size = initial_size; result->buf_size = initial_size;
result->buf_used = 0; result->buf_used = 0;
...@@ -115,11 +115,11 @@ MEMARENA memarena_create_presized (size_t initial_size) { ...@@ -115,11 +115,11 @@ MEMARENA memarena_create_presized (size_t initial_size) {
return result; return result;
} }
MEMARENA memarena_create (void) { MEMARENA toku_memarena_create (void) {
return memarena_create_presized(1024); return toku_memarena_create_presized(1024);
} }
void memarena_clear (MEMARENA ma) { void toku_memarena_clear (MEMARENA ma) {
// Free the other bufs. // Free the other bufs.
int i; int i;
for (i=0; i<ma->n_other_bufs; i++) { for (i=0; i<ma->n_other_bufs; i++) {
...@@ -143,7 +143,7 @@ round_to_page (size_t size) { ...@@ -143,7 +143,7 @@ round_to_page (size_t size) {
return result; return result;
} }
void* malloc_in_memarena (MEMARENA ma, size_t size) { void* toku_memarena_malloc (MEMARENA ma, size_t size) {
if (ma->buf_size < ma->buf_used + size) { if (ma->buf_size < ma->buf_used + size) {
// The existing block isn't big enough. // The existing block isn't big enough.
// Add the block to the vector of blocks. // Add the block to the vector of blocks.
...@@ -172,13 +172,13 @@ void* malloc_in_memarena (MEMARENA ma, size_t size) { ...@@ -172,13 +172,13 @@ void* malloc_in_memarena (MEMARENA ma, size_t size) {
return result; return result;
} }
void *memarena_memdup (MEMARENA ma, const void *v, size_t len) { void *toku_memarena_memdup (MEMARENA ma, const void *v, size_t len) {
void *r=malloc_in_memarena(ma, len); void *r=toku_memarena_malloc(ma, len);
memcpy(r,v,len); memcpy(r,v,len);
return r; return r;
} }
void memarena_close(MEMARENA *map) { void toku_memarena_destroy(MEMARENA *map) {
MEMARENA ma=*map; MEMARENA ma=*map;
if (ma->buf) { if (ma->buf) {
toku_free(ma->buf); toku_free(ma->buf);
...@@ -200,7 +200,7 @@ void memarena_close(MEMARENA *map) { ...@@ -200,7 +200,7 @@ void memarena_close(MEMARENA *map) {
#include <crtdbg.h> #include <crtdbg.h>
#endif #endif
void memarena_move_buffers(MEMARENA dest, MEMARENA source) { void toku_memarena_move_buffers(MEMARENA dest, MEMARENA source) {
int i; int i;
char **other_bufs = dest->other_bufs; char **other_bufs = dest->other_bufs;
static int move_counter = 0; static int move_counter = 0;
...@@ -241,21 +241,21 @@ void memarena_move_buffers(MEMARENA dest, MEMARENA source) { ...@@ -241,21 +241,21 @@ void memarena_move_buffers(MEMARENA dest, MEMARENA source) {
} }
size_t size_t
memarena_total_memory_size (MEMARENA m) toku_memarena_total_memory_size (MEMARENA m)
{ {
return (memarena_total_size_in_use(m) + return (toku_memarena_total_size_in_use(m) +
sizeof(*m) + sizeof(*m) +
m->n_other_bufs * sizeof(*m->other_bufs)); m->n_other_bufs * sizeof(*m->other_bufs));
} }
size_t size_t
memarena_total_size_in_use (MEMARENA m) toku_memarena_total_size_in_use (MEMARENA m)
{ {
return m->size_of_other_bufs + m->buf_used; return m->size_of_other_bufs + m->buf_used;
} }
size_t size_t
memarena_total_footprint (MEMARENA m) toku_memarena_total_footprint (MEMARENA m)
{ {
return m->footprint_of_other_bufs + toku_memory_footprint(m->buf, m->buf_used) + return m->footprint_of_other_bufs + toku_memory_footprint(m->buf, m->buf_used) +
sizeof(*m) + sizeof(*m) +
......
...@@ -102,33 +102,35 @@ PATENT RIGHTS GRANT: ...@@ -102,33 +102,35 @@ PATENT RIGHTS GRANT:
* A memarena (as currently implemented) is not suitable for interprocess memory sharing. No reason it couldn't be made to work though. * A memarena (as currently implemented) is not suitable for interprocess memory sharing. No reason it couldn't be made to work though.
*/ */
#include "fttypes.h" struct memarena;
MEMARENA memarena_create_presized (size_t initial_size); typedef struct memarena *MEMARENA;
MEMARENA toku_memarena_create_presized (size_t initial_size);
// Effect: Create a memarena with initial size. In case of ENOMEM, aborts. // Effect: Create a memarena with initial size. In case of ENOMEM, aborts.
MEMARENA memarena_create (void); MEMARENA toku_memarena_create (void);
// Effect: Create a memarena with default initial size. In case of ENOMEM, aborts. // Effect: Create a memarena with default initial size. In case of ENOMEM, aborts.
void memarena_clear (MEMARENA ma); void toku_memarena_clear (MEMARENA ma);
// Effect: Reset the internal state so that the allocated memory can be used again. // Effect: Reset the internal state so that the allocated memory can be used again.
void* malloc_in_memarena (MEMARENA ma, size_t size); void* toku_memarena_malloc (MEMARENA ma, size_t size);
// Effect: Allocate some memory. The returned value remains valid until the memarena is cleared or closed. // Effect: Allocate some memory. The returned value remains valid until the memarena is cleared or closed.
// In case of ENOMEM, aborts. // In case of ENOMEM, aborts.
void *memarena_memdup (MEMARENA ma, const void *v, size_t len); void *toku_memarena_memdup (MEMARENA ma, const void *v, size_t len);
void memarena_close(MEMARENA *ma); void toku_memarena_destroy(MEMARENA *ma);
void memarena_move_buffers(MEMARENA dest, MEMARENA source); void toku_memarena_move_buffers(MEMARENA dest, MEMARENA source);
// Effect: Move all the memory from SOURCE into DEST. When SOURCE is closed the memory won't be freed. When DEST is closed, the memory will be freed. (Unless DEST moves its memory to another memarena...) // Effect: Move all the memory from SOURCE into DEST. When SOURCE is closed the memory won't be freed. When DEST is closed, the memory will be freed. (Unless DEST moves its memory to another memarena...)
size_t memarena_total_memory_size (MEMARENA); size_t toku_memarena_total_memory_size (MEMARENA);
// Effect: Calculate the amount of memory used by a memory arena. // Effect: Calculate the amount of memory used by a memory arena.
size_t memarena_total_size_in_use (MEMARENA); size_t toku_memarena_total_size_in_use (MEMARENA);
size_t memarena_total_footprint (MEMARENA); size_t toku_memarena_total_footprint (MEMARENA);
#endif #endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment