You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@impala.apache.org by st...@apache.org on 2026/07/19 05:22:14 UTC

(impala) branch master updated: IMPALA-15088: Add ReadOnlyIntGauge

This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/impala.git


The following commit(s) were added to refs/heads/master by this push:
     new 834a77f4c IMPALA-15088: Add ReadOnlyIntGauge
834a77f4c is described below

commit 834a77f4cf3612f2a98f3b749d005c8e0623ca8b
Author: Aleksandr Efimov <dz...@gmail.com>
AuthorDate: Fri Jun 19 13:07:10 2026 +0300

    IMPALA-15088: Add ReadOnlyIntGauge
    
    Add ReadOnlyIntGauge as the common read-only interface for integer
    gauge metrics.
    
    SumGauge, NegatedGauge, MemTracker consumption metrics, and malloc
    memory metric accessors now use the read-only interface. This lets both
    IntGauge and FunctionGauge satisfy code that only needs GetValue().
    
    Replace the derived tcmalloc total-bytes-reserved and overhead metric
    classes with FunctionGauge instances.
    
    Testing:
    - git diff --check asf-gerrit/master..HEAD
    - python3 bin/jenkins/critique-gerrit-review.py --dryrun
    - focused Linux build: MallocUtilGoogleTcmalloc
    - focused Linux build: unified-be-test-executable
    - focused Linux test: MetricsTest.* (46 tests)
    
    Assisted-by: OpenAI Codex
    Change-Id: Ida124aa3225ee07cf30e9f0a481477474a50c4dd
    Reviewed-on: http://gerrit.cloudera.org:8080/24485
    Reviewed-by: Impala Public Jenkins <im...@cloudera.com>
    Tested-by: Impala Public Jenkins <im...@cloudera.com>
---
 be/src/runtime/exec-env.cc                |  4 +--
 be/src/runtime/mem-tracker.cc             |  2 +-
 be/src/runtime/mem-tracker.h              |  4 +--
 be/src/scheduling/admission-controller.cc |  2 +-
 be/src/util/malloc-util-googletcmalloc.h  | 29 +++++++-----------
 be/src/util/malloc-util-gperftools.h      | 49 ++++++++++++-------------------
 be/src/util/malloc-util-libc.h            |  8 ++---
 be/src/util/malloc-util-sanitizers.h      |  8 ++---
 be/src/util/malloc-util.h                 |  4 +--
 be/src/util/memory-metrics.cc             |  2 +-
 be/src/util/metrics-fwd.h                 |  5 ++++
 be/src/util/metrics-test.cc               | 35 ++++++++++++++++------
 be/src/util/metrics.h                     | 16 +++++-----
 13 files changed, 85 insertions(+), 83 deletions(-)

diff --git a/be/src/runtime/exec-env.cc b/be/src/runtime/exec-env.cc
index c85f77854..b0e063554 100644
--- a/be/src/runtime/exec-env.cc
+++ b/be/src/runtime/exec-env.cc
@@ -495,7 +495,7 @@ Status ExecEnv::Init() {
   RETURN_IF_ERROR(stream_mgr_->Init(data_svc_->mem_tracker()));
 
   // A MemTracker for malloc overhead
-  IntGauge* overhead_gauge = MallocUtil::GetInstance()->GetOverheadBytesMetric();
+  ReadOnlyIntGauge* overhead_gauge = MallocUtil::GetInstance()->GetOverheadBytesMetric();
   if (overhead_gauge != nullptr) {
     obj_pool_->Add(
         new MemTracker(overhead_gauge, -1, Substitute("$0 Overhead",
@@ -653,7 +653,7 @@ void ExecEnv::InitMemTracker(int64_t bytes_limit) {
       // Also need a MemTracker for unused reservations as a negative value. Unused
       // reservations are counted against queries but not against the process memory
       // consumption. This accounts for that difference.
-      IntGauge* negated_unused_reservation = obj_pool_->Add(new NegatedGauge(
+      NegatedGauge* negated_unused_reservation = obj_pool_->Add(new NegatedGauge(
           MakeTMetricDef("negated_unused_reservation", TMetricKind::GAUGE, TUnit::BYTES),
           BufferPoolMetric::UNUSED_RESERVATION_BYTES));
       obj_pool_->Add(new MemTracker(negated_unused_reservation, -1,
diff --git a/be/src/runtime/mem-tracker.cc b/be/src/runtime/mem-tracker.cc
index 06e1b23fa..20a39185b 100644
--- a/be/src/runtime/mem-tracker.cc
+++ b/be/src/runtime/mem-tracker.cc
@@ -95,7 +95,7 @@ MemTracker::MemTracker(RuntimeProfile* profile, int64_t byte_limit,
   Init();
 }
 
-MemTracker::MemTracker(IntGauge* consumption_metric, int64_t byte_limit,
+MemTracker::MemTracker(ReadOnlyIntGauge* consumption_metric, int64_t byte_limit,
     const string& label, MemTracker* parent, int64_t extra_bytes_to_gc)
   : limit_(byte_limit),
     soft_limit_(CalcSoftLimit(byte_limit)),
diff --git a/be/src/runtime/mem-tracker.h b/be/src/runtime/mem-tracker.h
index 536bbdf84..2b4347fec 100644
--- a/be/src/runtime/mem-tracker.h
+++ b/be/src/runtime/mem-tracker.h
@@ -113,7 +113,7 @@ class MemTracker {
   /// Consume()/Release() can still be called. This is used for the root process tracker
   /// (if 'parent' is NULL). It is also to report on other categories of memory under the
   /// process tracker, e.g. buffer pool free buffers (if 'parent - non-NULL).
-  MemTracker(IntGauge* consumption_metric, int64_t byte_limit = -1,
+  MemTracker(ReadOnlyIntGauge* consumption_metric, int64_t byte_limit = -1,
              const std::string& label = std::string(), MemTracker* parent = nullptr,
              int64_t extra_bytes_to_gc = DEFAULT_EXTRA_BYTES_TO_GC);
 
@@ -507,7 +507,7 @@ class MemTracker {
   /// If non-NULL, used to measure consumption (in bytes) rather than the values provided
   /// to Consume()/Release(). Only used for the process tracker, thus parent_ should be
   /// NULL if consumption_metric_ is set.
-  IntGauge* consumption_metric_;
+  ReadOnlyIntGauge* consumption_metric_;
 
   /// If non-NULL, counters from a corresponding ReservationTracker that should be
   /// reported in logs and other diagnostics. Owned by this MemTracker. The counters
diff --git a/be/src/scheduling/admission-controller.cc b/be/src/scheduling/admission-controller.cc
index 76096c41c..955e56d07 100644
--- a/be/src/scheduling/admission-controller.cc
+++ b/be/src/scheduling/admission-controller.cc
@@ -366,7 +366,7 @@ static bool RejectForAdmissionServiceMemory(
   int64_t mem_limit = AdmissiondEnv::GetInstance()->admission_service_mem_limit();
   if (mem_limit <= 0) return false;
 
-  IntGauge* used_bytes_gauge = MallocUtil::GetInstance()->GetUsedBytesMetric(
+  ReadOnlyIntGauge* used_bytes_gauge = MallocUtil::GetInstance()->GetUsedBytesMetric(
       /*include_overhead*/ false);
   DCHECK(used_bytes_gauge != nullptr);
   int64_t bytes_inuse = used_bytes_gauge->GetValue();
diff --git a/be/src/util/malloc-util-googletcmalloc.h b/be/src/util/malloc-util-googletcmalloc.h
index 9b2344219..417e88266 100644
--- a/be/src/util/malloc-util-googletcmalloc.h
+++ b/be/src/util/malloc-util-googletcmalloc.h
@@ -34,7 +34,7 @@ DECLARE_uint64(googletcmalloc_memory_release_rate_bytes_per_sec);
 namespace impala {
 
 /// Specialised metric which exposes numeric properties from tcmalloc.
-class GoogleTcmallocMetric : public IntGauge {
+class GoogleTcmallocMetric : public ReadOnlyIntGauge {
  public:
   // To help explain the stats, here is some descriptive text output from
   // tcmalloc:
@@ -93,16 +93,7 @@ class GoogleTcmallocMetric : public IntGauge {
 
   /// Derived metric computing the amount of memory (in bytes) used by tcmalloc for
   /// overhead (thread caches, unused memory in page heap, malloc metadata, etc).
-  class OverheadBytesMetric : public IntGauge {
-  public:
-    OverheadBytesMetric(const TMetricDef& def) : IntGauge(def, 0) { }
-
-    int64_t GetValue() override {
-      return PHYSICAL_BYTES_RESERVED->GetValue() - BYTES_IN_USE->GetValue();
-    }
-  };
-
-  static OverheadBytesMetric* OVERHEAD_BYTES;
+  static FunctionGauge* OVERHEAD_BYTES;
 
   static GoogleTcmallocMetric* CreateAndRegister(MetricGroup* metrics,
       const std::string& key, const std::string& tcmalloc_var) {
@@ -124,7 +115,7 @@ class GoogleTcmallocMetric : public IntGauge {
   const std::string tcmalloc_var_;
 
   GoogleTcmallocMetric(const TMetricDef& def, const std::string& tcmalloc_var)
-    : IntGauge(def, 0), tcmalloc_var_(tcmalloc_var) { }
+    : ReadOnlyIntGauge(def), tcmalloc_var_(tcmalloc_var) { }
 };
 
 GoogleTcmallocMetric* GoogleTcmallocMetric::BYTES_IN_USE = nullptr;
@@ -136,7 +127,7 @@ GoogleTcmallocMetric* GoogleTcmallocMetric::CURRENT_TOTAL_THREAD_CACHE_BYTES = n
 GoogleTcmallocMetric* GoogleTcmallocMetric::PHYSICAL_BYTES_RESERVED = nullptr;
 GoogleTcmallocMetric* GoogleTcmallocMetric::PAGEHEAP_UNMAPPED_BYTES = nullptr;
 GoogleTcmallocMetric* GoogleTcmallocMetric::TOTAL_BYTES_RESERVED = nullptr;
-GoogleTcmallocMetric::OverheadBytesMetric* GoogleTcmallocMetric::OVERHEAD_BYTES = nullptr;
+FunctionGauge* GoogleTcmallocMetric::OVERHEAD_BYTES = nullptr;
 
 class GoogleTcmallocMallocUtil : public MallocUtil {
  public:
@@ -275,13 +266,15 @@ class GoogleTcmallocMallocUtil : public MallocUtil {
     // Additional overhead metric (#7 - #1). This is higher than what the /memz output
     // would show, because #7 is calculated differently through the
     // MallocExtension::GetNumericProperty() API.
-    GoogleTcmallocMetric::OVERHEAD_BYTES =
-        tcmalloc_metrics->RegisterMetric(new GoogleTcmallocMetric::OverheadBytesMetric(
-            MetricDefs::Get("tcmalloc.overhead-bytes")));
+    GoogleTcmallocMetric::OVERHEAD_BYTES = tcmalloc_metrics->AddFunctionGauge(
+        "tcmalloc.overhead-bytes", []() {
+          return GoogleTcmallocMetric::PHYSICAL_BYTES_RESERVED->GetValue()
+              - GoogleTcmallocMetric::BYTES_IN_USE->GetValue();
+        });
     return Status::OK();
   }
 
-  IntGauge* GetUsedBytesMetric(bool include_overhead) const override {
+  ReadOnlyIntGauge* GetUsedBytesMetric(bool include_overhead) const override {
     DCHECK(initialized_);
     if (include_overhead) {
       DCHECK(GoogleTcmallocMetric::PHYSICAL_BYTES_RESERVED != nullptr);
@@ -292,7 +285,7 @@ class GoogleTcmallocMallocUtil : public MallocUtil {
     }
   }
 
-  IntGauge* GetOverheadBytesMetric() const override {
+  ReadOnlyIntGauge* GetOverheadBytesMetric() const override {
     DCHECK(initialized_);
     DCHECK(GoogleTcmallocMetric::OVERHEAD_BYTES != nullptr);
     return GoogleTcmallocMetric::OVERHEAD_BYTES;
diff --git a/be/src/util/malloc-util-gperftools.h b/be/src/util/malloc-util-gperftools.h
index ab5bed20b..ce949f48f 100644
--- a/be/src/util/malloc-util-gperftools.h
+++ b/be/src/util/malloc-util-gperftools.h
@@ -56,7 +56,7 @@ namespace impala {
 /// MALLOC: +    149757952 (  142.8 MiB) Bytes released to OS (aka unmapped) (#7)
 /// MALLOC:   ------------
 /// MALLOC: =   3833552896 ( 3656.0 MiB) Virtual address space used (#8)
-class TcmallocMetric : public IntGauge {
+class TcmallocMetric : public ReadOnlyIntGauge {
  public:
   /// #1: Number of bytes allocated by tcmalloc, currently used by the application.
   static TcmallocMetric* BYTES_IN_USE;
@@ -86,28 +86,11 @@ class TcmallocMetric : public IntGauge {
   /// #8: Derived metric computing the amount of virtual memory (in bytes) used by the
   /// process, including all different types of tcmalloc memory including unmapped
   /// virtual memory.
-  class TotalBytesReservedMetric : public IntGauge {
-  public:
-    TotalBytesReservedMetric(const TMetricDef& def) : IntGauge(def, 0) { }
-
-    virtual int64_t GetValue() override {
-      return PHYSICAL_BYTES_RESERVED->GetValue() + PAGEHEAP_UNMAPPED_BYTES->GetValue();
-    }
-  };
-  static TotalBytesReservedMetric* TOTAL_BYTES_RESERVED;
+  static FunctionGauge* TOTAL_BYTES_RESERVED;
 
   /// Derived metric computing the amount of memory (in bytes) used by tcmalloc for
   /// overhead (thread caches, unused memory in page heap, etc).
-  class OverheadBytesMetric : public IntGauge {
-  public:
-    OverheadBytesMetric(const TMetricDef& def) : IntGauge(def, 0) { }
-
-    int64_t GetValue() override {
-      return PHYSICAL_BYTES_RESERVED->GetValue() - BYTES_IN_USE->GetValue();
-    }
-  };
-
-  static OverheadBytesMetric* OVERHEAD_BYTES;
+  static FunctionGauge* OVERHEAD_BYTES;
 
   static TcmallocMetric* CreateAndRegister(MetricGroup* metrics, const std::string& key,
       const std::string& tcmalloc_var) {
@@ -127,7 +110,7 @@ class TcmallocMetric : public IntGauge {
   const std::string tcmalloc_var_;
 
   TcmallocMetric(const TMetricDef& def, const std::string& tcmalloc_var)
-    : IntGauge(def, 0), tcmalloc_var_(tcmalloc_var) { }
+    : ReadOnlyIntGauge(def), tcmalloc_var_(tcmalloc_var) { }
 };
 
 TcmallocMetric* TcmallocMetric::BYTES_IN_USE = nullptr;
@@ -137,8 +120,8 @@ TcmallocMetric* TcmallocMetric::TRANSFER_CACHE_FREE_BYTES = nullptr;
 TcmallocMetric* TcmallocMetric::CURRENT_TOTAL_THREAD_CACHE_BYTES = nullptr;
 TcmallocMetric* TcmallocMetric::PHYSICAL_BYTES_RESERVED = nullptr;
 TcmallocMetric* TcmallocMetric::PAGEHEAP_UNMAPPED_BYTES = nullptr;
-TcmallocMetric::TotalBytesReservedMetric* TcmallocMetric::TOTAL_BYTES_RESERVED = nullptr;
-TcmallocMetric::OverheadBytesMetric* TcmallocMetric::OVERHEAD_BYTES = nullptr;
+FunctionGauge* TcmallocMetric::TOTAL_BYTES_RESERVED = nullptr;
+FunctionGauge* TcmallocMetric::OVERHEAD_BYTES = nullptr;
 
 class GperftoolsMallocUtil : public MallocUtil {
  public:
@@ -283,19 +266,23 @@ class GperftoolsMallocUtil : public MallocUtil {
             "tcmalloc.pageheap-unmapped-bytes", "tcmalloc.pageheap_unmapped_bytes");
 
     /// #8
-    TcmallocMetric::TOTAL_BYTES_RESERVED =
-        tcmalloc_metrics->RegisterMetric(new TcmallocMetric::TotalBytesReservedMetric(
-            MetricDefs::Get("tcmalloc.total-bytes-reserved")));
+    TcmallocMetric::TOTAL_BYTES_RESERVED = tcmalloc_metrics->AddFunctionGauge(
+        "tcmalloc.total-bytes-reserved", []() {
+          return TcmallocMetric::PHYSICAL_BYTES_RESERVED->GetValue()
+              + TcmallocMetric::PAGEHEAP_UNMAPPED_BYTES->GetValue();
+        });
 
     /// Additional overhead metric (#6 - #1)
-    TcmallocMetric::OVERHEAD_BYTES =
-        tcmalloc_metrics->RegisterMetric(new TcmallocMetric::OverheadBytesMetric(
-            MetricDefs::Get("tcmalloc.overhead-bytes")));
+    TcmallocMetric::OVERHEAD_BYTES = tcmalloc_metrics->AddFunctionGauge(
+        "tcmalloc.overhead-bytes", []() {
+          return TcmallocMetric::PHYSICAL_BYTES_RESERVED->GetValue()
+              - TcmallocMetric::BYTES_IN_USE->GetValue();
+        });
 
     return Status::OK();
   }
 
-  IntGauge* GetUsedBytesMetric(bool include_overhead) const override {
+  ReadOnlyIntGauge* GetUsedBytesMetric(bool include_overhead) const override {
     DCHECK(initialized_);
     if (include_overhead) {
       DCHECK(TcmallocMetric::PHYSICAL_BYTES_RESERVED != nullptr);
@@ -306,7 +293,7 @@ class GperftoolsMallocUtil : public MallocUtil {
     }
   }
 
-  IntGauge* GetOverheadBytesMetric() const override {
+  ReadOnlyIntGauge* GetOverheadBytesMetric() const override {
     DCHECK(initialized_);
     DCHECK(TcmallocMetric::OVERHEAD_BYTES != nullptr);
     return TcmallocMetric::OVERHEAD_BYTES;
diff --git a/be/src/util/malloc-util-libc.h b/be/src/util/malloc-util-libc.h
index a46fc7d72..a31d91f0a 100644
--- a/be/src/util/malloc-util-libc.h
+++ b/be/src/util/malloc-util-libc.h
@@ -30,9 +30,9 @@ namespace impala {
 
 /// Libc doesn't have a simple API for tracking memory use from malloc, so we use
 /// resident set size.
-class LibcMallocMetric : public IntGauge {
+class LibcMallocMetric : public ReadOnlyIntGauge {
  public:
-  LibcMallocMetric(const TMetricDef& def) : IntGauge(def, 0) {}
+  LibcMallocMetric(const TMetricDef& def) : ReadOnlyIntGauge(def) {}
 
   static LibcMallocMetric* RESIDENT_SET_SIZE;
 
@@ -65,12 +65,12 @@ class LibcMallocUtil : public MallocUtil {
     return Status::OK();
   }
 
-  IntGauge* GetUsedBytesMetric(bool include_overhead) const override {
+  ReadOnlyIntGauge* GetUsedBytesMetric(bool include_overhead) const override {
     DCHECK(LibcMallocMetric::RESIDENT_SET_SIZE != nullptr);
     return LibcMallocMetric::RESIDENT_SET_SIZE;
   }
 
-  IntGauge* GetOverheadBytesMetric() const override {
+  ReadOnlyIntGauge* GetOverheadBytesMetric() const override {
     return nullptr;
   }
 
diff --git a/be/src/util/malloc-util-sanitizers.h b/be/src/util/malloc-util-sanitizers.h
index 85db0c694..cd90886a9 100644
--- a/be/src/util/malloc-util-sanitizers.h
+++ b/be/src/util/malloc-util-sanitizers.h
@@ -30,9 +30,9 @@
 namespace impala {
 
 /// The sanitizers replace malloc() and track memory usage themselves.
-class SanitizerMallocMetric : public IntGauge {
+class SanitizerMallocMetric : public ReadOnlyIntGauge {
  public:
-  SanitizerMallocMetric(const TMetricDef& def) : IntGauge(def, 0) {}
+  SanitizerMallocMetric(const TMetricDef& def) : ReadOnlyIntGauge(def) {}
 
   static SanitizerMallocMetric* BYTES_ALLOCATED;
 
@@ -69,12 +69,12 @@ class SanitizerMallocUtil : public MallocUtil {
     return Status::OK();
   }
 
-  IntGauge* GetUsedBytesMetric(bool include_overhead) const override {
+  ReadOnlyIntGauge* GetUsedBytesMetric(bool include_overhead) const override {
     DCHECK(SanitizerMallocMetric::BYTES_ALLOCATED != nullptr);
     return SanitizerMallocMetric::BYTES_ALLOCATED;
   }
 
-  IntGauge* GetOverheadBytesMetric() const override {
+  ReadOnlyIntGauge* GetOverheadBytesMetric() const override {
     return nullptr;
   }
 
diff --git a/be/src/util/malloc-util.h b/be/src/util/malloc-util.h
index a31d92e1a..4c141cd52 100644
--- a/be/src/util/malloc-util.h
+++ b/be/src/util/malloc-util.h
@@ -51,12 +51,12 @@ namespace impala {
 
     // Get the metric for used memory. This can only be called after initialization.
     // This must be non-null for all malloc implementations.
-    virtual IntGauge* GetUsedBytesMetric(bool include_overhead) const = 0;
+    virtual ReadOnlyIntGauge* GetUsedBytesMetric(bool include_overhead) const = 0;
 
     // Get the metric for the malloc implementation's overhead. This can only be
     // called after initialization. This will return null if the malloc
     // implementation does not have an overhead metric.
-    virtual IntGauge* GetOverheadBytesMetric() const = 0;
+    virtual ReadOnlyIntGauge* GetOverheadBytesMetric() const = 0;
 
     // Get information about the malloc implementation's support for huge pages.
     // MADVISE_COMPATIBLE - It is permissible to use madvise() with MADV_HUGEPAGE
diff --git a/be/src/util/memory-metrics.cc b/be/src/util/memory-metrics.cc
index 372919131..15a55b04d 100644
--- a/be/src/util/memory-metrics.cc
+++ b/be/src/util/memory-metrics.cc
@@ -84,7 +84,7 @@ Status impala::RegisterMemoryMetrics(MetricGroup* metrics, bool register_jvm_met
 
   // Add compound metrics that track totals across malloc and the buffer pool.
   // total-used should track the total physical memory in use.
-  vector<IntGauge*> used_metrics;
+  vector<ReadOnlyIntGauge*> used_metrics;
   if (FLAGS_mmap_buffers && global_reservations != nullptr) {
     // If we mmap() buffers, the buffers are not allocated via malloc. Ensure they are
     // properly tracked.
diff --git a/be/src/util/metrics-fwd.h b/be/src/util/metrics-fwd.h
index d187f6484..962e6e59d 100644
--- a/be/src/util/metrics-fwd.h
+++ b/be/src/util/metrics-fwd.h
@@ -23,6 +23,8 @@
 
 #pragma once
 
+#include <cstdint>
+
 #include "gen-cpp/MetricDefs_types.h"
 #include "gen-cpp/MetricDefs_constants.h"
 
@@ -65,6 +67,9 @@ typedef class LockedMetric<std::string,TMetricKind::PROPERTY> StringProperty;
 typedef class LockedMetric<double, TMetricKind::GAUGE> DoubleGauge;
 
 /// We write 'Int' as a placeholder for all integer types.
+/// ReadOnlyIntGauge exposes the common GetValue() interface for computed and mutable
+/// integer gauges without exposing mutation APIs such as SetValue() or Increment().
+typedef class ScalarMetric<int64_t, TMetricKind::GAUGE> ReadOnlyIntGauge;
 typedef class AtomicMetric<TMetricKind::GAUGE> IntGauge;
 typedef class AtomicMetric<TMetricKind::COUNTER> IntCounter;
 
diff --git a/be/src/util/metrics-test.cc b/be/src/util/metrics-test.cc
index 2818cca7d..5db1853e8 100644
--- a/be/src/util/metrics-test.cc
+++ b/be/src/util/metrics-test.cc
@@ -140,24 +140,37 @@ TEST_F(MetricsTest, AtomicHighWaterMarkGauge) {
 }
 
 TEST_F(MetricsTest, SumGauge) {
+  int64_t function_value = 10;
   MetricGroup metrics("SumGauge");
   AddMetricDef("gauge1", TMetricKind::GAUGE, TUnit::NONE);
   AddMetricDef("gauge2", TMetricKind::GAUGE, TUnit::NONE);
+  AddMetricDef("function_gauge", TMetricKind::GAUGE, TUnit::NONE);
   AddMetricDef("sum", TMetricKind::GAUGE, TUnit::NONE);
+  AddMetricDef("negated", TMetricKind::GAUGE, TUnit::NONE);
   IntGauge* gauge1 = metrics.AddGauge("gauge1", 0);
   IntGauge* gauge2 = metrics.AddGauge("gauge2", 0);
+  FunctionGauge* function_gauge =
+      metrics.AddFunctionGauge("function_gauge", [&function_value]() {
+        return function_value;
+      });
 
-  vector<IntGauge*> gauges({gauge1, gauge2});
-  IntGauge* sum_gauge =
+  vector<ReadOnlyIntGauge*> gauges({gauge1, gauge2, function_gauge});
+  SumGauge* sum_gauge =
       metrics.RegisterMetric(new SumGauge(MetricDefs::Get("sum"), gauges));
+  NegatedGauge* negated_gauge = metrics.RegisterMetric(
+      new NegatedGauge(MetricDefs::Get("negated"), function_gauge));
 
-  AssertValue(sum_gauge, 0, "0");
+  AssertValue(sum_gauge, 10, "10");
+  AssertValue(negated_gauge, -10, "-10");
   gauge1->Increment(1);
-  AssertValue(sum_gauge, 1, "1");
+  AssertValue(sum_gauge, 11, "11");
   gauge2->Increment(-1);
-  AssertValue(sum_gauge, 0, "0");
+  AssertValue(sum_gauge, 10, "10");
   gauge2->Increment(100);
-  AssertValue(sum_gauge, 100, "100");
+  AssertValue(sum_gauge, 110, "110");
+  function_value = -5;
+  AssertValue(sum_gauge, 95, "95");
+  AssertValue(negated_gauge, 5, "5");
 }
 
 TEST_F(MetricsTest, PropertyMetrics) {
@@ -268,11 +281,16 @@ TEST_F(MetricsTest, MemMetric) {
   scoped_ptr<vector<uint64_t>> chunk(new vector<uint64_t>(100 * 1024 * 1024));
   EXPECT_GT(bytes_in_use->GetValue(), cur_in_use);
 
-  IntGauge* total_bytes_reserved =
-      metrics.FindMetricForTesting<IntGauge>("tcmalloc.total-bytes-reserved");
+  ReadOnlyIntGauge* total_bytes_reserved =
+      metrics.FindMetricForTesting<ReadOnlyIntGauge>("tcmalloc.total-bytes-reserved");
   ASSERT_TRUE(total_bytes_reserved != NULL);
   ASSERT_GT(total_bytes_reserved->GetValue(), 0);
 
+  ReadOnlyIntGauge* overhead_bytes =
+      metrics.FindMetricForTesting<ReadOnlyIntGauge>("tcmalloc.overhead-bytes");
+  ASSERT_TRUE(overhead_bytes != NULL);
+  ASSERT_GE(overhead_bytes->GetValue(), 0);
+
   IntGauge* pageheap_free_bytes =
       metrics.FindMetricForTesting<IntGauge>("tcmalloc.pageheap-free-bytes");
   ASSERT_TRUE(pageheap_free_bytes != NULL);
@@ -937,4 +955,3 @@ TEST_F(MetricsTest, StatsMetricsNullPrometheus) {
   EXPECT_EQ("", stats_val.str());
 }
 }
-
diff --git a/be/src/util/metrics.h b/be/src/util/metrics.h
index e73510842..6e5bd8e88 100644
--- a/be/src/util/metrics.h
+++ b/be/src/util/metrics.h
@@ -317,10 +317,10 @@ class AtomicHighWaterMarkGauge : public ScalarMetric<int64_t, TMetricKind::GAUGE
 };
 
 /// Gauge metric that computes the sum of several gauges.
-class SumGauge : public IntGauge {
+class SumGauge : public ReadOnlyIntGauge {
  public:
-  SumGauge(const TMetricDef& metric_def, const std::vector<IntGauge*>& gauges)
-    : IntGauge(metric_def, 0), gauges_(gauges) {}
+  SumGauge(const TMetricDef& metric_def, const std::vector<ReadOnlyIntGauge*>& gauges)
+    : ReadOnlyIntGauge(metric_def), gauges_(gauges) {}
 
   virtual ~SumGauge() {}
 
@@ -334,14 +334,14 @@ class SumGauge : public IntGauge {
 
  private:
   /// The gauges to be summed.
-  std::vector<IntGauge*> gauges_;
+  std::vector<ReadOnlyIntGauge*> gauges_;
 };
 
 /// Gauge metric that negates another gauge.
-class NegatedGauge : public IntGauge {
+class NegatedGauge : public ReadOnlyIntGauge {
  public:
-  NegatedGauge(const TMetricDef& metric_def, IntGauge* gauge)
-    : IntGauge(metric_def, 0), gauge_(gauge) {}
+  NegatedGauge(const TMetricDef& metric_def, ReadOnlyIntGauge* gauge)
+    : ReadOnlyIntGauge(metric_def), gauge_(gauge) {}
 
   virtual ~NegatedGauge() {}
 
@@ -349,7 +349,7 @@ class NegatedGauge : public IntGauge {
 
  private:
   /// The metric to be negated.
-  IntGauge* gauge_;
+  ReadOnlyIntGauge* gauge_;
 };
 
 /// Container for a set of metrics. A MetricGroup owns the memory for every metric