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 
17 #include "metricblob.pb.h"
18 
19 #include <unistd.h>
20 
21 #include <blobs-ipmid/blobs.hpp>
22 
23 #include <atomic>
24 #include <cstdint>
25 #include <string>
26 #include <string_view>
27 #include <unordered_map>
28 #include <vector>
29 
30 namespace metric_blob
31 {
32 
33 class BmcHealthSnapshot
34 {
35   public:
36     BmcHealthSnapshot();
37 
38     /**
39      * Reads data from this metric
40      * @param offset: offset into the data to read
41      * @param requestedSize: how many bytes to read
42      * @returns Bytes able to read. Returns empty if nothing can be read.
43      */
44     std::string_view read(uint32_t offset, uint32_t requestedSize);
45 
46     /**
47      * Returns information about the amount of readable data and whether the
48      * metric has finished populating.
49      * @param meta: Struct to fill with the metadata info
50      */
51     bool stat(blobs::BlobMeta& meta);
52 
53     /**
54      * Start the metric collection process
55      */
56     void doWork();
57 
58     /**
59      * The size of the content string.
60      */
61     uint32_t size();
62 
63   private:
64     /**
65      * Serialize to the pb_dump_ array.
66      */
67     void serializeSnapshotToArray(
68         const bmcmetrics::metricproto::BmcMetricSnapshot& snapshot);
69 
70     // The two following functions access the snapshot's string table so they
71     // have to be member functions.
72     bmcmetrics::metricproto::BmcProcStatMetric getProcStatList();
73     bmcmetrics::metricproto::BmcFdStatMetric getFdStatList();
74 
75     int getStringID(const std::string_view s);
76     std::atomic<bool> done;
77     std::vector<char> pbDump;
78     std::unordered_map<std::string, int> stringTable;
79     int stringId;
80     long ticksPerSec;
81 };
82 
83 } // namespace metric_blob
84