1 // Copyright 2021 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://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,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 #include <blobs-ipmid/blobs.hpp>
17 
18 #include <atomic>
19 #include <cstdint>
20 #include <string>
21 #include <string_view>
22 #include <unordered_map>
23 #include <vector>
24 
25 namespace metric_blob
26 {
27 
28 class BmcHealthSnapshot
29 {
30   public:
31     BmcHealthSnapshot();
32 
33     /**
34      * Reads data from this metric
35      * @param offset: offset into the data to read
36      * @param requestedSize: how many bytes to read
37      * @returns Bytes able to read. Returns empty if nothing can be read.
38      */
39     std::string_view read(uint32_t offset, uint32_t requestedSize);
40 
41     /**
42      * Returns information about the amount of readable data and whether the
43      * metric has finished populating.
44      * @param meta: Struct to fill with the metadata info
45      */
46     bool stat(blobs::BlobMeta& meta);
47 
48     /**
49      * Start the metric collection process
50      */
51     void doWork();
52 
53     /**
54      * The size of the content string.
55      */
56     uint32_t size();
57 
58     /**
59      * Returns the ID of the provided string
60      */
61     int getStringID(const std::string_view s);
62 
63   private:
64     std::atomic<bool> done;
65     std::vector<char> pbDump;
66     std::unordered_map<std::string, int> stringTable;
67     int stringId;
68     long ticksPerSec;
69 };
70 
71 } // namespace metric_blob
72