Zigbee Protocol Controller 1.6.0
zigpc_mem_usage_metric.hpp
Go to the documentation of this file.
1/******************************************************************************
2 * # License
3 * <b>Copyright 2021 Silicon Laboratories Inc. www.silabs.com</b>
4 ******************************************************************************
5 * The licensor of this software is Silicon Laboratories Inc. Your use of this
6 * software is governed by the terms of Silicon Labs Master Software License
7 * Agreement (MSLA) available at
8 * www.silabs.com/about-us/legal/master-software-license-agreement. This
9 * software is distributed to you in Source Code format and is governed by the
10 * sections of the MSLA applicable to Source Code.
11 *
12 *****************************************************************************/
13
14#include <fstream>
15#include <iomanip>
16#include <iostream>
17#include <unistd.h>
18
20
22{
23static constexpr char LOG_TAG[] = "zigpc_metric_ram_usage";
24
25static constexpr char PATH_PROC_MEMINFO[] = "/proc/meminfo";
26static constexpr char PATH_PROC_STATM[] = "/proc/self/statm";
27
33static long int get_pagesize_kb(void)
34{
35 long int page_size = sysconf(_SC_PAGESIZE);
36 if (page_size == -1) {
37 sl_log_error(LOG_TAG, "Failed to get pagesize");
38 return page_size;
39 }
40
41 page_size /= 1024UL;
42
43 return page_size;
44}
45
51static unsigned long get_mem_total(void)
52{
53 unsigned long mem_total_kb = 0UL;
54
55 std::ifstream meminfo_f(PATH_PROC_MEMINFO, std::ifstream::in);
56 if (!meminfo_f.is_open()) {
57 sl_log_error(LOG_TAG, "Failed to read %s", PATH_PROC_MEMINFO);
58 return mem_total_kb;
59 }
60
61 std::string meminfo_field;
62 meminfo_f >> meminfo_field;
63 if (meminfo_field != "MemTotal:") {
64 sl_log_error(LOG_TAG, "Failed to find MemTotal from %s", PATH_PROC_MEMINFO);
65 return mem_total_kb;
66 }
67
68 meminfo_f >> mem_total_kb;
69 if (meminfo_f.fail()) {
70 sl_log_error(LOG_TAG, "Failed to read MemTotal from %s", PATH_PROC_MEMINFO);
71 return mem_total_kb;
72 }
73
74 return mem_total_kb;
75}
76
83static unsigned long get_resident_set_kb(void)
84{
85 unsigned long rss_kb = 0UL;
86
87 std::ifstream statm_file(PATH_PROC_STATM, std::ifstream::in);
88 if (!statm_file.is_open()) {
89 sl_log_error(LOG_TAG, "Failed to read %s", PATH_PROC_STATM);
90 return rss_kb;
91 }
92
93 unsigned long stat_m_field;
94 statm_file >> stat_m_field; // total_program_size: Ignore
95 statm_file >> stat_m_field; // resident_set_size: Used
96
97 if (statm_file.fail()) {
99 "Failed to read resident set from %s",
101 return rss_kb;
102 }
103
104 rss_kb = stat_m_field;
105
106 return rss_kb;
107}
108
109} // namespace zigpc_diagnostics::util
110
112{
113 private:
114 static constexpr char LOG_TAG[] = "zigpc_metric_ram_usage";
115
118 float mem_usage = 0.f;
119
120 public:
122 std::string const &metric_id) :
124 {}
125
131 float get_value(void) const
132 {
133 return mem_usage;
134 }
135
140 void update_value(void) final
141 {
142 if ((page_size_kb == -1) || (total_mem_kb == 0)) {
143 sl_log_error(LOG_TAG, "Failed to get valid page_size and total memory");
144 return;
145 }
146
147 unsigned long resident_set_kb
149 if (resident_set_kb == 0UL) {
150 sl_log_error(LOG_TAG, "Failed to get valid resident_set");
151 return;
152 }
153
154 // RAM Usage representative of what's seen from htop
156 = (static_cast<float>(resident_set_kb * page_size_kb) / total_mem_kb)
157 * 100.f;
158
159 // Format value representation
160 std::stringstream ss;
161 ss << std::fixed << std::setprecision(1) << mem_usage;
162 serialized_value = std::move(ss.str());
163
164 // Update Manager on value update complete
166 }
167};
Abstract top level metric from which all diagnostic metric derive from.
Definition: zigpc_diagnostics_metric.hpp:25
zigpc_diagnostics_notification & on_update_complete
Definition: zigpc_diagnostics_metric.hpp:28
std::string serialized_value
Definition: zigpc_diagnostics_metric.hpp:30
std::string metric_id
Definition: zigpc_diagnostics_metric.hpp:29
Definition: zigpc_diagnostics_notification.hpp:20
virtual void notify(std::string metric_id)
Definition: zigpc_diagnostics_notification.hpp:22
Definition: zigpc_mem_usage_metric.hpp:112
void update_value(void) final
Update the memory usage by process.
Definition: zigpc_mem_usage_metric.hpp:140
static constexpr char LOG_TAG[]
Definition: zigpc_mem_usage_metric.hpp:114
float get_value(void) const
Get the memory usage.
Definition: zigpc_mem_usage_metric.hpp:131
zigpc_mem_usage_metric(zigpc_diagnostics_notification &notif, std::string const &metric_id)
Definition: zigpc_mem_usage_metric.hpp:121
long int page_size_kb
Definition: zigpc_mem_usage_metric.hpp:116
float mem_usage
Definition: zigpc_mem_usage_metric.hpp:118
unsigned long total_mem_kb
Definition: zigpc_mem_usage_metric.hpp:117
#define sl_log_error(tag, fmtstr,...)
Definition: sl_log.h:145
Definition: zigpc_mem_usage_metric.hpp:22
static unsigned long get_mem_total(void)
Get system total memory usage in KiloBytes.
Definition: zigpc_mem_usage_metric.hpp:51
static constexpr char PATH_PROC_STATM[]
Definition: zigpc_mem_usage_metric.hpp:26
static constexpr char PATH_PROC_MEMINFO[]
Definition: zigpc_mem_usage_metric.hpp:25
static unsigned long get_resident_set_kb(void)
Get the resident memory pages used by program. This number is in KiloBytes.
Definition: zigpc_mem_usage_metric.hpp:83
static long int get_pagesize_kb(void)
Get system page size in KiloBytes.
Definition: zigpc_mem_usage_metric.hpp:33
static constexpr char LOG_TAG[]
Definition: zigpc_mem_usage_metric.hpp:23