• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "pw_metric/metric.h"
16 
17 #include <array>
18 
19 #include "pw_assert/check.h"
20 #include "pw_log/log.h"
21 #include "pw_preprocessor/compiler.h"
22 #include "pw_span/span.h"
23 #include "pw_tokenizer/base64.h"
24 
25 namespace pw::metric {
26 namespace {
27 
28 template <typename T>
AsSpan(const T & t)29 span<const std::byte> AsSpan(const T& t) {
30   return span<const std::byte>(reinterpret_cast<const std::byte*>(&t),
31                                sizeof(t));
32 }
33 
34 // A convenience class to encode a token as base64 while managing the storage.
35 // TODO(keir): Consider putting this into upstream pw_tokenizer.
36 struct Base64EncodedToken {
Base64EncodedTokenpw::metric::__anonc09a2caa0111::Base64EncodedToken37   Base64EncodedToken(Token token) {
38     size_t encoded_size = tokenizer::PrefixedBase64Encode(AsSpan(token), data);
39     data[encoded_size] = 0;
40   }
41 
valuepw::metric::__anonc09a2caa0111::Base64EncodedToken42   const char* value() { return data.data(); }
43   std::array<char, 16> data;
44 };
45 
Indent(int level)46 const char* Indent(int level) {
47   static const char* kWhitespace8 = "        ";
48   level = std::min(level, 4);
49   return kWhitespace8 + 8 - 2 * level;
50 }
51 
52 }  // namespace
53 
54 // Enable easier registration when used as a member.
Metric(Token name,float value,IntrusiveList<Metric> & metrics)55 Metric::Metric(Token name, float value, IntrusiveList<Metric>& metrics)
56     : Metric(name, value) {
57   metrics.push_front(*this);
58 }
Metric(Token name,uint32_t value,IntrusiveList<Metric> & metrics)59 Metric::Metric(Token name, uint32_t value, IntrusiveList<Metric>& metrics)
60     : Metric(name, value) {
61   metrics.push_front(*this);
62 }
63 
as_float() const64 float Metric::as_float() const {
65   PW_DCHECK(is_float());
66   return float_;
67 }
68 
as_int() const69 uint32_t Metric::as_int() const {
70   PW_DCHECK(is_int());
71   return uint_;
72 }
73 
Increment(uint32_t amount)74 void Metric::Increment(uint32_t amount) {
75   PW_DCHECK(is_int());
76   if (PW_ADD_OVERFLOW(uint_, amount, &uint_)) {
77     uint_ = std::numeric_limits<uint32_t>::max();
78   }
79 }
80 
Decrement(uint32_t amount)81 void Metric::Decrement(uint32_t amount) {
82   PW_DCHECK(is_int());
83   if (PW_SUB_OVERFLOW(uint_, amount, &uint_)) {
84     uint_ = 0;
85   }
86 }
87 
SetInt(uint32_t value)88 void Metric::SetInt(uint32_t value) {
89   PW_DCHECK(is_int());
90   uint_ = value;
91 }
92 
SetFloat(float value)93 void Metric::SetFloat(float value) {
94   PW_DCHECK(is_float());
95   float_ = value;
96 }
97 
Dump(int level)98 void Metric::Dump(int level) {
99   Base64EncodedToken encoded_name(name());
100   const char* indent = Indent(level);
101   if (is_float()) {
102     // Variadic macros promote float to double. Explicitly cast here to
103     // acknowledge this and allow projects to use -Wdouble-promotion.
104     PW_LOG_INFO("%s \"%s\": %f,",
105                 indent,
106                 encoded_name.value(),
107                 static_cast<double>(as_float()));
108   } else {
109     PW_LOG_INFO("%s \"%s\": %u,",
110                 indent,
111                 encoded_name.value(),
112                 static_cast<unsigned int>(as_int()));
113   }
114 }
115 
Dump(IntrusiveList<Metric> & metrics,int level)116 void Metric::Dump(IntrusiveList<Metric>& metrics, int level) {
117   for (auto& m : metrics) {
118     m.Dump(level);
119   }
120 }
121 
Group(Token name,IntrusiveList<Group> & groups)122 Group::Group(Token name, IntrusiveList<Group>& groups) : name_(name) {
123   groups.push_front(*this);
124 }
125 
Dump(int level)126 void Group::Dump(int level) {
127   Base64EncodedToken encoded_name(name());
128   const char* indent = Indent(level);
129   PW_LOG_INFO("%s \"%s\": {", indent, encoded_name.value());
130   Group::Dump(children(), level + 1);
131   Metric::Dump(metrics(), level + 1);
132   PW_LOG_INFO("%s }", indent);
133 }
134 
Dump(IntrusiveList<Group> & groups,int level)135 void Group::Dump(IntrusiveList<Group>& groups, int level) {
136   for (auto& group : groups) {
137     group.Dump(level);
138   }
139 }
140 
141 }  // namespace pw::metric
142