OpenSDN source code
cpuinfo.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include "sys/times.h"
7 #include <cstdlib>
8 #include <base/cpuinfo.h>
9 
10 #include <fstream>
11 #include <iostream>
12 
13 #include <boost/algorithm/string/find.hpp>
14 #include <boost/algorithm/string/find_iterator.hpp>
15 #include <boost/algorithm/string/split.hpp>
16 
17 using namespace boost;
18 
19 uint32_t NumCpus() {
20  static uint32_t count = 0;
21 
22  if (count != 0) {
23  return count;
24  }
25 
26  std::ifstream file("/proc/cpuinfo");
27  std::string content((std::istreambuf_iterator<char>(file)),
28  std::istreambuf_iterator<char>());
29  // Create a find_iterator
30  typedef find_iterator<std::string::iterator> string_find_iterator;
31 
32  for (string_find_iterator it =
33  make_find_iterator(content, first_finder("model name", is_iequal()));
34  it != string_find_iterator(); ++it, count++);
35  return count;
36 }
37 
38 void LoadAvg(CpuLoad &load) {
39  double averages[3];
40  uint32_t num_cpus = NumCpus();
41  getloadavg(averages, 3);
42  if (num_cpus > 0) {
43  load.one_min_avg = averages[0]/num_cpus;
44  load.five_min_avg = averages[1]/num_cpus;
45  load.fifteen_min_avg = averages[2]/num_cpus;
46  }
47 }
48 
49 
50 
52  std::ifstream file("/proc/self/status");
53  bool vmsize = false;
54  bool peak = false;
55  bool rss = false;
56  std::string line;
57  while (std::getline(file, line)) {
58  if (line.find("VmSize") != std::string::npos) {
59  std::stringstream vm(line);
60  std::string tmp; vm >> tmp; vm >> info.virt;
61  vmsize = true;
62  }
63  if (line.find("VmRSS") != std::string::npos) {
64  std::stringstream vm(line);
65  std::string tmp; vm >> tmp; vm >> info.res;
66  rss = true;
67  }
68  if (line.find("VmPeak") != std::string::npos) {
69  std::stringstream vm(line);
70  std::string tmp; vm >> tmp; vm >> info.peakvirt;
71  peak = true;
72  }
73  if (rss && vmsize && peak)
74  break;
75  }
76 }
77 
79  std::ifstream file("/proc/meminfo");
80  std::string tmp;
81  // MemTotal: 132010576 kB
82  file >> tmp; file >> info.total; file >> tmp;
83  // MemFree: 90333184 kB
84  file >> tmp; file >> info.free; file >> tmp;
85  // Buffers: 1029924 kB
86  file >> tmp; file >> info.buffers; file >> tmp;
87  // Cached: 10290012 kB
88  file >> tmp; file >> info.cached;
89  // Used = Total - Free
90  info.used = info.total - info.free;
91 }
uint32_t NumCpus()
Definition: cpuinfo.cc:19
void LoadAvg(CpuLoad &load)
Definition: cpuinfo.cc:38
void SystemMemInfo(SystemMemInfo &info)
Definition: cpuinfo.cc:78
void ProcessMemInfo(ProcessMemInfo &info)
Definition: cpuinfo.cc:51
double fifteen_min_avg
Definition: cpuinfo.h:16
double five_min_avg
Definition: cpuinfo.h:15
double one_min_avg
Definition: cpuinfo.h:14
uint32_t virt
Definition: cpuinfo.h:20
uint32_t peakvirt
Definition: cpuinfo.h:21
uint32_t res
Definition: cpuinfo.h:22
uint32_t cached
Definition: cpuinfo.h:30
uint32_t buffers
Definition: cpuinfo.h:29
uint32_t used
Definition: cpuinfo.h:27
uint32_t free
Definition: cpuinfo.h:28
uint32_t total
Definition: cpuinfo.h:26