linux_cpuload.hpp
Go to the documentation of this file.
1 /**
2  * @author: Daniel Fuchs
3  * @contact: fuxeysolutions@gmail.com
4  *
5  * distributed under the MIT License (MIT).
6  * Copyright (c) Daniel Fuchs
7  *
8  */
9 #pragma once
10 
11 #include <vector>
12 #include <string>
13 #include <tuple>
14 #include <map>
15 #include <unordered_map>
16 #include <chrono>
17 
18 class cpuLoad {
19 
20 public:
21  cpuLoad() = delete;
22  /**
23  * @brief constructor
24  * @param procFileName
25  */
26  explicit cpuLoad(std::string procFileName = "/proc/stat"):
27  procFile(std::move(procFileName)), cpuName("") {};
28 
29  /**
30  * @brief initialize the parsing algo
31  */
32  void initCpuUsage();
33  /**
34  * @brief get current cpu load in 0-100%
35  * @return current cpu load
36  */
37  double getCurrentCpuUsage();
38 
39  /**
40  * @brief get Cpu user / nice / system /idle time. used for cpu usage per process
41  * @return tuple<user,nice,system,idle>
42  */
43  std::tuple<uint64_t , uint64_t, uint64_t, uint64_t> getCpuTimes() {
44  auto cpuLoad_ = this->parseStatFile(this->procFile);
45  return std::make_tuple(cpuLoad_.at("cpu").at("user"),
46  cpuLoad_.at("cpu").at("nice"),
47  cpuLoad_.at("cpu").at("system"),
48  cpuLoad_.at("cpu").at("idle"));
49  }
50 
51  /**
52  * @brief get cpu Usage of all cores in percent
53  * @return vector of cpu load per core 0-100%
54  */
55  std::vector<double> getCurrentMultiCoreUsage();
56  /**
57  * @brief get CPU Description
58  * @param cpuNameFile - typical /proc/cpuinfo
59  * @return cpu Type string
60  */
61  std::string getCPUName(const std::string& cpuNameFile = "/proc/cpuinfo");
62 
63 private:
64  void calculateCpuUsage();
65  std::map<std::string, std::unordered_map<std::string, uint64_t>> parseStatFile(const std::string& fileName);
66  void upDateCPUUsage();
67  std::chrono::system_clock::time_point currentTime;
68  std::string procFile;
69  std::string cpuName;
70  std::map<std::string, double> cpuUsage;
71  std::map<std::string, std::unordered_map<std::string, uint64_t>> cpuLoadMap;
72  std::map<std::string, std::unordered_map<std::string, uint64_t>> oldCpuLoadMap;
73 };
74 
75 
cpuLoad::cpuLoad
cpuLoad()=delete
cpuLoad::getCPUName
std::string getCPUName(const std::string &cpuNameFile="/proc/cpuinfo")
get CPU Description
Definition: linux_cpuload.cpp:131
cpuLoad::getCurrentCpuUsage
double getCurrentCpuUsage()
get current cpu load in 0-100%
Definition: linux_cpuload.cpp:47
cpuLoad::getCpuTimes
std::tuple< uint64_t, uint64_t, uint64_t, uint64_t > getCpuTimes()
get Cpu user / nice / system /idle time.
Definition: linux_cpuload.hpp:43
cpuLoad::initCpuUsage
void initCpuUsage()
initialize the parsing algo
Definition: linux_cpuload.cpp:29
cpuLoad::cpuLoad
cpuLoad(std::string procFileName="/proc/stat")
constructor
Definition: linux_cpuload.hpp:26
std
Definition: Application.h:66
cpuLoad::getCurrentMultiCoreUsage
std::vector< double > getCurrentMultiCoreUsage()
get cpu Usage of all cores in percent
Definition: linux_cpuload.cpp:53
cpuLoad
Definition: linux_cpuload.hpp:18