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