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
/
utilities.cpp
164 lines (135 loc) · 4.72 KB
/
utilities.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// 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 <unistd.h>
#include <cmath>
#include <fstream>
#include <sstream>
#include <streambuf>
#include <string>
#include "utilities.hpp"
#include "rcutils/logging_macros.h"
namespace system_metrics_collector
{
namespace
{
constexpr const char CPU_LABEL[] = "cpu";
constexpr const char MEM_TOTAL[] = "MemTotal:";
constexpr const char MEM_AVAILABLE[] = "MemAvailable:";
constexpr const char EMPTY_FILE[] = "";
constexpr const int INVALID_MEMORY_SAMPLE = -1;
double computeCpuTotalTime(const ProcCpuData measurement1, const ProcCpuData measurement2)
{
const double total_time = (measurement2.getIdleTime() + measurement2.getActiveTime()) -
(measurement1.getIdleTime() + measurement1.getActiveTime());
return total_time;
}
} // namespace
std::string readFileToString(const std::string & file_name)
{
std::ifstream file_to_read(file_name);
if (!file_to_read.good()) {
RCUTILS_LOG_ERROR_NAMED("readFileToString", "unable to parse file: %s", file_name.c_str());
return EMPTY_FILE;
}
std::string to_return((std::istreambuf_iterator<char>(file_to_read)),
std::istreambuf_iterator<char>());
return to_return;
}
ProcCpuData processStatCpuLine(const std::string & stat_cpu_line)
{
ProcCpuData parsed_data;
if (!stat_cpu_line.empty()) {
if (!stat_cpu_line.compare(0, strlen(CPU_LABEL), CPU_LABEL)) {
std::istringstream ss(stat_cpu_line);
if (!ss.good()) {
return ProcCpuData();
}
ss >> parsed_data.cpu_label;
for (int i = 0;
i < static_cast<int>(ProcCpuStates::kNumProcCpuStates); ++i)
{
if (!ss.good()) {
return ProcCpuData();
}
ss >> parsed_data.times[i];
}
return parsed_data;
}
}
return parsed_data;
}
double computeCpuActivePercentage(
const ProcCpuData & measurement1,
const ProcCpuData & measurement2)
{
if (measurement1.isMeasurementEmpty() || measurement2.isMeasurementEmpty()) {
RCUTILS_LOG_ERROR_NAMED("computeCpuActivePercentage",
"a measurement was empty, unable to compute cpu percentage");
return std::nan("");
}
const double active_time = measurement2.getActiveTime() - measurement1.getActiveTime();
const double total_time = computeCpuTotalTime(measurement1, measurement2);
return 100.0 * active_time / total_time;
}
double processMemInfoLines(const std::string & lines)
{
std::istringstream process_lines_stream(lines);
if (!process_lines_stream.good()) {
RCUTILS_LOG_ERROR("unable to parse input lines");
return std::nan("");
}
std::string line;
int total = INVALID_MEMORY_SAMPLE;
int available = INVALID_MEMORY_SAMPLE;
std::istringstream parse_line(""); // parse each line from the input
std::string tlabel;
while (std::getline(process_lines_stream, line) && process_lines_stream.good()) {
parse_line.str(line);
if (!line.compare(0, strlen(MEM_TOTAL), MEM_TOTAL)) {
parse_line >> tlabel;
if (!parse_line.good()) {
RCUTILS_LOG_ERROR_NAMED("processMemInfoLines", "unable to parse %s label", MEM_TOTAL);
return std::nan("");
}
parse_line >> total;
if (!parse_line.good()) {
RCUTILS_LOG_ERROR_NAMED("processMemInfoLines", "unable to parse %s value", MEM_TOTAL);
return std::nan("");
}
} else if (!line.compare(0, strlen(MEM_AVAILABLE), MEM_AVAILABLE)) {
std::string tlabel;
parse_line >> tlabel;
if (!parse_line.good()) {
RCUTILS_LOG_ERROR_NAMED("processMemInfoLines", "unable to parse %s label", MEM_AVAILABLE);
return std::nan("");
}
parse_line >> available;
if (!parse_line.good()) {
RCUTILS_LOG_ERROR_NAMED("processMemInfoLines", "unable to parse %s value", MEM_AVAILABLE);
return std::nan("");
}
break; // no need to parse other lines after this label
}
parse_line.clear();
}
const double to_return = static_cast<double>(total - available) / static_cast<double>(total) *
100.0;
return total == INVALID_MEMORY_SAMPLE || available == INVALID_MEMORY_SAMPLE ?
std::nan("") : to_return;
}
int getPid()
{
return getpid();
}
} // namespace system_metrics_collector