This repository has been archived by the owner on Jun 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
linux_process_memory_measurement_node.cpp
93 lines (78 loc) · 2.62 KB
/
linux_process_memory_measurement_node.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "../../src/system_metrics_collector/linux_process_memory_measurement_node.hpp"
#include <sys/sysinfo.h>
#include <sys/types.h>
#include <chrono>
#include <cmath>
#include <fstream>
#include <sstream>
#include <string>
#include "rcutils/logging_macros.h"
namespace
{
constexpr const char kProc[] = "/proc/";
constexpr const char kStatm[] = "/statm";
constexpr const char kMetricName[] = "_memory_percent_used";
/**
* Return the total system memory.
*
* @return the total system memory in bytes
*/
double GetSystemTotalMemory()
{
// the following needs the struct keyword because sysinfo is also a function name
struct sysinfo si {};
const auto success = sysinfo(&si);
return success == -1 ? std::nan("") : static_cast<double>(si.totalram);
}
} // namespace
namespace system_metrics_collector
{
LinuxProcessMemoryMeasurementNode::LinuxProcessMemoryMeasurementNode(
const std::string & name,
const rclcpp::NodeOptions & options)
: PeriodicMeasurementNode(name, options),
pid_(std::to_string(GetPid())),
file_to_read_(kProc + std::to_string(GetPid()) + kStatm)
{
}
double LinuxProcessMemoryMeasurementNode::PeriodicMeasurement()
{
const auto statm_line = ReadFileToString(file_to_read_);
double p_mem;
try {
p_mem = static_cast<double>(GetProcessUsedMemory(statm_line));
} catch (const std::ifstream::failure & e) {
RCLCPP_ERROR(
this->get_logger(), "caught %s, failed to GetProcessUsedMemory from line %s",
e.what(), file_to_read_.c_str());
return std::nan("");
}
const auto total_mem = GetSystemTotalMemory();
return p_mem / total_mem * 100.0;
}
std::string LinuxProcessMemoryMeasurementNode::GetMetricName() const
{
return pid_ + kMetricName;
}
uint64_t GetProcessUsedMemory(const std::string & statm_process_file_contents)
{
std::istringstream ss{statm_process_file_contents};
ss.exceptions(std::ios::failbit | std::ios::badbit);
uint64_t process_memory_used;
ss >> process_memory_used;
return process_memory_used;
}
} // namespace system_metrics_collector