Skip to content

Commit

Permalink
Track length of time mutexes are held for (#2267)
Browse files Browse the repository at this point in the history
* Track length of time locks are held for

* Fix Windows build

* Fix linux broken build

* Fix MSVC warning
  • Loading branch information
wezrule authored Sep 3, 2019
1 parent 60b13fa commit 732de66
Show file tree
Hide file tree
Showing 78 changed files with 970 additions and 459 deletions.
12 changes: 11 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ set (NANO_TEST OFF CACHE BOOL "")
set (NANO_SECURE_RPC OFF CACHE BOOL "")
set (NANO_ROCKSDB OFF CACHE BOOL "")
set (NANO_WARN_TO_ERR OFF CACHE BOOL "")
set (NANO_TIMED_LOCKS 0 CACHE INTEGER "")

if (${NANO_TIMED_LOCKS} GREATER 0)
add_definitions (-DNANO_TIMED_LOCKS=${NANO_TIMED_LOCKS})
endif ()

if (NANO_ROCKSDB)
add_definitions (-DNANO_ROCKSDB=1)
Expand Down Expand Up @@ -182,7 +187,12 @@ set(Boost_USE_MULTITHREADED ON)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/Modules")

find_package (Boost 1.67.0 REQUIRED COMPONENTS filesystem log log_setup thread program_options system)
if (${NANO_TIMED_LOCKS} GREATER 0)
set (timed_locks_boost_libs context fiber)
endif ()

find_package (Boost 1.67.0 REQUIRED COMPONENTS filesystem log log_setup thread program_options system ${timed_locks_boost_libs})

if (NANO_ROCKSDB)
find_package (RocksDB REQUIRED)
find_package (ZLIB REQUIRED)
Expand Down
6 changes: 6 additions & 0 deletions ci/build-travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ if [[ $(grep -rl --exclude="*asio.hpp" "asio::async_write" ./nano) ]]; then
exit 1
fi

# prevent unsolicited use of std::lock_guard & std::unique_lock outside of allowed areas
if [[ $(grep -rl --exclude={"*random_pool.cpp","*random_pool.hpp","*locks.hpp"} "std::unique_lock\|std::lock_guard" ./nano) ]]; then
echo "using std::unique_lock or std::lock_guard is not permitted (except in nano/lib/locks.hpp and non-nano dependent libraries). Use the nano::* versions instead"
exit 1
fi

mkdir build
pushd build

Expand Down
1 change: 1 addition & 0 deletions nano/core_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ add_executable (core_test
gap_cache.cpp
ipc.cpp
ledger.cpp
locks.cpp
logger.cpp
network.cpp
node.cpp
Expand Down
20 changes: 10 additions & 10 deletions nano/core_test/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ TEST (active_transactions, adjusted_difficulty_priority)

// Check adjusted difficulty
{
std::lock_guard<std::mutex> active_guard (node1.active.mutex);
nano::lock_guard<std::mutex> active_guard (node1.active.mutex);
ASSERT_EQ (node1.active.roots.get<1> ().begin ()->election->status.winner->hash (), send1->hash ());
ASSERT_LT (node1.active.roots.find (send2->qualified_root ())->adjusted_difficulty, node1.active.roots.find (send1->qualified_root ())->adjusted_difficulty);
ASSERT_LT (node1.active.roots.find (open1->qualified_root ())->adjusted_difficulty, node1.active.roots.find (send1->qualified_root ())->adjusted_difficulty);
Expand All @@ -70,7 +70,7 @@ TEST (active_transactions, adjusted_difficulty_priority)
// Confirm elections
while (node1.active.size () != 0)
{
std::lock_guard<std::mutex> active_guard (node1.active.mutex);
nano::lock_guard<std::mutex> active_guard (node1.active.mutex);
auto it (node1.active.roots.begin ());
while (!node1.active.roots.empty () && it != node1.active.roots.end ())
{
Expand All @@ -81,7 +81,7 @@ TEST (active_transactions, adjusted_difficulty_priority)
}
{
system.deadline_set (10s);
std::unique_lock<std::mutex> active_lock (node1.active.mutex);
nano::unique_lock<std::mutex> active_lock (node1.active.mutex);
while (node1.active.confirmed.size () != 4)
{
active_lock.unlock ();
Expand Down Expand Up @@ -113,7 +113,7 @@ TEST (active_transactions, adjusted_difficulty_priority)
}

// Check adjusted difficulty
std::lock_guard<std::mutex> lock (node1.active.mutex);
nano::lock_guard<std::mutex> lock (node1.active.mutex);
uint64_t last_adjusted (0);
for (auto i (node1.active.roots.get<1> ().begin ()), n (node1.active.roots.get<1> ().end ()); i != n; ++i)
{
Expand Down Expand Up @@ -154,7 +154,7 @@ TEST (active_transactions, adjusted_difficulty_overflow_max)
}

{
std::lock_guard<std::mutex> active_guard (node1.active.mutex);
nano::lock_guard<std::mutex> active_guard (node1.active.mutex);
// Update difficulty to maximum
auto send1_root (node1.active.roots.find (send1->qualified_root ()));
auto send2_root (node1.active.roots.find (send2->qualified_root ()));
Expand Down Expand Up @@ -207,7 +207,7 @@ TEST (active_transactions, adjusted_difficulty_overflow_min)
}

{
std::lock_guard<std::mutex> active_guard (node1.active.mutex);
nano::lock_guard<std::mutex> active_guard (node1.active.mutex);
// Update difficulty to minimum
auto send1_root (node1.active.roots.find (send1->qualified_root ()));
auto send2_root (node1.active.roots.find (send2->qualified_root ()));
Expand Down Expand Up @@ -266,7 +266,7 @@ TEST (active_transactions, keep_local)
}
while (node1.active.size () != 0)
{
std::lock_guard<std::mutex> active_guard (node1.active.mutex);
nano::lock_guard<std::mutex> active_guard (node1.active.mutex);
auto it (node1.active.roots.begin ());
while (!node1.active.roots.empty () && it != node1.active.roots.end ())
{
Expand Down Expand Up @@ -329,7 +329,7 @@ TEST (active_transactions, prioritize_chains)
}
while (node1.active.size () != 0)
{
std::lock_guard<std::mutex> active_guard (node1.active.mutex);
nano::lock_guard<std::mutex> active_guard (node1.active.mutex);
auto it (node1.active.roots.get<1> ().begin ());
while (!node1.active.roots.empty () && it != node1.active.roots.get<1> ().end ())
{
Expand All @@ -355,7 +355,7 @@ TEST (active_transactions, prioritize_chains)
while (!done)
{
{
std::lock_guard<std::mutex> guard (node1.active.mutex);
nano::lock_guard<std::mutex> guard (node1.active.mutex);
done = node1.active.long_unconfirmed_size == 4;
}
ASSERT_NO_ERROR (system.poll ());
Expand All @@ -373,7 +373,7 @@ TEST (active_transactions, prioritize_chains)
while (!done)
{
{
std::lock_guard<std::mutex> guard (node1.active.mutex);
nano::lock_guard<std::mutex> guard (node1.active.mutex);
done = node1.active.long_unconfirmed_size == 4;
}
ASSERT_NO_ERROR (system.poll ());
Expand Down
14 changes: 7 additions & 7 deletions nano/core_test/conflicts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ TEST (conflicts, start_stop)
node1.active.start (send1);
ASSERT_EQ (1, node1.active.size ());
{
std::lock_guard<std::mutex> guard (node1.active.mutex);
nano::lock_guard<std::mutex> guard (node1.active.mutex);
auto existing1 (node1.active.roots.find (send1->qualified_root ()));
ASSERT_NE (node1.active.roots.end (), existing1);
auto votes1 (existing1->election);
Expand All @@ -45,7 +45,7 @@ TEST (conflicts, add_existing)
node1.active.vote (vote1);
ASSERT_EQ (1, node1.active.size ());
{
std::lock_guard<std::mutex> guard (node1.active.mutex);
nano::lock_guard<std::mutex> guard (node1.active.mutex);
auto votes1 (node1.active.roots.find (send2->qualified_root ())->election);
ASSERT_NE (nullptr, votes1);
ASSERT_EQ (2, votes1->last_votes.size ());
Expand Down Expand Up @@ -172,7 +172,7 @@ TEST (conflicts, reprioritize)
node1.process_active (send1);
node1.block_processor.flush ();
{
std::lock_guard<std::mutex> guard (node1.active.mutex);
nano::lock_guard<std::mutex> guard (node1.active.mutex);
auto existing1 (node1.active.roots.find (send1->qualified_root ()));
ASSERT_NE (node1.active.roots.end (), existing1);
ASSERT_EQ (difficulty1, existing1->difficulty);
Expand All @@ -183,7 +183,7 @@ TEST (conflicts, reprioritize)
node1.process_active (std::make_shared<nano::send_block> (send1_copy));
node1.block_processor.flush ();
{
std::lock_guard<std::mutex> guard (node1.active.mutex);
nano::lock_guard<std::mutex> guard (node1.active.mutex);
auto existing2 (node1.active.roots.find (send1->qualified_root ()));
ASSERT_NE (node1.active.roots.end (), existing2);
ASSERT_EQ (difficulty2, existing2->difficulty);
Expand All @@ -210,7 +210,7 @@ TEST (conflicts, dependency)
ASSERT_EQ (2, node1->active.size ());
// Check dependency for send block
{
std::lock_guard<std::mutex> guard (node1->active.mutex);
nano::lock_guard<std::mutex> guard (node1->active.mutex);
auto existing1 (node1->active.roots.find (send1->qualified_root ()));
ASSERT_NE (node1->active.roots.end (), existing1);
auto election1 (existing1->election);
Expand Down Expand Up @@ -258,7 +258,7 @@ TEST (conflicts, adjusted_difficulty)
}
std::unordered_map<nano::block_hash, uint64_t> adjusted_difficulties;
{
std::lock_guard<std::mutex> guard (node1.active.mutex);
nano::lock_guard<std::mutex> guard (node1.active.mutex);
ASSERT_EQ (node1.active.roots.get<1> ().begin ()->election->status.winner->hash (), send1->hash ());
for (auto i (node1.active.roots.get<1> ().begin ()), n (node1.active.roots.get<1> ().end ()); i != n; ++i)
{
Expand Down Expand Up @@ -292,7 +292,7 @@ TEST (conflicts, adjusted_difficulty)
ASSERT_NO_ERROR (system.poll ());
}
{
std::lock_guard<std::mutex> guard (node1.active.mutex);
nano::lock_guard<std::mutex> guard (node1.active.mutex);
ASSERT_EQ (node1.active.roots.get<1> ().begin ()->election->status.winner->hash (), open_epoch2->hash ());
}
}
4 changes: 2 additions & 2 deletions nano/core_test/gap_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ TEST (gap_cache, add_existing)
auto block1 (std::make_shared<nano::send_block> (0, 1, 2, nano::keypair ().prv, 4, 5));
auto transaction (system.nodes[0]->store.tx_begin_write ());
cache.add (transaction, block1->hash ());
std::unique_lock<std::mutex> lock (cache.mutex);
nano::unique_lock<std::mutex> lock (cache.mutex);
auto existing1 (cache.blocks.get<1> ().find (block1->hash ()));
ASSERT_NE (cache.blocks.get<1> ().end (), existing1);
auto arrival (existing1->arrival);
Expand All @@ -46,7 +46,7 @@ TEST (gap_cache, comparison)
auto block1 (std::make_shared<nano::send_block> (1, 0, 2, nano::keypair ().prv, 4, 5));
auto transaction (system.nodes[0]->store.tx_begin_write ());
cache.add (transaction, block1->hash ());
std::unique_lock<std::mutex> lock (cache.mutex);
nano::unique_lock<std::mutex> lock (cache.mutex);
auto existing1 (cache.blocks.get<1> ().find (block1->hash ()));
ASSERT_NE (cache.blocks.get<1> ().end (), existing1);
auto arrival (existing1->arrival);
Expand Down
14 changes: 7 additions & 7 deletions nano/core_test/ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ TEST (votes, check_signature)
ASSERT_EQ (nano::process_result::progress, node1.ledger.process (transaction, *send1).code);
}
node1.active.start (send1);
std::lock_guard<std::mutex> lock (node1.active.mutex);
nano::lock_guard<std::mutex> lock (node1.active.mutex);
auto votes1 (node1.active.roots.find (send1->qualified_root ())->election);
ASSERT_EQ (1, votes1->last_votes.size ());
auto vote1 (std::make_shared<nano::vote> (nano::test_genesis_key.pub, nano::test_genesis_key.prv, 1, send1));
Expand All @@ -757,7 +757,7 @@ TEST (votes, add_one)
auto transaction (node1.store.tx_begin_write ());
ASSERT_EQ (nano::process_result::progress, node1.ledger.process (transaction, *send1).code);
node1.active.start (send1);
std::unique_lock<std::mutex> lock (node1.active.mutex);
nano::unique_lock<std::mutex> lock (node1.active.mutex);
auto votes1 (node1.active.roots.find (send1->qualified_root ())->election);
ASSERT_EQ (1, votes1->last_votes.size ());
lock.unlock ();
Expand Down Expand Up @@ -786,7 +786,7 @@ TEST (votes, add_two)
auto transaction (node1.store.tx_begin_write ());
ASSERT_EQ (nano::process_result::progress, node1.ledger.process (transaction, *send1).code);
node1.active.start (send1);
std::unique_lock<std::mutex> lock (node1.active.mutex);
nano::unique_lock<std::mutex> lock (node1.active.mutex);
auto votes1 (node1.active.roots.find (send1->qualified_root ())->election);
lock.unlock ();
nano::keypair key2;
Expand Down Expand Up @@ -826,7 +826,7 @@ TEST (votes, add_existing)
ASSERT_FALSE (node1.active.vote (vote1));
// Block is already processed from vote
ASSERT_TRUE (node1.active.publish (send1));
std::unique_lock<std::mutex> lock (node1.active.mutex);
nano::unique_lock<std::mutex> lock (node1.active.mutex);
auto votes1 (node1.active.roots.find (send1->qualified_root ())->election);
ASSERT_EQ (1, votes1->last_votes[nano::test_genesis_key.pub].sequence);
nano::keypair key2;
Expand Down Expand Up @@ -869,7 +869,7 @@ TEST (votes, add_old)
ASSERT_EQ (nano::process_result::progress, node1.ledger.process (transaction, *send1).code);
node1.active.start (send1);
auto vote1 (std::make_shared<nano::vote> (nano::test_genesis_key.pub, nano::test_genesis_key.prv, 2, send1));
std::lock_guard<std::mutex> lock (node1.active.mutex);
nano::lock_guard<std::mutex> lock (node1.active.mutex);
auto votes1 (node1.active.roots.find (send1->qualified_root ())->election);
auto channel (std::make_shared<nano::transport::channel_udp> (node1.network.udp_channels, node1.network.endpoint (), node1.network_params.protocol.protocol_version));
node1.vote_processor.vote_blocking (transaction, vote1, channel);
Expand Down Expand Up @@ -902,7 +902,7 @@ TEST (votes, add_old_different_account)
ASSERT_EQ (nano::process_result::progress, node1.ledger.process (transaction, *send2).code);
node1.active.start (send1);
node1.active.start (send2);
std::unique_lock<std::mutex> lock (node1.active.mutex);
nano::unique_lock<std::mutex> lock (node1.active.mutex);
auto votes1 (node1.active.roots.find (send1->qualified_root ())->election);
auto votes2 (node1.active.roots.find (send2->qualified_root ())->election);
ASSERT_EQ (1, votes1->last_votes.size ());
Expand Down Expand Up @@ -940,7 +940,7 @@ TEST (votes, add_cooldown)
auto transaction (node1.store.tx_begin_write ());
ASSERT_EQ (nano::process_result::progress, node1.ledger.process (transaction, *send1).code);
node1.active.start (send1);
std::unique_lock<std::mutex> lock (node1.active.mutex);
nano::unique_lock<std::mutex> lock (node1.active.mutex);
auto votes1 (node1.active.roots.find (send1->qualified_root ())->election);
auto vote1 (std::make_shared<nano::vote> (nano::test_genesis_key.pub, nano::test_genesis_key.prv, 1, send1));
auto channel (std::make_shared<nano::transport::channel_udp> (node1.network.udp_channels, node1.network.endpoint (), node1.network_params.protocol.protocol_version));
Expand Down
Loading

0 comments on commit 732de66

Please sign in to comment.