Line data Source code
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 0 : uint32_t NumCpus() { 19 : static uint32_t count = 0; 20 : 21 0 : if (count != 0) { 22 0 : return count; 23 : } 24 : 25 0 : std::ifstream file("/proc/cpuinfo"); 26 : std::string content((std::istreambuf_iterator<char>(file)), 27 0 : std::istreambuf_iterator<char>()); 28 : // Create a find_iterator 29 : typedef find_iterator<std::string::iterator> string_find_iterator; 30 : 31 0 : for (string_find_iterator it = 32 0 : make_find_iterator(content, first_finder("model name", is_iequal())); 33 0 : it != string_find_iterator(); ++it, count++); 34 0 : return count; 35 0 : } 36 : 37 0 : void LoadAvg(CpuLoad &load) { 38 : double averages[3]; 39 0 : uint32_t num_cpus = NumCpus(); 40 0 : getloadavg(averages, 3); 41 0 : if (num_cpus > 0) { 42 0 : load.one_min_avg = averages[0]/num_cpus; 43 0 : load.five_min_avg = averages[1]/num_cpus; 44 0 : load.fifteen_min_avg = averages[2]/num_cpus; 45 : } 46 0 : } 47 : 48 : 49 : 50 0 : void ProcessMemInfo(ProcessMemInfo &info) { 51 0 : std::ifstream file("/proc/self/status"); 52 0 : bool vmsize = false; 53 0 : bool peak = false; 54 0 : bool rss = false; 55 0 : std::string line; 56 0 : while (std::getline(file, line)) { 57 0 : if (line.find("VmSize") != std::string::npos) { 58 0 : std::stringstream vm(line); 59 0 : std::string tmp; vm >> tmp; vm >> info.virt; 60 0 : vmsize = true; 61 0 : } 62 0 : if (line.find("VmRSS") != std::string::npos) { 63 0 : std::stringstream vm(line); 64 0 : std::string tmp; vm >> tmp; vm >> info.res; 65 0 : rss = true; 66 0 : } 67 0 : if (line.find("VmPeak") != std::string::npos) { 68 0 : std::stringstream vm(line); 69 0 : std::string tmp; vm >> tmp; vm >> info.peakvirt; 70 0 : peak = true; 71 0 : } 72 0 : if (rss && vmsize && peak) 73 0 : break; 74 : } 75 0 : } 76 : 77 0 : void SystemMemInfo(SystemMemInfo &info) { 78 0 : std::ifstream file("/proc/meminfo"); 79 0 : std::string tmp; 80 : // MemTotal: 132010576 kB 81 0 : file >> tmp; file >> info.total; file >> tmp; 82 : // MemFree: 90333184 kB 83 0 : file >> tmp; file >> info.free; file >> tmp; 84 : // Buffers: 1029924 kB 85 0 : file >> tmp; file >> info.buffers; file >> tmp; 86 : // Cached: 10290012 kB 87 0 : file >> tmp; file >> info.cached; 88 : // Used = Total - Free 89 0 : info.used = info.total - info.free; 90 0 : }