Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/yb/tserver/tablet_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,8 @@ Status TabletServer::Init() {

RETURN_NOT_OK(DbServerBase::Init());

MemTracker::InitializeGcMetrics(metric_entity());

RETURN_NOT_OK(path_handlers_->Register(web_server_.get()));

log_prefix_ = Format("P $0: ", permanent_uuid());
Expand Down
38 changes: 38 additions & 0 deletions src/yb/util/mem_tracker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,20 @@ DEFINE_RUNTIME_int64(mem_tracker_tcmalloc_gc_release_bytes, -1,

DECLARE_int64(server_tcmalloc_max_total_thread_cache_bytes);

METRIC_DEFINE_counter(server, mem_tracker_gc_tcmalloc_calls,
"MemTracker GC TCMalloc Calls",
yb::MetricUnit::kOperations,
"Number of times MemTracker::GcTcmallocIfNeeded() has been called");

METRIC_DEFINE_histogram(server, mem_tracker_gc_tcmalloc_bytes_per_gc,
"MemTracker GC TCMalloc Bytes Per GC",
yb::MetricUnit::kBytes,
"Histogram of bytes released per garbage collection event in "
"MemTracker::GcTcmallocIfNeeded(). Only records when GC actually occurs. "
"Concurrent allocations during GC may cause negative apparent releases "
"which are not recorded.",
60000000LU /* 60MB as max value */, 2 /* 2 digits precision */);

namespace yb {

// NOTE: this class has been adapted from Impala, so the code style varies
Expand Down Expand Up @@ -739,6 +753,10 @@ bool MemTracker::GcMemory(int64_t max_consumption) {

void MemTracker::GcTcmallocIfNeeded() {
#ifdef YB_TCMALLOC_ENABLED
if (gc_tcmalloc_calls_metric_) {
gc_tcmalloc_calls_metric_->Increment();
}

released_memory_since_gc = 0;
TRACE_EVENT0("process", "MemTracker::GcTcmallocIfNeeded");

Expand All @@ -748,7 +766,9 @@ void MemTracker::GcTcmallocIfNeeded() {
// Bytes allocated by the application.
int64_t bytes_used = GetTCMallocCurrentAllocatedBytes();

int64_t initial_overhead = bytes_overhead;
int64_t max_overhead = bytes_used * FLAGS_tcmalloc_max_free_bytes_percentage / 100.0;

if (bytes_overhead > max_overhead) {
int64_t extra = bytes_overhead - max_overhead;
while (extra > 0) {
Expand All @@ -762,6 +782,15 @@ void MemTracker::GcTcmallocIfNeeded() {
#endif // YB_GOOGLE_TCMALLOC
extra -= 1024 * 1024;
}

int64_t final_overhead = GetTCMallocPageHeapFreeBytes();
// bytes_released could theoretically be negative if memory increased during GC
// due to concurrent allocations. The > 0 check prevents recording negative values
// in the metrics, which could skew the statistics.
int64_t bytes_released = initial_overhead - final_overhead;
if (gc_tcmalloc_bytes_per_gc_metric_ && bytes_released > 0) {
gc_tcmalloc_bytes_per_gc_metric_->Increment(bytes_released);
}
}
#endif // YB_TCMALLOC_ENABLED
}
Expand Down Expand Up @@ -848,6 +877,15 @@ void MemTracker::TEST_SetReleasedMemorySinceGC(int64_t value) {
released_memory_since_gc = value;
}

scoped_refptr<Counter> MemTracker::gc_tcmalloc_calls_metric_;
scoped_refptr<Histogram> MemTracker::gc_tcmalloc_bytes_per_gc_metric_;

void MemTracker::InitializeGcMetrics(const scoped_refptr<MetricEntity>& metric_entity) {
gc_tcmalloc_calls_metric_ = METRIC_mem_tracker_gc_tcmalloc_calls.Instantiate(metric_entity);
gc_tcmalloc_bytes_per_gc_metric_ =
METRIC_mem_tracker_gc_tcmalloc_bytes_per_gc.Instantiate(metric_entity);
}

scoped_refptr<MetricEntity> MemTracker::metric_entity() const {
return metrics_ ? metrics_->metric_entity_ : nullptr;
}
Expand Down
7 changes: 7 additions & 0 deletions src/yb/util/mem_tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ namespace yb {

class MemTracker;
class MetricEntity;
class Counter;
class Histogram;
using MemTrackerPtr = std::shared_ptr<MemTracker>;

static const std::string kTCMallocTrackerNamePrefix = "TCMalloc ";
Expand Down Expand Up @@ -430,11 +432,16 @@ class MemTracker : public std::enable_shared_from_this<MemTracker> {
// This is needed in some tests to create deterministic GC behavior.
static void TEST_SetReleasedMemorySinceGC(int64_t bytes);

static void InitializeGcMetrics(const scoped_refptr<MetricEntity>& metric_entity);

bool IsRoot() {
return is_root_tracker_;
}

private:
static scoped_refptr<Counter> gc_tcmalloc_calls_metric_;
static scoped_refptr<Histogram> gc_tcmalloc_bytes_per_gc_metric_;

template<class GC>
using GarbageCollectorsContainer = boost::container::small_vector<GC, 8>;

Expand Down
2 changes: 2 additions & 0 deletions src/yb/yql/pggate/pggate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,8 @@ PgApiImpl::PgApiImpl(
ybctid_reader_provider_(pg_session_),
fk_reference_cache_(ybctid_reader_provider_, buffering_settings_, tablespace_map_),
explicit_row_lock_buffer_(ybctid_reader_provider_, tablespace_map_) {
MemTracker::InitializeGcMetrics(metric_entity_);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#17375 (comment)

This is primarily useful for the tserver, but could also be valuable for PG,

If I understand it correctly, we'd like to initialise metrics here as well for PG


PgBackendSetupSharedMemory();
// This is an RCU object, but there are no concurrent updates on PG side, only on tserver, so
// it's safe to just save the pointer.
Expand Down