MetroCollect  2.3.4
SourceProcMeminfo.h
Go to the documentation of this file.
1 //
2 // SourceProcMeminfo.h
3 //
4 // Created on August 23rd 2018
5 //
6 // Copyright 2018 CFM (www.cfm.fr)
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 //
20 
21 #pragma once
22 
23 #include <array>
24 #include <fstream>
25 #include <string>
26 #include <string_view>
27 #include <vector>
28 
29 #include "Source.h"
30 
31 using namespace std::literals;
32 
33 
38  class SourceProcMeminfo : public SourceBase {
39  protected:
40  static constexpr std::string_view filePath = "/proc/meminfo"sv;
41 
42  static constexpr std::string_view sourcePrefix = "memory"sv;
43  static constexpr std::array fieldUnitsAssociation = { KeyUnit{"huge_pages_"sv, ""sv} };
44  static constexpr std::string_view defaultUnit = "bytes"sv;
45 
46  std::ifstream file_;
47  std::vector<char> buffer_;
48  std::vector<std::string> fieldNames_;
49 
51  void parseFields();
52 
53  public:
57  static SourceProcMeminfo& get() {
58  static SourceProcMeminfo instance;
59  return instance;
60  }
61 
62  size_t fieldCount() const noexcept override final;
63  const std::vector<size_t> indexesOfFieldName(const FieldName& fieldName, Interests* interests = nullptr) const noexcept override final;
64  const std::string fieldNameSourcePrefix() const noexcept override final;
65  const FieldInfo fieldInfoAtIndex(size_t index) const noexcept override final;
66  const std::vector<FieldInfo> allFieldsInfo() const noexcept override final;
67 
68  void fetchData(const Interests& interests, DataArray::Iterator current) override final;
69  void computeDiff(const Interests& interests, DiffArray::Iterator diff, DataArray::ConstIterator current, DataArray::ConstIterator previous, double factor = 1) noexcept override final;
70  };
71 }
Object used to describe a field (or metric)
Definition: SourceField.h:37
Stuct to associate a field name containing a certain key with a unit.
Definition: SourceTools.h:78
std::vector< std::string > FieldName
Type used for field names (an array of strings)
Definition: SourceField.h:31
Namespace for sources of metrics objects and operations.
std::vector< DataValueType >::const_iterator ConstIterator
Const iterator type of MetricsDataArray.
Definition: MetricTypes.h:37
STL namespace.
std::vector< std::string > fieldNames_
Metrics names.
std::vector< char > buffer_
Buffer to put file contents into.
Boolean array to keep track of which fields a source has to fetch metrics for.
Sources of memory metrics extracted from the file /proc/meminfo.
Singleton abstract class, base class of all metric sources classes.
Definition: Source.h:42
std::vector< DataValueType >::iterator Iterator
Iterator type of MetricsDataArray.
Definition: MetricTypes.h:36