1 // Copyright 2023 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 "metrics.h"
16
17 #include <algorithm>
18 #include <cctype>
19 #include <cstring>
20
21 #include "pw_assert/check.h"
22 #include "pw_status/try.h"
23
24 namespace pw::fuzzer::examples {
25 namespace {
Hash(std::string_view str)26 Metric::Key Hash(std::string_view str) {
27 PW_CHECK(std::all_of(str.begin(), str.end(), isprint));
28 return static_cast<Metric::Key>(std::hash<std::string_view>{}(str));
29 }
30
31 template <typename T>
32 using IsCopyable =
33 std::enable_if_t<std::is_unsigned_v<T> && !std::is_same_v<T, bool>>;
34
35 template <typename T, typename = IsCopyable<T>>
CopyTo(pw::ByteSpan dst,size_t & offset,const T & src)36 Status CopyTo(pw::ByteSpan dst, size_t& offset, const T& src) {
37 size_t len = sizeof(src);
38 if (offset + len > dst.size()) {
39 return Status::ResourceExhausted();
40 }
41 memcpy(&dst[offset], &src, len);
42 offset += len;
43 return OkStatus();
44 }
45
46 template <typename T, typename = IsCopyable<T>>
CopyFrom(pw::ConstByteSpan src,size_t & offset,T & dst)47 Status CopyFrom(pw::ConstByteSpan src, size_t& offset, T& dst) {
48 size_t len = sizeof(dst);
49 if (offset + len > src.size()) {
50 return Status::ResourceExhausted();
51 }
52 memcpy(&dst, &src[offset], len);
53 offset += len;
54 return OkStatus();
55 }
56
57 } // namespace
58
Metric(std::string_view name_,Value value_)59 Metric::Metric(std::string_view name_, Value value_)
60 : name(name_), value(value_) {
61 key = Hash(name_);
62 }
63
GetValue(std::string_view name) const64 std::optional<Metric::Value> Metrics::GetValue(std::string_view name) const {
65 for (const auto& metric : metrics_) {
66 if (metric.name == name) {
67 return metric.value;
68 }
69 }
70 return std::optional<Metric::Value>();
71 }
72
SetValue(std::string_view name,Metric::Value value)73 Status Metrics::SetValue(std::string_view name, Metric::Value value) {
74 for (auto& metric : metrics_) {
75 if (metric.name == name) {
76 metric.value = value;
77 return OkStatus();
78 }
79 }
80 if (metrics_.full()) {
81 return Status::ResourceExhausted();
82 }
83 metrics_.emplace_back(name, value);
84 return OkStatus();
85 }
86
GetMetrics() const87 const Vector<Metric>& Metrics::GetMetrics() const { return metrics_; }
88
SetMetrics(const Vector<Metric> & metrics)89 Status Metrics::SetMetrics(const Vector<Metric>& metrics) {
90 if (metrics_.capacity() < metrics.size()) {
91 return Status::ResourceExhausted();
92 }
93 metrics_.assign(metrics.begin(), metrics.end());
94 return OkStatus();
95 }
96
Serialize(pw::ByteSpan buffer) const97 StatusWithSize Metrics::Serialize(pw::ByteSpan buffer) const {
98 size_t offset = 0;
99 PW_TRY_WITH_SIZE(CopyTo(buffer, offset, metrics_.size()));
100 for (const auto& metric : metrics_) {
101 PW_TRY_WITH_SIZE(CopyTo(buffer, offset, metric.key));
102 PW_TRY_WITH_SIZE(CopyTo(buffer, offset, metric.value));
103 }
104 return StatusWithSize(offset);
105 }
106
Deserialize(pw::ConstByteSpan buffer)107 Status Metrics::Deserialize(pw::ConstByteSpan buffer) {
108 size_t offset = 0;
109 size_t num_values = 0;
110 PW_TRY(CopyFrom(buffer, offset, num_values));
111 for (size_t i = 0; i < num_values; ++i) {
112 Metric::Key key;
113 PW_TRY(CopyFrom(buffer, offset, key));
114 Metric::Value value;
115 PW_TRY(CopyFrom(buffer, offset, value));
116 bool found = false;
117 for (auto& metric : metrics_) {
118 if (metric.key == key) {
119 metric.value = value;
120 found = true;
121 break;
122 }
123 }
124 if (!found) {
125 return Status::InvalidArgument();
126 }
127 }
128 return OkStatus();
129 }
130
131 } // namespace pw::fuzzer::examples
132