Zigbee Protocol Controller 1.6.0
zigpc_cpu_load_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 <array>
15#include <iomanip>
16#include <string>
17#include <sstream>
18
19// System headers
20#include <sys/types.h>
21#include <sys/sysinfo.h>
22
23// Unify includes
24#include <sl_status.h>
25
26// Component includes
28
30{
31 public:
36 enum class load_type : uint8_t { AVG_1_MIN, AVG_5_MIN, AVG_15_MIN };
37
38 private:
39 static constexpr char LOG_TAG[] = "diagnostics_metric_cpu_load";
40 static constexpr int LOAD_COUNT = 3U;
41 static constexpr std::array<std::string_view, LOAD_COUNT> LOAD_LABELS
42 = {"1Min", "5Min", "15Min"};
43
44 std::array<double, LOAD_COUNT> load_list = {};
45
46 public:
48 std::string metric_id) :
50 {}
51
52 double get_value(load_type type) const
53 {
54 switch (type) {
56 return load_list[0U];
58 return load_list[1U];
60 return load_list[2U];
61 default:
62 return -1.0;
63 }
64 }
65
67 {
68 int err = getloadavg(load_list.data(), LOAD_COUNT);
69 if (err < LOAD_COUNT) {
71 "Failed to retrieve sysinfo. err: %d, errono: %d",
72 err,
73 errno);
74 return;
75 }
76
77 std::stringstream ss;
78 ss << "{";
79 char sep = ' ';
80 for (size_t i = 0U; i < LOAD_COUNT; i++) {
81 ss << sep << '"' << LOAD_LABELS[i] << "\":";
82 ss << std::fixed << std::setprecision(2) << load_list[i];
83 sep = ',';
84 }
85 ss << "}";
86
87 serialized_value = std::move(ss.str());
88
90 }
91};
Definition: zigpc_cpu_load_metric.hpp:30
static constexpr int LOAD_COUNT
Definition: zigpc_cpu_load_metric.hpp:40
std::array< double, LOAD_COUNT > load_list
Definition: zigpc_cpu_load_metric.hpp:44
zigpc_cpu_load_metric(zigpc_diagnostics_notification &notif, std::string metric_id)
Definition: zigpc_cpu_load_metric.hpp:47
load_type
CPU Load average types.
Definition: zigpc_cpu_load_metric.hpp:36
static constexpr std::array< std::string_view, LOAD_COUNT > LOAD_LABELS
Definition: zigpc_cpu_load_metric.hpp:42
double get_value(load_type type) const
Definition: zigpc_cpu_load_metric.hpp:52
static constexpr char LOG_TAG[]
Definition: zigpc_cpu_load_metric.hpp:39
void update_value()
Definition: zigpc_cpu_load_metric.hpp:66
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
#define sl_log_error(tag, fmtstr,...)
Definition: sl_log.h:145
SL Status Codes.