Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
memory-metrics.cc
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "util/memory-metrics.h"
16 
17 #include <boost/algorithm/string.hpp>
18 #include <boost/foreach.hpp>
19 #include <gutil/strings/substitute.h>
20 
21 #include "util/jni-util.h"
22 #include "util/time.h"
23 
24 using boost::algorithm::to_lower;
25 using namespace impala;
26 using namespace strings;
27 
33 
34 Status impala::RegisterMemoryMetrics(MetricGroup* metrics, bool register_jvm_metrics) {
35 #ifndef ADDRESS_SANITIZER
37  "tcmalloc.bytes-in-use", "generic.current_allocated_bytes"));
38 
40  "tcmalloc.total-bytes-reserved", "generic.heap_size"));
41 
43  "tcmalloc.pageheap-free-bytes", "tcmalloc.pageheap_free_bytes"));
44 
46  "tcmalloc.pageheap-unmapped-bytes", "tcmalloc.pageheap_unmapped_bytes"));
47 
49  new TcmallocMetric::PhysicalBytesMetric("tcmalloc.physical-bytes-reserved"));
50 #endif
51 
52  if (register_jvm_metrics) {
54  }
55  return Status::OK;
56 }
57 
58 JvmMetric::JvmMetric(const string& key, const string& mempool_name, JvmMetricType type)
59  : IntGauge(key, TUnit::BYTES) {
60  mempool_name_ = mempool_name;
61  metric_type_ = type;
62 
63 }
64 
66  DCHECK(metrics != NULL);
67  TGetJvmMetricsRequest request;
68  request.get_all = true;
69  TGetJvmMetricsResponse response;
70  RETURN_IF_ERROR(JniUtil::GetJvmMetrics(request, &response));
71  BOOST_FOREACH(const TJvmMemoryPool& usage, response.memory_pools) {
72  string name = usage.name;
73  to_lower(name);
74  replace(name.begin(), name.end(), ' ', '-');
75  metrics->RegisterMetric(
76  new JvmMetric(Substitute("jvm.$0.max-usage-bytes", name), usage.name, MAX));
77  metrics->RegisterMetric(
78  new JvmMetric(Substitute("jvm.$0.current-usage-bytes", name),
79  usage.name, CURRENT));
80  metrics->RegisterMetric(
81  new JvmMetric(Substitute("jvm.$0.committed-usage-bytes", name), usage.name,
82  COMMITTED));
83  metrics->RegisterMetric(
84  new JvmMetric(Substitute("jvm.$0.init-usage-bytes", name), usage.name, INIT));
85  metrics->RegisterMetric(
86  new JvmMetric(Substitute("jvm.$0.peak-max-usage-bytes", name), usage.name,
87  PEAK_MAX));
88  metrics->RegisterMetric(
89  new JvmMetric(Substitute("jvm.$0.peak-current-usage-bytes", name),
90  usage.name, PEAK_CURRENT));
91  metrics->RegisterMetric(
92  new JvmMetric(Substitute("jvm.$0.peak-committed-usage-bytes", name), usage.name,
94  metrics->RegisterMetric(
95  new JvmMetric(Substitute("jvm.$0.peak-init-usage-bytes", name), usage.name,
96  PEAK_INIT));
97  }
98 
99  return Status::OK;
100 }
101 
103  TGetJvmMetricsRequest request;
104  request.get_all = false;
105  request.__set_memory_pool(mempool_name_);
106  TGetJvmMetricsResponse response;
107  if (!JniUtil::GetJvmMetrics(request, &response).ok()) return;
108  if (response.memory_pools.size() != 1) return;
109  TJvmMemoryPool& pool = response.memory_pools[0];
110  DCHECK(pool.name == mempool_name_);
111  switch (metric_type_) {
112  case MAX: value_ = pool.max;
113  return;
114  case INIT: value_ = pool.init;
115  return;
116  case CURRENT: value_ = pool.used;
117  return;
118  case COMMITTED: value_ = pool.committed;
119  return;
120  case PEAK_MAX: value_ = pool.peak_max;
121  return;
122  case PEAK_INIT: value_ = pool.peak_init;
123  return;
124  case PEAK_CURRENT: value_ = pool.peak_used;
125  return;
126  case PEAK_COMMITTED: value_ = pool.peak_committed;
127  return;
128  default: DCHECK(false) << "Unknown JvmMetricType: " << metric_type_;
129  }
130 }
JvmMetric(const std::string &key, const std::string &mempool_name, JvmMetricType type)
Private constructor to ensure only InitMetrics() can create JvmMetrics.
#define RETURN_IF_ERROR(stmt)
some generally useful macros
Definition: status.h:242
M * RegisterMetric(M *metric)
Definition: metrics.h:211
MetricGroup * GetChildGroup(const std::string &name)
Creates or returns an already existing child metric group.
Definition: metrics.cc:169
Status RegisterMemoryMetrics(MetricGroup *metrics, bool register_jvm_metrics)
MetricGroups may be organised hierarchically as a tree.
Definition: metrics.h:200
static TcmallocMetric * PAGEHEAP_UNMAPPED_BYTES
JvmMetricType metric_type_
static PhysicalBytesMetric * PHYSICAL_BYTES_RESERVED
static Status InitMetrics(MetricGroup *metrics)
static TcmallocMetric * PAGEHEAP_FREE_BYTES
virtual void CalculateValue()
std::string mempool_name_
The name of the memory pool, defined by the Jvm.
ObjectPool pool
Specialised metric which exposes numeric properties from tcmalloc.
static const Status OK
Definition: status.h:87
static TcmallocMetric * TOTAL_BYTES_RESERVED
static Status GetJvmMetrics(const TGetJvmMetricsRequest &request, TGetJvmMetricsResponse *result)
Definition: jni-util.cc:188
static TcmallocMetric * BYTES_IN_USE
Number of bytes allocated by tcmalloc, currently used by the application.
string name
Definition: cpu-info.cc:50
JvmMetricType
Each names one of the fields in TJvmMemoryPool.