MetroCollect  2.3.4
SourceProcStat.h
Go to the documentation of this file.
1 //
2 // SourceProcStat.h
3 //
4 // Created on July 13th 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 SourceProcStat : public SourceBase {
39  protected:
40  static constexpr std::string_view filePath = "/proc/stat"sv;
41 
42  static constexpr std::string_view sourcePrefix = "cpu"sv;
43  static constexpr std::string_view fieldNameOtherPrefix = "other"sv;
44  static constexpr std::string_view fieldNameAllCoresPrefix = "all"sv;
45  static constexpr std::string_view fieldNameCorePrefix = "core"sv;
46  static constexpr std::string_view fieldNameCoreDescription = "cpuID"sv;
47  static constexpr std::string_view fieldNameCoreAll = "*"sv;
48  static constexpr std::string_view cpuField = "cpu"sv;
49  static constexpr size_t cpuFieldCount = 10;
50  static constexpr std::array<std::string_view, cpuFieldCount> cpuFieldNames = {"user"sv, "nice"sv, "system"sv, "idle"sv, "iowait"sv, "irq"sv, "softirq"sv, "steal"sv, "guest"sv, "guestNice"sv};
51  static constexpr std::string_view percentUnit = "%"sv;
52  static constexpr std::string_view defaultUnit = ""sv;
53 
54  size_t cpuCount_;
55  std::vector<std::string> otherFieldNames_;
56  std::ifstream file_;
57  std::vector<char> buffer_;
58 
59  SourceProcStat();
60  void parseFields();
61 
62  public:
66  static SourceProcStat& get() {
67  static SourceProcStat instance;
68  return instance;
69  }
70 
71  size_t fieldCount() const noexcept override final;
72  const std::vector<size_t> indexesOfFieldName(const FieldName& fieldName, Interests* interests = nullptr) const noexcept override final;
73  const std::string fieldNameSourcePrefix() const noexcept override final;
74  const FieldInfo fieldInfoAtIndex(size_t index) const noexcept override final;
75  const std::vector<FieldInfo> allFieldsInfo() const noexcept override final;
76 
77  void fetchData(const Interests& interests, DataArray::Iterator current) override final;
78  void computeDiff(const Interests& interests, DiffArray::Iterator diff, DataArray::ConstIterator current, DataArray::ConstIterator previous, double factor = 1) noexcept override final;
79  };
80 }
Object used to describe a field (or metric)
Definition: SourceField.h:37
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.
Sources of CPU metrics extracted from the file /proc/stat.
Boolean array to keep track of which fields a source has to fetch metrics for.
std::vector< char > buffer_
Buffer to put file contents into.
Singleton abstract class, base class of all metric sources classes.
Definition: Source.h:42
std::vector< std::string > otherFieldNames_
Names of other CPU metrics.
std::vector< DataValueType >::iterator Iterator
Iterator type of MetricsDataArray.
Definition: MetricTypes.h:36
std::ifstream file_
File descriptor.