Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit d052644

Browse files
wangziqi2013pervazea
authored andcommitted
Mapping table fix to avoid preallocating memory (#1190)
* Adding new mapping table * Revert "Fixing non-unique key insert problem" This reverts commit 4267752. * Revert LOG_INFO to LOG_TRACE * Fix segment fault problem by moving munmap() to after ~EpochManager() * Avoid compiler error * Enhance log message for mmap()'ed mapping table
1 parent 7df6cb6 commit d052644

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/include/index/bwtree.h

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
#include <cstddef>
3636
#include <vector>
3737

38+
#include <sys/mman.h>
39+
3840
/*
3941
* BWTREE_PELOTON - Specifies whether Peloton-specific features are
4042
* Compiled or not
@@ -2864,6 +2866,21 @@ class BwTree : public BwTreeBase {
28642866
* the mapping table rather than CAS with nullptr
28652867
*/
28662868
void InitMappingTable() {
2869+
mapping_table = (std::atomic<const BaseNode *> *) \
2870+
mmap(NULL, 1024 * 1024 * 1024,
2871+
PROT_READ | PROT_WRITE,
2872+
MAP_ANONYMOUS | MAP_PRIVATE,
2873+
-1, 0);
2874+
// If allocation fails, we throw an error because this is uncoverable
2875+
// The upper level functions should either catch this exception
2876+
// and then use another index instead, or simply kill the system
2877+
if(mapping_table == (void *)-1) {
2878+
LOG_ERROR("Failed to initialize mapping table");
2879+
throw IndexException("mmap() failed to initialize mapping table for Bw-Tree");
2880+
}
2881+
2882+
LOG_TRACE("Mapping table allocated via mmap()");
2883+
28672884
LOG_TRACE("Initializing mapping table.... size = %lu", MAPPING_TABLE_SIZE);
28682885
LOG_TRACE("Fast initialization: Do not set to zero");
28692886

@@ -7379,7 +7396,7 @@ class BwTree : public BwTreeBase {
73797396
NodeID first_leaf_id;
73807397

73817398
std::atomic<NodeID> next_unused_node_id;
7382-
std::array<std::atomic<const BaseNode *>, MAPPING_TABLE_SIZE> mapping_table;
7399+
std::atomic<const BaseNode *> *mapping_table;
73837400

73847401
// This list holds free NodeID which was removed by remove delta
73857402
// We recycle NodeID in epoch manager
@@ -7568,6 +7585,8 @@ class BwTree : public BwTreeBase {
75687585
// would always fail, until we have cleaned all epoch nodes
75697586
current_epoch_p = nullptr;
75707587

7588+
LOG_DEBUG("Clearing the epoch in ~EpochManager()...");
7589+
75717590
// If all threads has exited then all thread counts are
75727591
// 0, and therefore this should proceed way to the end
75737592
ClearEpoch();
@@ -7605,6 +7624,20 @@ class BwTree : public BwTreeBase {
76057624
epoch_leave.load());
76067625
#endif
76077626

7627+
// NOTE: Only unmap memory here because we need to access the mapping
7628+
// table in the above routine. If it was unmapped in ~BwTree() then this
7629+
// function will invoke illegal memory access
7630+
int munmap_ret = munmap(tree_p->mapping_table, 1024 * 1024 * 1024);
7631+
7632+
// Although failure of munmap is not fatal, we still print out
7633+
// an error log entry
7634+
// Otherwise just trace log
7635+
if(munmap_ret != 0) {
7636+
LOG_ERROR("munmap() returns with %d", munmap_ret);
7637+
} else {
7638+
LOG_TRACE("Mapping table is unmapped for Bw-Tree");
7639+
}
7640+
76087641
return;
76097642
}
76107643

src/index/bwtree_index.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "index/scan_optimizer.h"
1717
#include "statistics/stats_aggregator.h"
1818
#include "settings/settings_manager.h"
19-
19+
2020
namespace peloton {
2121
namespace index {
2222

0 commit comments

Comments
 (0)