Skip to content

Commit

Permalink
TSAN error for send_node_id_handshake test (#2060)
Browse files Browse the repository at this point in the history
  • Loading branch information
wezrule authored and Russel Waters committed Jun 11, 2019
1 parent b57b448 commit 12f4d72
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 36 deletions.
77 changes: 43 additions & 34 deletions nano/lib/stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,47 +260,50 @@ void nano::stat::update (uint32_t key_a, uint64_t value)
auto now (std::chrono::steady_clock::now ());

std::unique_lock<std::mutex> lock (stat_mutex);
auto entry (get_entry_impl (key_a, config.interval, config.capacity));

// Counters
auto old (entry->counter.get_value ());
entry->counter.add (value);
entry->count_observers.notify (old, entry->counter.get_value ());

std::chrono::duration<double, std::milli> duration = now - log_last_count_writeout;
if (config.log_interval_counters > 0 && duration.count () > config.log_interval_counters)
if (!stopped)
{
log_counters_impl (log_count);
log_last_count_writeout = now;
}
auto entry (get_entry_impl (key_a, config.interval, config.capacity));

// Samples
if (config.sampling_enabled && entry->sample_interval > 0)
{
entry->sample_current.add (value, false);
// Counters
auto old (entry->counter.get_value ());
entry->counter.add (value);
entry->count_observers.notify (old, entry->counter.get_value ());

std::chrono::duration<double, std::milli> duration = now - entry->sample_start_time;
if (duration.count () > entry->sample_interval)
std::chrono::duration<double, std::milli> duration = now - log_last_count_writeout;
if (config.log_interval_counters > 0 && duration.count () > config.log_interval_counters)
{
entry->sample_start_time = now;

// Make a snapshot of samples for thread safety and to get a stable container
entry->sample_current.set_timestamp (std::chrono::system_clock::now ());
entry->samples.push_back (entry->sample_current);
entry->sample_current.set_value (0);
log_counters_impl (log_count);
log_last_count_writeout = now;
}

if (!entry->sample_observers.observers.empty ())
{
auto snapshot (entry->samples);
entry->sample_observers.notify (snapshot);
}
// Samples
if (config.sampling_enabled && entry->sample_interval > 0)
{
entry->sample_current.add (value, false);

// Log sink
duration = now - log_last_sample_writeout;
if (config.log_interval_samples > 0 && duration.count () > config.log_interval_samples)
std::chrono::duration<double, std::milli> duration = now - entry->sample_start_time;
if (duration.count () > entry->sample_interval)
{
log_samples_impl (log_sample);
log_last_sample_writeout = now;
entry->sample_start_time = now;

// Make a snapshot of samples for thread safety and to get a stable container
entry->sample_current.set_timestamp (std::chrono::system_clock::now ());
entry->samples.push_back (entry->sample_current);
entry->sample_current.set_value (0);

if (!entry->sample_observers.observers.empty ())
{
auto snapshot (entry->samples);
entry->sample_observers.notify (snapshot);
}

// Log sink
duration = now - log_last_sample_writeout;
if (config.log_interval_samples > 0 && duration.count () > config.log_interval_samples)
{
log_samples_impl (log_sample);
log_last_sample_writeout = now;
}
}
}
}
Expand All @@ -313,6 +316,12 @@ std::chrono::seconds nano::stat::last_reset ()
return std::chrono::duration_cast<std::chrono::seconds> (now - timestamp);
}

void nano::stat::stop ()
{
std::lock_guard<std::mutex> guard (stat_mutex);
stopped = true;
}

void nano::stat::clear ()
{
std::unique_lock<std::mutex> lock (stat_mutex);
Expand Down
6 changes: 6 additions & 0 deletions nano/lib/stats.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,9 @@ class stat final
/** Returns a new JSON log sink */
std::unique_ptr<stat_log_sink> log_sink_json () const;

/** Stop stats being output */
void stop ();

private:
static std::string type_to_string (uint32_t key);
static std::string detail_to_string (uint32_t key);
Expand Down Expand Up @@ -500,6 +503,9 @@ class stat final
std::chrono::steady_clock::time_point log_last_count_writeout{ std::chrono::steady_clock::now () };
std::chrono::steady_clock::time_point log_last_sample_writeout{ std::chrono::steady_clock::now () };

/** Whether stats should be output */
bool stopped{ false };

/** All access to stat is thread safe, including calls from observers on the same thread */
std::mutex stat_mutex;
};
Expand Down
3 changes: 2 additions & 1 deletion nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ node (init_a, io_ctx_a, application_path_a, alarm_a, nano::node_config (peering_
nano::node::node (nano::node_init & init_a, boost::asio::io_context & io_ctx_a, boost::filesystem::path const & application_path_a, nano::alarm & alarm_a, nano::node_config const & config_a, nano::work_pool & work_a, nano::node_flags flags_a, bool delay_frontier_confirmation_height_updating) :
io_ctx (io_ctx_a),
config (config_a),
stats (config.stat_config),
flags (flags_a),
alarm (alarm_a),
work (work_a),
Expand All @@ -222,7 +223,6 @@ block_processor_thread ([this]() {
this->block_processor.process_blocks ();
}),
online_reps (*this, config.online_weight_minimum.number ()),
stats (config.stat_config),
vote_uniquer (block_uniquer),
active (*this, delay_frontier_confirmation_height_updating),
confirmation_height_processor (pending_confirmation_height, store, ledger.stats, active, ledger.epoch_link, logger),
Expand Down Expand Up @@ -828,6 +828,7 @@ void nano::node::stop ()
port_mapping.stop ();
checker.stop ();
wallets.stop ();
stats.stop ();
}
}

Expand Down
2 changes: 1 addition & 1 deletion nano/node/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ class node final : public std::enable_shared_from_this<nano::node>
boost::latch node_initialized_latch;
nano::network_params network_params;
nano::node_config config;
nano::stat stats;
std::shared_ptr<nano::websocket::listener> websocket_server;
nano::node_flags flags;
nano::alarm & alarm;
Expand All @@ -237,7 +238,6 @@ class node final : public std::enable_shared_from_this<nano::node>
nano::block_arrival block_arrival;
nano::online_reps online_reps;
nano::votes_cache votes_cache;
nano::stat stats;
nano::keypair node_id;
nano::block_uniquer block_uniquer;
nano::vote_uniquer vote_uniquer;
Expand Down

0 comments on commit 12f4d72

Please sign in to comment.