Commit 1f9a77d3 authored by John Esmet's avatar John Esmet

FT-300 Use an environment variable to determine which file the block

allocator trace gets written to
parent a0252cbe
...@@ -106,12 +106,32 @@ PATENT RIGHTS GRANT: ...@@ -106,12 +106,32 @@ PATENT RIGHTS GRANT:
#define VALIDATE() #define VALIDATE()
#endif #endif
static inline bool ba_trace_enabled() { static FILE *ba_trace_file = nullptr;
#if 0
return true; void block_allocator::maybe_initialize_trace(void) {
#else const char *ba_trace_path = getenv("TOKU_BA_TRACE_PATH");
return false; if (ba_trace_path != nullptr) {
#endif ba_trace_file = toku_os_fopen(ba_trace_path, "w");
if (ba_trace_file == nullptr) {
fprintf(stderr, "tokuft: error: block allocator trace path found in environment (%s), "
"but it could not be opened for writing (errno %d)\n",
ba_trace_path, get_maybe_error_errno());
} else {
fprintf(stderr, "tokuft: block allocator tracing enabled, path: %s\n", ba_trace_path);
}
}
}
void block_allocator::maybe_close_trace() {
if (ba_trace_file != nullptr) {
int r = toku_os_fclose(ba_trace_file);
if (r != 0) {
fprintf(stderr, "tokuft: error: block allocator trace file did not close properly (r %d, errno %d)\n",
r, get_maybe_error_errno());
} else {
fprintf(stderr, "tokuft: block allocator tracing finished, file closed successfully\n");
}
}
} }
void block_allocator::_create_internal(uint64_t reserve_at_beginning, uint64_t alignment) { void block_allocator::_create_internal(uint64_t reserve_at_beginning, uint64_t alignment) {
...@@ -131,16 +151,16 @@ void block_allocator::_create_internal(uint64_t reserve_at_beginning, uint64_t a ...@@ -131,16 +151,16 @@ void block_allocator::_create_internal(uint64_t reserve_at_beginning, uint64_t a
void block_allocator::create(uint64_t reserve_at_beginning, uint64_t alignment) { void block_allocator::create(uint64_t reserve_at_beginning, uint64_t alignment) {
_create_internal(reserve_at_beginning, alignment); _create_internal(reserve_at_beginning, alignment);
if (ba_trace_enabled()) { if (ba_trace_file != nullptr) {
fprintf(stderr, "ba_trace_create %p\n", this); fprintf(ba_trace_file, "ba_trace_create %p\n", this);
} }
} }
void block_allocator::destroy() { void block_allocator::destroy() {
toku_free(_blocks_array); toku_free(_blocks_array);
if (ba_trace_enabled()) { if (ba_trace_file != nullptr) {
fprintf(stderr, "ba_trace_destroy %p\n", this); fprintf(ba_trace_file, "ba_trace_destroy %p\n", this);
} }
} }
...@@ -264,8 +284,8 @@ done: ...@@ -264,8 +284,8 @@ done:
_n_blocks++; _n_blocks++;
VALIDATE(); VALIDATE();
if (ba_trace_enabled()) { if (ba_trace_file != nullptr) {
fprintf(stderr, "ba_trace_alloc %p %lu %lu\n", fprintf(ba_trace_file, "ba_trace_alloc %p %lu %lu\n",
this, static_cast<unsigned long>(size), static_cast<unsigned long>(*offset)); this, static_cast<unsigned long>(size), static_cast<unsigned long>(*offset));
} }
} }
...@@ -282,11 +302,11 @@ int64_t block_allocator::find_block(uint64_t offset) { ...@@ -282,11 +302,11 @@ int64_t block_allocator::find_block(uint64_t offset) {
uint64_t lo = 0; uint64_t lo = 0;
uint64_t hi = _n_blocks; uint64_t hi = _n_blocks;
while (1) { while (1) {
assert(lo<hi); // otherwise no such block exists. assert(lo < hi); // otherwise no such block exists.
uint64_t mid = (lo+hi)/2; uint64_t mid = (lo + hi) / 2;
uint64_t thisoff = _blocks_array[mid].offset; uint64_t thisoff = _blocks_array[mid].offset;
if (thisoff < offset) { if (thisoff < offset) {
lo = mid+1; lo = mid + 1;
} else if (thisoff > offset) { } else if (thisoff > offset) {
hi = mid; hi = mid;
} else { } else {
...@@ -305,13 +325,13 @@ void block_allocator::free_block(uint64_t offset) { ...@@ -305,13 +325,13 @@ void block_allocator::free_block(uint64_t offset) {
int64_t bn = find_block(offset); int64_t bn = find_block(offset);
assert(bn >= 0); // we require that there is a block with that offset. assert(bn >= 0); // we require that there is a block with that offset.
_n_bytes_in_use -= _blocks_array[bn].size; _n_bytes_in_use -= _blocks_array[bn].size;
memmove(&_blocks_array[bn], &_blocks_array[bn +1 ], memmove(&_blocks_array[bn], &_blocks_array[bn + 1],
(_n_blocks - bn - 1) * sizeof(struct blockpair)); (_n_blocks - bn - 1) * sizeof(struct blockpair));
_n_blocks--; _n_blocks--;
VALIDATE(); VALIDATE();
if (ba_trace_enabled()) { if (ba_trace_file != nullptr) {
fprintf(stderr, "ba_trace_free %p %lu\n", fprintf(ba_trace_file, "ba_trace_free %p %lu\n",
this, static_cast<unsigned long>(offset)); this, static_cast<unsigned long>(offset));
} }
......
...@@ -215,6 +215,14 @@ public: ...@@ -215,6 +215,14 @@ public:
// report->checkpoint_bytes_additional is ignored on return // report->checkpoint_bytes_additional is ignored on return
void get_statistics(TOKU_DB_FRAGMENTATION report); void get_statistics(TOKU_DB_FRAGMENTATION report);
// Block allocator tracing.
// - Enabled by setting TOKU_BA_TRACE_PATH to the file that the trace file
// should be written to.
// - Trace may be replayed by ba_trace_replay tool in tools/ directory
// eg: "cat mytracefile | ba_trace_replay"
static void maybe_initialize_trace();
static void maybe_close_trace();
private: private:
void _create_internal(uint64_t reserve_at_beginning, uint64_t alignment); void _create_internal(uint64_t reserve_at_beginning, uint64_t alignment);
void grow_blocks_array_by(uint64_t n_to_add); void grow_blocks_array_by(uint64_t n_to_add);
......
...@@ -97,6 +97,7 @@ PATENT RIGHTS GRANT: ...@@ -97,6 +97,7 @@ PATENT RIGHTS GRANT:
#include "ft/node.h" #include "ft/node.h"
#include "ft/logger/log-internal.h" #include "ft/logger/log-internal.h"
#include "ft/txn/rollback.h" #include "ft/txn/rollback.h"
#include "ft/serialize/block_allocator.h"
#include "ft/serialize/block_table.h" #include "ft/serialize/block_table.h"
#include "ft/serialize/compress.h" #include "ft/serialize/compress.h"
#include "ft/serialize/ft_node-serialize.h" #include "ft/serialize/ft_node-serialize.h"
...@@ -141,18 +142,19 @@ struct toku_thread_pool *get_ft_pool(void) { ...@@ -141,18 +142,19 @@ struct toku_thread_pool *get_ft_pool(void) {
return ft_pool; return ft_pool;
} }
void void toku_ft_serialize_layer_init(void) {
toku_ft_serialize_layer_init(void) {
num_cores = toku_os_get_number_active_processors(); num_cores = toku_os_get_number_active_processors();
int r = toku_thread_pool_create(&ft_pool, num_cores); lazy_assert_zero(r); int r = toku_thread_pool_create(&ft_pool, num_cores);
lazy_assert_zero(r);
block_allocator::maybe_initialize_trace();
} }
void void toku_ft_serialize_layer_destroy(void) {
toku_ft_serialize_layer_destroy(void) {
toku_thread_pool_destroy(&ft_pool); toku_thread_pool_destroy(&ft_pool);
block_allocator::maybe_close_trace();
} }
enum {FILE_CHANGE_INCREMENT = (16<<20)}; enum { FILE_CHANGE_INCREMENT = (16 << 20) };
static inline uint64_t static inline uint64_t
alignup64(uint64_t a, uint64_t b) { alignup64(uint64_t a, uint64_t b) {
......
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