Commit 75052cdc authored by John Esmet's avatar John Esmet

fixes #127 Add a class for scoped mallocs, which are satisfied from a

thread-local buffer when possible.
parent 8527aa5a
......@@ -89,6 +89,8 @@ PATENT RIGHTS GRANT:
#ident "$Id$"
#include <toku_portability.h>
#include <util/scoped_malloc.h>
#include <zlib.h>
#include <lzma.h>
......@@ -241,10 +243,10 @@ void toku_decompress (Bytef *dest, uLongf destLen,
}
case TOKU_QUICKLZ_METHOD:
if (sourceLen>1) {
qlz_state_decompress *XCALLOC(qsd);
toku::scoped_calloc state_buf(sizeof(qlz_state_decompress));
qlz_state_decompress *qsd = reinterpret_cast<qlz_state_decompress *>(state_buf.get());
uLongf actual_destlen = qlz_decompress((char*)source+1, dest, qsd);
assert(actual_destlen == destLen);
toku_free(qsd);
} else {
// length 1 means there is no data, so do nothing.
assert(destLen==0);
......
......@@ -223,6 +223,7 @@ basement nodes, bulk fetch, and partial fetch:
#include <util/status.h>
#include <util/rwlock.h>
#include <util/sort.h>
#include <util/scoped_malloc.h>
#include <stdint.h>
......@@ -4478,7 +4479,8 @@ bnc_apply_messages_to_basement_node(
// the relevant messages' offsets and sort them by MSN, then apply
// them in MSN order.
const int buffer_size = ((stale_ube - stale_lbi) + (fresh_ube - fresh_lbi) + bnc->broadcast_list.size());
int32_t *XMALLOC_N(buffer_size, offsets);
toku::scoped_malloc offsets_buf(buffer_size * sizeof(int32_t));
int32_t *offsets = reinterpret_cast<int32_t *>(offsets_buf.get());
struct store_fifo_offset_extra sfo_extra = { .offsets = offsets, .i = 0 };
// Populate offsets array with offsets to stale messages
......@@ -4504,8 +4506,6 @@ bnc_apply_messages_to_basement_node(
struct fifo_entry *entry = toku_fifo_get_entry(bnc->buffer, offsets[i]);
do_bn_apply_cmd(t, bn, entry, oldest_referenced_xid, &workdone_this_ancestor, &stats_delta);
}
toku_free(offsets);
} else if (stale_lbi == stale_ube) {
// No stale messages to apply, we just apply fresh messages, and mark them to be moved to stale later.
struct iterate_do_bn_apply_cmd_extra iter_extra = { .t = t, .bn = bn, .bnc = bnc, .oldest_referenced_xid = oldest_referenced_xid, .workdone = &workdone_this_ancestor, .stats_to_update = &stats_delta };
......@@ -6368,12 +6368,11 @@ int toku_ft_layer_init(void) {
toku_checkpoint_init();
toku_ft_serialize_layer_init();
toku_mutex_init(&ft_open_close_lock, NULL);
toku_scoped_malloc_init();
exit:
return r;
}
void toku_ft_layer_destroy(void) {
toku_mutex_destroy(&ft_open_close_lock);
toku_ft_serialize_layer_destroy();
......@@ -6382,6 +6381,7 @@ void toku_ft_layer_destroy(void) {
txn_status_destroy();
toku_context_status_destroy();
partitioned_counters_destroy();
toku_scoped_malloc_destroy();
//Portability must be cleaned up last
toku_portability_destroy();
}
......
This diff is collapsed.
......@@ -115,6 +115,7 @@ PATENT RIGHTS GRANT:
#include "txn_manager.h"
#include "ule-internal.h"
#include <util/status.h>
#include <util/scoped_malloc.h>
#define ULE_DEBUG 0
......@@ -469,16 +470,10 @@ toku_le_apply_msg(FT_MSG msg,
uint64_t oldmemsize = 0;
uint32_t keylen = ft_msg_get_keylen(msg);
LEAFENTRY copied_old_le = NULL;
bool old_le_malloced = false;
size_t old_le_size = old_leafentry ? leafentry_memsize(old_leafentry) : 0;
toku::scoped_malloc copied_old_le_buf(old_le_size);
if (old_leafentry) {
size_t old_le_size = leafentry_memsize(old_leafentry);
if (old_le_size > 100*1024) { // completely arbitrary limit
CAST_FROM_VOIDP(copied_old_le, toku_malloc(old_le_size));
old_le_malloced = true;
}
else {
CAST_FROM_VOIDP(copied_old_le, alloca(old_le_size));
}
CAST_FROM_VOIDP(copied_old_le, copied_old_le_buf.get());
memcpy(copied_old_le, old_leafentry, old_le_size);
}
......@@ -506,9 +501,6 @@ toku_le_apply_msg(FT_MSG msg,
}
*numbytes_delta_p = newnumbytes - oldnumbytes;
ule_cleanup(&ule);
if (old_le_malloced) {
toku_free(copied_old_le);
}
}
bool toku_le_worth_running_garbage_collection(LEAFENTRY le, TXNID oldest_referenced_xid_known) {
......@@ -564,16 +556,10 @@ toku_le_garbage_collect(LEAFENTRY old_leaf_entry,
int64_t oldnumbytes = 0;
int64_t newnumbytes = 0;
LEAFENTRY copied_old_le = NULL;
bool old_le_malloced = false;
size_t old_le_size = old_leaf_entry ? leafentry_memsize(old_leaf_entry) : 0;
toku::scoped_malloc copied_old_le_buf(old_le_size);
if (old_leaf_entry) {
size_t old_le_size = leafentry_memsize(old_leaf_entry);
if (old_le_size > 100*1024) { // completely arbitrary limit
CAST_FROM_VOIDP(copied_old_le, toku_malloc(old_le_size));
old_le_malloced = true;
}
else {
CAST_FROM_VOIDP(copied_old_le, alloca(old_le_size));
}
CAST_FROM_VOIDP(copied_old_le, copied_old_le_buf.get());
memcpy(copied_old_le, old_leaf_entry, old_le_size);
}
......@@ -607,9 +593,6 @@ toku_le_garbage_collect(LEAFENTRY old_leaf_entry,
}
*numbytes_delta_p = newnumbytes - oldnumbytes;
ule_cleanup(&ule);
if (old_le_malloced) {
toku_free(copied_old_le);
}
}
/////////////////////////////////////////////////////////////////////////////////
......
......@@ -4,6 +4,7 @@ set(util_srcs
mempool
partitioned_counter
threadpool
scoped_malloc
)
add_library(util SHARED ${util_srcs})
......
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
/*
COPYING CONDITIONS NOTICE:
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as
published by the Free Software Foundation, and provided that the
following conditions are met:
* Redistributions of source code must retain this COPYING
CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
PATENT MARKING NOTICE (below), and the PATENT RIGHTS
GRANT (below).
* Redistributions in binary form must reproduce this COPYING
CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
PATENT MARKING NOTICE (below), and the PATENT RIGHTS
GRANT (below) in the documentation and/or other materials
provided with the distribution.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
COPYRIGHT NOTICE:
TokuDB, Tokutek Fractal Tree Indexing Library.
Copyright (C) 2007-2013 Tokutek, Inc.
DISCLAIMER:
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
UNIVERSITY PATENT NOTICE:
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.
PATENT MARKING NOTICE:
This software is covered by US Patent No. 8,185,551.
This software is covered by US Patent No. 8,489,638.
PATENT RIGHTS GRANT:
"THIS IMPLEMENTATION" means the copyrightable works distributed by
Tokutek as part of the Fractal Tree project.
"PATENT CLAIMS" means the claims of patents that are owned or
licensable by Tokutek, both currently or in the future; and that in
the absence of this license would be infringed by THIS
IMPLEMENTATION or by using or running THIS IMPLEMENTATION.
"PATENT CHALLENGE" shall mean a challenge to the validity,
patentability, enforceability and/or non-infringement of any of the
PATENT CLAIMS or otherwise opposing any of the PATENT CLAIMS.
Tokutek hereby grants to you, for the term and geographical scope of
the PATENT CLAIMS, a non-exclusive, no-charge, royalty-free,
irrevocable (except as stated in this section) patent license to
make, have made, use, offer to sell, sell, import, transfer, and
otherwise run, modify, and propagate the contents of THIS
IMPLEMENTATION, where such license applies only to the PATENT
CLAIMS. This grant does not include claims that would be infringed
only as a consequence of further modifications of THIS
IMPLEMENTATION. If you or your agent or licensee institute or order
or agree to the institution of patent litigation against any entity
(including a cross-claim or counterclaim in a lawsuit) alleging that
THIS IMPLEMENTATION constitutes direct or contributory patent
infringement, or inducement of patent infringement, then any rights
granted to you under this License shall terminate as of the date
such litigation is filed. If you or your agent or exclusive
licensee institute or order or agree to the institution of a PATENT
CHALLENGE, then Tokutek may terminate any rights granted to you
under this License.
*/
#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."
#include <pthread.h>
#include <toku_include/memory.h>
#include <util/scoped_malloc.h>
namespace toku {
// see pthread_key handling at the bottom
//
// when we use gcc 4.8, we can use the 'thread_local' keyword and proper
// c++ constructors/destructors instead of this pthread wizardy.
static pthread_key_t tl_stack_destroy_pthread_key;
class tl_stack {
// 1MB
static const size_t STACK_SIZE = 1 * 1024 * 1024;
public:
void init() {
m_stack = reinterpret_cast<char *>(toku_xmalloc(STACK_SIZE));
m_current_offset = 0;
int r = pthread_setspecific(tl_stack_destroy_pthread_key, this);
invariant_zero(r);
}
void destroy() {
if (m_stack != NULL) {
toku_free(m_stack);
m_stack = NULL;
}
}
// Allocate 'size' bytes and return a pointer to the first byte
void *alloc(const size_t size) {
if (m_stack == NULL) {
init();
}
invariant(m_current_offset + size <= STACK_SIZE);
void *mem = &m_stack[m_current_offset];
m_current_offset += size;
return mem;
}
// Give back a previously allocated region of 'size' bytes.
void dealloc(const size_t size) {
invariant(m_current_offset >= size);
m_current_offset -= size;
}
// Get the current size of free-space in bytes.
size_t get_free_space() const {
invariant(m_current_offset <= STACK_SIZE);
return STACK_SIZE - m_current_offset;
}
private:
// Offset of the free region in the stack
size_t m_current_offset;
char *m_stack;
};
// Each thread has its own local stack.
static __thread tl_stack local_stack;
// Memory is allocated from thread-local storage if available, otherwise from malloc(1).
scoped_malloc::scoped_malloc(const size_t size) :
m_size(size),
m_local(local_stack.get_free_space() >= m_size),
m_buf(m_local ? local_stack.alloc(m_size) : toku_xmalloc(m_size)) {
}
scoped_malloc::~scoped_malloc() {
if (m_local) {
local_stack.dealloc(m_size);
} else {
toku_free(m_buf);
}
}
} // namespace toku
// pthread key handling:
// - there is a process-wide pthread key that is associated with the destructor for a tl_stack
// - on process construction, we initialize the key; on destruction, we clean it up.
// - when a thread first uses its tl_stack, it calls pthread_setspecific(&destroy_key, "some key"),
// associating the destroy key with the tl_stack_destroy destructor
// - when a thread terminates, it calls the associated destructor; tl_stack_destroy.
static void tl_stack_destroy(void *key) {
invariant_notnull(key);
toku::tl_stack *st = reinterpret_cast<toku::tl_stack *>(key);
st->destroy();
}
void toku_scoped_malloc_init(void) {
int r = pthread_key_create(&toku::tl_stack_destroy_pthread_key, tl_stack_destroy);
invariant_zero(r);
}
void toku_scoped_malloc_destroy(void) {
// We're deregistering the destructor key here. When this thread exits,
// the tl_stack destructor won't get called, so we need to do that first.
tl_stack_destroy(&toku::local_stack);
int r = pthread_key_delete(toku::tl_stack_destroy_pthread_key);
invariant_zero(r);
}
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
/*
COPYING CONDITIONS NOTICE:
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as
published by the Free Software Foundation, and provided that the
following conditions are met:
* Redistributions of source code must retain this COPYING
CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
PATENT MARKING NOTICE (below), and the PATENT RIGHTS
GRANT (below).
* Redistributions in binary form must reproduce this COPYING
CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
PATENT MARKING NOTICE (below), and the PATENT RIGHTS
GRANT (below) in the documentation and/or other materials
provided with the distribution.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
COPYRIGHT NOTICE:
TokuDB, Tokutek Fractal Tree Indexing Library.
Copyright (C) 2007-2013 Tokutek, Inc.
DISCLAIMER:
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
UNIVERSITY PATENT NOTICE:
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.
PATENT MARKING NOTICE:
This software is covered by US Patent No. 8,185,551.
This software is covered by US Patent No. 8,489,638.
PATENT RIGHTS GRANT:
"THIS IMPLEMENTATION" means the copyrightable works distributed by
Tokutek as part of the Fractal Tree project.
"PATENT CLAIMS" means the claims of patents that are owned or
licensable by Tokutek, both currently or in the future; and that in
the absence of this license would be infringed by THIS
IMPLEMENTATION or by using or running THIS IMPLEMENTATION.
"PATENT CHALLENGE" shall mean a challenge to the validity,
patentability, enforceability and/or non-infringement of any of the
PATENT CLAIMS or otherwise opposing any of the PATENT CLAIMS.
Tokutek hereby grants to you, for the term and geographical scope of
the PATENT CLAIMS, a non-exclusive, no-charge, royalty-free,
irrevocable (except as stated in this section) patent license to
make, have made, use, offer to sell, sell, import, transfer, and
otherwise run, modify, and propagate the contents of THIS
IMPLEMENTATION, where such license applies only to the PATENT
CLAIMS. This grant does not include claims that would be infringed
only as a consequence of further modifications of THIS
IMPLEMENTATION. If you or your agent or licensee institute or order
or agree to the institution of patent litigation against any entity
(including a cross-claim or counterclaim in a lawsuit) alleging that
THIS IMPLEMENTATION constitutes direct or contributory patent
infringement, or inducement of patent infringement, then any rights
granted to you under this License shall terminate as of the date
such litigation is filed. If you or your agent or exclusive
licensee institute or order or agree to the institution of a PATENT
CHALLENGE, then Tokutek may terminate any rights granted to you
under this License.
*/
#pragma once
#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."
#include <string.h>
namespace toku {
class scoped_malloc {
public:
// Memory is allocated from thread-local storage if available, otherwise from malloc(3).
scoped_malloc(const size_t size);
~scoped_malloc();
void *get() const {
return m_buf;
}
private:
// Non-copyable
scoped_malloc();
const size_t m_size;
const bool m_local;
void *const m_buf;
};
class scoped_calloc : public scoped_malloc {
public:
// A scoped malloc whose bytes are initialized to zero, as in calloc(3)
scoped_calloc(const size_t size) :
scoped_malloc(size) {
memset(scoped_malloc::get(), 0, size);
}
};
class scoped_malloc_aligned : public scoped_malloc {
public:
scoped_malloc_aligned(const size_t size, const size_t alignment) :
scoped_malloc(size + alignment) {
invariant(size >= alignment);
invariant(alignment > 0);
const uintptr_t addr = reinterpret_cast<uintptr_t>(scoped_malloc::get());
const uintptr_t aligned_addr = (addr + alignment) - (addr % alignment);
invariant(aligned_addr < addr + size + alignment);
m_aligned_buf = reinterpret_cast<char *>(aligned_addr);
}
void *get() const {
return m_aligned_buf;
}
private:
void *m_aligned_buf;
};
} // namespace toku
void toku_scoped_malloc_init(void);
void toku_scoped_malloc_destroy(void);
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