0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MetricsProcess.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; version 3
9  * of the License.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
27 
28 #include <Common/Compat.h>
29 
30 #include "MetricsProcess.h"
31 
32 #include <Common/Logger.h>
33 #include <Common/StatsSystem.h>
34 #include <Common/Time.h>
35 
36 using namespace Hypertable;
37 using namespace std;
38 
41  system_stats.refresh();
42  m_last_timestamp = get_ts64();
43  m_last_sys = system_stats.proc_stat.cpu_sys;
44  m_last_user = system_stats.proc_stat.cpu_user;
45 }
46 
47 void MetricsProcess::collect(int64_t now, MetricsCollector *collector) {
49 
50  system_stats.refresh();
51 
52  int64_t elapsed_millis = (now - m_last_timestamp) / 1000000LL;
53  int64_t diff_sys = (system_stats.proc_stat.cpu_sys - m_last_sys) / System::cpu_info().total_cores;
54  int64_t diff_user = (system_stats.proc_stat.cpu_user - m_last_user) / System::cpu_info().total_cores;
55  int32_t pct = 0;
56 
57  // CPU sys
58  if (elapsed_millis)
59  pct = (diff_sys * 100) / elapsed_millis;
60  collector->update("cpu.sys", pct);
61 
62  // CPU user
63  if (elapsed_millis)
64  pct = (diff_user * 100) / elapsed_millis;
65  collector->update("cpu.user", pct);
66 
67  // Virtual memory
68  collector->update("memory.virtual",
69  (float)system_stats.proc_stat.vm_size / 1024.0);
70 
71  // Resident memory
72  collector->update("memory.resident",
73  (float)system_stats.proc_stat.vm_resident / 1024.0);
74 
75  // Heap size
76  collector->update("memory.heap",
77  (float)system_stats.proc_stat.heap_size / 1000000000.0);
78 
79  // Heap slack bytes
80  collector->update("memory.heapSlack",
81  (float)system_stats.proc_stat.heap_slack / 1000000000.0);
82 
83  // Hypertable version
84  collector->update("version", version_string());
85 
86  m_last_timestamp = now;
87  m_last_sys = system_stats.proc_stat.cpu_sys;
88  m_last_user = system_stats.proc_stat.cpu_user;
89 
90 }
static const CpuInfo & cpu_info()
Retrieves updated CPU information (see SystemInfo.h)
Definition: SystemInfo.cc:322
STL namespace.
Collecting and (de)serializing system-wide statistics.
Abstract metrics collector.
Declarations for MetricsProcess.
Logging routines and macros.
void refresh()
Refreshes the statistics.
Definition: StatsSystem.cc:141
Compatibility Macros for C/C++.
Time related declarations.
struct ProcStat proc_stat
Process statistics (CPU user time, system time, vm size etc)
Definition: StatsSystem.h:124
Collects, serializes and deserializes system-wide statistics.
Definition: StatsSystem.h:43
Hypertable definitions
void collect(int64_t now, MetricsCollector *collector) override
Collects process metrics.
virtual void update(const std::string &name, const std::string &value)=0
Updates string metric value.
const char * version_string()
Definition: Version.cc:37
int64_t get_ts64()
Returns the current time in nanoseconds as a 64bit number.
Definition: Time.cc:40