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 #pragma once 15 16 #include <cstddef> 17 #include <cstdint> 18 #include <functional> 19 #include <optional> 20 #include <span> 21 #include <string_view> 22 23 #include "pw_bytes/span.h" 24 #include "pw_containers/vector.h" 25 #include "pw_status/status.h" 26 #include "pw_status/status_with_size.h" 27 #include "pw_string/string.h" 28 29 namespace pw::fuzzer::examples { 30 31 // DOCSTAG: [pwfuzzer_examples_fuzztest-metrics_h] 32 // Represent a named value. In order to transmit these values efficiently, they 33 // can be referenced by fixed length, generated keys instead of names. 34 struct Metric { 35 using Key = uint16_t; 36 using Value = uint32_t; 37 38 static constexpr size_t kMaxNameLen = 32; 39 40 Metric() = default; 41 Metric(std::string_view name_, Value value_); 42 43 InlineString<kMaxNameLen> name; 44 Key key = 0; 45 Value value = 0; 46 }; 47 48 // Represents a set of measurements from a particular source. 49 // 50 // In order to transmit metrics efficiently, the names of metrics are hashed 51 // internally into fixed length keys. The names can be shared once via `GetKeys` 52 // and `SetKeys`, after which metrics can be efficiently shared via `Serialize` 53 // and `Deserialize`. 54 class Metrics { 55 public: 56 static constexpr size_t kMaxMetrics = 32; 57 static constexpr size_t kMaxSerializedSize = 58 sizeof(size_t) + 59 kMaxMetrics * (sizeof(Metric::Key) + sizeof(Metric::Value)); 60 61 // Retrieves the value of a named metric and stores it in `out_value`. The 62 // name must consist of printable ASCII characters. Returns false if the named 63 // metric was not `Set` or `Import`ed. 64 std::optional<Metric::Value> GetValue(std::string_view name) const; 65 66 // Sets the value of a named metric. The name must consist of printable ASCII 67 // characters, and will be added to the mapping of names to keys. 68 Status SetValue(std::string_view name, Metric::Value value); 69 70 // Returns the current mapping of names to keys. 71 const Vector<Metric>& GetMetrics() const; 72 73 // Replaces the current mapping of names to keys. 74 Status SetMetrics(const Vector<Metric>& metrics); 75 76 // Serializes this object to the given `buffer`. Does not write more bytes 77 // than `buffer.size()`. Returns the number of number of bytes written or an 78 // error if insufficient space. 79 StatusWithSize Serialize(pw::ByteSpan buffer) const; 80 81 // Populates this object from the data in the given `buffer`. 82 // Returns whether this buffer could be deserialized. 83 Status Deserialize(pw::ConstByteSpan buffer); 84 85 private: 86 Vector<Metric, kMaxMetrics> metrics_; 87 }; 88 // DOCSTAG: [pwfuzzer_examples_fuzztest-metrics_h] 89 90 } // namespace pw::fuzzer::examples 91