CpuStat.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
17 #include "CpuStat.hh"
18 
19 // Custom includes
20 #include <fstream>
22 #include "Console/Sysdir.hh"
23 #include "Console/ParsedCommand.hh"
24 
25 #define prefix_
26 //-/////////////////////////////////////////////////////////////////////////////////////////////////
27 
29  unsigned system_, unsigned nice_, unsigned idle_, unsigned iowait_,
30  unsigned irq_, unsigned softirq_, unsigned steal_, unsigned guest_)
31  : duration(duration_), user(user_), system(system_), nice(nice_), idle(idle_),
32  iowait(iowait_), irq(irq_), softirq(softirq_), steal(steal_), guest(guest_)
33 {
34 }
35 
37 {
38  std::ifstream statfd ("/proc/stat");
39 
40  CpuStatProbs current;
41 
42  std::string prefix;
43  statfd >> prefix;
44 
45  if (prefix.compare("cpu") != 0)
46  return CpuStat();
47 
49  boost::long_long_type x;
50  for (unsigned i=0; statfd.good() && statfd >> x; ++i) {
51  current.push_back( x);
52  }
53 
54  if (probs.size() == 0 || time == ClockService::seconds(0)) {
55  probs = current;
56  time = now;
57  return CpuStat();
58  }
59 
60  boost::long_long_type sum = 0;
61  CpuStatProbs::iterator c = current.begin();
62  CpuStatProbs::iterator p = probs.begin();
63  for (; c != current.end() && p != probs.end(); ++c, ++p) {
64  sum += *c - *p;
65  }
66 
67  if (sum <= 0) {
68  time = now;
69  return CpuStat();
70  }
71 
72  CpuStat cs (now - time,
73  100 * (current[0] - probs[0]) / sum,
74  100 * (current[1] - probs[1]) / sum,
75  100 * (current[2] - probs[2]) / sum,
76  100 * (current[3] - probs[3]) / sum,
77  100 * (current[4] - probs[4]) / sum,
78  100 * (current[5] - probs[5]) / sum,
79  100 * (current[6] - probs[6]) / sum,
80  100 * (current[7] - probs[7]) / sum,
81  100 * (current[8] - probs[8]) / sum);
82 
83  probs = current;
84  time = now;
85 
86  return cs;
87 }
88 
90 {
91  namespace fty = senf::console::factory;
92 
93  console::sysdir().add("cpustat", fty::Command( &CpuStatConsole::dump, this))
94  .doc("Returns the CPU usage based on /proc/stat since last call of this function");
95 }
96 
97 prefix_ void senf::CpuStatConsole::dump(std::ostream & os)
98 {
99  os << procStats_.cpuStat();
100 }
101 
102 
103 prefix_ std::ostream & senf::operator<<(std::ostream & os, CpuStat const & cs)
104 {
105  boost::format fmtStat ("user=%3.0d%% sys=%3.0d%% nice=%3.0d%% idle=%3.0d%% io=%3.0d%% irq=%3.0d%% sirq=%3.0d%% steal=%3.0d%% guest=%3.0d%% duration=%dms ");
106  os << fmtStat % cs.user % cs.system % cs.nice % cs.idle % cs.iowait % cs.irq % cs.softirq % cs.steal % cs.guest % ClockService::in_milliseconds(cs.duration) << std::endl;
107  return os;
108 }
109 
110 //-/////////////////////////////////////////////////////////////////////////////////////////////////
111 #undef prefix_
112 
113 // Local Variables:
114 // mode: c++
115 // fill-column: 100
116 // c-file-style: "senf"
117 // indent-tabs-mode: nil
118 // ispell-local-dictionary: "american"
119 // compile-command: "scons -u test"
120 // comment-column: 40
121 // End:
config::time_type clock_type
unsigned idle
Definition: CpuStat.hh:34
static SENF_CLOCKSERVICE_CONSTEXPR int64_type in_milliseconds(clock_type const &v)
unsigned steal
Definition: CpuStat.hh:38
static SENF_CLOCKSERVICE_CONSTEXPR clock_type seconds(int64_type const &v)
unsigned system
Definition: CpuStat.hh:32
CPU Stat public header.
ClockService::clock_type const & now()
void dump(std::ostream &os)
Definition: CpuStat.cc:97
unsigned softirq
Definition: CpuStat.hh:37
std::ostream & operator<<(std::ostream &os, CpuStat const &cs)
Definition: CpuStat.cc:103
#define prefix_
Definition: CpuStat.cc:25
unsigned user
Definition: CpuStat.hh:31
unsigned iowait
Definition: CpuStat.hh:35
CpuStat(ClockService::clock_type duration=ClockService::milliseconds(0), unsigned user=0, unsigned system=0, unsigned nice=0, unsigned idle=0, unsigned iowait=0, unsigned irq=0, unsigned softirq=0, unsigned steal=0, unsigned guest=0)
Definition: CpuStat.cc:28
unsigned guest
Definition: CpuStat.hh:39
ClockService::clock_type duration
Definition: CpuStat.hh:30
static clock_type now()
CpuStat cpuStat()
Definition: CpuStat.cc:36
unsigned irq
Definition: CpuStat.hh:36
std::int32_t sum
unsigned nice
Definition: CpuStat.hh:33